A while back, I wrote about the sound test project I created in Silverlight that quickly turned into an experimentation on audio synthesis.
I plan to set up a codeplex site for this once I get back from vacation. However, since I’ve had a few requests for the source, I thought I’d quickly post it here.
Issues
- My mixing algorithm is bad. If anyone knows the right algorithm for mixing multiple channels of sound, while keeping the same end volume regardless of how many channels are mixed (including times when some of the channels are silent) I’d love to know it. Please share!
- Related to above, you can get distortion pretty quickly when you press more than a few notes
- Fair bit of dead code – this is a test project :)
None of those are Silverlight issues – they’re issues with my code.
One key thing that changed in Silverlight 3 RTW vs. the beta was the addition of the new MediaStreamSource AudioBufferLength property. It represents the number of milliseconds of sound to buffer before output. The minimum value is 15, but on most machines you won’t be able to get close to that unless you have highly optimized sound generation code (doing wavetable lookups rather than real-time sawtooth waves, for example)
This value is highly machine dependent, so if you do anything serious with this code, you’ll want to make that something the user can change to suit their own machine. To low a value and the sound will stutter, too high and the sound will have an audible delay.
The value is in the InitializeMediaStreamSource method of Synthesizer.MediaStreamSource.cs
AudioBufferLength = 30;
Source code is available here. I hope it helps jumpstart your own projects.