electronoora / webaudio-mod-player

MOD/S3M/XM module player for Web Audio
https://mod.haxor.fi/
MIT License
373 stars 53 forks source link

Added function to load from bytes array #31

Open nb-programmer opened 2 years ago

nb-programmer commented 2 years ago

Added a function loadBuffer(buffer) to load Module data from an existing Buffer (useful for game engines that preload resources)

Still some things pending in this PR that I can't do without asking you:

I have also put these as TODO in the code that will need to be removed

electronoora commented 2 years ago

To forgo the need for the user to explicitly set the format and player properties before calling loadBuffer, we could maybe add an identify function to each of the format-specific player classes. Eg.

Screamtracker.prototype.identify(buffer)

which would return a boolean value depending on whether the contents of the buffer look like a S3M module - probably just by checking the file signature.

loadBuffer could then call identify for each format-specific class to autodetect the format contained within the buffer, and set the format and player properties accordingly.

This would probably be a more robust way for detecting the file type also for load as currently it just makes a guess based on the provided URL. One advantage with the current method is, however, that it can be done already before the XHR.

nb-programmer commented 2 years ago

The identify function could be useful, but I feel relying on auto-detection for basically user-supplied data is a bad idea, as there could be false detections. Perhaps the format argument can be made optional. If it is set, just assign the correct tracker class instance. Maybe another argument to prevent replacing the player property. If not set, we can try and guess using the identify methods