Jump to content
  • Advertisement
Sign in to follow this  
Neva

Unity Generate sound with SDL (sine wave)

This topic is 4038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am looking for a way to create a sound (sine wave with a given frequency) and play it using the soundcard. It's a bit like in those old games where the app would generate all the sounds. All I have found so far where examples of .wav files being loaded, i.e. relying external audio sources. But what I would like to have is something like this (which uses DirectSound): http://www.gamedev.net/community/forums/topic.asp?topic_id=322115 Would be glad if anyone could give me a hint on this [smile]. [Edited by - Neva on October 2, 2007 4:53:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
I have done what you have described before.

What you need to do is to make a new Mix Chunk variable under SDL_mixer and allocate the proper size of the buffer and assign its pointer to the abuf field in the Mix Chunk and assign the length of the buffer in bytes. Also, be sure to set the volume level of the Mix Chunk to something reasonable or you won't be able to hear it.

Keep in mind that if you simply assign the address of an array to abuf that you set the "allocated" field in the Mix Chunk to 0. If you allocated it with malloc, then assign the "allocated" field a 1.

Also remember that the default format of the sample is the size of the sample in the processor's native endianness. In other words, if you activate SDL_mixer with "if(Mix_OpenAudio(22050, AUDIOS16SYS, 1, 2048) )", your sample frequency is 22050 samples per second and Sint16 is the format of each sample.

Since your samples are two bytes long, the number of samples is half of the number of bytes you allocated. You allocate the number of samples that is from the following fragment of code:

Sint16 buffer[22050/frequency];


Then you calculate the sine wave with a for loop counting to the length of the buffer in samples multiplying the sine by 32767 to give you a full 16-bit range.

This should get you started. If you run into any trouble post back here.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!