diff --git a/src/audio_stream.c b/src/audio_stream.c index 542129e..ce4be5d 100644 --- a/src/audio_stream.c +++ b/src/audio_stream.c @@ -39,6 +39,7 @@ static long speaker_freq_ctr = 0; static u8 audio_volume = AUDIO_VOLUME_MAX; static double audio_prev_time; static int audio_freq; +static bool audio_signed; #ifdef AUDIO_STREAM_DEBUG static void audio_stream_print(int pos) { @@ -71,10 +72,11 @@ static void audio_stream_flag_speaker_overrun(void) { } } -void audio_stream_init(long time, int freq) { +void audio_stream_init(long time, int freq, bool asigned) { speaker_overrun_flagged = 0; audio_prev_time = -1; audio_freq = freq; + audio_signed = asigned; } u8 audio_stream_get_volume() { @@ -102,10 +104,14 @@ void audio_stream_generate_u8(long time, u8 *stream, int len) { double audio_dfrom, audio_dto; long audio_from, audio_to, audio_last_to = 0; + u8 audio_smp_min = audio_signed ? (-audio_volume) : (128 - audio_volume); + u8 audio_smp_max = audio_signed ? audio_volume : (128 + audio_volume); + u8 audio_smp_center = audio_signed ? 0 : 128; + // handle the first if (audio_prev_time < 0) { audio_prev_time = time; - memset(stream, 128, len); + memset(stream, audio_smp_center, len); speaker_entry_pos = 0; return; } @@ -125,7 +131,7 @@ void audio_stream_generate_u8(long time, u8 *stream, int len) { if (speaker_entry_pos == 0) { audio_curr_time = time; - memset(stream, 128, len); + memset(stream, audio_smp_center, len); } else { double last_ms = audio_prev_time; for (i = 0; i < speaker_entry_pos; i++) { @@ -173,9 +179,9 @@ void audio_stream_generate_u8(long time, u8 *stream, int len) { pos_samples_fixed = (speaker_freq_ctr << 8) % (freq_samples_fixed << 1); for (j = audio_from; j < audio_to; j++) { if ((pos_samples_fixed & 0xFFFFFF00) < (freq_samples_fixed & 0xFFFFFF00)) - stream[j] = 128+audio_volume; + stream[j] = audio_smp_max; else - stream[j] = 128-audio_volume; + stream[j] = audio_smp_min; pos_samples_fixed = (pos_samples_fixed + 256) % (freq_samples_fixed << 1); } speaker_freq_ctr += audio_to - audio_from; @@ -184,7 +190,7 @@ void audio_stream_generate_u8(long time, u8 *stream, int len) { fprintf(stderr, "[callback] emitting off (%ld, %ld)\n", audio_from, audio_to); #endif speaker_freq_ctr = 0; - memset(stream + audio_from, 128, audio_to - audio_from); + memset(stream + audio_from, audio_smp_center, audio_to - audio_from); } } diff --git a/src/audio_stream.h b/src/audio_stream.h index efc81c0..e163a98 100644 --- a/src/audio_stream.h +++ b/src/audio_stream.h @@ -26,7 +26,7 @@ #include "types.h" USER_FUNCTION -void audio_stream_init(long time, int freq); +void audio_stream_init(long time, int freq, bool asigned); USER_FUNCTION u8 audio_stream_get_volume(); USER_FUNCTION diff --git a/src/psp/frontend.c b/src/psp/frontend.c index 4200bee..1852fcd 100644 --- a/src/psp/frontend.c +++ b/src/psp/frontend.c @@ -276,12 +276,11 @@ void psp_audio_callback(void *stream, unsigned int len, void *userdata) { return; } - u8 *stream_u8 = ((u8*) stream) + (len * 3); + s8 *stream_s8 = ((s8*) stream) + (len * 3); s16 *stream_s16 = ((s16*) stream); - audio_stream_generate_u8(zeta_time_ms(), stream_u8, len); - for (int i = 0; i < len; i++, stream_u8++, stream_s16+=2) { - s8 sample_s8 = (s8) (stream_u8[0] ^ 0x80); + audio_stream_generate_u8(zeta_time_ms(), (u8*) stream_s8, len); + for (int i = 0; i < len; i++, stream_s8++, stream_s16+=2) { s16 val = ((s16) sample_s8) << 8; stream_s16[0] = (s16) val; stream_s16[1] = (s16) val; diff --git a/src/sdl/frontend.c b/src/sdl/frontend.c index 4549ed0..b95d0a5 100644 --- a/src/sdl/frontend.c +++ b/src/sdl/frontend.c @@ -367,7 +367,7 @@ int main(int argc, char **argv) { } if (audio_device != 0) { - audio_stream_init(zeta_time_ms(), audio_spec.freq); + audio_stream_init(zeta_time_ms(), audio_spec.freq, audio_spec.format == AUDIO_S8); if (posix_zzt_arg_note_delay >= 0.0) { audio_set_note_delay(posix_zzt_arg_note_delay); } diff --git a/src/types.h b/src/types.h index 2e9a652..101f2f5 100644 --- a/src/types.h +++ b/src/types.h @@ -23,6 +23,8 @@ #ifndef __TYPES_H__ #define __TYPES_H__ +#include + #ifdef __EMSCRIPTEN__ #define USER_FUNCTION EMSCRIPTEN_KEEPALIVE #define IMPLEMENT_FUNCTION diff --git a/web/src/audio.js b/web/src/audio.js index ba5e7ac..28cca7e 100644 --- a/web/src/audio.js +++ b/web/src/audio.js @@ -156,7 +156,7 @@ export class BufferBasedAudio { this.time = audioCtx.currentTime; - this.emu._audio_stream_init(time_ms(), this.sampleRate); + this.emu._audio_stream_init(time_ms(), this.sampleRate, false); this.emu._audio_stream_set_volume(Math.floor(this.volume * this.emu._audio_stream_get_max_volume())); if (this.noteDelay) {