예제 #1
0
 protected void RaiseDataAvailable(WaveInBuffer buffer)
 {
     if (DataAvailable != null && buffer.Recorded > 0)
     {
         DataAvailable(this, new DataAvailableEventArgs(buffer.Buffer, 0, buffer.Recorded, WaveFormat));
     }
 }
예제 #2
0
 private void RaiseDataAvailable(WaveInBuffer buffer)
 {
     if (DataAvailable != null && buffer.WaveHeader.bytesRecorded > 0)
     {
         DataAvailable(this,
                       new DataAvailableEventArgs(buffer.Buffer, 0, buffer.WaveHeader.bytesRecorded, WaveFormat));
     }
 }
        private void RaiseDataAvailable(WaveInBuffer buffer)
        {
            EventHandler <DataAvailableEventArgs> handler = this.DataAvailable;

            if (handler != null && buffer.WaveHeader.bytesRecorded > 0)
            {
                handler(this,
                        new DataAvailableEventArgs(buffer.Buffer, 0, buffer.WaveHeader.bytesRecorded, WaveFormat));
            }
        }
예제 #4
0
        private void CreateBuffers()
        {
            int count = _bufferCount;
            int size  = (int)WaveFormat.MillisecondsToBytes(BufferSizeMs);

            WaveInBuffer[] buffers = new WaveInBuffer[count];
            for (int i = 0; i < count; i++)
            {
                WaveInBuffer buffer = new WaveInBuffer(this, size);
                buffer.Initialize();
                buffers[i] = buffer;
            }
            _buffers = buffers;
        }
        private void InitializeInternal()
        {
            var supportedFormats = new Queue <WaveFormat>(Device.SupportedFormats
                                                          .OrderBy(x => Math.Abs(x.SampleRate - WaveFormat.SampleRate))
                                                          .ThenBy(x => Math.Abs(x.BitsPerSample - WaveFormat.BitsPerSample))
                                                          .ThenBy(x => Math.Abs(x.Channels - WaveFormat.Channels)));
            var finalFormat = WaveFormat;

            do
            {
                try
                {
                    _waveInHandle = CreateWaveInHandle(finalFormat);
                }
                catch (MmException exception)
                {
                    if (exception.Result == MmResult.BadFormat && supportedFormats.Count > 0)
                    {
                        finalFormat = supportedFormats.Dequeue();
                    }
                    else if (exception.Result == MmResult.BadFormat && supportedFormats.Count == 0)
                    {
                        throw new Exception("No valid format could be found.", exception);
                    }
                    else
                    {
                        throw;
                    }
                }
            } while (_waveInHandle == IntPtr.Zero);

            _failedBuffers.Clear();
            var bufferSize = (int)WaveFormat.MillisecondsToBytes(_latency);

            _buffers = new WaveInBuffer[BufferCount];
            for (int i = 0; i < _buffers.Length; i++)
            {
                _buffers[i] = new WaveInBuffer(_waveInHandle, bufferSize, (IntPtr)i);
            }
        }
예제 #6
0
파일: WaveIn.cs 프로젝트: opcon/cscore
 private void RaiseDataAvailable(WaveInBuffer buffer)
 {
     EventHandler<DataAvailableEventArgs> handler = this.DataAvailable;
     if (handler != null && buffer.WaveHeader.bytesRecorded > 0)
     {
         handler(this,
             new DataAvailableEventArgs(buffer.Buffer, 0, buffer.WaveHeader.bytesRecorded, WaveFormat));
     }
 }
예제 #7
0
파일: WaveIn.cs 프로젝트: opcon/cscore
        private void InitializeInternal()
        {
            var supportedFormats = new Queue<WaveFormat>(Device.SupportedFormats
                .OrderBy(x => Math.Abs(x.SampleRate - WaveFormat.SampleRate))
                .ThenBy(x => Math.Abs(x.BitsPerSample - WaveFormat.BitsPerSample))
                .ThenBy(x => Math.Abs(x.Channels - WaveFormat.Channels)));
            var finalFormat = WaveFormat;
            do
            {
                try
                {
                    _waveInHandle = CreateWaveInHandle(finalFormat);
                }
                catch (MmException exception)
                {
                    if (exception.Result == MmResult.BadFormat && supportedFormats.Count > 0)
                        finalFormat = supportedFormats.Dequeue();
                    else if (exception.Result == MmResult.BadFormat && supportedFormats.Count == 0)
                        throw new Exception("No valid format could be found.", exception);
                    else
                        throw;
                }
            } while (_waveInHandle == IntPtr.Zero);

            _failedBuffers.Clear();
            var bufferSize = (int) WaveFormat.MillisecondsToBytes(_latency);
            _buffers = new WaveInBuffer[BufferCount];
            for (int i = 0; i < _buffers.Length; i++)
            {
                _buffers[i] = new WaveInBuffer(_waveInHandle, bufferSize, (IntPtr) i);
            }
        }
예제 #8
0
파일: WaveIn.cs 프로젝트: opcon/cscore
 private void FireUpBuffer(WaveInBuffer buffer)
 {
     buffer.AddBufferToQueue();
     Interlocked.Increment(ref _activeBuffers);
 }
 private void FireUpBuffer(WaveInBuffer buffer)
 {
     buffer.AddBufferToQueue();
     Interlocked.Increment(ref _activeBuffers);
 }
예제 #10
0
        private void Callback(IntPtr handle, WaveMsg msg, IntPtr user, WaveHeader header, IntPtr reserved)
        {
            Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "|" + msg);
            if (_waveInHandle != handle)
            {
                return;
            }

            if (msg == WaveMsg.WIM_DATA)
            {
                if (_callbackThread == null)
                {
                    _callbackThread = Thread.CurrentThread;
                }

                //Debug.Assert(_callbackThread == Thread.CurrentThread, "Strange thread?");
                if (_callbackThread != Thread.CurrentThread)
                {
                    Debugger.Break();
                }

                var          index  = (int)header.userData;
                WaveInBuffer buffer = _buffers[index];

                Interlocked.Decrement(ref _activeBuffers);

                if (Monitor.TryEnter(_lockObj, 10))
                {
                    try
                    {
callback0:
                        //only add buffer to queue again, if we are still recording
                        if (buffer != null && RecordingState != RecordingState.Stopped)
                        {
                            try
                            {
                                //todo: should we care about recordingstate when firing dataavailable?
                                RaiseDataAvailable(buffer);
                                FireUpBuffer(buffer);
                            }
                            catch (Exception exception)
                            {
                                StopFromCallback(exception);
                            }
                        }

                        if (_failedBuffers.Count > 0)
                        {
                            while (_failedBuffers.Count > 0 &&
                                   (buffer = _buffers[(index = _failedBuffers.Dequeue())]).IsInQueue == false)
                            {
                                Debug.WriteLine("Already queued.");
                            }
                            if (buffer != null && !buffer.IsInQueue)
                            {
                                Debug.WriteLine("Failed buffer: " + index);
                                goto callback0;
                            }
                        }
                    }
                    finally
                    {
                        Monitor.Exit(_lockObj);
                    }
                }
                else
                {
                    _failedBuffers.Enqueue(index);
                }

                if (_activeBuffers <= 0)
                {
                    StopFromCallback(null);
                }

                _callbackThread = null;
            }
            else if (msg == WaveMsg.WIM_CLOSE)
            {
                if (RecordingState != RecordingState.Stopped)
                {
                    StopFromCallback(null);
                }
            }
        }
예제 #11
0
파일: WaveIn.cs 프로젝트: hoangduit/cscore
 private void RaiseDataAvailable(WaveInBuffer buffer)
 {
     if (DataAvailable != null && buffer.WaveHeader.bytesRecorded > 0)
     {
         DataAvailable(this,
             new DataAvailableEventArgs(buffer.Buffer, 0, buffer.WaveHeader.bytesRecorded, WaveFormat));
     }
 }
예제 #12
0
파일: WaveIn.cs 프로젝트: CheViana/AudioLab
        private void CreateBuffers()
        {
            int count = _bufferCount;
            int size = (int)WaveFormat.MillisecondsToBytes(BufferSizeMs);

            WaveInBuffer[] buffers = new WaveInBuffer[count];
            for (int i = 0; i < count; i++)
            {
                WaveInBuffer buffer = new WaveInBuffer(this, size);
                buffer.Initialize();
                buffers[i] = buffer;
            }
            _buffers = buffers;
        }
예제 #13
0
파일: WaveIn.cs 프로젝트: CheViana/AudioLab
 protected void RaiseDataAvailable(WaveInBuffer buffer)
 {
     if (DataAvailable != null && buffer.Recorded > 0)
     {
         DataAvailable(this, new DataAvailableEventArgs(buffer.Buffer, 0, buffer.Recorded, WaveFormat));
     }
 }