Sign in to follow this  
LazySumo

C#: Need assistance with audio

Recommended Posts

LazySumo    122
Yes, I've already asked in the "Music and Sound" area, they pointed me this way. :) I have an object bouncing around in a 2D space, when it collides with another object I want it to sound a musical note based on the angle of collision. So theoretically I could be using the entire range of notes available. I could of course setup 300+ .wav files, load them all up and away I go, but that lacks any kind of elegance at all. Is there another way to get DirectX or C# to sound a simple, single musical note? Thanks!

Share this post


Link to post
Share on other sites
Kylotan    10007
A semitone in musical terms is roughly equivalent to a frequency ratio of 1.059463 - in other words, play a C at 1.059463x its normal speed and you get a C#. You can use that ratio to scale one note up or down a bit so you can use fewer samples to cover the range that you want.

Or you could just generate the tone directly, by taking a musical note frequency table and using that as the parameters to a sine wave, but making that sound good can be tricky.

Share this post


Link to post
Share on other sites
LazySumo    122
Thanks Kyl, so you're saying only have a few .wav files and then adjust those on the fly?

I've done a search on "musical note frequency table" and couldn't find anything useful. Any links or suggestions?

Thanks again!

Share this post


Link to post
Share on other sites
Kylotan    10007
Yes, in fact that is how cheap sample libraries will work - they give you something like 4 samples per octave and stretch them to fit the other semitones in between.

Note frequency table. Again, this is only of use if you want to generate the note yourself. If you just want to stretch wav files (or if you can play them back at a different rate, achieving the same effect), you just need to know the ratio I posted above. Note that it's an exponential scale, so to go up 3 semitones you multiply the original frequency by that ratio three times, for example.

I'm not sure how C# play sounds, short of using the WinMM PlaySound function, but if you can find a library that allows you to alter the frequency of playback, that will be the easiest route to take. Failing that, you may have to write a little routine to 'stretch' the wave data in memory.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this