Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created December 8, 2013 21:00
Show Gist options
  • Select an option

  • Save DevJohnC/7863768 to your computer and use it in GitHub Desktop.

Select an option

Save DevJohnC/7863768 to your computer and use it in GitHub Desktop.
Aural example application
using System;
using FragLabs.Aural;
using FragLabs.Aural.Encoding;
using FragLabs.Aural.Encoding.Opus;
using FragLabs.Aural.IO;
using FragLabs.Aural.Processing.Filters;
namespace AuralTest
{
class Program
{
static void Main(string[] args)
{
var platform = PlatformDetails.CpuArchitecture;
Console.WriteLine("Running on {0}", platform);
Console.WriteLine("Output devices:");
foreach(var device in OpenALOutput.GetOutputDevices())
{
Console.WriteLine("\t{0}", device);
}
Console.WriteLine("Input devices:");
foreach(var device in OpenALInput.GetInputDevices())
{
Console.WriteLine("\t{0}", device);
}
Console.WriteLine();
var inputAudioFormat = new AudioFormat
{
SampleRate = 48000,
BitDepth = 16,
Channels = 1
};
var downSampleFactor = 6;
var filterChain = new FilterChain();
filterChain.Add(new LowPass(inputAudioFormat, 8000));
filterChain.Add(new DecimationDownSampler(inputAudioFormat, downSampleFactor));
var outputAudioFormat = filterChain.OutputAudioFormat;
var encoder = new OpusEncoder(outputAudioFormat.SampleRate, 1, Application.Voip);
var decoder = new OpusDecoder(outputAudioFormat.SampleRate, 1);
var frameSize = encoder.PermittedFrameSizes[1]; // 5ms
var frameSizeInBytes = encoder.FrameSizeInBytes(frameSize);
Console.WriteLine("Opening {0} for input", OpenALInput.GetInputDevices()[0]);
var capture = new OpenALInput(OpenALInput.GetInputDevices()[0], inputAudioFormat, (frameSizeInBytes * downSampleFactor) * 4);
Console.WriteLine("Opening {0} for output", OpenALOutput.GetOutputDevices()[0]);
var output = new OpenALOutput(OpenALOutput.GetOutputDevices()[0], outputAudioFormat);
var encodeBuffer = new byte[frameSizeInBytes];
var decodeBuffer = new byte[frameSizeInBytes];
capture.AudioReceived += (sender, eventArgs) =>
{
var filterInput = FormatHelper.Convert16BitToDouble(eventArgs.Buffer);
var filterOutput = new double[filterInput.Length];
var sampleCount = filterChain.Process(filterInput, 0, filterOutput, 0, filterInput.Length);
var pcmData = FormatHelper.ConvertDoubleto16Bit(filterOutput);
var encodedLen = encoder.Encode(pcmData, 0, encodeBuffer, 0, sampleCount);
var decodedLen = decoder.Decode(encodeBuffer, 0, encodedLen, decodeBuffer, 0);
var decodedSamples = decodedLen / FormatHelper.SampleSize(16, 1);
output.Write(decodeBuffer, 0, decodedSamples);
if (!output.IsPlaying)
output.Play();
};
capture.StartReading(frameSize * downSampleFactor);
Console.ReadLine();
capture.StopReading();
output.Dispose();
capture.Dispose();
encoder.Dispose();
decoder.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment