0

I'm playing around doing some audio synthesis in real time with C# .Net.

I've got a VCO class that updates it's output waveform whenever the output waveform is read. In order to play a sound, I want to feed it into the DirectSound secondary buffer. I've played around doing this using a byte array not populated in real time.

However, in order to play my VCO in real time, I presume I need to read the output at the same rate as the sample rate specified for the direct sound object.

Is there a way I can have a timer or callback type function that raises an event at 1/sample rate so that the real time vco output can be matched to the direct sound sample rate ?

I suppose I can have a loop and interogate StopWatch.Ticks, but is there a neater way of having an event automatically raised, with no processor load in between ?

  • I think most audio code implementations operate with buffers of samples at the API level, not individual samples. DirectSound should provide you with events (or should let you poll its state) to know when a buffer of samples is needed or ready for consumption. – Alexey Frunze Feb 29 '12 at 11:51
  • Thanks Alex. Yes, whenever the sample rate clock ticked, I was going to add the latest sample to an array in real time, and then flush the array into the media buffer when it needed more data. – user1240122 Feb 29 '12 at 12:10
  • 1
    Having an event per sample is wasteful (too many context switches per second) and sometimes even impossible. You may lose events or samples if you have an event per sample. – Alexey Frunze Feb 29 '12 at 12:14
  • I suppose I could respond every 100 samples, and extrapolate the 100 next samples each time the handler is fired. – user1240122 Feb 29 '12 at 12:19

0 Answers0