Search code examples
c#playbacknaudiomemorystream

Play from Memorystream, stop when reached end and start recording again


I am using NAudio, and i am currently playing around.

And now, i have a problem.

I record my mic to a memorystream.

When i click a button, it stops recording (to prevent write and read at the same time) And play from that memorystream.

But i don´t know how to tell it, when it has reached the end if should start over again.

So that after it has played, it will record my mic again, and i can redo the process.

Thanks

private void play_Click(object sender, EventArgs e)
        {
            StreamReader streamu = new StreamReader(waveStream);


                //waveWriter.Close();

                sourceStream.StopRecording();
               // Wasout.Dispose();
               waveStream.Seek(0, SeekOrigin.Begin);

                //waveStream.Read(bytes, 0, waveStream.Length);
                playwave = new NAudio.Wave.RawSourceWaveStream(@waveStream, sourceStream.WaveFormat);
                waveOut2.Init(playwave);
                //Aut.InitRecordAndPlayback(playwave, 2, 48000);
                waveOut2.Play();

                if (streamu.EndOfStream)
                {
                    waveOut2.Dispose();
                    waveOut2 = new NAudio.Wave.DirectSoundOut();
                    sourceStream.StartRecording();
                    Wasout.Init(waveIn);
                    Wasout.Play();
                }


        }

This is what i am doing right now, and as you can see, i am messing around. And well, it doesn´t work;P


Solution

  • Create a LoopStream, like this one from NAudioDemo, and pass your RawSourceWaveStream into that. (probably could be simplified a bit, but demonstrates the basic idea)

    class LoopStream : WaveStream
    {
        WaveStream sourceStream;
    
        public LoopStream(WaveStream source)
        {
            this.sourceStream = source;
        }
    
        public override WaveFormat WaveFormat
        {
            get { return sourceStream.WaveFormat; }
        }
    
        public override long Length
        {
            get { return long.MaxValue / 32; }
        }
    
        public override long Position
        {
            get
            {
                return sourceStream.Position;
            }
            set
            {
                sourceStream.Position = value;
            }
        }
    
        public override bool HasData(int count)
        {
            // infinite loop
            return true;
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            int read = 0;
            while (read < count)
            {
                int required = count - read;
                int readThisTime = sourceStream.Read(buffer, offset + read, required);
                if (readThisTime < required)
                {
                    sourceStream.Position = 0;
                }
    
                if (sourceStream.Position >= sourceStream.Length)
                {
                    sourceStream.Position = 0;
                }
                read += readThisTime;
            }
            return read;
        }
    
        protected override void Dispose(bool disposing)
        {
            sourceStream.Dispose();
            base.Dispose(disposing);
        }
    }