Allowing users to load audio files into the game?

Fluury

Member
Heya.

I'd like the user to be able to load sprites and audio files into the game. The sprite part is easily handled by sprite_add(), however loading audio into the game seems vastly more complex, and from what I have seen, only possible in a very limited fashion.

It appears that the only way to achieve this is to load a specific audio file format (RAW, although some say wav also works, unsure about the latter) into a buffer, and then using an audio buffer function to turn it into a sound to play.

The search for tutorials or code examples was somewhat unsuccessful, a lot of the threads were either over 4 years old, or just didn't get a lot of attention in the first place, leaving the OP without an answer.

So here I am, with the following questions:

1. The documentation says only PCM audio is supported, however I am not familiar with the term. Some sources suggest that .wav is supported, some say only partially, and the massive gaps in time the statements were made makes me wonder if something maybe changed. So, what file formats are supported?

2. How would you even load an audio file, be it raw or .wav into a buffer? Do I just open an audio file using file_bin_open, then just read through it byte by byte until it's end and feed said information to a buffer?

3. Could anyone reference any coding examples/tutorials?

Thanks!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
PCM stands for "Pulse-Code Modulation" which is typically what is used for CD audio, and is supported by WAV files. SO you should be able to load WAV files into a buffer and then play them back. As for loading in the audio, you should be able to call buffer_load directly with a wav file (or use a binary file function like you suggest), then use the buffer_read or buffer_peek functions to get the header details for playback. Some rough code would be like this:

GML:
var _file = buffer_load("sound.wav");
var _sample_rate = buffer_peek(_file, 24, buffer_u32);
var _audio_bits = buffer_peek(_file, 34, buffer_u16);
var _samples = (buffer_get_size(_file) - 44) div _audio_bits;
sound = audio_create_buffer_sound(_file, buffer_s16, _sample_rate, 44, _samples, audio_mono);
audio_play_sound(sound, 1, false);
Note, I haven't tested this, but in theory it should work... :p
 

Fluury

Member
PCM stands for "Pulse-Code Modulation" which is typically what is used for CD audio, and is supported by WAV files. SO you should be able to load WAV files into a buffer and then play them back. As for loading in the audio, you should be able to call buffer_load directly with a wav file (or use a binary file function like you suggest), then use the buffer_read or buffer_peek functions to get the header details for playback. Some rough code would be like this:

GML:
var _file = buffer_load("sound.wav");
var _sample_rate = buffer_peek(_file, 24, buffer_u32);
var _audio_bits = buffer_peek(_file, 34, buffer_u16);
var _samples = (buffer_get_size(_file) - 44) div _audio_bits;
sound = audio_create_buffer_sound(_file, buffer_s16, _sample_rate, 44, _samples, audio_mono);
audio_play_sound(sound, 1, false);
Note, I haven't tested this, but in theory it should work... :p
When I read through the documentation it suggested that buffer_load only works for files saved via buffer_save. I'll give it a shot though, thanks!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
When I read through the documentation it suggested that buffer_load only works for files saved via buffer_save. I'll give it a shot though, thanks!
Yeaaaaah... that's not striictly true and you can pretty much load ANY file you want into a buffer. I mean, you should be loading buffers that you've previously saved, but it's not a 100% ONLY DO THIS rule... I'll look at rewording that... :)
 

Fluury

Member
Swiftly tried it out and some audio is playing, but unfortunately not the audio file itself. It sounds somewhat corrupted.

More research made me find this thread in particular where the response seems to suggest that .wav seems to be causing some issues regarding the function that turns buffer content to a sound. Unfortunate, given I don't expect Users to be able to get past those very specific export settings :p
 
Top