Search code examples
c#midinaudio

USB Keyboard - buffering files


I have another problem with my little application. I use N-Audio library and external MIDI keyboard (88-keys) connected to PC via USB cable.

Everything is ok, I can play on my MIDI keyboard, application reads keys and plays specific Notes = .wav files from resources.

But the problem is when I play a little longer than 2 minutes. After that time sounds are playing creepy, something is wrong with them - they sounds like crazy compression quality appears, and finally they are gone at all, but application is still receiving signals from MIDI keyboard.

I have reader like this (for each of 88 keys):

// WaveFileReader
private NAudio.Wave.WaveFileReader wave = null;
private NAudio.Wave.WaveFileReader wave0 = null;

and direct sound out:

// DirectSoundOut
private NAudio.Wave.DirectSoundOut output = null;
private NAudio.Wave.DirectSoundOut output0 = null;

and then, all sounds (notes) are played this way (for each wave, wave0, wave1, etc... and output, output1, output2, etc...):

if (nutka == "C1" && msg.Velocity != 0)
{
    wave1 = new NAudio.Wave.WaveFileReader(Properties.Resources.C1);
    output1 = new NAudio.Wave.DirectSoundOut();
    output1.Init(new NAudio.Wave.WaveChannel32(wave1));
    output1.Play();
}

Can't find out what the realy main problem is, and how can I resolve it.


Solution

  • You should not be opening multiple instances of WaveOut. Instead, just open 1 and mix all the inputs with a MixingSampleProvider