• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

It's a myth you can put a sound asset in an audio buffer and play it back

obscene

Member
A sound asset. Not an external file.

I say this because I can't find one single example. All the posts about it end in people saying they will do it but they don't. The only example in the documentation contains an error and doesn't make a sound. There are no videos on the subject. There are no tech blogs on the subject. An asset I wasted time downloading from the marketplace that says it makes sounds from audio buffers doesn't make a sound.

I'm sure I'm wrong, but I can't prove I'm wrong.

snd_test. Prove me wrong.
 

FoxyOfJungle

Kazan Games
You are wrong, it is totally possible to generate sounds using buffers through GML, mainly to generate frequencies...
I have a project that does that, but I don't know where it is now....
 

obscene

Member
Direct copy and paste from the doc example here:

1603506666955.png

Compiles despite the error, yet makes no sound. And I don't know anything about audio buffers to understand the error, hence why I'm reading the documentation, so you can see my dilemma.

However this is only the beginning of the problem, as I want to put sound into a buffer and play it back as so many people seem to swear is possible. Not for any specific reason, just to understand if it works. If it can be done in this topic, this will be the sole proof of concept on the entire internet.
 
Direct copy and paste from the doc example here:

View attachment 35238

Compiles despite the error, yet makes no sound. And I don't know anything about audio buffers to understand the error, hence why I'm reading the documentation, so you can see my dilemma.

However this is only the beginning of the problem, as I want to put sound into a buffer and play it back as so many people seem to swear is possible. Not for any specific reason, just to understand if it works. If it can be done in this topic, this will be the sole proof of concept on the entire internet.
That's an obvious error with the documentation. buffer_seek is a built-in function. Just remove the var before buffer_seek(bufferID, buffer_seek_start, 0). Don't know why that doesn't just explode and fail to compile, though. You should file a bug report about the erroneous documentation. Looks like this error's been there since GMS2. GMS1's documentation is fine:
GML:
rate = 44100;
hertz = irandom_range(220, 880);
samples = 44100;
bufferId = buffer_create(rate, buffer_fast, 1);
buffer_seek(bufferId, buffer_seek_start, 0);
num_to_write = rate / hertz;
val_to_write = 1;
for (var i = 0; i < (samples / num_to_write) + 1; i++;)
   {
   for (var j = 0; j < num_to_write; j++;)
      {
      buffer_write(bufferId, buffer_u8, val_to_write * 255);
      }
   val_to_write = (1 - val_to_write);
   }
soundId = audio_create_buffer_sound(bufferId, buffer_u8, rate, 0, 44100, audio_mono);
Looks like they just appended var to the start of each line and forgot that one of them was a function, not a variable... Oops!
 
Last edited:

obscene

Member
Interesting. I see now that the online doc has changed, just not the local version. But even still, I ran it and got this error...

audio_create_buffer_sound: not enough data in buffer to create sound with offset 0 length 44100Error: no sound exists for soundid 4

So I changed the length to 22050 (I don't know why) and yeah now this thing is blowing out my eardrums so I guess it works, but I still don't see how you would ever get an actual sound asset (not a file) into it.
 

TsukaYuriko

🌠
Forum Staff
Moderator
Nobody ever said that you can use sound assets with this. It creates a sound out of a buffer, not a buffer out of a sound. There are no resources on this because it's not the intended functionality.

What are you trying to do that requires turning a sound asset into a... well, another sound asset and then playing that? I fail to see what benefit this would even bring you, as you're essentially converting a sound asset to a sound asset, so... no change at all. Am I misunderstanding something here? If you let us know what you're trying to do, we may be able to figure something out.
 

kburkhart84

Firehammer Games
As I understand it, the point of audio buffers is more about manipulation of sound directly, possibly procedurally. If you were to create sound effects similarly something like SFXR, you would then put the sound data(raw PCM stuff) into the buffer and play it at need.

To properly be able to use this(the way it was intended as I understand it), you need knowledge of how sound waves work. Using that knowledge and whatever code, you can create a wave and stick that into the buffer. It isn't about taking a sound asset and sticking in there, its about procedurally creating the sound asset in code(or possibly loading it from a file format that isn't supported, or one of the file formats that works like MIDI but contains the samples along with the music notation).
 

obscene

Member
Not trying to do anything in particular, just trying to figure out how this stuff works. An example might be stitching together looping audio. Supposedly you can create an audio_queue for this, according to dozens of forum posters who never provide an example, but everyone uses workarounds where you just check every step and get/set the track position. I'm guessin you could actually do it with an audio_queue but with external audio files only, which seems like a shame.

Personally I was interested into exploring some audio processing. I've written some fake reverb scripts which basically just spams every available audio channel and it seems that if I could just dump these sounds into a buffer and create reverb using one audio channel it would much more practical. Assuming I could learn to layer sounds in a similar way. Also there are possibilities of manipulating the data to do some EQing. But since I can't just throw a simple sound effect into a buffer in real time to even begin to do anything with it, seems to be a pointless endeavor. Can't use external audio files for every single sound effect in game. So what even is the point of audio buffers in the context of a game making engine.
 

kburkhart84

Firehammer Games
So what even is the point of audio buffers in the context of a game making engine.
As I said in the post above, its about procedurally creating sounds at run-time, which allows you to do procedural sounds, or load PCM data in unsupported formats for example.

Additionally, I AGREE that it would be much more feature-complete if you could directly take the PCM data from sound assets(as loaded from the IDE) and stick them in the buffers for future manipulation. It wouldn't work for compressed sounds but for raw sounds(like WAVs) it could.
 

TsukaYuriko

🌠
Forum Staff
Moderator
Can't use external audio files for every single sound effect in game.
Why not? The audio buffer functions operate on raw, uncompressed PCM audio. External files are the only thing that's going to provide you with an interface for that in the current version. There's no way to retrieve an uncompressed audio file from a sound asset at run time.

There is also no function that reads an internal audio asset into a buffer. If you want this to be doable, please file a feature request. I can't guarantee that it will be implemented and can't comment on whether it's even possible to implement it, but it's always worth a try.

Not trying to do anything in particular, just trying to figure out how this stuff works. An example might be stitching together looping audio. Supposedly you can create an audio_queue for this, according to dozens of forum posters who never provide an example, but everyone uses workarounds where you just check every step and get/set the track position. I'm guessin you could actually do it with an audio_queue but with external audio files only, which seems like a shame.
Documentation and examples about this are available on the Audio Playback page. You are correct about this being limited to external audio files as it requires audio buffers.
 
Top