Skip to content

Instantly share code, notes, and snippets.

@amaitland
Created May 3, 2019 02:48
Show Gist options
  • Save amaitland/3846e438e330cfae458af45a2c1d164d to your computer and use it in GitHub Desktop.
Save amaitland/3846e438e330cfae458af45a2c1d164d to your computer and use it in GitHub Desktop.
AudioHandler
// Copyright © 2019 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using CefSharp.Enums;
namespace CefSharp.Example.Handlers
{
public class AudioHandler : IAudioHandler
{
private readonly MemoryStream stream = new MemoryStream();
private ChannelLayout channelLayout;
private int channelCount;
private int sampleRate;
void IAudioHandler.OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, int audioStreamId, IntPtr data, int noOfFrames, long pts)
{
var channelLayoutSize = 2;
var sizeOfData = noOfFrames * channelLayoutSize * 4;
byte[] managedData = new byte[sizeOfData];
Marshal.Copy(data, managedData, 0, sizeOfData);
stream.Write(managedData, 0, managedData.Length);
}
void IAudioHandler.OnAudioStreamStarted(IWebBrowser chromiumWebBrowser, IBrowser browser, int audioStreamId, int channels, ChannelLayout channelLayout, int sampleRate, int framesPerBuffer)
{
this.channelLayout = channelLayout;
this.channelCount = channels;
this.sampleRate = sampleRate;
}
void IAudioHandler.OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser, int audioStreamId)
{
//WriteWavHeader(memoryStream, false, (ushort)channelCount, 24, sampleRate, sampleRate * channelCount);
using (var file = File.OpenWrite("test.pcm"))
{
//file.Write(byteArray, 0, byteArray.Length);
stream.WriteTo(file);
}
}
// totalSampleCount needs to be the combined count of samples of all channels. So if the left and right channels contain 1000 samples each, then totalSampleCount should be 2000.
// isFloatingPoint should only be true if the audio data is in 32-bit floating-point format.
private void WriteWavHeader(MemoryStream stream, bool isFloatingPoint, ushort channelCount, ushort bitDepth, int sampleRate, int totalSampleCount)
{
stream.Position = 0;
// RIFF header.
// Chunk ID.
stream.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4);
// Chunk size.
stream.Write(BitConverter.GetBytes(((bitDepth / 8) * totalSampleCount) + 36), 0, 4);
// Format.
stream.Write(Encoding.ASCII.GetBytes("WAVE"), 0, 4);
// Sub-chunk 1.
// Sub-chunk 1 ID.
stream.Write(Encoding.ASCII.GetBytes("fmt "), 0, 4);
// Sub-chunk 1 size.
stream.Write(BitConverter.GetBytes(16), 0, 4);
// Audio format (floating point (3) or PCM (1)). Any other format indicates compression.
stream.Write(BitConverter.GetBytes((ushort)(isFloatingPoint ? 3 : 1)), 0, 2);
// Channels.
stream.Write(BitConverter.GetBytes(channelCount), 0, 2);
// Sample rate.
stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);
// Bytes rate.
stream.Write(BitConverter.GetBytes(sampleRate * channelCount * (bitDepth / 8)), 0, 4);
// Block align.
stream.Write(BitConverter.GetBytes(channelCount * (bitDepth / 8)), 0, 2);
// Bits per sample.
stream.Write(BitConverter.GetBytes(bitDepth), 0, 2);
// Sub-chunk 2.
// Sub-chunk 2 ID.
stream.Write(Encoding.ASCII.GetBytes("data"), 0, 4);
// Sub-chunk 2 size.
stream.Write(BitConverter.GetBytes((bitDepth / 8) * totalSampleCount), 0, 4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment