GMS 2 Generating audio

Slothagami

Member
I'm trying to make a music engine and I want to be able to generate instruments from waves (you would choose a wave type, and add effects to it)
I'm new to the sound side of things, so bear with me, how can I do this?
Is there a way to save them to a sound file?

Thanks in advance
 

Yal

🐧 *penguin noises*
GMC Elder
audio_sound_pitch lets you change the pitch of a sound, this lets you use the same sound for multiple notes (a 2x speed factor means it's transposed one octave up, etc - you might wanna look up https://en.wikipedia.org/wiki/Musical_tuning for details; there's several ways to define intermediate steps, like Equal Temperament, and the "notes are 1 / 12th of an octave apart" system which is the easiest to code doesn't necessarily sound the best)
 

Slothagami

Member
audio_sound_pitch lets you change the pitch of a sound, this lets you use the same sound for multiple notes (a 2x speed factor means it's transposed one octave up, etc - you might wanna look up https://en.wikipedia.org/wiki/Musical_tuning for details; there's several ways to define intermediate steps, like Equal Temperament, and the "notes are 1 / 12th of an octave apart" system which is the easiest to code doesn't necessarily sound the best)
Thanks, that is helpful for the different notes and things I need! But I'm also trying to generate the sound that I would apply this to (eg. from a square wave) so that I don't need any sound recourses in the project file.
 

Yal

🐧 *penguin noises*
GMC Elder
Generate sound data from within GM itself? There's always audio_create_buffer_sound which interprets a buffer's content as PCM audio. Not sure how viable it is to generate the waveforms on the fly, though.
 

Slothagami

Member
Generate sound data from within GM itself? There's always audio_create_buffer_sound which interprets a buffer's content as PCM audio. Not sure how viable it is to generate the waveforms on the fly, though.
Thanks! That's just what I was looking for! I'm not sure how I would use this though, the documentation doesn't really explain how to make the sound, I can't seem to get it to work, even by directly pasting it from the documentation.
 

Kyon

Member
Thanks! That's just what I was looking for! I'm not sure how I would use this though, the documentation doesn't really explain how to make the sound, I can't seem to get it to work, even by directly pasting it from the documentation.
You can't really do it from within gamemaker.
Although the pitch thingy in gamemaker is quite good. If you just export a good sine, saw, triangle etc. wave where the start and the end of the wave are at 0 so that it's loopable you could kind of do what you want to do.
As in, just loop a sample, change the pitch to match the wave speed.
 

Yal

🐧 *penguin noises*
GMC Elder
Thanks! That's just what I was looking for! I'm not sure how I would use this though, the documentation doesn't really explain how to make the sound, I can't seem to get it to work, even by directly pasting it from the documentation.
Afaik, GM just passes the audio data to the sound library as-is, and you're meant to use it to load raw WAV data to a buffer when handling external audio. To generate waveforms from within GM, you'd need to look up the PCM format and figure out how it works.

A good starting point would be the wikipedia page on PCM audio:

And if that doesn't make things clear, I guess opening a bunch of WAV files of a square, sine and saw wave in a hex editor could be the next step. E.g., Vim in binary mode or xxd - you get all the Unix standard programs if you install the Windows version of Git and leave the "which command line tool would you like?" setting at the default ,"Cygwin Bash", so it's a fast way to get a bunch of advanced programming tools if you don't wanna bother doing in-depth research.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
If you want an example of how to generate a sound using the audio buffer options, then check out my one-script game OSG Asteroids (it's free): https://marketplace.yoyogames.com/assets/4454/osg-asteroids I used buffer audio to create the three sound effects that the game uses, and while they are VERY basic, it shows you the principles behind how it can be done. It's actually fairly simple and straightforward, with the difficult part being know what actual data to put into the buffers! I had to do a lot of trial and error...
 

Slothagami

Member
Thanks to everyone who helped I've managed to get something working! Here is the code for those who are interested: (the code is not mine)
GML:
        // Global left pressed event
        rate = 44100
        samples = 70000 // sound duration
        bufferId = buffer_create(samples, buffer_fixed, 1);
        buffer_seek(bufferId, buffer_seek_start, 0);
        var time = 0
        for (var i = 0; i < samples; i++;) {
            //Make an waveform of an sinewave
            //buffer_write(bufferId, buffer_s16, dsin(time*mouse_y)*500);
            buffer_write(bufferId, buffer_s16, (dsin(time*mouse_y*2)/((time/20)+1))*600 );
            time += 0.01
        }
        soundId = audio_create_buffer_sound(bufferId, buffer_s16, rate, 0, samples, audio_mono);

        audio_play_sound(soundId,10,false);
I have one more question, I tried to download the sound using buffer_save() and calling it "sound.wav" but the resulting file is unplayable. Is there a way I can make it playable?
 
Last edited:
Top