Skip to content

Commit

Permalink
audio_stream: allow emitting signed samples
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Aug 25, 2020
1 parent ae649c2 commit e2cb9b5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
18 changes: 12 additions & 6 deletions src/audio_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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++) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/audio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/psp/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/sdl/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifndef __TYPES_H__
#define __TYPES_H__

#include <stdbool.h>

#ifdef __EMSCRIPTEN__
#define USER_FUNCTION EMSCRIPTEN_KEEPALIVE
#define IMPLEMENT_FUNCTION
Expand Down
2 changes: 1 addition & 1 deletion web/src/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e2cb9b5

Please sign in to comment.