0

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

Rotem Harel
  • 736
  • 8
  • 16
Zerowalker
  • 761
  • 2
  • 8
  • 27

1 Answers1

0

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);
    }
}
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Is there a way that can be written easier? This is to hard;S Can´t i just detect when it has played till the end, clear the memstream, and record again? Thanks – Zerowalker Jul 20 '13 at 02:42
  • There's not much too it, just cut and paste that code into your project, and call waveOut.Init(new LoopStream(playWave)); – Mark Heath Jul 20 '13 at 07:47
  • Actually, I think I may have misunderstood your question. To detect playback ending, use the PlaybackStopped event. Then you can start another record. – Mark Heath Jul 20 '13 at 07:48
  • I don´t even know how to use classes, need to look up on that. But will try the Playbackstopped, if it is like the (record audio when audio is available) then i can hopefully work something out. Thanks! – Zerowalker Jul 20 '13 at 21:27