mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-17 11:35:08 -04:00
Simplify OpenAL backend
This commit is contained in:
parent
630a43bea2
commit
53475ce993
@ -19,12 +19,9 @@ namespace SharpWave {
|
|||||||
this.volume = volume;
|
this.volume = volume;
|
||||||
if (source == uint.MaxValue) return;
|
if (source == uint.MaxValue) return;
|
||||||
|
|
||||||
lock (contextLock) {
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alSourcef(source, ALSourcef.Gain, volume);
|
AL.alSourcef(source, ALSourcef.Gain, volume);
|
||||||
CheckError("SetVolume");
|
CheckError("SetVolume");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override void Create(int numBuffers) {
|
public override void Create(int numBuffers) {
|
||||||
bufferIDs = new uint[numBuffers];
|
bufferIDs = new uint[numBuffers];
|
||||||
@ -38,19 +35,16 @@ namespace SharpWave {
|
|||||||
lock (contextLock) {
|
lock (contextLock) {
|
||||||
if (context == IntPtr.Zero) CreateContext();
|
if (context == IntPtr.Zero) CreateContext();
|
||||||
contextRefs++;
|
contextRefs++;
|
||||||
|
}
|
||||||
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alDistanceModel(ALDistanceModel.None);
|
AL.alDistanceModel(ALDistanceModel.None);
|
||||||
CheckError("DistanceModel");
|
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);
|
AL.alGetSourcei(source, ALGetSourcei.BuffersProcessed, &processed);
|
||||||
CheckError("GetSources");
|
CheckError("GetSources");
|
||||||
@ -58,7 +52,6 @@ namespace SharpWave {
|
|||||||
|
|
||||||
AL.alSourceUnqueueBuffers(source, 1, &buffer);
|
AL.alSourceUnqueueBuffers(source, 1, &buffer);
|
||||||
CheckError("SourceUnqueueBuffers");
|
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,20 +66,15 @@ namespace SharpWave {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int state = 0;
|
int state = 0;
|
||||||
lock (contextLock) {
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alGetSourcei(source, ALGetSourcei.SourceState, &state);
|
AL.alGetSourcei(source, ALGetSourcei.SourceState, &state);
|
||||||
return state != (int)ALSourceState.Playing;
|
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;
|
||||||
|
|
||||||
lock (contextLock) {
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alBufferData(buffer, dataFormat, (IntPtr)data,
|
AL.alBufferData(buffer, dataFormat, (IntPtr)data,
|
||||||
chunk.Length, Format.SampleRate);
|
chunk.Length, Format.SampleRate);
|
||||||
CheckError("BufferData");
|
CheckError("BufferData");
|
||||||
@ -95,15 +83,11 @@ namespace SharpWave {
|
|||||||
CheckError("QueueBuffers");
|
CheckError("QueueBuffers");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override void Play() {
|
public override void Play() {
|
||||||
lock (contextLock) {
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alSourcePlay(source);
|
AL.alSourcePlay(source);
|
||||||
CheckError("SourcePlay");
|
CheckError("SourcePlay");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void CheckError(string location) {
|
void CheckError(string location) {
|
||||||
ALError error = AL.alGetError();
|
ALError error = AL.alGetError();
|
||||||
@ -124,18 +108,15 @@ namespace SharpWave {
|
|||||||
if (source == uint.MaxValue) return;
|
if (source == uint.MaxValue) return;
|
||||||
uint sourceU = source;
|
uint sourceU = source;
|
||||||
|
|
||||||
fixed (uint* buffers = bufferIDs) {
|
|
||||||
lock (contextLock) {
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alDeleteSources(1, &sourceU);
|
AL.alDeleteSources(1, &sourceU);
|
||||||
source = uint.MaxValue;
|
source = uint.MaxValue;
|
||||||
CheckError("DeleteSources");
|
CheckError("DeleteSources");
|
||||||
|
|
||||||
|
fixed (uint* buffers = bufferIDs) {
|
||||||
AL.alDeleteBuffers(NumBuffers, buffers);
|
AL.alDeleteBuffers(NumBuffers, buffers);
|
||||||
CheckError("DeleteBuffers");
|
CheckError("DeleteBuffers");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override void SetFormat(AudioFormat format) {
|
public override void SetFormat(AudioFormat format) {
|
||||||
dataFormat = GetALFormat(format.Channels, format.BitsPerSample);
|
dataFormat = GetALFormat(format.Channels, format.BitsPerSample);
|
||||||
@ -147,8 +128,6 @@ namespace SharpWave {
|
|||||||
uint sourceU = 0;
|
uint sourceU = 0;
|
||||||
|
|
||||||
fixed (uint* buffers = bufferIDs) {
|
fixed (uint* buffers = bufferIDs) {
|
||||||
lock (contextLock) {
|
|
||||||
MakeContextCurrent();
|
|
||||||
AL.alGenSources(1, &sourceU);
|
AL.alGenSources(1, &sourceU);
|
||||||
source = sourceU;
|
source = sourceU;
|
||||||
CheckError("GenSources");
|
CheckError("GenSources");
|
||||||
@ -156,7 +135,6 @@ namespace SharpWave {
|
|||||||
AL.alGenBuffers(NumBuffers, buffers);
|
AL.alGenBuffers(NumBuffers, buffers);
|
||||||
CheckError("GenBuffers");
|
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