Пример #1
0
        /// <summary>
        ///   Starts recording audio from the sound card
        /// </summary>
        ///
        private void btnRecord_Click(object sender, EventArgs e)
        {
            // Create capture device
            source = new AudioCaptureDevice()
            {
                // Listen on 22050 Hz
                DesiredFrameSize = 4096,
                SampleRate       = 22050,

                // We will be reading 16-bit PCM
                Format = SampleFormat.Format16Bit
            };

            // Wire up some events
            source.NewFrame         += source_NewFrame;
            source.AudioSourceError += source_AudioSourceError;

            // Create buffer for wavechart control
            current = new float[source.DesiredFrameSize];

            // Create stream to store file
            stream  = new MemoryStream();
            encoder = new WaveEncoder(stream);

            // Start
            source.Start();
            updateButtons();
        }
Пример #2
0
        public Encoder(EventHandler <NewFrameEventArgs> NewFrame, EventHandler <AudioSourceErrorEventArgs> AudioSourceError)
        {
            AudioDeviceCollection audioDevices = new AudioDeviceCollection(AudioDeviceCategory.Capture);
            AudioDeviceInfo       INfo         = null;

            foreach (var item in audioDevices)
            {
                INfo = item;
            }
            // Create capture device
            this.source = new AudioCaptureDevice(INfo.Guid)
            {
                // Listen on 22050 Hz
                DesiredFrameSize = FRAME_SIZE,
                SampleRate       = SAMPLE_RATE,

                // We will be reading 16-bit PCM
                Format = SampleFormat.Format16Bit
            };


            // Wire up some events
            source.NewFrame         += NewFrame;
            source.AudioSourceError += AudioSourceError;

            // Create buffer for wavechart control
            this.current = new float[source.DesiredFrameSize];

            // Create stream to store file
            this.stream  = new MemoryStream();
            this.encoder = new WaveEncoder(stream);
        }
Пример #3
0
 public WaveStream(AudioDecoder decoder)
 {
     this.decoder = decoder;
     this.encoder = new WaveEncoder(decoder, new MemoryStream(), decoder.StreamInfo);
     dataBuffer = new byte[DATA_BUFFER_SIZE];
     bufferHead = 0;
     bufferTail = 0;
 }
Пример #4
0
 public static void SalvarSinal(Signal signal)
 {
     File.Delete(@"C:\a\teste.wav");
     using (var fs = new FileStream(@"C:\a\teste.wav", FileMode.CreateNew))
     {
         var audioEncoder = new WaveEncoder(fs);
         audioEncoder.Encode(signal);
     }
 }
        private void initalizeAudioStuff()
        {
            try
            {
                AudioSource = new AudioCaptureDevice();
                AudioDeviceInfo info = null;
                var             adc  = new AudioDeviceCollection(AudioDeviceCategory.Capture);
                foreach (var ad in adc)
                {
                    string desc = ad.Description;
                    if (desc.IndexOf("Audio") > -1)
                    {
                        info = ad;
                    }
                }
                if (info == null)
                {
                    AudioSource = new AudioCaptureDevice();
                }
                else
                {
                    AudioSource = new AudioCaptureDevice(info);
                }

                //AudioCaptureDevice source = new AudioCaptureDevice();
                AudioSource.DesiredFrameSize = 4096;
                AudioSource.SampleRate       = 44100;
                //int sampleRate = 44100;
                //int sampleRate = 22050;

                AudioSource.NewFrame += AudioSource_NewFrame;
                // AudioSource.Format = SampleFormat.Format64BitIeeeFloat;
                AudioSource.AudioSourceError += AudioSource_AudioSourceError;
                // AudioSource.Start();
                int x = 1;
            }
            catch
            {
            }

            // Create buffer for wavechart control
            current = new float[AudioSource.DesiredFrameSize];

            // Create stream to store file
            stream  = new MemoryStream();
            encoder = new WaveEncoder(stream);

            frames  = 0;
            samples = 0;



            // Start
            AudioSource.Start();
        }
Пример #6
0
        public Recorder(int xlocation, int ylocation, int width, int height, AudioDeviceInfo audioDevice)
        {
            audioEncoder  = new WaveEncoder(@"D:\Projects\test_audio.wav");
            audio         = new AudioCaptureDevice(audioDevice);
            captureStream = new ScreenCaptureStream(new System.Drawing.Rectangle(xlocation, ylocation, width, height));
            video         = new VideoFileWriter();

            audio.DesiredFrameSize = 4096;
            audio.SampleRate       = 44100;
            audio.NewFrame        += Audio_NewFrame;

            captureStream.FrameInterval = 1;
            captureStream.NewFrame     += CaptureStream_NewFrame;
        }
Пример #7
0
        /// <summary>
        ///   Starts recording audio from the sound card
        ///   开始从声卡录制音频
        /// </summary>
        ///
        private void BtnRecord_Click(object sender, EventArgs e)
        {
            // Create capture device
            //[1]创建本地音频捕获设备
            #region 本地音频捕获设备

            /*
             * 本地音频捕获设备(即麦克风)的音频源。
             * 备考:此accord.audio.iaudiosource捕获从本地音频获取的音频数据
             * 捕获设备,如麦克风。使用DirectSound捕获音频
             * 通过slimdx。//有关如何列出捕获设备的说明,
             * 请参见accord.directsound.audioDeviceCollection//文档页。
             */
            #endregion
            source = new AudioCaptureDevice()
            {
                //获取或设置所需的帧大小。
                //监听22050赫兹
                // Listen on 22050 Hz
                DesiredFrameSize = 4096,
                SampleRate       = 22050,

                #region 我们将读取16位PCM(脉冲编码调制)
                //我们将读取16位PCM(脉冲编码调制)
                // We will be reading 16-bit PCM
                //PCM 即脉冲编码调制 (Pulse Code Modulation)。
                //https://baike.baidu.com/item/pcm%E7%BC%96%E7%A0%81/10865033?fr=aladdin
                #endregion

                Format = SampleFormat.Format16Bit
            };

            // Wire up some events
            //注册音频资源事件
            source.NewFrame         += source_NewFrame;
            source.AudioSourceError += source_AudioSourceError;

            // Create buffer for wavechart control
            //每帧上要读取的样本量。
            Current = new float[source.DesiredFrameSize];

            // Create stream to store file
            //创建流以存储文件
            stream  = new MemoryStream();
            encoder = new WaveEncoder(stream);

            // Start
            source.Start();
            updateButtons();
        }
Пример #8
0
        private void StopVoiceCapture()
        {
            AudioCapture.SignalToStop();

            FrameRate    = (double)BitmapsCount / _waveEncoder.Duration * 1000;
            BitmapsCount = 0;

            AudioCapture.NewFrame         -= NewAudioFrame;
            AudioCapture.AudioSourceError -= AudioError;
            AudioCapture = null;
            _waveEncoder = null;
            _audioStream.Close();
            _audioStream = null;
            stopAll     -= StopVoiceCapture;
        }
Пример #9
0
        private void StartVoiceCapture(string fileName)
        {
            AudioCapture = new AudioCaptureDevice(new Guid(Properties.UserSettings.Default.Microphone));
            // Specify capturing options
            AudioCapture.DesiredFrameSize = 4096;
            AudioCapture.SampleRate       = 44100;

            AudioCapture.NewFrame         += NewAudioFrame;
            AudioCapture.AudioSourceError += AudioError;

            _audioStream = new FileStream(baseDirectory + "AppFiles/TempFiles/" + fileName, FileMode.Create);
            _waveEncoder = new WaveEncoder(_audioStream);

            AudioCapture.Start();
        }
Пример #10
0
        public async void RecordAudioToFile(string _path = "1.wav")
        {
            if (_microphone == null)
            {
                SetRecorder();
            }
            if (FinishRecord == null)
            {
                throw new Exception("FinishRecord function not set!");
            }
            if (_path == null)
            {
                throw new ArgumentNullException("Error! Argument path is null!");
            }
            else if (_path != "1.wav")
            {
                RecordPath = _path;
            }


            Task t = new Task(OnListeningAsync);

            try
            {
                _fileStream = new FileStream(RecordPath, FileMode.OpenOrCreate, FileAccess.Write);
                _audioSaver = new WaveEncoder();
                _audioSaver.Open(_fileStream);
                IsRecordContinue = true;
                _microphone.Start();
                t.Start();
                await t;
                _microphone.WaitForStop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                t.Dispose();
                _microphone.Dispose();
                _audioSaver?.Close();
                _fileStream?.Close();
                IsRecordContinue = false;
            }
        }
Пример #11
0
        private void adjustVolume(float value)
        {
            // First, we rewind the stream
            stream.Seek(0, SeekOrigin.Begin);

            // Then we create a decoder for it
            decoder = new WaveDecoder(stream);

            var signal = decoder.Decode();

            // We apply the volume filter
            var volume = new VolumeFilter(value);

            volume.ApplyInPlace(signal);

            // Then we store it again
            stream.Seek(0, SeekOrigin.Begin);
            encoder = new WaveEncoder(stream);
            encoder.Encode(signal);
        }
Пример #12
0
        private void btnRecord_Click(object sender, EventArgs e)
        {
            // Create capture device
            source = new AudioCaptureDevice();
            source.DesiredFrameSize  = 4096;
            source.SampleRate        = 22050;
            source.NewFrame         += source_NewFrame;
            source.AudioSourceError += source_AudioSourceError;

            // Create buffer for wavechart control
            current = new float[source.DesiredFrameSize];

            // Create stream to store file
            stream  = new MemoryStream();
            encoder = new WaveEncoder(stream);

            // Start
            source.Start();
            updateButtons();
        }
Пример #13
0
        public void StopRecording()
        {
            if (IsRecording())
            {
                MergeAudioAndVideo();
            }

            if (videoFileWriter != null)
            {
                videoFileWriter.Close();
                videoFileWriter.Dispose();
                videoFileWriter = null;
            }

            if (audioFileWriter != null || waveEncoder != null)
            {
                waveEncoder.Close();
                waveEncoder     = null;
                audioFileWriter = null;
            }
        }
Пример #14
0
        public void WaveEncoderConstructorTest()
        {
            // Load a file in PCM 16bpp format
            // Number of samples: 352.800
            // Number of frames:  176.400
            // Sample rate:        44.100 Hz
            // Block align:         4
            // Channels:            2
            // Duration:            4.0   s
            // Bytes:             705.644
            // Bitrate:           1411kbps

            // sizeof(float) = 4
            // sizeof(int)   = 4
            // sizeof(short) = 2

            UnmanagedMemoryStream sourceStream      = Properties.Resources.Grand_Piano___Fazioli___major_A_middle;
            MemoryStream          destinationStream = new MemoryStream();

            // Create a decoder for the source stream
            WaveDecoder sourceDecoder = new WaveDecoder(sourceStream);

            Assert.AreEqual(2, sourceDecoder.Channels);
            Assert.AreEqual(352800, sourceDecoder.Samples);
            Assert.AreEqual(176400, sourceDecoder.Frames);
            Assert.AreEqual(4000, sourceDecoder.Duration);
            Assert.AreEqual(44100, sourceDecoder.SampleRate);
            Assert.AreEqual(16, sourceDecoder.BitsPerSample);
            Assert.AreEqual(1411200, sourceDecoder.AverageBitsPerSecond);

            // Decode the signal in the source stream
            Signal sourceSignal = sourceDecoder.Decode();

            Assert.AreEqual(352800, sourceSignal.Samples);
            Assert.AreEqual(176400, sourceSignal.Length);
            Assert.AreEqual(4000, sourceSignal.Duration);
            Assert.AreEqual(2, sourceSignal.Channels);
            Assert.AreEqual(44100, sourceSignal.SampleRate);
            Assert.AreEqual(sizeof(float) * 352800, sourceSignal.RawData.Length);
            Assert.AreEqual(sizeof(short) * 352800, sourceDecoder.Bytes);


            // Create a encoder for the destination stream
            WaveEncoder encoder = new WaveEncoder(destinationStream);

            // Encode the signal to the destination stream
            encoder.Encode(sourceSignal);

            Assert.AreEqual(2, encoder.Channels);
            Assert.AreEqual(352800, encoder.Samples);
            Assert.AreEqual(176400, encoder.Frames);
            Assert.AreEqual(4000, encoder.Duration);
            Assert.AreEqual(44100, encoder.SampleRate);
            Assert.AreEqual(32, encoder.BitsPerSample);
            Assert.AreEqual(sizeof(float) * 352800, encoder.Bytes);


            // Rewind both streams, them attempt to read the destination
            sourceStream.Seek(0, SeekOrigin.Begin);
            destinationStream.Seek(0, SeekOrigin.Begin);

            // Create a decoder to read the destination stream
            WaveDecoder destDecoder = new WaveDecoder(destinationStream);

            Assert.AreEqual(2, destDecoder.Channels);
            Assert.AreEqual(176400, destDecoder.Frames);
            Assert.AreEqual(352800, destDecoder.Samples);
            Assert.AreEqual(4000, destDecoder.Duration);
            Assert.AreEqual(44100, destDecoder.SampleRate);
            Assert.AreEqual(32, destDecoder.BitsPerSample);
            Assert.AreEqual(1411200, sourceDecoder.AverageBitsPerSecond);


            // Decode the destination stream
            Signal destSignal = destDecoder.Decode();

            // Assert that the signal which has been saved to the destination
            // stream and the signal which has just been read from this same
            // stream are identical
            Assert.AreEqual(sourceSignal.Length, destSignal.Length);
            Assert.AreEqual(sourceSignal.SampleFormat, destSignal.SampleFormat);
            Assert.AreEqual(sourceSignal.SampleRate, destSignal.SampleRate);
            Assert.AreEqual(sourceSignal.Samples, destSignal.Samples);
            Assert.AreEqual(sourceSignal.Duration, destSignal.Duration);


            for (int i = 0; i < sourceSignal.RawData.Length; i++)
            {
                byte actual   = sourceSignal.RawData[i];
                byte expected = destSignal.RawData[i];
                Assert.AreEqual(expected, actual);
            }
        }
Пример #15
0
 private void InitAudioFileWriter()
 {
     audioFileWriter = new FileStream(audioOutputFile, FileMode.Create);
     waveEncoder     = new WaveEncoder(audioFileWriter);
 }
Пример #16
0
        public void WaveEncoderConstructorTest()
        {
            string basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources");

            #region doc_properties
            // Let's say we would like to decode a wave file:
            string fileName = Path.Combine(basePath, "a.wav");

            // File is in PCM 16bpp format:
            // Number of samples: 352.800
            // Number of frames:  176.400
            // Sample rate:        44.100 Hz
            // Block align:         4
            // Channels:            2
            // Duration:            4.0   s
            // Bytes:             705.644
            // Bitrate:           1411kbps

            // First, create a decoder for the file stream:
            var sourceDecoder = new WaveDecoder(fileName);

            // Now we can use it to check some of the the stream properties:
            int numberOfChannels     = sourceDecoder.NumberOfChannels; // 2
            int numberOfSamples      = sourceDecoder.NumberOfSamples;  // 352800
            int numberOfFrames       = sourceDecoder.NumberOfFrames;   // 176400
            int durationMilliseconds = sourceDecoder.Duration;         // 4000
            int sampleRate           = sourceDecoder.SampleRate;       // 44100
            int bitsPerSample        = sourceDecoder.BitsPerSample;    // 16
            int bps = sourceDecoder.AverageBitsPerSecond;              // 141200

            // Decode the signal in the source stream:
            Signal sourceSignal = sourceDecoder.Decode();

            // As we can see, all properties are kept in the signal:
            int      signalChannels   = sourceSignal.NumberOfChannels; // 2
            int      signalSamples    = sourceSignal.NumberOfSamples;  // 352800
            int      signalFrames     = sourceSignal.NumberOfFrames;   // 176400
            TimeSpan signalDuration   = sourceSignal.Duration;         // {00:00:04}
            int      signalLength     = sourceSignal.Length;           // 176400
            int      signalSampleRate = sourceSignal.SampleRate;       // 44100
            int      signalBytes      = sourceSignal.NumberOfBytes;    // 1411200

            // And this is the total number of bytes that have been read:
            int numberOfBytes = sourceDecoder.NumberOfBytesRead; // 705600
            #endregion


            Assert.AreEqual(2, numberOfChannels);
            Assert.AreEqual(352800, numberOfSamples);
            Assert.AreEqual(176400, numberOfFrames);
            Assert.AreEqual(4000, durationMilliseconds);
            Assert.AreEqual(44100, sampleRate);
            Assert.AreEqual(16, bitsPerSample);
            Assert.AreEqual(1411200, bps);
            Assert.AreEqual(sizeof(short) * 352800, numberOfBytes);

            Assert.AreEqual(352800, signalSamples);
            Assert.AreEqual(176400, signalLength);
            Assert.AreEqual(4000, signalDuration.TotalMilliseconds);
            Assert.AreEqual(2, signalChannels);
            Assert.AreEqual(44100, signalSampleRate);
            Assert.AreEqual(sizeof(float) * 352800, signalBytes);

            MemoryStream destinationStream = new MemoryStream();

            // Create a encoder for the destination stream
            WaveEncoder encoder = new WaveEncoder(destinationStream);

            // Encode the signal to the destination stream
            encoder.Encode(sourceSignal);

            Assert.AreEqual(2, encoder.Channels);
            Assert.AreEqual(352800, encoder.Samples);
            Assert.AreEqual(176400, encoder.Frames);
            Assert.AreEqual(4000, encoder.Duration);
            Assert.AreEqual(44100, encoder.SampleRate);
            Assert.AreEqual(32, encoder.BitsPerSample);
            Assert.AreEqual(sizeof(float) * 352800, encoder.Bytes);


            // Rewind both streams, them attempt to read the destination
            destinationStream.Seek(0, SeekOrigin.Begin);

            // Create a decoder to read the destination stream
            WaveDecoder destDecoder = new WaveDecoder(destinationStream);
            Assert.AreEqual(2, destDecoder.Channels);
            Assert.AreEqual(176400, destDecoder.Frames);
            Assert.AreEqual(352800, destDecoder.Samples);
            Assert.AreEqual(4000, destDecoder.Duration);
            Assert.AreEqual(44100, destDecoder.SampleRate);
            Assert.AreEqual(32, destDecoder.BitsPerSample);
            Assert.AreEqual(1411200, sourceDecoder.AverageBitsPerSecond);


            // Decode the destination stream
            Signal destSignal = destDecoder.Decode();

            // Assert that the signal which has been saved to the destination
            // stream and the signal which has just been read from this same
            // stream are identical
            Assert.AreEqual(sourceSignal.Length, destSignal.Length);
            Assert.AreEqual(sourceSignal.SampleFormat, destSignal.SampleFormat);
            Assert.AreEqual(sourceSignal.SampleRate, destSignal.SampleRate);
            Assert.AreEqual(sourceSignal.Samples, destSignal.Samples);
            Assert.AreEqual(sourceSignal.Duration, destSignal.Duration);

            byte[] actual   = sourceSignal.ToByte();
            byte[] expected = destSignal.ToByte();
            Assert.AreEqual(expected, actual);
        }
Пример #17
0
 public void Init(IntPtr handle)
 {
     audioOutputDeviceOwnerHandle = handle;
     memoryStream = new MemoryStream();
     encoder      = new WaveEncoder(memoryStream);
 }