private void record_loop() { while (cSamplePos < mSamplePos) { if (stop_flag) { break; } if (sampleQueue.Count > 0) { check_diskspace(); byte[] packet = (byte[])sampleQueue.Dequeue(); SoundPacket p = new SoundPacket(packet); form.Invoke(form.myDelegate, new object[] { p.averageDB }); writer.Write(packet, 0, packet.Length); } } this.isRecording = false; writer.Close(); PortAudio.Pa_StopStream(stream); }
private void play_loop() { while (reader.Position < reader.Length) { if (stop_flag) { break; } if (pause_flag) { continue; //just wait for it to be unpaused } //This is essentially working as a circular/FIFO buffer, where new sample packets are only added to the queue //if there's room in the queue. Once the packet is read out of the queue in the callback function it's //removed from the queue and there is room to add more info to the queue if (sampleQueue.Count < QUEUE_LENGTH) { //Console.WriteLine("Writing"); byte[] buffer = new byte[NUM_SAMPLES]; //buffer to read the wav raw bytes into int bytesRead = reader.Read(buffer, 0, NUM_SAMPLES); //read a block of bytes out from the wav cSamplePos += bytesRead; SoundPacket packet = new SoundPacket(buffer); sampleQueue.Enqueue(packet); //send the buffer to the queue } } this.stop_flag = true; while (PortAudio.Pa_IsStreamActive(stream) != 0) { } PortAudio.Pa_StopStream(stream); this.isPlaying = false; cSamplePos = 0; this.stop_flag = false; }