Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# + OpenTK.Audio.OpenAL sample
using OpenTK;
using OpenTK.Audio.OpenAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4 {
class Program {
static unsafe void Main(string[] args) {
//Initialize
var device = Alc.OpenDevice(null);
var context = Alc.CreateContext(device, (int*)null);
Alc.MakeContextCurrent(context);
var version = AL.Get(ALGetString.Version);
var vendor = AL.Get(ALGetString.Vendor);
var renderer = AL.Get(ALGetString.Renderer);
Console.WriteLine(version);
Console.WriteLine(vendor);
Console.WriteLine(renderer);
Console.ReadKey();
//Process
int buffers, source;
AL.GenBuffers(1, out buffers);
AL.GenSources(1, out source);
int sampleFreq = 44100;
double dt = 2 * Math.PI / sampleFreq;
double amp = 0.5;
int freq = 440;
var dataCount = sampleFreq / freq;
var sinData = new short[dataCount];
for (int i = 0; i < sinData.Length; ++i) {
sinData[i] = (short)(amp * short.MaxValue * Math.Sin(i * dt * freq));
}
AL.BufferData(buffers, ALFormat.Mono16, sinData, sinData.Length, sampleFreq);
AL.Source(source, ALSourcei.Buffer, buffers);
AL.Source(source, ALSourceb.Looping, true);
AL.SourcePlay(source);
Console.ReadKey();
///Dispose
if (context != ContextHandle.Zero) {
Alc.MakeContextCurrent(ContextHandle.Zero);
Alc.DestroyContext(context);
}
context = ContextHandle.Zero;
if (device != IntPtr.Zero) {
Alc.CloseDevice(device);
}
device = IntPtr.Zero;
}
}
}
using OpenTK;
using OpenTK.Audio.OpenAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication4 {
class Program {
static unsafe void Main(string[] args) {
//Initialize
var device = Alc.OpenDevice(null);
var context = Alc.CreateContext(device, (int*)null);
Alc.MakeContextCurrent(context);
var version = AL.Get(ALGetString.Version);
var vendor = AL.Get(ALGetString.Vendor);
var renderer = AL.Get(ALGetString.Renderer);
Console.WriteLine(version);
Console.WriteLine(vendor);
Console.WriteLine(renderer);
//Process
int sampleFreq = 44100;
double dt = 2 * Math.PI / sampleFreq;
var dataCount = 100;
double amp = 0.5;
for (int freq = 440; freq < 10000; freq += 100) {
int source;
int buffers;
AL.GenBuffers(1, out buffers);
AL.GenSources(1, out source);
var sinData = new short[dataCount];
for (int i = 0;
i < sinData.Length; ++i) {
sinData[i] = (short)(amp * short.MaxValue * Math.Sin(i * dt * freq));
}
AL.BufferData(buffers, ALFormat.Mono16, sinData, sinData.Length, sampleFreq);
AL.Source(source, ALSourcei.Buffer, buffers);
AL.Source(source, ALSourceb.Looping, true);
AL.SourcePlay(source);
Thread.Sleep(100);
}
Console.WriteLine("fin");
Console.ReadKey();
///Dispose
if (context != ContextHandle.Zero) {
Alc.MakeContextCurrent(ContextHandle.Zero);
Alc.DestroyContext(context);
}
context = ContextHandle.Zero;
if (device != IntPtr.Zero) {
Alc.CloseDevice(device);
}
device = IntPtr.Zero;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.