//this gets called from the soundcard public int Read(byte[] buffer, int offset, int count) { var channels = WaveFormat.Channels; int samplesNeeded = count / (4*channels); WaveBuffer wb = new WaveBuffer(buffer); //fix buffer size FMixerBuffer = BufferHelpers.Ensure(FMixerBuffer, samplesNeeded); //empty buffer wb.Clear(); lock(FSourceLock) { //first notify to prepare for buffer foreach(var notify in FNotifys) { try { notify.NotifyProcess(samplesNeeded); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.Source); System.Diagnostics.Debug.WriteLine(e.StackTrace); } } //evaluate the sinks, //e.g. buffer writers should write first to have the latest data in the buffer storage foreach(var sink in FSinks) { try { sink.Read(offset / 4, samplesNeeded); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.Source); System.Diagnostics.Debug.WriteLine(e.StackTrace); } } //evaluate the inputs var inputCount = FSources.Count; for (int i = 0; i < inputCount; i++) { try { if (FSources[i].Signal != null) { //starts the calculation of the audio graph FSources[i].Signal.Read(FMixerBuffer, offset / 4, samplesNeeded); var chan = FSources[i].Channel % channels; //add to output buffer for (int j = 0; j < samplesNeeded; j++) { wb.FloatBuffer[j * channels + chan] += FMixerBuffer[j]; FMixerBuffer[j] = 0; } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.Source); System.Diagnostics.Debug.WriteLine(e.StackTrace); } } //tell the engine that reading has finished FReadingFinished(samplesNeeded); } return count; //always run }
//this is called from the soundcard public int Read(byte[] buffer, int offset, int count) { int samplesNeeded = count / 4; WaveBuffer wb = new WaveBuffer(buffer); //fix buffer size FMixerBuffer = BufferHelpers.Ensure(FMixerBuffer, samplesNeeded); //empty buffer wb.Clear(); lock(source) { var inputCount = source.Count; //var invCount = 1.0f/inputCount; for(int i=0; i<inputCount; i++) { if(source[i] != null) { //starts the calculation of the audio graph source[i].Read(FMixerBuffer, offset / 4, samplesNeeded); //add to output buffer for(int j=0; j<samplesNeeded; j++) { wb.FloatBuffer[j] += FMixerBuffer[j]; FMixerBuffer[j] = 0; } } } //tell the engine that reading has finished FReadingFinished(); } return count; //always run }