Use SDL/audio/audio.h
This commit is contained in:
parent
8f03312ad6
commit
bb5d7bcf00
@ -5,12 +5,14 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <Core/gb.h>
|
#include <Core/gb.h>
|
||||||
|
|
||||||
|
unsigned GB_audio_default_sample_rate(void);
|
||||||
bool GB_audio_is_playing(void);
|
bool GB_audio_is_playing(void);
|
||||||
void GB_audio_set_paused(bool paused);
|
void GB_audio_set_paused(bool paused);
|
||||||
void GB_audio_clear_queue(void);
|
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);
|
size_t GB_audio_get_queue_length(void);
|
||||||
void GB_audio_queue_sample(GB_sample_t *sample);
|
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 */
|
#endif /* sdl_audio_h */
|
||||||
|
@ -29,6 +29,11 @@ static SDL_AudioSpec want_aspec, have_aspec;
|
|||||||
static unsigned buffer_pos = 0;
|
static unsigned buffer_pos = 0;
|
||||||
static GB_sample_t audio_buffer[AUDIO_BUFFER_SIZE];
|
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)
|
bool GB_audio_is_playing(void)
|
||||||
{
|
{
|
||||||
return SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
|
return SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
|
||||||
@ -45,7 +50,7 @@ void GB_audio_clear_queue(void)
|
|||||||
SDL_ClearQueuedAudio(device_id);
|
SDL_ClearQueuedAudio(device_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned GB_audio_get_frequency(void)
|
unsigned GB_audio_get_sample_rate(void)
|
||||||
{
|
{
|
||||||
return have_aspec.freq;
|
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 */
|
/* Configure Audio */
|
||||||
memset(&want_aspec, 0, sizeof(want_aspec));
|
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.format = AUDIO_S16SYS;
|
||||||
want_aspec.channels = 2;
|
want_aspec.channels = 2;
|
||||||
want_aspec.samples = 512;
|
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);
|
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);
|
||||||
|
}
|
||||||
|
24
Windows/resources.rc~
Normal file
24
Windows/resources.rc~
Normal file
@ -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"
|
@ -26,8 +26,8 @@ endif
|
|||||||
|
|
||||||
CORE_DIR += ..
|
CORE_DIR += ..
|
||||||
|
|
||||||
VERSION := 0.11.1
|
#VERSION := 0.13
|
||||||
export VERSION
|
#export VERSION
|
||||||
CONF ?= debug
|
CONF ?= debug
|
||||||
|
|
||||||
BIN := $(CORE_DIR)/build/wasm_bin
|
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 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 += -s USE_WEBGL2=1
|
||||||
# CFLAGS += -Wcast-align -Wover-aligned -s SAFE_HEAP=1 -s WARN_UNALIGNED=1
|
# CFLAGS += -Wcast-align -Wover-aligned -s SAFE_HEAP=1 -s WARN_UNALIGNED=1
|
||||||
WASM_LDFLAGS :=
|
WASM_LDFLAGS := -lidbfs.js
|
||||||
|
|
||||||
LDFLAGS += -lc -lm -ldl
|
LDFLAGS += -lc -lm -ldl
|
||||||
CFLAGS += -Wno-deprecated-declarations
|
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_SOURCES := $(shell realpath --relative-to=$(CORE_DIR) $(CORE_SOURCES_RAW))
|
||||||
CORE_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(CORE_SOURCES))
|
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))
|
WASM_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(WASM_SOURCES))
|
||||||
|
|
||||||
WEB_SOURCES := $(shell ls ressources/.)
|
WEB_SOURCES := $(shell ls ressources/.)
|
||||||
|
28
wasm/main.c
28
wasm/main.c
@ -10,6 +10,8 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
|
||||||
|
#include "SDL/audio/audio.h"
|
||||||
|
|
||||||
GB_gameboy_t gb;
|
GB_gameboy_t gb;
|
||||||
|
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
@ -17,13 +19,11 @@ SDL_Renderer *renderer;
|
|||||||
SDL_Surface *screen;
|
SDL_Surface *screen;
|
||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
SDL_PixelFormat *pixel_format;
|
SDL_PixelFormat *pixel_format;
|
||||||
SDL_AudioDeviceID device_id;
|
|
||||||
|
|
||||||
shader_t shader;
|
shader_t shader;
|
||||||
|
|
||||||
static SDL_Rect rect;
|
static SDL_Rect rect;
|
||||||
static unsigned factor;
|
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 pixel_buffer_1[256 * 224], pixel_buffer_2[256 * 224];
|
||||||
static uint32_t *active_pixel_buffer = pixel_buffer_1;
|
static uint32_t *active_pixel_buffer = pixel_buffer_1;
|
||||||
static uint32_t *previous_pixel_buffer = pixel_buffer_2;
|
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)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
SDL_QueueAudio(device_id, sample, sizeof(*sample));
|
|
||||||
|
GB_audio_queue_sample(sample);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_viewport(void)
|
void update_viewport(void)
|
||||||
@ -245,7 +246,7 @@ void init_gb() {
|
|||||||
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
||||||
GB_set_pixels_output(&gb, active_pixel_buffer);
|
GB_set_pixels_output(&gb, active_pixel_buffer);
|
||||||
GB_set_rgb_encode_callback(&gb, rgb_encode);
|
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_color_correction_mode(&gb, configuration.color_correction_mode);
|
||||||
GB_set_highpass_filter_mode(&gb, configuration.highpass_mode);
|
GB_set_highpass_filter_mode(&gb, configuration.highpass_mode);
|
||||||
GB_set_rewind_length(&gb, 0);
|
GB_set_rewind_length(&gb, 0);
|
||||||
@ -352,20 +353,7 @@ int EMSCRIPTEN_KEEPALIVE init() {
|
|||||||
unsigned audio_sample_rate = query_sample_rate_of_audiocontexts();
|
unsigned audio_sample_rate = query_sample_rate_of_audiocontexts();
|
||||||
fprintf(stderr, "Sample rate: %u\n", audio_sample_rate);
|
fprintf(stderr, "Sample rate: %u\n", audio_sample_rate);
|
||||||
|
|
||||||
memset(&want_aspec, 0, sizeof(want_aspec));
|
GB_audio_init(audio_sample_rate);
|
||||||
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);
|
|
||||||
|
|
||||||
EM_ASM({
|
EM_ASM({
|
||||||
function audio_workaround(e) {
|
function audio_workaround(e) {
|
||||||
@ -418,7 +406,7 @@ int EMSCRIPTEN_KEEPALIVE init() {
|
|||||||
}
|
}
|
||||||
update_viewport();
|
update_viewport();
|
||||||
|
|
||||||
SDL_PauseAudioDevice(device_id, 0);
|
GB_audio_set_paused(false);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user