0
votes

I'm trying to play sound provided by an Ethernet microphone.

The device stream live audio via udp packets, that I read in a network receiver thread :

MemoryStream msAudio = new MemoryStream();

private void process_stream(byte[] buffer)
{
    msAudio.Write(fragment, 0, fragment.Length);
}

process_stream is called in a task

Then I have another task to play the stream in NAudio (NAudio isn't mandatory) :

  while (IsConnected)
        {
            msAudio.Position = 0;
            var waveFormat = new WaveFormat(8000, 16, 1); // Same format
            using (WaveStream blockAlignedStream = new BlockAlignReductionStream(
                                                            WaveFormatConversionStream.CreatePcmStream(
                                                                new RawSourceWaveStream(msAudio , waveFormat))))
            {
                using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                {
                    waveOut.Init(blockAlignedStream);
                    waveOut.Play();
                    while (waveOut.PlaybackState == PlaybackState.Playing)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }
            }
        }

My problems is :

  • I hear Toc Toc Toc noise (about 4 times by second)
  • The audio is sloooowed, voice is deformed like bitrate is too low (but 8khz is correct)
  • The audio is looped, I think i have to flush my stream, but I don't see were...

Thanks a lot if you can tell me some advice...

P.S : for helping, original code is working in android using AudioTrack. the code is here

P.S 2 : Here the "image" of the audio noise that I have : RawAudio

1
For the Loop Issue : use BufferedWaveProvider in place of a memoryStream, with .AddSample() It offer circular buffer which is very simple to implement - vil.coyote.ch
For the Slow Speed Issue : Trying to double SampleRate.... Yes it'working with 16KHz... I'don't know why specification is incorrect... - vil.coyote.ch
For the Noise Issue : Analyse the RawAudio seems that you have Extra byte in beginning or end of your byte[] Try to remove some byte.... Yes, It's working... I thinks that first 4 bytes provide some header informations, Now I have a clear sound... - vil.coyote.ch

1 Answers

0
votes

I don't like to respond myself.. but I think that my question is too specific... So I resolve my problems with :

  • Using BufferedWaveProvider in place of a memoryStream
  • Doubling SampleRate (16KHz)
  • Removing first 4 byte of the buffer

Now My code look like :

    public ctor()
    {            
        var waveFormat = new WaveFormat(16000, 16, 1);
        buffer = new BufferedWaveProvider(waveFormat)
        {
            BufferDuration = TimeSpan.FromSeconds(10),
            DiscardOnBufferOverflow = true
        };
    }

    internal void OnDataReceived(byte[] currentFrame)
    {
        if (mPlaying && mAudioTrack != null)
        {               
            buffer.AddSamples(currentFrame, 4, currentFrame.Length);
        }
    }

    internal void ConfigureCodec()
    {                   
        mAudioTrack = new WaveOut(WaveCallbackInfo.FunctionCallback());
        mAudioTrack.Init(buffer);
        if (mPlaying)
        {
            mAudioTrack.Play();
        }            
    }

No more Thread....