mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-17 11:35:08 -04:00
fix OpenAL and WebAudio backends
This commit is contained in:
parent
97fcc429bf
commit
caf02d439f
43
src/Audio.c
43
src/Audio.c
@ -56,7 +56,8 @@ static void AudioWarn(cc_result res, const char* action) {
|
|||||||
static void AudioBase_Clear(struct AudioContext* ctx);
|
static void AudioBase_Clear(struct AudioContext* ctx);
|
||||||
static cc_bool AudioBase_AdjustSound(struct AudioContext* ctx, struct AudioData* data);
|
static cc_bool AudioBase_AdjustSound(struct AudioContext* ctx, struct AudioData* data);
|
||||||
static cc_result AudioBase_PlaySound(struct AudioContext* ctx, struct AudioData* data);
|
static cc_result AudioBase_PlaySound(struct AudioContext* ctx, struct AudioData* data);
|
||||||
|
/* achieve higher speed by playing samples at higher sample rate */
|
||||||
|
#define Audio_AdjustSampleRate(data) ((data->sampleRate * data->rate) / 100)
|
||||||
|
|
||||||
#if defined CC_BUILD_OPENAL
|
#if defined CC_BUILD_OPENAL
|
||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
@ -294,10 +295,12 @@ cc_bool Audio_FastPlay(struct AudioContext* ctx, struct AudioData* data) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result Audio_PlayData(struct AudioContext* ctx, struct Sound* snd, int volume) {
|
cc_result Audio_PlayData(struct AudioContext* ctx, struct AudioData* data) {
|
||||||
cc_book ok = AudioBase_AdjustSound(ctx, snd, volume);
|
cc_bool ok = AudioBase_AdjustSound(ctx, data);
|
||||||
if (ok) return AudioBase_PlaySound(ctx, snd, data);
|
if (!ok) return ERR_OUT_OF_MEMORY;
|
||||||
return ERR_OUT_OF_MEMORY;
|
|
||||||
|
data->sampleRate = Audio_AdjustSampleRate(data);
|
||||||
|
return AudioBase_PlaySound(ctx, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char* GetError(cc_result res) {
|
static const char* GetError(cc_result res) {
|
||||||
@ -477,8 +480,7 @@ cc_result Audio_Poll(struct AudioContext* ctx, int* inUse) {
|
|||||||
|
|
||||||
*inUse = count; return res;
|
*inUse = count; return res;
|
||||||
}
|
}
|
||||||
/* achieve higher speed by playing samples at higher sample rate */
|
|
||||||
#define Audio_AdjustSampleRate(data) ((data->sampleRate * data->rate) / 100)
|
|
||||||
|
|
||||||
cc_bool Audio_FastPlay(struct AudioContext* ctx, struct AudioData* data) {
|
cc_bool Audio_FastPlay(struct AudioContext* ctx, struct AudioData* data) {
|
||||||
int channels = data->channels;
|
int channels = data->channels;
|
||||||
@ -731,7 +733,7 @@ struct AudioContext { int contextID; };
|
|||||||
extern int interop_InitAudio(void);
|
extern int interop_InitAudio(void);
|
||||||
extern int interop_AudioCreate(void);
|
extern int interop_AudioCreate(void);
|
||||||
extern void interop_AudioClose(int contextID);
|
extern void interop_AudioClose(int contextID);
|
||||||
extern int interop_AudioPlay(int contextID, struct Sound* snd, int volume);
|
extern int interop_AudioPlay(int contextID, const void* name, int volume, int rate);
|
||||||
extern int interop_AudioPoll(int contetID, int* inUse);
|
extern int interop_AudioPoll(int contetID, int* inUse);
|
||||||
extern int interop_AudioDescribe(int res, char* buffer, int bufferLen);
|
extern int interop_AudioDescribe(int res, char* buffer, int bufferLen);
|
||||||
|
|
||||||
@ -767,10 +769,10 @@ cc_bool Audio_FastPlay(struct AudioContext* ctx, struct AudioData* data) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result Audio_PlayData(struct AudioContext* ctx, struct Sound* snd, int volume) {
|
cc_result Audio_PlayData(struct AudioContext* ctx, struct AudioData* data) {
|
||||||
if (!ctx->contextID)
|
if (!ctx->contextID)
|
||||||
ctx->contextID = interop_AudioCreate();
|
ctx->contextID = interop_AudioCreate();
|
||||||
return interop_AudioPlay(ctx->contextID, snd, volume);
|
return interop_AudioPlay(ctx->contextID, data->data, data->volume, data->rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
|
cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
|
||||||
@ -1083,27 +1085,6 @@ static const struct SoundID { int group; const char* name; } sounds_list[] =
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* TODO this is a terrible solution */
|
/* TODO this is a terrible solution */
|
||||||
#include <emscripten/emscripten.h>
|
|
||||||
EMSCRIPTEN_KEEPALIVE void Audio_SoundReady(const char* name, int channels, int sampleRate) {
|
|
||||||
struct Soundboard* board = name[0] == 's' ? &stepBoard : &digBoard;
|
|
||||||
struct SoundGroup* group;
|
|
||||||
cc_string str = String_FromReadonly(name);
|
|
||||||
int i, j;
|
|
||||||
/* This awful hack is because otherwise Sounds_Play tries to play the sound with samplerate of 0 */
|
|
||||||
|
|
||||||
for (i = 0; i < SOUND_COUNT; i++) {
|
|
||||||
group = &board->groups[i];
|
|
||||||
|
|
||||||
for (j = 0; j < group->count; j++) {
|
|
||||||
if (!String_CaselessEqualsConst(&str, group->sounds[j].data)) continue;
|
|
||||||
|
|
||||||
group->sounds[j].channels = channels;
|
|
||||||
group->sounds[j].sampleRate = sampleRate;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void InitWebSounds(void) {
|
static void InitWebSounds(void) {
|
||||||
struct Soundboard* board = &stepBoard;
|
struct Soundboard* board = &stepBoard;
|
||||||
struct SoundGroup* group;
|
struct SoundGroup* group;
|
||||||
|
@ -713,8 +713,8 @@ mergeInto(LibraryManager.library, {
|
|||||||
HEAP32[inUse >> 2] = src.playing; // only 1 buffer
|
HEAP32[inUse >> 2] = src.playing; // only 1 buffer
|
||||||
return 0;
|
return 0;
|
||||||
},
|
},
|
||||||
interop_AudioPlay: function(ctxID, snd, volume) {
|
interop_AudioPlay: function(ctxID, snd, volume, rate) {
|
||||||
var nameAddr = HEAP32[(snd|0 + 8|0) >> 2];
|
var nameAddr = HEAP32[(snd|0) >> 2];
|
||||||
var src = AUDIO.sources[ctxID - 1|0];
|
var src = AUDIO.sources[ctxID - 1|0];
|
||||||
var name = UTF8ToString(nameAddr);
|
var name = UTF8ToString(nameAddr);
|
||||||
|
|
||||||
@ -728,7 +728,6 @@ mergeInto(LibraryManager.library, {
|
|||||||
// still downloading or failed to download this file
|
// still downloading or failed to download this file
|
||||||
var buffer = AUDIO.buffers[name];
|
var buffer = AUDIO.buffers[name];
|
||||||
if (!buffer) return 0;
|
if (!buffer) return 0;
|
||||||
var sampleRate = HEAP32[(snd|0 + 4|0) >> 2];
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!src.gain) src.gain = AUDIO.context.createGain();
|
if (!src.gain) src.gain = AUDIO.context.createGain();
|
||||||
@ -740,8 +739,7 @@ mergeInto(LibraryManager.library, {
|
|||||||
src.source = AUDIO.context.createBufferSource();
|
src.source = AUDIO.context.createBufferSource();
|
||||||
src.source.buffer = buffer;
|
src.source.buffer = buffer;
|
||||||
src.gain.gain.value = volume / 100;
|
src.gain.gain.value = volume / 100;
|
||||||
// game adjusts samplerate to playback audio faster - undo that
|
src.source.playbackRate.value = rate / 100;
|
||||||
src.source.playbackRate.value = sampleRate / buffer.sampleRate;
|
|
||||||
|
|
||||||
// source -> gain -> output
|
// source -> gain -> output
|
||||||
src.source.connect(src.gain);
|
src.source.connect(src.gain);
|
||||||
@ -762,8 +760,6 @@ mergeInto(LibraryManager.library, {
|
|||||||
var data = xhr.response;
|
var data = xhr.response;
|
||||||
AUDIO.context.decodeAudioData(data, function(buffer) {
|
AUDIO.context.decodeAudioData(data, function(buffer) {
|
||||||
AUDIO.buffers[name] = buffer;
|
AUDIO.buffers[name] = buffer;
|
||||||
// TODO rethink this, this is a terrible solution
|
|
||||||
Module["_Audio_SoundReady"](nameAddr, buffer.channels, buffer.sampleRate);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
xhr.send();
|
xhr.send();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user