Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the size of the next packet
        /// </summary>
        public int GetNextPacketSize()
        {
            int numFramesInNextPacket;

            Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetNextPacketSize(out numFramesInNextPacket));
            return(numFramesInNextPacket);
        }
Ejemplo n.º 3
0
        public void Read(Packet packet)
        {
            int captureSize = 0;

            while (_continueProcessing && captureSize == 0)
            {
                Checked(_audioCaptureClient.GetNextPacketSize(out captureSize), "GetNextPacketSize");

                if (captureSize == 0)
                {
                    WaitHandle.WaitAny(new WaitHandle[] { _stopReading, _dataAvailable });
                }
            }

            if (captureSize > 0)
            {
                _audioCaptureClient.GetBuffer(out var buffer, out var frames, out var flags, out var pos, out var s);

                //TODO: investigate which is better
                var time = _timeAdjuster.Add(s, Core.GetCurrentTime());
                packet.InitFromBuffer(buffer, frames * _bytesPerFrame, time);
                //Core.LogInfo($"DA: {frames * _bytesPerFrame} {s}");
                _audioCaptureClient.ReleaseBuffer(frames);
            }
            else
            {
                throw new OperationCanceledException();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the size of the next packet
        /// </summary>
        public int GetNextPacketSize()
        {
            int numFramesInNextPacket;
            int hresult = audioCaptureClientInterface.GetNextPacketSize(out numFramesInNextPacket);

            if (hresult != 0)
            {
                this.EventWriterDLL.WriteLine(EventWriterDLL.SeverityTypes.Error, 0x01, "Error Code in AudioCaptureClient::GetNextPacketSize: " + hresult);
            }
            return(numFramesInNextPacket);
        }
Ejemplo n.º 5
0
        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()");
                }
            }
        }