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