fix buzzing sound in audio

This commit is contained in:
Michael Hahn
2021-01-16 19:21:24 -05:00
parent 373bf57703
commit 1e5e73e9bf
3 changed files with 43 additions and 17 deletions
+25 -16
View File
@@ -264,6 +264,11 @@ typedef struct stPokeyState
} PokeyState;
static struct {
double s16;
double s8;
} volume;
PokeyState pokey_states[NPOKEYS];
/* Forward declarations for ResetPokeyState */
@@ -1421,6 +1426,10 @@ int MZPOKEYSND_Init(ULONG freq17, int playback_freq, UBYTE num_pokeys,
#ifdef SYNCHRONIZED_SOUND
init_syncsound();
#endif
volume.s8 = POKEYSND_volume * 0xff / 256.0;
volume.s16 = POKEYSND_volume * 0xffff / 256.0;
return 0; /* OK */
}
@@ -2341,16 +2350,16 @@ static void mzpokeysnd_process_8(void* sndbuffer, int sndn)
#endif
#ifdef VOL_ONLY_SOUND
buffer[0] = (UBYTE)floor((generate_sample(pokey_states) + POKEYSND_sampout - MAX_SAMPLE / 2.0)
* (255.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
buffer[0] = (UBYTE)floor((generate_sample(pokey_states) + POKEYSND_sampout)
* (255.0 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95) + 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
#else
buffer[0] = (UBYTE)floor((generate_sample(pokey_states) - MAX_SAMPLE / 2.0)
* (255.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
buffer[0] = (UBYTE)floor(generate_sample(pokey_states)
* (255.0 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95) + 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
#endif
for(i=1; i<num_cur_pokeys; i++)
{
buffer[i] = (UBYTE)floor((generate_sample(pokey_states + i) - MAX_SAMPLE / 2.0)
* (255.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
buffer[i] = (UBYTE)floor(generate_sample(pokey_states + i)
* (255.0 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95) + 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
}
buffer += num_cur_pokeys;
nsam -= num_cur_pokeys;
@@ -2389,16 +2398,16 @@ static void mzpokeysnd_process_16(void* sndbuffer, int sndn)
}
#endif
#ifdef VOL_ONLY_SOUND
buffer[0] = (SWORD)floor((generate_sample(pokey_states) + POKEYSND_sampout - MAX_SAMPLE / 2.0)
* (65535.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
buffer[0] = (SWORD)floor((generate_sample(pokey_states) + POKEYSND_sampout)
* (65535.0 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95) + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
#else
buffer[0] = (SWORD)floor((generate_sample(pokey_states) - MAX_SAMPLE / 2.0)
* (65535.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
buffer[0] = (SWORD)floor(generate_sample(pokey_states)
* (65535.0 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95) + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
#endif
for(i=1; i<num_cur_pokeys; i++)
{
buffer[i] = (SWORD)floor((generate_sample(pokey_states + i) - MAX_SAMPLE / 2.0)
* (65535.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
buffer[i] = (SWORD)floor(generate_sample(pokey_states + i)
* (65535.0 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95) + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
}
buffer += num_cur_pokeys;
nsam -= num_cur_pokeys;
@@ -2434,16 +2443,16 @@ static void generate_sync(unsigned int num_ticks)
advance_ticks(pokey_states + i, ticks);
if (POKEYSND_snd_flags & POKEYSND_BIT16) {
*((SWORD *)buffer) = (SWORD)floor(
(interp_read_resam_all(pokey_states + i, samp_pos) - MAX_SAMPLE / 2.0)
* (65535.0 / MAX_SAMPLE / 4 * M_PI * 0.95)
interp_read_resam_all(pokey_states + i, samp_pos)
* (volume.s16 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95)
+ 0.5 + 0.5 * rand() / RAND_MAX - 0.25
);
buffer += 2;
}
else
*buffer++ = (UBYTE)floor(
(interp_read_resam_all(pokey_states + i, samp_pos) - MAX_SAMPLE / 2.0)
* (255.0 / MAX_SAMPLE / 4 * M_PI * 0.95)
interp_read_resam_all(pokey_states + i, samp_pos)
* (volume.s8 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95)
+ 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25
);
}
+17 -1
View File
@@ -168,6 +168,8 @@ int POKEYSND_bienias_fix = TRUE; /* when TRUE, high frequencies get emulated: b
int POKEYSND_stereo_enabled = FALSE;
#endif
int POKEYSND_volume = 0x100;
/* multiple sound engine interface */
static void pokeysnd_process_8(void *sndbuffer, int sndn);
static void pokeysnd_process_16(void *sndbuffer, int sndn);
@@ -1246,6 +1248,16 @@ static void Update_serio_sound_rf(int out, UBYTE data)
}
#endif /* SERIO_SOUND */
void POKEYSND_SetVolume(int vol)
{
if (vol > 100)
vol = 100;
if (vol < 0)
vol = 0;
POKEYSND_volume = vol * 0x100 / 100;
}
static void pokeysnd_process_16(void *sndbuffer, int sndn)
{
UWORD *buffer = (UWORD *) sndbuffer;
@@ -1254,7 +1266,11 @@ static void pokeysnd_process_16(void *sndbuffer, int sndn)
pokeysnd_process_8(buffer, sndn);
for (i = sndn - 1; i >= 0; i--) {
int smp = ((int) (((UBYTE *) buffer)[i]) - 0x80) * 0x100;
#ifndef POKEYSND_SIGNED_SAMPLES
int smp = ((int) (((UBYTE *) buffer)[i]) - 0x80) * POKEYSND_volume;
#else
int smp = ((int) ((SBYTE *) buffer)[i]) * POKEYSND_volume;
#endif
if (smp > 32767)
smp = 32767;
+1
View File
@@ -84,6 +84,7 @@ extern "C" {
extern SLONG POKEYSND_playback_freq;
extern UBYTE POKEYSND_num_pokeys;
extern int POKEYSND_snd_flags;
extern int POKEYSND_volume;
extern int POKEYSND_enable_new_pokey;
extern int POKEYSND_stereo_enabled;