Example #1
0
        private void CaptureI16()
        {
            IntPtr ptr = IntPtr.Zero;
            int    totalSamples, samples = 0;

            try
            {
                long a, b; AudioClientBufferFlags flags;
                audioCaptureClient.GetBuffer(out ptr, out samples, out flags, out a, out b);
                if (ptr == IntPtr.Zero || samples <= 0)
                {
                    return;
                }
                totalSamples = samples * channelCount;
                if (i16Buf == null || i16Buf.Length < totalSamples)
                {
                    i16Buf = new short[totalSamples];
                }
                Marshal.Copy(ptr, i16Buf, 0, totalSamples);
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    audioCaptureClient.ReleaseBuffer(samples);
                }
            }
            int index = 0;

            while (index < samples)
            {
                int samplesToWrite = bufferSize - samplesWritten;
                int samplesLeft    = samples - index;
                if (samplesToWrite > samplesLeft)
                {
                    samplesToWrite = samplesLeft;
                }
                int sampleEnd = samplesWritten + samplesToWrite;
                for (int c = 0; c < channelCount; c++)
                {
                    float[] buffer    = buffers[c];
                    int     readIndex = index * channelCount + c;
                    for (int sample = samplesWritten; sample < sampleEnd; sample++, readIndex += channelCount)
                    {
                        buffer[sample] = i16Buf[readIndex] * (1.0f / 32768.0f);
                    }
                }
                index          += samplesToWrite;
                samplesWritten += samplesToWrite;
                if (samplesWritten >= bufferSize)
                {
                    receiveCallback(this, buffers);
                    samplesWritten = 0;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Gets a pointer to the buffer
        /// </summary>
        /// <returns>Pointer to the buffer</returns>
        public IntPtr GetBuffer(
            out int numFramesToRead,
            out AudioClientBufferFlags bufferFlags,
            out long devicePosition,
            out long qpcPosition)
        {
            IntPtr bufferPointer;

            Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out bufferPointer, out numFramesToRead, out bufferFlags, out devicePosition, out qpcPosition));
            return(bufferPointer);
        }
Example #3
0
        /// <summary>
        /// バッファの中の現在の1パケットを取得する
        /// <remarks>バッファ内の全データを取得するには、NextPacketSizeが0になるまでGetBuffer()を繰り返す必要がある</remarks>
        /// </summary>
        /// <param name="data">取得したデータのバイト列</param>
        /// <param name="numFramesToRead">取得したデータのフレーム数(ブロック単位)</param>
        /// <param name="flags"></param>
        /// <param name="devicePosition"></param>
        /// <param name="qpcPosition"></param>
        public void GetBuffer(
            out byte[] data,
            out uint numFramesToRead,
            out AudioClientBufferFlags flags,
            out ulong devicePosition,
            out ulong qpcPosition)
        {
            lock (_lockObj)
            {
                IntPtr p;
                int    hr = _RealClient.GetBuffer(
                    out p, out numFramesToRead,
                    out flags, out devicePosition,
                    out qpcPosition);
                Marshal.ThrowExceptionForHR(hr);
                if (numFramesToRead == 0)
                {
                    data = new byte[0];
                }
                else
                {
                    int byteSize = (int)numFramesToRead * _Parent.MixFormat.nBlockAlign;
                    data = new byte[byteSize];
                    Marshal.Copy(p, data, 0, byteSize);
                }

                hr = _RealClient.ReleaseBuffer(numFramesToRead);
                Marshal.ThrowExceptionForHR(hr);
            }
        }
Example #4
0
        private void ReadBuffer(Stream stream)
        {
            IntPtr p;
            int    count;
            long   devicePosition;
            long   qpcPosition;
            AudioClientBufferFlags flags;

            _capture.GetBuffer(out p, out count, out flags, out devicePosition, out qpcPosition);
            Marshal.Copy(p, this.Buffer, 0, count * this.FrameSize);
            _capture.ReleaseBuffer(count);

            stream.Write(this.Buffer, 0, count * this.FrameSize);
        }
        private void AudioPluseMonitorWork()
        {
            uint bufferCount      = 0;
            int  packageSize      = -1;
            int  numberFramToRead = -1;
            long devicePosition   = 0;
            long qpcPosition      = 0;
            AudioClientBufferFlags bufferFlag;
            IntPtr packageBuffer = IntPtr.Zero;
            var    blockAlign    = waveFormat.NBlockAlign;
            var    result        = _audioClient.GetBufferSize(ref bufferCount);
            // Calculate the actual duration of the allocated buffer.
            var hnsActualDuration = (double)REFTIMES_PER_SEC *
                                    bufferCount / waveFormat.NSamplesPerSec;

            while (isAudioPluseMonitor)
            {
                Thread.Sleep((int)(hnsActualDuration / REFTIMES_PER_MILLISEC / 2));
                result = _audioCaptureClient.GetNextPacketSize(out packageSize);
                if (result == 0 && packageSize > 0)
                {
                    while (packageSize > 0)
                    {
                        _audioCaptureClient.GetBuffer(out packageBuffer, out numberFramToRead, out bufferFlag, out devicePosition, out qpcPosition);
                        if (0 == numberFramToRead)
                        {
                            continue;
                        }
                        long lBytesToWrite = numberFramToRead * blockAlign;

                        if (AudioClientBufferFlags.Silent == bufferFlag)
                        {
                            _audiopluseCallBack(0, 0);
                        }
                        else
                        {
                            var pkgData = StructureToByteArray(packageBuffer);
                            ComputeDo3Band(pkgData, lBytesToWrite, waveFormat);
                        }
                        _audioCaptureClient.ReleaseBuffer(numberFramToRead);
                        result = _audioCaptureClient.GetNextPacketSize(out packageSize);
                    }
                }
                else
                {
                    _audiopluseCallBack(0, 0);
                }
            }
        }
        void Loop()
        {
            int padding = 0;

            _IAudioClient.GetCurrentPadding(out padding);

            while (true)
            {
                IntPtr pBuffer;
                System.Threading.Thread.Sleep(1);
                lock (this)
                {
                    uint  numFrame;
                    uint  flag;
                    ulong position, hpTimer;
                    bool  discontinuity = false;

                    _IAudioCaptureClient.GetBuffer(out pBuffer, out numFrame, out flag, out position, out hpTimer);

                    discontinuity = (flag & 1) != 0;
                    flag          = (uint)(flag & ~1);

                    if (numFrame > 0 && flag == 0 && pBuffer.ToInt64() != 0)
                    {
                        byte[] buffer = GetBuffer((int)(numFrame * _FrameSize));
                        System.Runtime.InteropServices.Marshal.Copy(pBuffer, buffer, 0, (int)(numFrame * _FrameSize));
                        _IAudioCaptureClient.ReleaseBuffer(numFrame);
                        _Stream.PushFrame(buffer);
                    }
                    else
                    {
                        _IAudioCaptureClient.ReleaseBuffer(numFrame);
                    }
                }
            }
        }
        /// <summary>
        /// Gets a pointer to the buffer
        /// </summary>
        /// <param name="numFramesToRead">Number of frames to read</param>
        /// <param name="bufferFlags">Buffer flags</param>
        /// <returns>Pointer to the buffer</returns>
        public IntPtr GetBuffer(
            out int numFramesToRead,
            out AudioClientBufferFlags bufferFlags)
        {
            IntPtr bufferPointer;
            long   devicePosition;
            long   qpcPosition;
            int    hresult = audioCaptureClientInterface.GetBuffer(out bufferPointer, out numFramesToRead, out bufferFlags, out devicePosition, out qpcPosition);

            if (hresult != 0)
            {
                this.EventWriterDLL.WriteLine(EventWriterDLL.SeverityTypes.Error, 0x01, "Error Code in AudioCaptureClient::GetBuffer: " + hresult);
            }
            return(bufferPointer);
        }
        public void recordingloop()
        {
            uint  packetLength;
            uint  numFramesAvailable;
            uint  flags;
            ulong junk1;
            ulong junk2;

            byte[]     pData;
            uint       AUDCLNT_BUFFERFLAGS_SILENT = 2;
            uint       bytespersample             = (uint)(waveformat.nChannels * waveformat.wBitsPerSample / 8);
            WaveFormat _wf        = new WaveFormat((int)waveformat.nSamplesPerSec, (int)waveformat.wBitsPerSample, (int)waveformat.nChannels);
            BE_CONFIG  _mp3config = new BE_CONFIG(_wf);
            Mp3Writer  _mp3writer = new Mp3Writer(memStream, _wf, _mp3config);

            int retVal = iAudioClient.Start();

            if (retVal != 0)
            {
                throw new Exception("IAudioClient.Start()");
            }
            keepgoing = true;
            while (keepgoing)
            {
                Thread.Sleep((int)(hnsActualDuration / 5000));
                retVal = iAudioCaptureClient.GetNextPacketSize(out packetLength);
                if (retVal != 0)
                {
                    throw new Exception("iAudioCaptureClient.GetNextPacketSize()");
                }
                while (packetLength != 0)
                {
                    IntPtr dataPtr = IntPtr.Zero;
                    retVal = iAudioCaptureClient.GetBuffer(out dataPtr, out numFramesAvailable, out flags, out junk1, out junk2);
                    if (retVal != 0)
                    {
                        throw new Exception("iAudioCaptureClient.GetBuffer()");
                    }
                    pData = new byte[bytespersample * numFramesAvailable];
                    if ((flags & AUDCLNT_BUFFERFLAGS_SILENT) != 0)
                    {
                        for (int i = 0; i < pData.Length; i++)
                        {
                            pData[i] = 0;
                        }
                    }
                    else
                    {
                        Marshal.Copy(dataPtr, pData, 0, (int)(bytespersample * numFramesAvailable));
                    }
                    retVal = iAudioCaptureClient.ReleaseBuffer(numFramesAvailable);
                    if (retVal != 0)
                    {
                        throw new Exception("iAudioCaptureClient.ReleaseBuffer()");
                    }

                    lock (synclock)
                    {
                        _mp3writer.Write(pData, 0, pData.Length);
                    }
                    if (notify != null)
                    {
                        notify();
                    }

                    retVal = iAudioCaptureClient.GetNextPacketSize(out packetLength);
                    if (retVal != 0)
                    {
                        throw new Exception("iAudioCaptureClient.GetNextPacketSize()");
                    }
                }
            }
            _mp3writer.Close();
            memStream.Close();
            memStream.Dispose();
            if (iAudioClient != null)
            {
                retVal = iAudioClient.Stop();
                if (retVal != 0)
                {
                    throw new Exception("iAudioClient.Stop()");
                }
            }
        }