📄 mzpokeysnd.c
字号:
}
else
{
if(ps->c2divstart <= lim)
{
ps->c2stop = 1;
hfa = 1;
}
}
}
else if(ps->c2sw3 && !ps->c2sw2 && ps->c2sw1 && !ps->c0sw4) /* if channel 2 is poly4 and no filter for c0... */
{
/* period for poly4 signal is 15 cycles */
if(ps->c3_f2)
{
if(ps->c3divstart <= lim*2/15) /* all poly4 signal is above Nyquist */
{
ps->c2stop = 1;
hfa = 1;
}
}
else
{
if(ps->c2divstart <= lim*2/15)
{
ps->c2stop = 1;
hfa = 1;
}
}
}
ps->outvol_2 = 2*ps->readout_2(ps);
if(hfa)
ps->outvol_2 = ps->vol2;
}
static void Update_c3stop(PokeyState* ps)
{
unsigned long lim = pokey_frq/2/audible_frq;
unsigned char hfa = 0;
ps->c3stop = 0;
if(!ps->c1sw4 && !ps->c3_f2 && (ps->c3vo || ps->vol3 == 0))
ps->c3stop = 1;
/* If channel 3 is a pure tone */
else if(ps->c3sw3 && ps->c3sw2 && !ps->c1sw4 && ps->c3divstart <= lim)
{
ps->c3stop = 1;
hfa = 1;
}
else if(ps->c3sw3 && !ps->c3sw2 && ps->c3sw1 && !ps->c1sw4 && ps->c3divstart <= lim*2/15) /* all poly4 signal is above Nyquist */
{
ps->c3stop = 1;
hfa = 1;
}
ps->outvol_3 = 2*ps->readout_3(ps);
if(hfa)
ps->outvol_3 = ps->vol3;
}
static void Update_pokey_sound_mz(uint16 addr, uint8 val, uint8 chip, uint8 gain)
{
PokeyState* ps = pokey_states+chip;
switch(addr & 0x0f)
{
case _AUDF1:
ps->c0diva = val;
Update_c0divstart(ps);
if(ps->c1_f0)
{
Update_c1divstart(ps);
Update_c1stop(ps);
}
Update_c0stop(ps);
ps->forcero = 1;
break;
case _AUDC1:
ps->c0sw1 = (val & 0x40) != 0;
ps->c0sw2 = (val & 0x20) != 0;
ps->c0sw3 = (val & 0x80) != 0;
ps->vol0 = val & 0xF;
ps->c0vo = (val & 0x10) != 0;
Update_readout_0(ps);
Update_event0(ps);
Update_c0stop(ps);
ps->forcero = 1;
break;
case _AUDF2:
ps->c1diva = val;
Update_c1divstart(ps);
if(ps->c1_f0)
{
Update_c0divstart(ps);
Update_c0stop(ps);
}
Update_c1stop(ps);
ps->forcero = 1;
break;
case _AUDC2:
ps->c1sw1 = (val & 0x40) != 0;
ps->c1sw2 = (val & 0x20) != 0;
ps->c1sw3 = (val & 0x80) != 0;
ps->vol1 = val & 0xF;
ps->c1vo = (val & 0x10) != 0;
Update_readout_1(ps);
Update_event1(ps);
Update_c1stop(ps);
ps->forcero = 1;
break;
case _AUDF3:
ps->c2diva = val;
Update_c2divstart(ps);
if(ps->c3_f2)
{
Update_c3divstart(ps);
Update_c3stop(ps);
}
Update_c2stop(ps);
ps->forcero = 1;
break;
case _AUDC3:
ps->c2sw1 = (val & 0x40) != 0;
ps->c2sw2 = (val & 0x20) != 0;
ps->c2sw3 = (val & 0x80) != 0;
ps->vol2 = val & 0xF;
ps->c2vo = (val & 0x10) != 0;
Update_readout_2(ps);
Update_event2(ps);
Update_c2stop(ps);
ps->forcero = 1;
break;
case _AUDF4:
ps->c3diva = val;
Update_c3divstart(ps);
if(ps->c3_f2)
{
Update_c2divstart(ps);
Update_c2stop(ps);
}
Update_c3stop(ps);
ps->forcero = 1;
break;
case _AUDC4:
ps->c3sw1 = (val & 0x40) != 0;
ps->c3sw2 = (val & 0x20) != 0;
ps->c3sw3 = (val & 0x80) != 0;
ps->vol3 = val & 0xF;
ps->c3vo = (val & 0x10) != 0;
Update_readout_3(ps);
Update_event3(ps);
Update_c3stop(ps);
ps->forcero = 1;
break;
case _AUDCTL:
ps->selpoly9 = (val & 0x80) != 0;
Update_audctl(ps,val);
Update_readout_0(ps);
Update_readout_1(ps);
Update_readout_2(ps);
Update_readout_3(ps);
Update_c0divstart(ps);
Update_c1divstart(ps);
Update_c2divstart(ps);
Update_c3divstart(ps);
Update_c0stop(ps);
Update_c1stop(ps);
Update_c2stop(ps);
Update_c3stop(ps);
ps->forcero = 1;
break;
case _STIMER:
if(ps->c1_f0)
ps->c0divpos = ps->c0divstart_p;
else
ps->c0divpos = ps->c0divstart;
ps->c1divpos = ps->c1divstart;
if(ps->c3_f2)
ps->c2divpos = ps->c2divstart_p;
else
ps->c2divpos = ps->c2divstart;
ps->c3divpos = ps->c3divstart;
ps->c0t2 = 1;
ps->c1t2 = 1;
ps->c2t2 = 0;
ps->c3t2 = 0;
break;
}
}
void Pokey_debugreset(uint8 chip)
{
PokeyState* ps = pokey_states+chip;
if(ps->c1_f0)
ps->c0divpos = ps->c0divstart_p;
else
ps->c0divpos = ps->c0divstart;
ps->c1divpos = ps->c1divstart;
if(ps->c3_f2)
ps->c2divpos = ps->c2divstart_p;
else
ps->c2divpos = ps->c2divstart;
ps->c3divpos = ps->c3divstart;
ps->c0t2 = 1;
ps->c1t2 = 1;
ps->c2t2 = 1;
ps->c3t2 = 1;
}
/**************************************************************
Master gain and DC offset calculation
by Michael Borisov
In order to use the available 8-bit or 16-bit dynamic range
to full extent, reducing the influence of quantization
noise while simultaneously avoiding overflows, gain
and DC offset should be set to appropriate value.
All Pokey-generated sounds have maximal amplitude of 15.
When all four channels sound simultaneously and in the
same phase, amplidudes would add up to 60.
If Pokey is generating a 'pure tone', it always has a DC
offset of half its amplitude. For other signals (produced
by poly generators) DC offset varies, but it is always near
to half amplitude and never exceeds this value.
In case that pure tone base frequency is outside of audible
range (ultrasound frequency for high sample rates and above-
Nyquist frequency for low sample rates), to speed up the engine,
the generator is stopped while having only DC offset on the
output (half of corresponding AUDV value). In order that this
DC offset can be always represented as integer, AUDV values
are multiplied by 2 when the generator works.
Therefore maximum integer value before resampling filters
would be 60*2 = 120 while having maximum DC offset of 60.
Resampling does not change the DC offset, therefore we may
subtract it from the signal either before or after resampling.
In mzpokeysnd, DC offset is subtracted after resampling, however
for better understanding in further measurements I assume
subtracting DC before. So, input range for the resampler
becomes [-60 .. 60].
Resampling filter removes some harmonics from the signal as if
the rectangular wave was Fourier transformed forth-and-back,
while zeroing all harmonics above cutoff frequency. In case
of high-frequency pure tone (above samplerate/8), only first
harmonic of the Fourier transofm will remain. As it
is known, Fourier-transform of the rectangular function of
amplitude 1 has first oscillation component of amplitude 4/M_PI.
Therefore, maximum sample values for filtered rectangular
signal may exceed the amplitude of rectangular signal
by up to 4/M_PI times.
Since our range before resampler is -60 .. 60, taking into
account mentioned effect with band limiting, range of values
on the resampler output appears to be in the following bounds:
[-60*4/M_PI .. 60*4/M_PI]
In order to map this into signed 8-bit range [-128 .. 127], we
should multiply the resampler output by 127/60/4*M_PI.
As it is common for sound hardware to have 8-bit sound unsigned,
additional DC offset of 128 must be added.
For 16-bit case the output range is [-32768 .. 32767], and
we should multiply the resampler output by 32767/60/4*M_PI
To make some room for numerical errors, filter ripples and
quantization noise, so that they do not cause overflows in
quantization, dynamic range is reduced in mzpokeysnd by
multiplying the output amplitude with 0.95, reserving 5%
of the total range for such effects, which is about 0.51db.
Mentioned gain and DC values were tested with 17kHz audio
playing synchronously on 4 channels, which showed to be
utilizing 95% of the sample values range.
Since any other gain value will be not optimal, I removed
user gain setting and hard-coded the gain into mzpokeysnd
---
A note from Piotr Fusik:
I've added support for the key click sound generated by GTIA. Its
volume seems to be pretty much like 8 on single POKEY's channel.
So, the volumes now can sum up to 136 (4 channels * 15 * 2
+ 8 * 2 for GTIA), not 120.
A note from Mark Grebe:
I've added back in the console and sio sounds from the old
pokey version. So, now the volumes can sum up to 152
(4 channesl * 15 * 2 + 8 * 4 for old sound), not 120 or 136.
******************************************************************/
/******************************************************************
Quantization effects and dithering
by Michael Borisov
Quantization error in the signal has an expectation value of half
the LSB, when the rounding is performed properly. Sometimes they
express quantization error as a random function with even
distribution over the range [-0.5 to 0.5]. Spectrum of this function
is flat, because it's a white noise.
Power of a discrete signal (including noise) is calculated as
mean square of its samples. For the mentioned above noise
this is approximately 0.33. Therefore, in decibels for 8-bit case,
our noise will have power of 10*log10(0.33/256/256) = -53dB
Because noise is white, this power of -53dB will be evenly
distributed over the whole signal band upto Nyquist frequency.
The larger the band is (higher sampling frequency), less
is the quantisation noise floor. For 8000Hz noise floor is
10*log10(0.33/256/256/4000) = -89dB/Hz, and for 44100Hz noise
floor is 10*log10(0.33/256/256/22050) = -96.4dB/Hz.
This shows that higher sampling rates are better in sense of
quantization noise. Moreover, as large part of quantization noise
in case of 44100Hz will fall into ultrasound and hi-frequency
area 10-20kHz where human ear is less sensitive, this will
show up as great improvement in quantization noise performance
compared to 8000Hz.
I was plotting spectral analysis for sounds produced by mzpokeysnd
to check these measures. And it showed up that in 8-bit case
there is no expected flat noise floor of -89db/Hz for 8000Hz,
but some distortion spectral peaks had higher amplitude than
the aliasing peaks in 16-bit case. This was a proof to another
statement which says that quantization noise tends to become
correlated with the signal. Correlation is especially strong
for simple signals generated by Pokey. Correlation means that
the noise power of -53db is no longer evenly distributed
across the whole frequency range, but concentrates in larger
peaks at locations which depend on the Pokey signal.
To decorrelate quantization distortion and make it again
white noise, which would improve the sound spectrum, since
the maximum distortion peaks will have less amplitude,
dithering is used. Another white noise is added to the signal
before quantization. Since this added noise is not correlated
with the signal, it shows itself as a flat noise floor.
Quantization noise now tries to correlate with the dithering
noise, but this does not lead to appearance of sharp
spectral peaks any more :)
Another thing is that for listening, white noise is better than
distortion. This is because human hearing has some 'noise
reduction' system which makes it easier to percept sounds
on the white noise background.
From the other point of view, if a signal has high and low
spectral peaks, it is desirable that there is no distortion
component with peaks of amplitude comparable to those of
the true signal. Otherwise, perception of background low-
amplitude signals will be disrupted. That's why they say
that dithering extends dynamic range.
Dithering does not eliminate correlation of quantization noise
completely. Degree of reduction of this effect depends on
the dithering noise power. The higher is dithering noise,
the more quantization noise is decorrelated. But this also
leads to increase of noise percepted by the listener. So, an
optimum value should be selected. My experiments show that
unbiased rand() noise of amplitude 0.25 LSB is doing well.
Test spectral pictures for 8-bit sound, 8kHz sampling rate,
dithered, show a noise floor of approx. -87dB/Hz.
******************************************************************/
#define MAX_SAMPLE 152
extern int atari_speaker;
static void Pokey_process_8(void* sndbuffer, unsigned sndn)
{
unsigned short i;
int nsam = sndn;
uint8 *buffer = (uint8 *) sndbuffer;
if(num_cur_pokeys<1)
return; /* module was not initialized */
/* if there are two pokeys, then the signal is stereo
we assume even sndn */
while(nsam >= (int) num_cur_pokeys)
{
#ifdef VOL_ONLY_SOUND
if( sampbuf_rptr!=sampbuf_ptr )
{ int l;
if( sampbuf_cnt[sampbuf_rptr]>0 )
sampbuf_cnt[sampbuf_rptr]-=1280;
while( (l=sampbuf_cnt[sampbuf_rptr])<=0 )
{ sampout=sampbuf_val[sampbuf_rptr];
sampbuf_rptr++;
if( sampbuf_rptr>=SAMPBUF_MAX )
sampbuf_rptr=0;
if( sampbuf_rptr!=sampbuf_ptr )
{
sampbuf_cnt[sampbuf_rptr]+=l;
}
else break;
}
}
#endif
#ifdef VOL_ONLY_SOUND
buffer[0] = (uint8)floor((generate_sample(pokey_states) + sampout - MAX_SAMPLE / 2.0)
* (255.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 128 + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
#else
buffer[0] = (uint8)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);
#endif
for(i=1; i<num_cur_pokeys; i++)
{
buffer[i] = (uint8)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 += num_cur_pokeys;
nsam -= num_cur_pokeys;
}
}
static void Pokey_process_16(void* sndbuffer, unsigned sndn)
{
unsigned short i;
int nsam = sndn;
int16 *buffer = (int16 *) sndbuffer;
if(num_cur_pokeys<1)
return; /* module was not initialized */
/* if there are two pokeys, then the signal is stereo
we assume even sndn */
while(nsam >= (int) num_cur_pokeys)
{
#ifdef VOL_ONLY_SOUND
if( sampbuf_rptr!=sampbuf_ptr )
{ int l;
if( sampbuf_cnt[sampbuf_rptr]>0 )
sampbuf_cnt[sampbuf_rptr]-=1280;
while( (l=sampbuf_cnt[sampbuf_rptr])<=0 )
{ sampout=sampbuf_val[sampbuf_rptr];
sampbuf_rptr++;
if( sampbuf_rptr>=SAMPBUF_MAX )
sampbuf_rptr=0;
if( sampbuf_rptr!=sampbuf_ptr )
{
sampbuf_cnt[sampbuf_rptr]+=l;
}
else break;
}
}
#endif
#ifdef VOL_ONLY_SOUND
buffer[0] = (int16)floor((generate_sample(pokey_states) + sampout - MAX_SAMPLE / 2.0)
* (65535.0 / MAX_SAMPLE / 4 * M_PI * 0.95) + 0.5 + 0.5 * rand() / RAND_MAX - 0.25);
#else
buffer[0] = (int16)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);
#endif
for(i=1; i<num_cur_pokeys; i++)
{
buffer[i] = (int16)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 += num_cur_pokeys;
nsam -= num_cur_pokeys;
}
}
#ifdef SERIO_SOUND
static void Update_serio_sound_mz( int out, UBYTE data )
{
#ifdef VOL_ONLY_SOUND
int bits,pv,future;
if (!serio_sound_enabled) return;
pv=0;
future=0;
bits= (data<<1) | 0x200;
while( bits )
{
sampbuf_lastval-=pv;
pv=(bits&0x01)*pokey_states[0].vol3;
sampbuf_lastval+=pv;
sampbuf_val[sampbuf_ptr]=sampbuf_lastval;
sampbuf_cnt[sampbuf_ptr]=
(cpu_clock+future-sampbuf_last)*128*samp_freq/178979;
sampbuf_last=cpu_clock+future;
sampbuf_ptr++;
if( sampbuf_ptr>=SAMPBUF_MAX )
sampbuf_ptr=0;
if( sampbuf_ptr==sampbuf_rptr )
{ sampbuf_rptr++;
if( sampbuf_rptr>=SAMPBUF_MAX )
sampbuf_rptr=0;
}
/* 1789790/19200 = 93 */
future+=93; /* ~ 19200 bit/s - FIXME!!! set speed form AUDF [2] ??? */
bits>>=1;
}
sampbuf_lastval-=pv;
#endif /* VOL_ONLY_SOUND */
}
#endif /* SERIO_SOUND */
#ifdef CONSOLE_SOUND
static void Update_consol_sound_mz( int set )
{
#ifdef VOL_ONLY_SOUND
static int prev_atari_speaker=0;
static unsigned int prev_cpu_clock=0;
int d;
if (!console_sound_enabled) return;
if( !set && samp_consol_val==0 ) return;
sampbuf_lastval-=samp_consol_val;
if( prev_atari_speaker!=atari_speaker )
{ samp_consol_val=atari_speaker*8*4; /* gain */
prev_cpu_clock=cpu_clock;
}
else if( !set )
{ d=cpu_clock - prev_cpu_clock;
if( d<114 )
{ sampbuf_lastval+=samp_consol_val; return; }
while( d>=114 /* CPUL */ )
{ samp_consol_val=samp_consol_val*99/100;
d-=114;
}
prev_cpu_clock=cpu_clock-d;
}
sampbuf_lastval+=samp_consol_val;
prev_atari_speaker=atari_speaker;
sampbuf_val[sampbuf_ptr]=sampbuf_lastval;
sampbuf_cnt[sampbuf_ptr]=
(cpu_clock-sampbuf_last)*128*samp_freq/178979;
sampbuf_last=cpu_clock;
sampbuf_ptr++;
if( sampbuf_ptr>=SAMPBUF_MAX )
sampbuf_ptr=0;
if( sampbuf_ptr==sampbuf_rptr )
{ sampbuf_rptr++;
if( sampbuf_rptr>=SAMPBUF_MAX )
sampbuf_rptr=0;
}
#endif /* VOL_ONLY_SOUND */
}
#endif
#ifdef VOL_ONLY_SOUND
static void Update_vol_only_sound_mz( void )
{
#ifdef CONSOLE_SOUND
Update_consol_sound(0); /* mmm */
#endif /* CONSOLE_SOUND */
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -