streaming real time audio from bluetooth stethoscope

Jul 10, 2013 at 7:40 PM
So here is my situation.

I have a 'bluetooth stethoscope' from 3M which comes with an SDK. I use the following code to record packets coming from the stethoscope and then add a wave header and play them back. That's easy enough, but I'm running into trouble when I want to hear the sound in 'realtime' as the stethoscope is being used.

I'm pretty much new to audio programming and my basic idea was just to recording very small samples of a second or less and then play them back but I get tiny little pauses and that just doesn't work. Can anyone steer me in the right direction?

int numberOfAudioPacketsToReceive = 16;
        byte[] stethoscopeAudioData = new byte[512 * numberOfAudioPacketsToReceive];
        byte[] packet = new byte[512];
        int offSet = 0;
        int receivedPackets = 0;

        stethoscope.StartAudioInput();

        while (receivedPackets < numberOfAudioPacketsToReceive)
        {
            int bytesRead = stethoscope.AudioInputStream.Read(packet, 0, packet.Length);


            if (bytesRead > 0)
            {
                // Data was bytesRead from the stream. Add the received packet to the audio data.
                Array.Copy(packet, 0, stethoscopeAudioData, offSet, bytesRead);

                receivedPackets++;
                offSet += bytesRead;
            }
        }
Coordinator
Jul 10, 2013 at 7:42 PM
try using the BufferedWaveProvider and putting audio into that as you receive it. Then just play from the BufferedWaveProvider.
Jul 20, 2013 at 10:19 PM
Edited Jul 20, 2013 at 10:26 PM
Ok, I got it to work...kind off. it streams for about 10 seconds, then it stops and starts up again when i'm done collecting data. Help!
    static NAudio.Wave.WaveFormat frm = new NAudio.Wave.WaveFormat(4000, 16, 1);
    static BufferedWaveProvider bwProvider;

    public static void Stream()
    {
        byte[] packet = new byte[128];
        int offSet = 0;
        int receivedPackets = 0;

        bwProvider = new BufferedWaveProvider(frm);
        bwProvider.BufferLength = 160000;

        byte[] stethoscopeAudioData = new byte[128 * 1000];

        System.Threading.Thread newThread = new System.Threading.Thread(waveOutPlay);
        newThread.Start();

        stethoscope.StartAudioInput();

        while (receivedPackets < 1000)
        {
            int bytesRead = stethoscope.AudioInputStream.Read(packet, 0, packet.Length);

            if (bytesRead > 0)
            {
                // Data was bytesRead from the stream. Add the received packet to the audio data.
                Array.Copy(packet, 0, stethoscopeAudioData, offSet, bytesRead);
                bwProvider.AddSamples(stethoscopeAudioData, offSet, bytesRead);

                receivedPackets++;
                offSet += bytesRead;
            }
        }

        stethoscope.StopAudioInput();
        bwProvider = null;
        newThread.Abort();

    }

    private static void waveOutPlay(object obj)
    {
        WaveOut waveOut = new WaveOut();

        waveOut.Init(bwProvider);
        waveOut.Play();

    }
Coordinator
Jul 22, 2013 at 1:29 PM
no need to create another thread. Just use WaveOutEvent and call init and play before going into your receive loop. I'd also stick a few seconds of silence into the BufferedWaveProvider to start off, so you don't run into buffering problems.
Jul 23, 2013 at 4:16 AM
I tried doing it without a separate thread and it just waited until it reached the end of the function to actually start playing. Is that a result of not have anything before I start receiving audio? Not sure how exactly I would create 'silence'.
Jul 24, 2013 at 5:50 AM
I got it! Still couldn't get it to work without threading, but it works like a charm like this. Now, I have to get it to stream over the network good enough so that there won't be noticeable lag as far away as India...
    NAudio.Wave.WaveFormat frm = new NAudio.Wave.WaveFormat(2000, 16, 2);
    BufferedWaveProvider bwProvider;
    WaveOut waveOut;

    public void Stream()
    {
        byte[] packet = new byte[128];
        int offSet = 0;
        int receivedPackets = 0;
        byte[] stethoscopeAudioData = new byte[128 * 5000];

        bwProvider = new BufferedWaveProvider(frm);
        bwProvider.BufferLength = (128 * 5000);

        new Thread(() =>
        {
            waveOut = new WaveOut();
            waveOut.Init(bwProvider);
            waveOut.Play();


        }).Start();

        stethoscope.StartAudioInput();

        while (receivedPackets < 5000)
        {
            int bytesRead = stethoscope.AudioInputStream.Read(packet, 0, packet.Length);

            if (bytesRead > 0)
            {
                // Data was bytesRead from the stream. Add the received packet to the audio data.
                Array.Copy(packet, 0, stethoscopeAudioData, offSet, bytesRead);
                bwProvider.AddSamples(stethoscopeAudioData, offSet, bytesRead);

                receivedPackets++;
                offSet += bytesRead;
            }
        }

        stethoscope.StopAudioInput();
    }