poliver
Member
GM Version: Studio 2
Target Platform: Windows
Download: n/a
Links: n/a
Summary:
Load and play .WAV file from an included file
I've had a bit of trouble of figuring this out at first so thought it might be a good idea to leave this out in the open in case somebody's ever looking for a solution.
In short, GameMaker lets you load and playback included .WAV files from a buffer without the need of creating a "GameMaker Asset".
Why would you want that?
1. It lets you use audio_create_play_queue and audio_queue_sound functions in which audio files can be pushed into the queue asynchronosly.
2. For you audio nerds out there, it lets you manipulate the sound on per sample basis.
3. It makes your audio files accessible to the player from the included files folder (but you could encrypt them if wanted).
To make the .WAV file usable in the GameMaker we need to strip it of it's .WAV header.
A Header is everything that precedes 'sample 1' in the data subchunk.
Header sizes may differ depending on the software that was used when creating a .WAV file.
If you need to convert a different audio format to .WAV or want to change the sample rate I suggest using REAPER.
There are at least 2 ways of removing a header:
1. Removing the .WAV header using an external HEX editor.
Open up your .WAV file in the HEX editor. Find the "data" string. And remove everything preceding the data string, data string itself and 4 bytes following the data string. What you'll be left with would be just sample data.
To playback your newly trimmed .WAV file add it to the project.
Load it into the buffer -
Create a buffer sound -
As parameters pass your buffer name
set buffer type to buffer_s16 (standard for audio)
set sample rate to .WAV files original files sample rate (44100, 22050 etc.) //must be same as sample rate in your wav file or file will be played back at a different speed
set the size of the buffer by using buffer_get_size()
and set the audio width to be the same as in your original .WAV file (mono, stereo).
Play it like you would play a regular asset sound -
2. Removing the .WAV header in GameMaker.
Add your .WAV file straight to the project.
Load it into the buffer -
If you examine the buffer in the debugger, you'll see that it still has the header which we don't need. If you try to create a buffer sound from the file in the state it currently is you'll get a really harsh noise.
Start a buffer seek
We know that the sample data starts at the 5th byte after the "data" string.
To find the starting byte of the sample data we will run a for loop inside of which we'll be searching for the "data" string as we know that it is constant across different .WAV files.
The byte values of the "data" string are 100, 97, 116, 100. You can check that by examining the buffer in 1-byte hexadecimal mode.
We'll check every byte using buffer_peek() and upon hitting the right byte value move on to looking for next value.
If the next byte doesn't contain the right next value we'll revert to looking for the first value again till all the values are found in order.
Once the string is found since we know the offsets now we'll create an audio file from buffer using those offsets.
And we'll also break out of the loop.
The "i + 5" in the offset when creating is buffer sound is needed cause at the moment of finding the last character of the "data" string iterator is located on the "a" character. We know that samples start at the location of 4 bytes after the "data".
The "- i - 5" when specifying the size of the buffer for buffer sound is needed for same reason. Since samples start at i + 5 location of the original buffer, the new buffer needs to be i + 5 shorter than the original one.
After we play the sound
What you could do instead of creating the buffer sound in the for loop, upon finding the offset you could create another buffer and start dumping the clean trimmed data into it for later use.
And there you go. You've now loaded a .wav file into a buffer which you can manipulate any way you want to your hearts content, and from which you can create a buffer sound whenever you'd want to play it back.
Target Platform: Windows
Download: n/a
Links: n/a
Summary:
Load and play .WAV file from an included file
I've had a bit of trouble of figuring this out at first so thought it might be a good idea to leave this out in the open in case somebody's ever looking for a solution.
In short, GameMaker lets you load and playback included .WAV files from a buffer without the need of creating a "GameMaker Asset".
Why would you want that?
1. It lets you use audio_create_play_queue and audio_queue_sound functions in which audio files can be pushed into the queue asynchronosly.
2. For you audio nerds out there, it lets you manipulate the sound on per sample basis.
3. It makes your audio files accessible to the player from the included files folder (but you could encrypt them if wanted).
To make the .WAV file usable in the GameMaker we need to strip it of it's .WAV header.
A Header is everything that precedes 'sample 1' in the data subchunk.
Header sizes may differ depending on the software that was used when creating a .WAV file.
If you need to convert a different audio format to .WAV or want to change the sample rate I suggest using REAPER.
There are at least 2 ways of removing a header:
1. Removing the .WAV header using an external HEX editor.
Open up your .WAV file in the HEX editor. Find the "data" string. And remove everything preceding the data string, data string itself and 4 bytes following the data string. What you'll be left with would be just sample data.
To playback your newly trimmed .WAV file add it to the project.
Load it into the buffer -
GML:
newAudioFile = buffer_load("newAudioFile.wav");
As parameters pass your buffer name
set buffer type to buffer_s16 (standard for audio)
set sample rate to .WAV files original files sample rate (44100, 22050 etc.) //must be same as sample rate in your wav file or file will be played back at a different speed
set the size of the buffer by using buffer_get_size()
and set the audio width to be the same as in your original .WAV file (mono, stereo).
GML:
newSound = audio_create_buffer_sound(newAudioFile, buffer_s16, 44100, 0, buffer_get_size(newAudioFile), audio_mono);
GML:
audio_play_sound(newSound, 100, false);
2. Removing the .WAV header in GameMaker.
Add your .WAV file straight to the project.
Load it into the buffer -
GML:
newAudioFile = buffer_load("newAudioFile.wav");
Start a buffer seek
GML:
buffer_seek(newAudioFile , buffer_seek_start, 0);
To find the starting byte of the sample data we will run a for loop inside of which we'll be searching for the "data" string as we know that it is constant across different .WAV files.
The byte values of the "data" string are 100, 97, 116, 100. You can check that by examining the buffer in 1-byte hexadecimal mode.
We'll check every byte using buffer_peek() and upon hitting the right byte value move on to looking for next value.
If the next byte doesn't contain the right next value we'll revert to looking for the first value again till all the values are found in order.
Once the string is found since we know the offsets now we'll create an audio file from buffer using those offsets.
And we'll also break out of the loop.
GML:
var data_found = 0;
for (i = 0; i < buffer_get_size(newAudioFile ); i++) {
if (data_found == 4)
break;
switch(data_found) {
case 0:
if(buffer_peek(newAudioFile , i, buffer_u8) == 100) { data_found++; } else { data_found := 0; }
break;
case 1:
if(buffer_peek(newAudioFile , i, buffer_u8) == 97) { data_found++; } else { data_found := 0; }
break;
case 2:
if(buffer_peek(newAudioFile , i, buffer_u8) == 116) { data_found++; } else { data_found := 0; }
break;
case 3:
if(buffer_peek(newAudioFile , i, buffer_u8) == 97) {
data_found++;
newSound = audio_create_buffer_sound(newAudioFile , buffer_s16, 44100, i + 5, buffer_get_size(newAudioFile) - i - 5, audio_mono);
}
else {
data_found := 0;
}
break;
}
}
The "- i - 5" when specifying the size of the buffer for buffer sound is needed for same reason. Since samples start at i + 5 location of the original buffer, the new buffer needs to be i + 5 shorter than the original one.
After we play the sound
GML:
audio_play_sound(newSound, 100, false);
And there you go. You've now loaded a .wav file into a buffer which you can manipulate any way you want to your hearts content, and from which you can create a buffer sound whenever you'd want to play it back.
Attachments
-
128 KB Views: 72
Last edited: