Newer
Older
Correlator / PipeGallery / Correlator / PlayWav.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathWorks.MATLAB.NET.Arrays;
using playWaveNamespace;
using System.IO;
using System.Threading;
using PipeGallery.Manage;

namespace PipeGallery.Correlator
{
    public class PlayWav
    {
        private WaveOut m_Player;
        private WaveFormat m_Format;
        //private WaveOutStream m_AudioStream;
        private bool isPlaying;

        private WaveOutStream waveOutStream;

        private static PlayWav instance;
        private PlayWav() 
        {
          
        }

        public static PlayWav getInstance()
        {
            if (instance == null)
            {
                instance = new PlayWav();
            }
            return instance;
        }
        
        public void initWaveOut()
        {
            try
            {
                WaveOutStream S = new WaveOutStream(Directory.GetCurrentDirectory() + "\\listen");
                m_Format = S.Format;
                //m_AudioStream = S;
                waveOutStream = S;
            }
            catch (Exception exp)
            {
                CloseFile();
            }

            if (waveOutStream != null)
            {
                waveOutStream.Position = 0;
                m_Player = new WaveOut(-1, m_Format, m_Format.nAvgBytesPerSec, 20, new BufferFillEventHandler(Filler));
                isPlaying = true;
            }
        }

        public void Stop()
        {
            if (m_Player != null)
            {
                try
                {
                    m_Player.Dispose();
                }
                finally
                {
                    m_Player = null;
                }
            }
            CloseFile();

        }

        private void CloseFile()
        {
            if (waveOutStream != null)
                try
                {
                    waveOutStream.Close();
                }
                finally
                {
                    waveOutStream = null;
                }
        }

        public void listen(byte[] bytesData)
        {
            int totalReceivedBytesNum = 0;
            int receiveBytesNum = bytesData.Length;
            if (waveOutStream != null)
            {
                lock (this)
                {
                    waveOutStream.Write(bytesData, 0, receiveBytesNum);
                    totalReceivedBytesNum += receiveBytesNum;
                }
            }
        }


        private void Filler(IntPtr data, int size)
        {
            byte[] b = new byte[size];
            if (waveOutStream != null)
            {
                int pos = 0;
                while (pos < size)
                {
                    int toget = size - pos;
                    int got = waveOutStream.Read(b, pos, toget);
                    if (got <= 0)
                    {
                        Thread.Sleep(1000);
                        //Console.WriteLine("Got bytes: {0}", got);
                    }
                    if (got < toget)
                        waveOutStream.Position = 0; // loop if the file ends
                    pos += got;
                }
            }
            else
            {
                for (int i = 0; i < b.Length; i++)
                    b[i] = 0;
            }
            System.Runtime.InteropServices.Marshal.Copy(b, 0, data, size);
        }


        public void Dispose()
        {
            if (m_Player != null)
            {
                try
                {
                    m_Player.Dispose();
                }
                finally
                {
                    m_Player = null;
                }
            }
        }

        ~PlayWav()
        {
            Dispose();
        }
    }
}