示例#1
0
 public void Dispose()
 {
     if (m_Thread != null)
     {
         try
         {
             m_Finished = true;
             if (m_WaveOut != IntPtr.Zero)
             {
                 WaveNative.waveOutReset(m_WaveOut);
             }
             m_Thread.Join();
             m_FillProc = null;
             FreeBuffers();
             if (m_WaveOut != IntPtr.Zero)
             {
                 WaveNative.waveOutClose(m_WaveOut);
             }
         }
         finally
         {
             m_Thread  = null;
             m_WaveOut = IntPtr.Zero;
         }
     }
     GC.SuppressFinalize(this);
 }
示例#2
0
        public bool playSounds(string soundFileName)
        {
            if (string.IsNullOrEmpty(soundFileName))
            {
                return(false);
            }
            if (!InitAudioDevice())
            {
                return(false);
            }
            byte[] block;
            int    blockSize;

            //WaveNative.waveOutOpen(out m_WaveOut, device, format, m_BufferProc, 0, WaveNative.CALLBACK_FUNCTION)
            //new WaveLib.WaveOutPlayer(-1, fmt, 16384, 3, new WaveLib.BufferFillEventHandler(Filler));
            if (WaveNative.waveOutOpen(out hWaveOut, -1, waveForm, null, 0, 0) != WaveNative.MMSYSERR_NOERROR)
            {
                //::AfxMessageBox("音频设备打卡失败",MB_OK);
                return(false);
            }

            if ((block = loadAudioBlock(soundFileName, out blockSize)) == null)
            {
                //::AfxMessageBox("无法打开音频文件",MB_OK);
                return(false);
            }
            writeAudioBlock(hWaveOut, block, blockSize);
            WaveNative.waveOutClose(hWaveOut);
            return(true);
        }
示例#3
0
    protected unsafe void OnButton1Clicked(object sender, System.EventArgs e)
    {
        IntPtr ptr;

        WaveNative.snd_pcm_open(out ptr, "default", WaveNative.snd_pcm_stream_t.SND_PCM_STREAM_PLAYBACK, 0);
        Console.WriteLine(ptr.ToString());
        IntPtr param;

        WaveNative.snd_pcm_hw_params_malloc(out param);
        WaveNative.snd_pcm_hw_params_any(ptr, param);
        WaveNative.snd_pcm_hw_params_set_access(ptr, param, WaveNative.snd_pcm_access_t.SND_PCM_ACCESS_RW_INTERLEAVED);
        WaveNative.snd_pcm_hw_params_set_format(ptr, param, WaveNative.snd_pcm_format_t.SND_PCM_FORMAT_S16_LE);
        uint srate = 44100;

        WaveNative.snd_pcm_hw_params_set_rate_near(ptr, param, ref srate, null);
        WaveNative.snd_pcm_hw_params_set_channels(ptr, param, 1);
        WaveNative.snd_pcm_hw_params(ptr, param);
        WaveNative.snd_pcm_hw_params_free(param);
        WaveNative.snd_pcm_prepare(ptr);
        sine_gen sg = new sine_gen(100.0 / 44100);

        short[] buf = new short[4096];
        fixed(short *bufp = buf)
        {
            while (true)
            {
                sg.generate(buf, buf.Length, 0, short.MaxValue);
                WaveNative.snd_pcm_writei(ptr, bufp, (ulong)buf.Length);
            }
        }
    }
示例#4
0
        private int WriteWaveOut(IntPtr buf, int size)
        {
            int ret;

            WaveNative.WaveHdr m_Header       = new WaveNative.WaveHdr();;
            GCHandle           m_HeaderHandle = GCHandle.Alloc(m_Header, GCHandleType.Pinned);

            m_Header.dwUser         = (IntPtr)GCHandle.Alloc(this);
            m_Header.lpData         = buf;
            m_Header.dwBufferLength = size;
            ret = WaveNative.waveOutPrepareHeader(waveOut, ref m_Header, Marshal.SizeOf(m_Header));
            if (ret != WaveNative.MMSYSERR_NOERROR)
            {
                throw new Exception("can not open wave device");
            }

            lock (queue)
            {
                ret = WaveNative.waveOutWrite(waveOut, ref m_Header, Marshal.SizeOf(m_Header));
            }
            if (ret != WaveNative.MMSYSERR_NOERROR)
            {
                throw new Exception("can not open wave device");
            }

            while (WaveNative.waveOutUnprepareHeader(waveOut, ref m_Header, Marshal.SizeOf(m_Header)) == WaveNative.WAVERR_STILLPLAYING)
            {
                Thread.Sleep(5);
            }

            ((GCHandle)m_Header.dwUser).Free();
            m_HeaderHandle.Free();
            return(ret);
        }
示例#5
0
 public bool Play()
 {
     lock (this)
     {
         m_PlayEvent.Reset();
         m_Playing = WaveNative.waveOutWrite(m_WaveOut, ref m_Header, Marshal.SizeOf(m_Header)) == WaveNative.MMRESULT.MMSYSERR_NOERROR;
         return(m_Playing);
     }
 }
示例#6
0
 public bool Play()
 {
     lock (_lockObj)
     {
         m_PlayEvent.Reset();
         m_Playing = (MMErrors)WaveNative.waveOutWrite(m_WaveOut, ref m_Header, Marshal.SizeOf(m_Header)) == MMErrors.MMSYSERR_NOERROR;
         return(m_Playing);
     }
 }
示例#7
0
 public bool Restart()
 {
     if (WaveNative.waveOutRestart(hWaveOut) != WaveNative.MMSYSERR_NOERROR)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
示例#8
0
 public bool Pause()
 {
     if (WaveNative.waveOutPause(hWaveOut) != WaveNative.MMSYSERR_NOERROR)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
示例#9
0
        public bool GetPosition()
        {
            int lpInfo = 0;

            WaveNative.WaveHdr header = new WaveNative.WaveHdr();
            if (WaveNative.waveOutGetPosition(hWaveOut, out lpInfo, Marshal.SizeOf(header)) != WaveNative.MMSYSERR_NOERROR)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
示例#10
0
 public void Dispose()
 {
     if (m_Header.lpData != IntPtr.Zero)
     {
         WaveNative.waveOutUnprepareHeader(m_WaveOut, ref m_Header, Marshal.SizeOf(m_Header));
         m_HeaderHandle.Free();
         m_Header.lpData = IntPtr.Zero;
     }
     m_PlayEvent.Close();
     if (m_HeaderDataHandle.IsAllocated)
     {
         m_HeaderDataHandle.Free();
     }
     GC.SuppressFinalize(this);
 }
示例#11
0
        public void Stop()
        {
            threadWorking = false;
            queue.Close();
            foreach (var thread in threads)
            {
                thread.Join();
            }

            if (waveOut != IntPtr.Zero)
            {
                WaveNative.waveOutClose(waveOut);
                waveOut = IntPtr.Zero;
            }
        }
示例#12
0
        public WaveOut(int device, WAVEFORMATEX format, int bufferSize, int bufferCount, BufferFillEventHandler fillProc)
        {
            m_zero     = format.wBitsPerSample == 8 ? (byte)128 : (byte)0;
            m_FillProc = fillProc;

            var errorCode = (MMErrors)WaveNative.waveOutOpen(out m_WaveOut, device, ref format, m_BufferProc, 0, (int)CallBackFlag.CALLBACK_FUNCTION);

            if (errorCode != MMErrors.MMSYSERR_NOERROR)
            {
                throw new MixerException(errorCode, Audio.GetErrorDescription(FuncName.fnWaveOutOpen, errorCode));
            }

            AllocateBuffers(bufferSize, bufferCount);
            m_Thread = new Thread(new ThreadStart(ThreadProc));
            m_Thread.Start();
        }
示例#13
0
        public WaveOutBuffer(IntPtr waveOutHandle, int size)
        {
            m_WaveOut = waveOutHandle;

            m_HeaderHandle          = GCHandle.Alloc(m_Header, GCHandleType.Pinned);
            m_Header.dwUser         = (IntPtr)GCHandle.Alloc(this);
            m_HeaderData            = new byte[size];
            m_HeaderDataHandle      = GCHandle.Alloc(m_HeaderData, GCHandleType.Pinned);
            m_Header.lpData         = m_HeaderDataHandle.AddrOfPinnedObject();
            m_Header.dwBufferLength = size;

            var errorCode = (MMErrors)WaveNative.waveOutPrepareHeader(m_WaveOut, ref m_Header, Marshal.SizeOf(m_Header));

            if (errorCode != MMErrors.MMSYSERR_NOERROR)
            {
                throw new MixerException(errorCode, Audio.GetErrorDescription(FuncName.fnCustom, errorCode));
            }
        }
示例#14
0
 private void AskSoundForm_Load(object sender, EventArgs e)
 {
     if (WaveNative.waveInGetNumDevs() == 0)
     {
         consoletext += DateTime.Now.ToString() + " : No audio input devices detected\r\n";
     }
     else
     {
         consoletext += DateTime.Now.ToString() + " : Audio input device detected\r\n";
         if (_isPlayer == true)
         {
             _streamOut = new FifoStream();
         }
         _audioFrame = new AudioFrame(_isTest);
         //_audioFrame.IsDetectingEvents = TVSender.Properties.Settings.Default.SettingIsDetectingEvents;
         //_audioFrame.AmplitudeThreshold = TVSender.Properties.Settings.Default.SettingAmplitudeThreshold;
         _streamMemory = new MemoryStream();
         Start();
     }
 }
示例#15
0
        void writeAudioBlock(IntPtr hWaveOut, byte[] block, int size)
        {
            WaveNative.WaveHdr header = new WaveNative.WaveHdr();
            //ZeroMemory(&header, sizeof(WAVEHDR));
            header.dwBufferLength = size;
            GCHandle hObject = GCHandle.Alloc(block, GCHandleType.Pinned);

            header.lpData = hObject.AddrOfPinnedObject();
            if (hObject.IsAllocated)
            {
                hObject.Free();
            }
            WaveNative.waveOutPrepareHeader(hWaveOut, ref header, Marshal.SizeOf(header));
            WaveNative.waveOutWrite(hWaveOut, ref header, Marshal.SizeOf(header));
            Thread.Sleep(500);
            while (WaveNative.waveOutUnprepareHeader(hWaveOut, ref header, Marshal.SizeOf(header)) == WaveNative.MMSYSERR_NOERROR)
            {
                Thread.Sleep(100);
            }
        }
示例#16
0
        private void _Stop()
        {
            threadWorking = false;
            queue.Close();
            foreach (var thread in threads)
            {
                thread.Join();
            }

            if (waveOut != IntPtr.Zero)
            {
                WaveNative.waveOutClose(waveOut);
                waveOut = IntPtr.Zero;
            }

            if (pcm != IntPtr.Zero)
            {
                Asound.snd_pcm_close(pcm);
                pcm = IntPtr.Zero;
            }
        }
示例#17
0
        public void PutSample(WaveDataType type)
        {
            int ret;
            int size    = type.size;
            int rate    = type.sample_rate == 0 ? 44100 : type.sample_rate;
            int bit     = type.bit_per_sample == 0 ? 16 : type.bit_per_sample;
            int channel = type.channel == 0 ? 2 : type.channel;

            if (waveOut == IntPtr.Zero)
            {
                WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(rate, bit, channel);
                ret = WaveNative.waveOutOpen(out waveOut, -1, fmt, null, 0, WaveNative.CALLBACK_NULL);
                if (ret != WaveNative.MMSYSERR_NOERROR)
                {
                    throw new Exception("can not open wave device");
                }
            }


            queue.Enqueue(type);
        }
示例#18
0
        private void PlayUsingWaveOut(AudioFrame frame)
        {
            var type = frame.WaveDate;
            int ret;
            int size    = type.size;
            int rate    = type.sample_rate == 0 ? 44100 : type.sample_rate;
            int bit     = type.bit_per_sample == 0 ? 16 : type.bit_per_sample;
            int channel = type.channel == 0 ? 2 : type.channel;

            if (waveOut == IntPtr.Zero)
            {
                WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(rate, bit, channel);
                ret = WaveNative.waveOutOpen(out waveOut, -1, fmt, null, 0, WaveNative.CALLBACK_NULL);
                if (ret != WaveNative.MMSYSERR_NOERROR)
                {
                    throw new Exception("can not open wave device");
                }
            }


            queue.Enqueue(type);
            //ret = WriteWaveOut(frame);
        }
示例#19
0
        public static WaveOutDeviceInfo[] GetWaveOutDevices()
        {
            // enumerate WaveOut devices
            int devices = WaveNative.waveOutGetNumDevs();

            WaveOutDeviceInfo[]    result = new WaveOutDeviceInfo[devices];
            WaveNative.WAVEOUTCAPS caps   = new WaveNative.WAVEOUTCAPS();

            for (int i = 0; i < devices; i++)
            {
                WaveNative.waveOutGetDevCaps((IntPtr)i, ref caps, (uint)Marshal.SizeOf(caps));
                result[i]             = new WaveOutDeviceInfo();
                result[i].DisplayName = caps.szPname;
            }

            // expand names
            GCHandle result_handle = GCHandle.Alloc(result);

            WaveNative.DirectSoundEnumerate(new WaveNative.DSEnumCallback(EnumCallback), (IntPtr)result_handle);
            result_handle.Free();

            return(result);
        }
示例#20
0
文件: Audio.cs 项目: modernstar/core
        private AudioDevice GetDefaultDevice(MixerType mixerType)
        {
            MMErrors errorCode = 0;
            IntPtr   hWave     = IntPtr.Zero;
            IntPtr   hMixer    = IntPtr.Zero;

            try
            {
                WAVEFORMATEX waveFormat = WAVEFORMATEX.Empty;

                waveFormat.formatTag       = WaveFormatTag.PCM;
                waveFormat.nChannels       = 1;
                waveFormat.nSamplesPerSec  = 8000;
                waveFormat.wBitsPerSample  = 8;
                waveFormat.nBlockAlign     = (short)(waveFormat.nChannels * (waveFormat.wBitsPerSample / 8));
                waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
                waveFormat.cbSize          = 0;

                // Get default mixer HWND
                if (mixerType == MixerType.Recording)
                {
                    errorCode = (MMErrors)WaveNative.waveInOpen(out hWave, WaveNative.WAVE_MAPPER, ref waveFormat, CallbackForm.Handle, IntPtr.Zero, (int)CallBackFlag.CALLBACK_WINDOW);
                }
                else if (mixerType == MixerType.Playback)
                {
                    errorCode = (MMErrors)WaveNative.waveOutOpen(out hWave, WaveNative.WAVE_MAPPER, ref waveFormat, CallbackForm.Handle, IntPtr.Zero, (int)CallBackFlag.CALLBACK_WINDOW);
                }
                if (errorCode != MMErrors.MMSYSERR_NOERROR)
                {
                    // TODO: Log error!!!
                    //throw new MixerException(errorCode, Audio.GetErrorDescription(FuncName.fnWaveInOpen, errorCode));
                    return(null);
                }

                if (mixerType == MixerType.Recording)
                {
                    errorCode = (MMErrors)MixerNative.mixerOpen(out hMixer, hWave, IntPtr.Zero, IntPtr.Zero, ((uint)MIXER_OBJECTFLAG.HWAVEIN));
                }
                else if (mixerType == MixerType.Playback)
                {
                    errorCode = (MMErrors)MixerNative.mixerOpen(out hMixer, hWave, IntPtr.Zero, IntPtr.Zero, ((uint)MIXER_OBJECTFLAG.HWAVEOUT));
                }
                if (errorCode != MMErrors.MMSYSERR_NOERROR)
                {
                    // TODO: Log error!!!
                    //throw new MixerException(errorCode, Mixers.GetErrorDescription(FuncName.fnMixerOpen, errorCode));
                    return(null);
                }

                int deviceId = -1;
                errorCode = (MMErrors)MixerNative.mixerGetID(hMixer, ref deviceId, MIXER_OBJECTFLAG.MIXER);
                if (errorCode != MMErrors.MMSYSERR_NOERROR)
                {
                    // TODO: Log error!!!
                    //throw new MixerException(errorCode, Mixers.GetErrorDescription(FuncName.fnMixerGetID, errorCode));
                    return(null);
                }

                MIXERCAPS wic = new MIXERCAPS();
                errorCode = (MMErrors)MixerNative.mixerGetDevCaps(deviceId, ref wic, Marshal.SizeOf(wic));
                if (errorCode != MMErrors.MMSYSERR_NOERROR)
                {
                    // TODO: Log error!!!
                    //throw new MixerException(errorCode, GetErrorDescription(FuncName.fnMixerGetDevCaps, errorCode));
                    return(null);
                }

                return(GetAudioDeviceByName(wic.szPname, mixerType == MixerType.Playback));
            }
            finally
            {
                if (hMixer != IntPtr.Zero)
                {
                    MixerNative.mixerClose(hMixer);
                }
                if (hWave != IntPtr.Zero && mixerType == MixerType.Playback)
                {
                    WaveNative.waveOutClose(hWave);
                }
                if (hWave != IntPtr.Zero && mixerType == MixerType.Recording)
                {
                    WaveNative.waveInClose(hWave);
                }
            }
        }