From 6e51e8975eda81432f64dfac0a5cf93c8bbe32a4 Mon Sep 17 00:00:00 2001 From: SarahW Date: Sat, 10 Jun 2017 21:00:23 +0100 Subject: [PATCH] Sound buffer size now configurable. --- src/dac.c | 3 ++- src/ibm.h | 2 -- src/pc.c | 7 +++++++ src/pc.rc | 9 +++++++++ src/resources.h | 3 +++ src/sound.c | 31 ++++++++++++++++++++++--------- src/sound.h | 6 ++++++ src/sound_ad1848.h | 2 +- src/sound_adlibgold.c | 4 ++-- src/sound_cms.c | 2 +- src/sound_emu8k.h | 2 +- src/sound_gus.c | 2 +- src/sound_opl.h | 4 +++- src/sound_pas16.c | 2 +- src/sound_ps1.c | 2 +- src/sound_pssj.c | 2 +- src/sound_sb_dsp.h | 2 +- src/sound_sn76489.h | 4 +++- src/sound_speaker.c | 2 +- src/sound_ssi2001.c | 2 +- src/soundopenal.c | 3 +++ src/win.c | 22 ++++++++++++++++++++++ 22 files changed, 92 insertions(+), 26 deletions(-) diff --git a/src/dac.c b/src/dac.c index b24561b..b8f7052 100644 --- a/src/dac.c +++ b/src/dac.c @@ -1,5 +1,6 @@ #include "ibm.h" #include "dac.h" +#include "sound.h" uint8_t dac,dac2; uint8_t dacctrl; @@ -43,7 +44,7 @@ void pollss() } } -int16_t dacbuffer[SOUNDBUFLEN+20]; +int16_t dacbuffer[MAXSOUNDBUFLEN+20]; int dacbufferpos=0; void getdacsamp() { diff --git a/src/ibm.h b/src/ibm.h index 20cabea..183d462 100644 --- a/src/ibm.h +++ b/src/ibm.h @@ -485,8 +485,6 @@ float VGACONST1,VGACONST2; float RTCCONST; int gated,speakval,speakon; -#define SOUNDBUFLEN (48000/20) - /*Sound Blaster*/ #define SADLIB 1 /*No DSP*/ diff --git a/src/pc.c b/src/pc.c index 474489b..df2c991 100644 --- a/src/pc.c +++ b/src/pc.c @@ -36,6 +36,7 @@ #include "serial.h" #include "sound.h" #include "sound_cms.h" +#include "sound_dbopl.h" #include "sound_opl.h" #include "sound_sb.h" #include "sound_speaker.h" @@ -430,6 +431,8 @@ void resetpchard() ioctl_reset(); } } + + sound_update_buf_length(); } char romsets[17][40]={"IBM PC","IBM XT","Generic Turbo XT","Euro PC","Tandy 1000","Amstrad PC1512","Sinclair PC200","Amstrad PC1640","IBM AT","AMI 286 clone","Dell System 200","Misc 286","IBM AT 386","Misc 386","386 clone","486 clone","486 clone 2"}; @@ -631,6 +634,8 @@ void loadconfig(char *fn) window_x = config_get_int(CFG_GLOBAL, NULL, "window_x", 0); window_y = config_get_int(CFG_GLOBAL, NULL, "window_y", 0); window_remember = config_get_int(CFG_GLOBAL, NULL, "window_remember", 0); + + sound_buf_len = config_get_int(CFG_GLOBAL, NULL, "sound_buf_len", 200); GAMEBLASTER = config_get_int(CFG_MACHINE, NULL, "gameblaster", 0); GUS = config_get_int(CFG_MACHINE, NULL, "gus", 0); @@ -781,6 +786,8 @@ void saveconfig(char *fn) config_set_int(CFG_GLOBAL, NULL, "window_x", window_x); config_set_int(CFG_GLOBAL, NULL, "window_y", window_y); config_set_int(CFG_GLOBAL, NULL, "window_remember", window_remember); + + config_set_int(CFG_GLOBAL, NULL, "sound_buf_len", sound_buf_len); config_set_int(CFG_MACHINE, NULL, "gameblaster", GAMEBLASTER); config_set_int(CFG_MACHINE, NULL, "gus", GUS); diff --git a/src/pc.rc b/src/pc.rc index 429788d..9a3f432 100644 --- a/src/pc.rc +++ b/src/pc.rc @@ -53,6 +53,15 @@ BEGIN MENUITEM SEPARATOR MENUITEM "Disc activity indicator", IDM_VID_DISC END + POPUP "&Sound" + BEGIN + POPUP "&Buffer length" + BEGIN + MENUITEM "&100ms", IDM_BUF_100MS + MENUITEM "&200ms", IDM_BUF_200MS + MENUITEM "&400ms", IDM_BUF_400MS + END + END END POPUP "&Misc" BEGIN diff --git a/src/resources.h b/src/resources.h index af0bac4..9e1b226 100644 --- a/src/resources.h +++ b/src/resources.h @@ -21,6 +21,9 @@ #define IDM_VID_FS_43 40072 #define IDM_VID_FS_SQ 40073 #define IDM_VID_FS_INT 40074 +#define IDM_BUF_100MS 40080 +#define IDM_BUF_200MS 40081 +#define IDM_BUF_400MS 40082 #define IDM_CDROM_EMPTY 40100 #define IDM_CDROM_REAL 40100 #define IDM_CDROM_DISABLED 40200 diff --git a/src/sound.c b/src/sound.c index 26a93a2..d8a351b 100644 --- a/src/sound.c +++ b/src/sound.c @@ -120,7 +120,18 @@ static int16_t cd_buffer[CD_BUFLEN * 2]; static thread_t *sound_cd_thread_h; static event_t *sound_cd_event; static unsigned int cd_vol_l, cd_vol_r; -static int cd_buf_update = CD_BUFLEN / SOUNDBUFLEN; + +int sound_buf_len = 48000 / 10; + +void sound_update_buf_length() +{ + int new_buf_len = (48000 / (1000 / sound_buf_len)) / 4; + + if (new_buf_len > MAXSOUNDBUFLEN) + new_buf_len = MAXSOUNDBUFLEN; + + SOUNDBUFLEN = new_buf_len; +} void sound_set_cd_volume(unsigned int vol_l, unsigned int vol_r) { @@ -194,7 +205,7 @@ void sound_init() initalmain(0,NULL); inital(); - outbuffer = malloc(SOUNDBUFLEN * 2 * sizeof(int32_t)); + outbuffer = malloc(MAXSOUNDBUFLEN * 2 * sizeof(int32_t)); sound_cd_event = thread_create_event(); sound_cd_thread_h = thread_create(sound_cd_thread, NULL); @@ -207,9 +218,17 @@ void sound_add_handler(void (*get_buffer)(int32_t *buffer, int len, void *p), vo sound_handlers_num++; } +static int cd_pos = 0; void sound_poll(void *priv) { sound_poll_time += sound_poll_latch; + + cd_pos++; + if (cd_pos == (CD_BUFLEN * 48000) / CD_FREQ) + { + cd_pos = 0; + thread_set_event(sound_cd_event); + } sound_pos_global++; if (sound_pos_global == SOUNDBUFLEN) @@ -238,14 +257,8 @@ void sound_poll(void *priv) if (soundon) givealbuffer(outbuffer); - cd_buf_update--; - if (!cd_buf_update) - { - cd_buf_update = (48000 / SOUNDBUFLEN) / (CD_FREQ / CD_BUFLEN); - thread_set_event(sound_cd_event); - } - sound_pos_global = 0; + sound_update_buf_length(); } } diff --git a/src/sound.h b/src/sound.h index f5c59a3..4bf0395 100644 --- a/src/sound.h +++ b/src/sound.h @@ -27,3 +27,9 @@ void inital(); void givealbuffer(int32_t *buf); void givealbuffer_cd(int16_t *buf); + +extern int sound_buf_len; +void sound_update_buf_length(); + +extern int SOUNDBUFLEN; +#define MAXSOUNDBUFLEN (48000 / 10) diff --git a/src/sound_ad1848.h b/src/sound_ad1848.h index 5cba0f1..34b707a 100644 --- a/src/sound_ad1848.h +++ b/src/sound_ad1848.h @@ -21,7 +21,7 @@ typedef struct ad1848_t int timer_count, timer_latch; - int16_t buffer[SOUNDBUFLEN * 2]; + int16_t buffer[MAXSOUNDBUFLEN * 2]; int pos; } ad1848_t; diff --git a/src/sound_adlibgold.c b/src/sound_adlibgold.c index bc5b445..1f62dc4 100644 --- a/src/sound_adlibgold.c +++ b/src/sound_adlibgold.c @@ -55,8 +55,8 @@ typedef struct adgold_t int vol_l, vol_r; int treble, bass; - int16_t opl_buffer[SOUNDBUFLEN * 2]; - int16_t mma_buffer[2][SOUNDBUFLEN]; + int16_t opl_buffer[MAXSOUNDBUFLEN * 2]; + int16_t mma_buffer[2][MAXSOUNDBUFLEN]; int pos; diff --git a/src/sound_cms.c b/src/sound_cms.c index 7c4a33f..6361dd9 100644 --- a/src/sound_cms.c +++ b/src/sound_cms.c @@ -23,7 +23,7 @@ typedef struct cms_t int noisecount[2][2]; int noisetype[2][2]; - int16_t buffer[SOUNDBUFLEN * 2]; + int16_t buffer[MAXSOUNDBUFLEN * 2]; int pos; } cms_t; diff --git a/src/sound_emu8k.h b/src/sound_emu8k.h index ac091de..f54576a 100644 --- a/src/sound_emu8k.h +++ b/src/sound_emu8k.h @@ -81,7 +81,7 @@ typedef struct emu8k_t int16_t out_l, out_r; int pos; - int32_t buffer[SOUNDBUFLEN * 2]; + int32_t buffer[MAXSOUNDBUFLEN * 2]; } emu8k_t; void emu8k_init(emu8k_t *emu8k, int onboard_ram); diff --git a/src/sound_gus.c b/src/sound_gus.c index c6322ba..bc45155 100644 --- a/src/sound_gus.c +++ b/src/sound_gus.c @@ -39,7 +39,7 @@ typedef struct gus_t int32_t out_l, out_r; - int16_t buffer[2][SOUNDBUFLEN]; + int16_t buffer[2][MAXSOUNDBUFLEN]; int pos; int samp_timer, samp_latch; diff --git a/src/sound_opl.h b/src/sound_opl.h index 8d67bb9..0728d67 100644 --- a/src/sound_opl.h +++ b/src/sound_opl.h @@ -1,3 +1,5 @@ +#include "sound.h" + typedef struct opl_t { int chip_nr[2]; @@ -7,7 +9,7 @@ typedef struct opl_t int16_t filtbuf[2]; - int16_t buffer[SOUNDBUFLEN * 2]; + int16_t buffer[MAXSOUNDBUFLEN * 2]; int pos; } opl_t; diff --git a/src/sound_pas16.c b/src/sound_pas16.c index bbb2fbc..c736611 100644 --- a/src/sound_pas16.c +++ b/src/sound_pas16.c @@ -132,7 +132,7 @@ typedef struct pas16_t opl_t opl; sb_dsp_t dsp; - int16_t pcm_buffer[2][SOUNDBUFLEN]; + int16_t pcm_buffer[2][MAXSOUNDBUFLEN]; int pos; } pas16_t; diff --git a/src/sound_ps1.c b/src/sound_ps1.c index a7de420..fb8a9a5 100644 --- a/src/sound_ps1.c +++ b/src/sound_ps1.c @@ -21,7 +21,7 @@ typedef struct ps1_audio_t uint8_t dac_val; - int16_t buffer[SOUNDBUFLEN]; + int16_t buffer[MAXSOUNDBUFLEN]; int pos; } ps1_audio_t; diff --git a/src/sound_pssj.c b/src/sound_pssj.c index 3ffaaae..d692f33 100644 --- a/src/sound_pssj.c +++ b/src/sound_pssj.c @@ -27,7 +27,7 @@ typedef struct pssj_t int wave_pos; int pulse_width; - int16_t buffer[SOUNDBUFLEN]; + int16_t buffer[MAXSOUNDBUFLEN]; int pos; } pssj_t; diff --git a/src/sound_sb_dsp.h b/src/sound_sb_dsp.h index 81b4641..e945b40 100644 --- a/src/sound_sb_dsp.h +++ b/src/sound_sb_dsp.h @@ -57,7 +57,7 @@ typedef struct sb_dsp_t int wb_time, wb_full; - int16_t buffer[SOUNDBUFLEN * 2]; + int16_t buffer[MAXSOUNDBUFLEN * 2]; int pos; } sb_dsp_t; diff --git a/src/sound_sn76489.h b/src/sound_sn76489.h index 2f7a687..d5acb62 100644 --- a/src/sound_sn76489.h +++ b/src/sound_sn76489.h @@ -1,3 +1,5 @@ +#include "sound.h" + enum { SN76496, @@ -23,7 +25,7 @@ typedef struct sn76489_t int type; int extra_divide; - int16_t buffer[SOUNDBUFLEN]; + int16_t buffer[MAXSOUNDBUFLEN]; int pos; double psgconst; diff --git a/src/sound_speaker.c b/src/sound_speaker.c index a6c386d..d70651c 100644 --- a/src/sound_speaker.c +++ b/src/sound_speaker.c @@ -4,7 +4,7 @@ int speaker_mute = 0; -static int16_t speaker_buffer[SOUNDBUFLEN]; +static int16_t speaker_buffer[MAXSOUNDBUFLEN]; static int speaker_pos = 0; diff --git a/src/sound_ssi2001.c b/src/sound_ssi2001.c index ff83041..2efc15c 100644 --- a/src/sound_ssi2001.c +++ b/src/sound_ssi2001.c @@ -10,7 +10,7 @@ typedef struct ssi2001_t { void *psid; - int16_t buffer[SOUNDBUFLEN * 2]; + int16_t buffer[MAXSOUNDBUFLEN * 2]; int pos; } ssi2001_t; diff --git a/src/soundopenal.c b/src/soundopenal.c index 639bbcc..5dd088f 100644 --- a/src/soundopenal.c +++ b/src/soundopenal.c @@ -16,6 +16,9 @@ ALuint buffers_cd[4]; // front and back buffers static ALuint source[2]; // audio source #endif #define FREQ 48000 + +int SOUNDBUFLEN = 48000/20; + #define BUFLEN SOUNDBUFLEN void closeal(); diff --git a/src/win.c b/src/win.c index c9d2093..9769261 100644 --- a/src/win.c +++ b/src/win.c @@ -646,6 +646,9 @@ int WINAPI WinMain (HINSTANCE hThisInstance, CheckMenuItem(menu, IDM_VID_REMEMBER, window_remember ? MF_CHECKED : MF_UNCHECKED); CheckMenuItem(menu, IDM_BPB_DISABLE, bpb_disable ? MF_CHECKED : MF_UNCHECKED); CheckMenuItem(menu, IDM_VID_DISC, vid_disc_indicator ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(menu, IDM_BUF_100MS, (sound_buf_len == 100) ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(menu, IDM_BUF_200MS, (sound_buf_len == 200) ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(menu, IDM_BUF_400MS, (sound_buf_len == 400) ? MF_CHECKED : MF_UNCHECKED); if (!loadbios()) { @@ -1051,6 +1054,25 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM saveconfig(NULL); break; + case IDM_BUF_100MS: case IDM_BUF_200MS: case IDM_BUF_400MS: + switch (LOWORD(wParam)) + { + case IDM_BUF_100MS: + sound_buf_len = 100; + break; + case IDM_BUF_200MS: + sound_buf_len = 200; + break; + case IDM_BUF_400MS: + sound_buf_len = 400; + break; + } + CheckMenuItem(hmenu, IDM_BUF_100MS, (sound_buf_len == 100) ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(hmenu, IDM_BUF_200MS, (sound_buf_len == 200) ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(hmenu, IDM_BUF_400MS, (sound_buf_len == 400) ? MF_CHECKED : MF_UNCHECKED); + saveconfig(NULL); + break; + case IDM_CDROM_DISABLED: if (cdrom_enabled) {