Skip to content

Instantly share code, notes, and snippets.

@erksch
Created April 30, 2020 10:46
Show Gist options
  • Save erksch/662772d6a90d6605c0368b964f937b5c to your computer and use it in GitHub Desktop.
Save erksch/662772d6a90d6605c0368b964f937b5c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Windows.Foundation;
using Windows.Media;
using Windows.Media.Audio;
using Windows.Media.MediaProperties;
using Windows.Media.Render;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
namespace App1
{
[ComImport]
[Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
void GetBuffer(out byte* buffer, out uint capacity);
}
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private AudioFrameOutputNode frameOutputNode;
private List<byte> allBytes = new List<byte>();
public MainPage()
{
this.InitializeComponent();
PrintActualBytes();
CreateAudioGraph();
}
private void PrintActualBytes()
{
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
FileStream fIn = new FileStream(localFolder.Path + @"/audio.wav", FileMode.Open);
BinaryReader br = new BinaryReader(fIn);
// Skip the first 44 bytes since they are header stuff
br.ReadBytes(44);
Debug.WriteLine("Actual bytes:");
for (int i = 0; i < 400; i++)
{
if (i > 0 && i % 2 == 0) Debug.Write("| ");
Debug.Write(br.ReadByte() + " ");
}
Debug.WriteLine("");
}
private async void CreateAudioGraph()
{
var encoding = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Auto);
encoding.Audio = AudioEncodingProperties.CreatePcm(16000, 1, 16);
var settings = new AudioGraphSettings(AudioRenderCategory.Speech);
settings.EncodingProperties = encoding.Audio;
var result = await AudioGraph.CreateAsync(settings);
var graph = result.Graph;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await localFolder.GetFileAsync("audio.wav");
var fileInputNodeResult = await graph.CreateFileInputNodeAsync(file);
var fileInputNode = fileInputNodeResult.FileInputNode;
fileInputNode.FileCompleted += async (AudioFileInputNode sender, object args) =>
{
graph.Stop();
Debug.WriteLine("Bytes from FrameOutputNode frames:");
// Print first 100 floats in bytes
for (int i = 0; i < 4 * 100; i++)
{
if (i > 0 && i % 4 == 0) Debug.Write("| ");
Debug.Write(allBytes[i] + " ");
}
};
frameOutputNode = graph.CreateFrameOutputNode(encoding.Audio);
fileInputNode.AddOutgoingConnection(frameOutputNode);
graph.QuantumStarted += AudioGraph_QuantumStarted;
graph.Start();
}
private void AudioGraph_QuantumStarted(AudioGraph sender, object args)
{
AudioFrame frame = frameOutputNode.GetFrame();
ProcessFrameOutput(frame);
}
unsafe private void ProcessFrameOutput(AudioFrame frame)
{
AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Read);
IMemoryBufferReference reference = buffer.CreateReference();
((IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacityInBytes);
for (int i = 0; i < capacityInBytes; i++)
{
allBytes.Add(dataInBytes[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment