diff --git a/SDL/audio/audio.h b/SDL/audio/audio.h index acaa011..3d50657 100644 --- a/SDL/audio/audio.h +++ b/SDL/audio/audio.h @@ -5,12 +5,14 @@ #include #include +unsigned GB_audio_default_sample_rate(void); bool GB_audio_is_playing(void); void GB_audio_set_paused(bool paused); void GB_audio_clear_queue(void); -unsigned GB_audio_get_frequency(void); +unsigned GB_audio_get_sample_rate(void); size_t GB_audio_get_queue_length(void); void GB_audio_queue_sample(GB_sample_t *sample); -void GB_audio_init(void); +void GB_audio_init(unsigned sample_rate); +void GB_audio_destroy(void); #endif /* sdl_audio_h */ diff --git a/SDL/audio/sdl.c b/SDL/audio/sdl.c index 12ee69a..cf5be00 100644 --- a/SDL/audio/sdl.c +++ b/SDL/audio/sdl.c @@ -29,6 +29,11 @@ static SDL_AudioSpec want_aspec, have_aspec; static unsigned buffer_pos = 0; static GB_sample_t audio_buffer[AUDIO_BUFFER_SIZE]; +unsigned GB_audio_default_sample_rate(void) +{ + return AUDIO_FREQUENCY; +} + bool GB_audio_is_playing(void) { return SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; @@ -45,7 +50,7 @@ void GB_audio_clear_queue(void) SDL_ClearQueuedAudio(device_id); } -unsigned GB_audio_get_frequency(void) +unsigned GB_audio_get_sample_rate(void) { return have_aspec.freq; } @@ -65,11 +70,11 @@ void GB_audio_queue_sample(GB_sample_t *sample) } } -void GB_audio_init(void) +void GB_audio_init(unsigned sample_rate) { /* Configure Audio */ memset(&want_aspec, 0, sizeof(want_aspec)); - want_aspec.freq = AUDIO_FREQUENCY; + want_aspec.freq = sample_rate == 0 ? GB_audio_default_sample_rate() : sample_rate; want_aspec.format = AUDIO_S16SYS; want_aspec.channels = 2; want_aspec.samples = 512; @@ -94,3 +99,9 @@ void GB_audio_init(void) device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE); } + +void GB_audio_destroy() { + GB_audio_set_paused(true); + + SDL_CloseAudioDevice(device_id); +} diff --git a/Windows/resources.rc~ b/Windows/resources.rc~ new file mode 100644 index 0000000..bb4ec29 --- /dev/null +++ b/Windows/resources.rc~ @@ -0,0 +1,24 @@ +1 VERSIONINFO +FILEOS 0x4 +FILETYPE 0x1 +{ + BLOCK "StringFileInfo" + { + BLOCK "040904E4" + { + VALUE "CompanyName", "Lior Halphon" + VALUE "FileDescription", "SameBoy" + VALUE "FileVersion", VERSION + VALUE "LegalCopyright", "Copyright © 2015-2020 Lior Halphon" + VALUE "ProductName", "SameBoy" + VALUE "ProductVersion", VERSION + VALUE "WWW", "https://github.com/LIJI32/SameBoy" + } + } + BLOCK "VarFileInfo" + { + VALUE "Translation", 0x0409 0x04E4 + } +} + +IDI_ICON1 ICON DISCARDABLE "SameBoy.ico" diff --git a/wasm/Makefile b/wasm/Makefile index 7131388..3ba0580 100644 --- a/wasm/Makefile +++ b/wasm/Makefile @@ -26,8 +26,8 @@ endif CORE_DIR += .. -VERSION := 0.11.1 -export VERSION +#VERSION := 0.13 +#export VERSION CONF ?= debug BIN := $(CORE_DIR)/build/wasm_bin @@ -61,7 +61,7 @@ CFLAGS += -I$(CORE_DIR) CFLAGS += -s WASM=1 -s USE_SDL=2 --preload-file $(BOOTROMS_DIR)@/BootROMs --preload-file $(CORE_DIR)/Shaders@/Shaders -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap', 'getValue', 'AsciiToString', 'FS']" CFLAGS += -s USE_WEBGL2=1 # CFLAGS += -Wcast-align -Wover-aligned -s SAFE_HEAP=1 -s WARN_UNALIGNED=1 -WASM_LDFLAGS := +WASM_LDFLAGS := -lidbfs.js LDFLAGS += -lc -lm -ldl CFLAGS += -Wno-deprecated-declarations @@ -90,7 +90,7 @@ CORE_SOURCES_RAW := $(shell ls $(CORE_DIR)/Core/*.c) CORE_SOURCES := $(shell realpath --relative-to=$(CORE_DIR) $(CORE_SOURCES_RAW)) CORE_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(CORE_SOURCES)) -WASM_SOURCES := $(shell ls *.c) +WASM_SOURCES := $(shell ls *.c) $(CORE_DIR)/SDL/audio/sdl.c WASM_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(WASM_SOURCES)) WEB_SOURCES := $(shell ls ressources/.) diff --git a/wasm/main.c b/wasm/main.c index 99e3f8e..65012de 100644 --- a/wasm/main.c +++ b/wasm/main.c @@ -10,6 +10,8 @@ #include "utils.h" #include "shader.h" +#include "SDL/audio/audio.h" + GB_gameboy_t gb; SDL_Window *window; @@ -17,13 +19,11 @@ SDL_Renderer *renderer; SDL_Surface *screen; SDL_Texture *texture; SDL_PixelFormat *pixel_format; -SDL_AudioDeviceID device_id; shader_t shader; static SDL_Rect rect; static unsigned factor; -static SDL_AudioSpec want_aspec, have_aspec; static uint32_t pixel_buffer_1[256 * 224], pixel_buffer_2[256 * 224]; static uint32_t *active_pixel_buffer = pixel_buffer_1; static uint32_t *previous_pixel_buffer = pixel_buffer_2; @@ -120,10 +120,11 @@ unsigned query_sample_rate_of_audiocontexts() { static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) { - if ((SDL_GetQueuedAudioSize(device_id) / sizeof(GB_sample_t)) > have_aspec.freq / 12) { + if (GB_audio_get_queue_length() / sizeof(*sample) > GB_audio_get_sample_rate() / 4) { return; } - SDL_QueueAudio(device_id, sample, sizeof(*sample)); + + GB_audio_queue_sample(sample); } void update_viewport(void) @@ -245,7 +246,7 @@ void init_gb() { GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); GB_set_pixels_output(&gb, active_pixel_buffer); GB_set_rgb_encode_callback(&gb, rgb_encode); - GB_set_sample_rate(&gb, have_aspec.freq); + GB_set_sample_rate(&gb, GB_audio_get_sample_rate()); GB_set_color_correction_mode(&gb, configuration.color_correction_mode); GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); GB_set_rewind_length(&gb, 0); @@ -352,20 +353,7 @@ int EMSCRIPTEN_KEEPALIVE init() { unsigned audio_sample_rate = query_sample_rate_of_audiocontexts(); fprintf(stderr, "Sample rate: %u\n", audio_sample_rate); - memset(&want_aspec, 0, sizeof(want_aspec)); - want_aspec.freq = audio_sample_rate; - want_aspec.format = AUDIO_S16SYS; - want_aspec.channels = 2; - want_aspec.samples = 2048; - - device_id = SDL_OpenAudioDevice(NULL, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); - - if (device_id == 0) { - fprintf(stderr, "Failed to open audio: %s", SDL_GetError()); - } - - fprintf(stderr, "WANT:\nfreq: %d\nchannels: %d\nsilence: %d\nsamples: %d\nsize: %d\nformat: %d\n", want_aspec.freq, want_aspec.channels, want_aspec.silence, want_aspec.samples, want_aspec.size, want_aspec.format); - fprintf(stderr, "HAVE:\nfreq: %d\nchannels: %d\nsilence: %d\nsamples: %d\nsize: %d\nformat: %d\n", have_aspec.freq, have_aspec.channels, have_aspec.silence, have_aspec.samples, have_aspec.size, have_aspec.format); + GB_audio_init(audio_sample_rate); EM_ASM({ function audio_workaround(e) { @@ -418,7 +406,7 @@ int EMSCRIPTEN_KEEPALIVE init() { } update_viewport(); - SDL_PauseAudioDevice(device_id, 0); + GB_audio_set_paused(false); return EXIT_SUCCESS; }