mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-17 03:25:14 -04:00
Simplify OpenAL backend
This commit is contained in:
parent
630a43bea2
commit
53475ce993
@ -50,7 +50,7 @@ namespace OpenTK.Audio.OpenAL {
|
|||||||
[DllImport(lib, CallingConvention = style)]
|
[DllImport(lib, CallingConvention = style)]
|
||||||
public static extern IntPtr alcCreateContext(IntPtr device, int* attrlist);
|
public static extern IntPtr alcCreateContext(IntPtr device, int* attrlist);
|
||||||
[DllImport(lib, CallingConvention = style)]
|
[DllImport(lib, CallingConvention = style)]
|
||||||
public static extern bool alcMakeContextCurrent(IntPtr context);
|
public static extern bool alcMakeContextCurrent(IntPtr context);
|
||||||
[DllImport(lib, CallingConvention = style)]
|
[DllImport(lib, CallingConvention = style)]
|
||||||
public static extern void alcDestroyContext(IntPtr context);
|
public static extern void alcDestroyContext(IntPtr context);
|
||||||
|
|
||||||
|
@ -19,11 +19,8 @@ namespace SharpWave {
|
|||||||
this.volume = volume;
|
this.volume = volume;
|
||||||
if (source == uint.MaxValue) return;
|
if (source == uint.MaxValue) return;
|
||||||
|
|
||||||
lock (contextLock) {
|
AL.alSourcef(source, ALSourcef.Gain, volume);
|
||||||
MakeContextCurrent();
|
CheckError("SetVolume");
|
||||||
AL.alSourcef(source, ALSourcef.Gain, volume);
|
|
||||||
CheckError("SetVolume");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Create(int numBuffers) {
|
public override void Create(int numBuffers) {
|
||||||
@ -38,27 +35,23 @@ namespace SharpWave {
|
|||||||
lock (contextLock) {
|
lock (contextLock) {
|
||||||
if (context == IntPtr.Zero) CreateContext();
|
if (context == IntPtr.Zero) CreateContext();
|
||||||
contextRefs++;
|
contextRefs++;
|
||||||
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alDistanceModel(ALDistanceModel.None);
|
|
||||||
CheckError("DistanceModel");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AL.alDistanceModel(ALDistanceModel.None);
|
||||||
|
CheckError("DistanceModel");
|
||||||
Console.WriteLine("al context:" + context);
|
Console.WriteLine("al context:" + context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool IsCompleted(int index) {
|
public override bool IsCompleted(int index) {
|
||||||
int processed = 0;
|
int processed = 0;
|
||||||
uint buffer = 0;
|
uint buffer = 0;
|
||||||
lock (contextLock) {
|
|
||||||
MakeContextCurrent();
|
AL.alGetSourcei(source, ALGetSourcei.BuffersProcessed, &processed);
|
||||||
|
CheckError("GetSources");
|
||||||
AL.alGetSourcei(source, ALGetSourcei.BuffersProcessed, &processed);
|
if (processed == 0) return completed[index];
|
||||||
CheckError("GetSources");
|
|
||||||
if (processed == 0) return completed[index];
|
AL.alSourceUnqueueBuffers(source, 1, &buffer);
|
||||||
|
CheckError("SourceUnqueueBuffers");
|
||||||
AL.alSourceUnqueueBuffers(source, 1, &buffer);
|
|
||||||
CheckError("SourceUnqueueBuffers");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < NumBuffers; i++) {
|
for (int i = 0; i < NumBuffers; i++) {
|
||||||
if (bufferIDs[i] == buffer) completed[i] = true;
|
if (bufferIDs[i] == buffer) completed[i] = true;
|
||||||
@ -73,36 +66,27 @@ namespace SharpWave {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int state = 0;
|
int state = 0;
|
||||||
lock (contextLock) {
|
AL.alGetSourcei(source, ALGetSourcei.SourceState, &state);
|
||||||
MakeContextCurrent();
|
return state != (int)ALSourceState.Playing;
|
||||||
AL.alGetSourcei(source, ALGetSourcei.SourceState, &state);
|
|
||||||
return state != (int)ALSourceState.Playing;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void BufferData(int index, AudioChunk chunk) {
|
public override void BufferData(int index, AudioChunk chunk) {
|
||||||
fixed (byte* data = chunk.Data) {
|
fixed (byte* data = chunk.Data) {
|
||||||
uint buffer = bufferIDs[index];
|
uint buffer = bufferIDs[index];
|
||||||
completed[index] = false;
|
completed[index] = false;
|
||||||
|
|
||||||
|
AL.alBufferData(buffer, dataFormat, (IntPtr)data,
|
||||||
|
chunk.Length, Format.SampleRate);
|
||||||
|
CheckError("BufferData");
|
||||||
|
|
||||||
lock (contextLock) {
|
AL.alSourceQueueBuffers(source, 1, &buffer);
|
||||||
MakeContextCurrent();
|
CheckError("QueueBuffers");
|
||||||
AL.alBufferData(buffer, dataFormat, (IntPtr)data,
|
|
||||||
chunk.Length, Format.SampleRate);
|
|
||||||
CheckError("BufferData");
|
|
||||||
|
|
||||||
AL.alSourceQueueBuffers(source, 1, &buffer);
|
|
||||||
CheckError("QueueBuffers");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Play() {
|
public override void Play() {
|
||||||
lock (contextLock) {
|
AL.alSourcePlay(source);
|
||||||
MakeContextCurrent();
|
CheckError("SourcePlay");
|
||||||
AL.alSourcePlay(source);
|
|
||||||
CheckError("SourcePlay");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckError(string location) {
|
void CheckError(string location) {
|
||||||
@ -124,16 +108,13 @@ namespace SharpWave {
|
|||||||
if (source == uint.MaxValue) return;
|
if (source == uint.MaxValue) return;
|
||||||
uint sourceU = source;
|
uint sourceU = source;
|
||||||
|
|
||||||
|
AL.alDeleteSources(1, &sourceU);
|
||||||
|
source = uint.MaxValue;
|
||||||
|
CheckError("DeleteSources");
|
||||||
|
|
||||||
fixed (uint* buffers = bufferIDs) {
|
fixed (uint* buffers = bufferIDs) {
|
||||||
lock (contextLock) {
|
AL.alDeleteBuffers(NumBuffers, buffers);
|
||||||
MakeContextCurrent();
|
CheckError("DeleteBuffers");
|
||||||
AL.alDeleteSources(1, &sourceU);
|
|
||||||
source = uint.MaxValue;
|
|
||||||
CheckError("DeleteSources");
|
|
||||||
|
|
||||||
AL.alDeleteBuffers(NumBuffers, buffers);
|
|
||||||
CheckError("DeleteBuffers");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,15 +128,12 @@ namespace SharpWave {
|
|||||||
uint sourceU = 0;
|
uint sourceU = 0;
|
||||||
|
|
||||||
fixed (uint* buffers = bufferIDs) {
|
fixed (uint* buffers = bufferIDs) {
|
||||||
lock (contextLock) {
|
AL.alGenSources(1, &sourceU);
|
||||||
MakeContextCurrent();
|
source = sourceU;
|
||||||
AL.alGenSources(1, &sourceU);
|
CheckError("GenSources");
|
||||||
source = sourceU;
|
|
||||||
CheckError("GenSources");
|
AL.alGenBuffers(NumBuffers, buffers);
|
||||||
|
CheckError("GenBuffers");
|
||||||
AL.alGenBuffers(NumBuffers, buffers);
|
|
||||||
CheckError("GenBuffers");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (volume != 1) SetVolume(volume);
|
if (volume != 1) SetVolume(volume);
|
||||||
@ -192,7 +170,7 @@ namespace SharpWave {
|
|||||||
}
|
}
|
||||||
CheckContextErrors();
|
CheckContextErrors();
|
||||||
|
|
||||||
MakeContextCurrent();
|
AL.alcMakeContextCurrent(context);
|
||||||
CheckContextErrors();
|
CheckContextErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,10 +190,6 @@ namespace SharpWave {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void MakeContextCurrent() {
|
|
||||||
AL.alcMakeContextCurrent(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void DestroyContext() {
|
static void DestroyContext() {
|
||||||
if (device == IntPtr.Zero) return;
|
if (device == IntPtr.Zero) return;
|
||||||
AL.alcMakeContextCurrent(IntPtr.Zero);
|
AL.alcMakeContextCurrent(IntPtr.Zero);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user