/// <summary> /// Event handler for audio sample being supplied by local capture device. /// </summary> private void LocalAudioSampleAvailable(object sender, WaveInEventArgs args) { //WaveBuffer wavBuffer = new WaveBuffer(args.Buffer.Take(args.BytesRecorded).ToArray()); //byte[] encodedSample = _audioEncoder.EncodeAudio(wavBuffer.ShortBuffer, _selectedSourceFormat, AudioSourceSamplingRate); byte[] encodedSample = _audioEncoder.EncodeAudio(args.Buffer.Take(args.BytesRecorded).ToArray(), _selectedSourceFormat, AudioSourceSamplingRate); OnAudioSourceEncodedSample?.Invoke((uint)encodedSample.Length, encodedSample); }
private unsafe void AudioDecoder_OnAudioFrame(ref AVFrame avFrame) { if (OnAudioSourceEncodedSample == null) { return; } // Avoid to create several times buffer of the same size if (_currentNbSamples != avFrame.nb_samples) { bufferSize = ffmpeg.av_samples_get_buffer_size(null, avFrame.channels, avFrame.nb_samples, AVSampleFormat.AV_SAMPLE_FMT_S16, 1); buffer = new byte[bufferSize]; _currentNbSamples = avFrame.nb_samples; } // Convert audio int dstSampleCount; fixed(byte *pBuffer = buffer) dstSampleCount = ffmpeg.swr_convert(_audioDecoder._swrContext, &pBuffer, bufferSize, avFrame.extended_data, avFrame.nb_samples).ThrowExceptionIfError(); Console.WriteLine($"nb_samples:{avFrame.nb_samples} - bufferSize:{bufferSize} - dstSampleCount:{dstSampleCount}"); if (dstSampleCount > 0) { // FFmpeg AV_SAMPLE_FMT_S16 will store the bytes in the correct endianess for the underlying platform. short[] pcm = buffer.Take(dstSampleCount * 2).Where((x, i) => i % 2 == 0).Select((y, i) => BitConverter.ToInt16(buffer, i * 2)).ToArray(); var encodedSample = _audioEncoder.EncodeAudio(pcm, _audioFormatManager.SelectedFormat); OnAudioSourceEncodedSample?.Invoke((uint)encodedSample.Length, encodedSample); } }
private void FileSourceDecoder_OnAudioFrame(byte[] buffer) { if (OnAudioSourceEncodedSample != null && buffer.Length > 0) { // FFmpeg AV_SAMPLE_FMT_S16 will store the bytes in the correct endianess for the underlying platform. short[] pcm = buffer.Where((x, i) => i % 2 == 0).Select((y, i) => BitConverter.ToInt16(buffer, i * 2)).ToArray(); var encodedSample = _audioEncoder.EncodeAudio(pcm, _audioFormatManager.SelectedFormat); OnAudioSourceEncodedSample((uint)encodedSample.Length, encodedSample); } }
/// <summary> /// Event handler for audio sample being supplied by local capture device. /// </summary> private void LocalAudioSampleAvailable(object sender, WaveInEventArgs args) { // Note NAudio.Wave.WaveBuffer.ShortBuffer does not take into account little endian. // https://github.com/naudio/NAudio/blob/master/NAudio/Wave/WaveOutputs/WaveBuffer.cs // WaveBuffer wavBuffer = new WaveBuffer(args.Buffer.Take(args.BytesRecorded).ToArray()); // byte[] encodedSample = _audioEncoder.EncodeAudio(wavBuffer.ShortBuffer, _audioFormatManager.SelectedFormat); byte[] buffer = args.Buffer.Take(args.BytesRecorded).ToArray(); short[] pcm = buffer.Where((x, i) => i % 2 == 0).Select((y, i) => BitConverter.ToInt16(buffer, i * 2)).ToArray(); byte[] encodedSample = _audioEncoder.EncodeAudio(pcm, _audioFormatManager.SelectedFormat); OnAudioSourceEncodedSample?.Invoke((uint)encodedSample.Length, encodedSample); }
void WriteDataCallback(byte[] buffer, int offset, int count) { var copy = new byte[count]; Buffer.BlockCopy(buffer, offset, copy, 0, count); byte[] encodedSample = _audioEncoder.EncodeAudio( copy.Where((x, i) => i % 2 == 0).Select((y, i) => (short)(copy[i * 2] << 8 | copy[i * 2 + 1])).ToArray(), _audioFormatManager.SelectedFormat); OnAudioSourceEncodedSample?.Invoke((uint)encodedSample.Length, encodedSample); }
void WriteDataCallback(byte[] buffer, int offset, int count) { byte[] encodedSample = _audioEncoder.EncodeAudio(buffer.Skip(offset).Take(count).ToArray(), _selectedSourceFormat, AudioSourceSamplingRate); OnAudioSourceEncodedSample?.Invoke((uint)encodedSample.Length, encodedSample); }