Last active
September 8, 2022 18:53
-
-
Save kamiyaowl/32fb397e0141c65792e1 to your computer and use it in GitHub Desktop.
C# + OpenTK.Audio.OpenAL sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
The
size
parameter toAL.BufferData
is in bytes, not samples, soShould be