sound, fix mcd cdda (mono, resampling), type cleanup, remove minimp3

This commit is contained in:
kub 2022-02-08 20:49:43 +00:00
parent 9f1d5acdb4
commit f7741cac91
14 changed files with 193 additions and 89 deletions

View file

@ -8,6 +8,7 @@
*/
#include <string.h>
#include "../pico_int.h"
#define MAXOUT (+32767)
#define MINOUT (-32768)
@ -15,7 +16,7 @@
/* limitter */
#define Limit16(val) \
val -= val >> 3; /* reduce level to avoid clipping */ \
if ((short)val != val) val = (val < 0 ? MINOUT : MAXOUT)
if ((s16)val != val) val = (val < 0 ? MINOUT : MAXOUT)
int mix_32_to_16l_level;
@ -81,17 +82,17 @@ static inline int filter_null(struct iir *fi2, int x)
lfi2 = lf, rfi2 = rf; \
}
void mix_32_to_16l_stereo_lvl(short *dest, int *src, int count)
void mix_32_to_16l_stereo_lvl(s16 *dest, s32 *src, int count)
{
mix_32_to_16l_stereo_core(dest, src, count, mix_32_to_16l_level, filter);
}
void mix_32_to_16l_stereo(short *dest, int *src, int count)
void mix_32_to_16l_stereo(s16 *dest, s32 *src, int count)
{
mix_32_to_16l_stereo_core(dest, src, count, 0, filter);
}
void mix_32_to_16_mono(short *dest, int *src, int count)
void mix_32_to_16_mono(s16 *dest, s32 *src, int count)
{
int l;
struct iir lf = lfi2;
@ -108,7 +109,7 @@ void mix_32_to_16_mono(short *dest, int *src, int count)
}
void mix_16h_to_32(int *dest_buf, short *mp3_buf, int count)
void mix_16h_to_32(s32 *dest_buf, s16 *mp3_buf, int count)
{
while (count--)
{
@ -116,7 +117,7 @@ void mix_16h_to_32(int *dest_buf, short *mp3_buf, int count)
}
}
void mix_16h_to_32_s1(int *dest_buf, short *mp3_buf, int count)
void mix_16h_to_32_s1(s32 *dest_buf, s16 *mp3_buf, int count)
{
count >>= 1;
while (count--)
@ -127,7 +128,7 @@ void mix_16h_to_32_s1(int *dest_buf, short *mp3_buf, int count)
}
}
void mix_16h_to_32_s2(int *dest_buf, short *mp3_buf, int count)
void mix_16h_to_32_s2(s32 *dest_buf, s16 *mp3_buf, int count)
{
count >>= 1;
while (count--)
@ -138,6 +139,30 @@ void mix_16h_to_32_s2(int *dest_buf, short *mp3_buf, int count)
}
}
// mixes cdda audio @44.1 KHz into dest_buf, resampling with nearest neighbour
void mix_16h_to_32_resample_stereo(s32 *dest_buf, s16 *cdda_buf, int count, int fac16)
{
int pos16 = 0;
while (count--) {
int pos = 2 * (pos16>>16);
*dest_buf++ += cdda_buf[pos ] >> 1;
*dest_buf++ += cdda_buf[pos+1] >> 1;
pos16 += fac16;
}
}
// mixes cdda audio @44.1 KHz into dest_buf, resampling with nearest neighbour
void mix_16h_to_32_resample_mono(s32 *dest_buf, s16 *cdda_buf, int count, int fac16)
{
int pos16 = 0;
while (count--) {
int pos = 2 * (pos16>>16);
*dest_buf += cdda_buf[pos ] >> 2;
*dest_buf++ += cdda_buf[pos+1] >> 2;
pos16 += fac16;
}
}
void mix_reset(int alpha_q16)
{
memset(&lfi2, 0, sizeof(lfi2));

View file

@ -1,11 +1,14 @@
//void mix_32_to_32(int *dest, int *src, int count);
void mix_16h_to_32(int *dest, short *src, int count);
void mix_16h_to_32_s1(int *dest, short *src, int count);
void mix_16h_to_32_s2(int *dest, short *src, int count);
void mix_32_to_16l_stereo(short *dest, int *src, int count);
void mix_32_to_16_mono(short *dest, int *src, int count);
void mix_16h_to_32(s32 *dest, s16 *src, int count);
void mix_16h_to_32_s1(s32 *dest, s16 *src, int count);
void mix_16h_to_32_s2(s32 *dest, s16 *src, int count);
void mix_16h_to_32_resample_stereo(s32 *dest, s16 *src, int count, int fac16);
void mix_16h_to_32_resample_mono(s32 *dest, s16 *src, int count, int fac16);
void mix_32_to_16l_stereo(s16 *dest, s32 *src, int count);
void mix_32_to_16_mono(s16 *dest, s32 *src, int count);
extern int mix_32_to_16l_level;
void mix_32_to_16l_stereo_lvl(short *dest, int *src, int count);
void mix_32_to_16l_stereo_lvl(s16 *dest, s32 *src, int count);
void mix_reset(int alpha_q16);

View file

@ -153,6 +153,92 @@ m16_32_s2_no_unal2:
.global mix_16h_to_32_resample_stereo @ int *dest, short *src, int count, int fac16
mix_16h_to_32_resample_stereo:
stmfd sp!, {r4-r9,lr}
subs r2, r2, #2
mov r4, #0
bmi m16_32_rss_end
m16_32_rss_loop:
ldmia r0, {r5-r8}
lsr r9, r4, #16
ldr r12,[r1, r9, lsl #2]
add r4, r4, r3
lsr r9, r4, #16
ldr lr ,[r1, r9, lsl #2]
add r4, r4, r3
subs r2, r2, #2
add r6, r6, r12,asr #17
mov r12,r12,lsl #16
add r5, r5, r12,asr #17 @ we use half volume
add r8, r8, lr, asr #17
mov lr, lr, lsl #16
add r7, r7, lr, asr #17
stmia r0!,{r5-r8}
bpl m16_32_rss_loop
m16_32_rss_end:
tst r2, #1
ldmeqfd sp!, {r4-r9,pc}
lsr r9, r4, #16
ldr lr ,[r1, r9, lsl #2]
ldmia r0, {r5,r6}
mov r12,lr, lsl #16
add r5, r5, r12,asr #17
add r6, r6, lr, asr #17
stmia r0!,{r5,r6}
ldmfd sp!, {r4-r9,lr}
bx lr
.global mix_16h_to_32_resample_mono @ int *dest, short *src, int count, int fac16
mix_16h_to_32_resample_mono:
stmfd sp!, {r4-r6,r9,lr}
subs r2, r2, #2
mov r4, #0
bmi m16_32_rsm_end
m16_32_rsm_loop:
ldmia r0, {r5-r6}
lsr r9, r4, #16
ldr r12,[r1, r9, lsl #2]
add r4, r4, r3
lsr r9, r4, #16
ldr lr ,[r1, r9, lsl #2]
add r4, r4, r3
subs r2, r2, #2
add r5, r5, r12,asr #18
mov r12,r12,lsl #16
add r5, r5, r12,asr #18 @ we use half volume (= quarter vol per channel)
add r6, r6, lr, asr #18
mov lr, lr, lsl #16
add r6, r6, lr, asr #18
stmia r0!,{r5-r6}
bpl m16_32_rsm_loop
m16_32_rsm_end:
tst r2, #1
ldmeqfd sp!, {r4-r6,r9,pc}
lsr r9, r4, #16
ldr lr ,[r1, r9, lsl #2]
ldr r5, [r0]
mov r12,lr, lsl #16
add r5, r5, r12,asr #18
add r5, r5, lr, asr #18
str r5, [r0]
ldmfd sp!, {r4-r6,r9,lr}
bx lr
@ limit
@ reg=int_sample, r12=1, r8=tmp, kills flags
.macro Limit reg

View file

@ -14,7 +14,7 @@
#include "mix.h"
#include "emu2413/emu2413.h"
void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;
void (*PsndMix_32_to_16l)(s16 *dest, s32 *src, int count) = mix_32_to_16l_stereo;
// master int buffer to mix to
// +1 for a fill triggered by an instruction overhanging into the next scanline
@ -98,6 +98,9 @@ void PsndRerate(int preserve_state)
Pico.snd.smpl_mult = 65536LL * PicoIn.sndRate / (target_fps*target_lines);
// samples per z80 clock (Q20)
Pico.snd.clkl_mult = 16 * Pico.snd.smpl_mult * 15/7 / 488;
// samples per 44.1 KHz sample
Pico.snd.cdda_mult = 65536LL * 44100 / PicoIn.sndRate;
Pico.snd.cdda_div = 65536LL * PicoIn.sndRate / 44100;
// clear all buffers
memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);
@ -154,12 +157,12 @@ PICO_INTERNAL void PsndDoDAC(int cyc_to)
// y[n] = (x[n] + x[n-1])*(1/2) (3dB cutoff at 11025 Hz, no gain)
// 1 sample delay for correct IIR filtering over audio frame boundaries
if (PicoIn.opt & POPT_EN_STEREO) {
short *d = PicoIn.sndOut + pos*2;
s16 *d = PicoIn.sndOut + pos*2;
// left channel only, mixed ro right channel in mixing phase
*d++ += Pico.snd.dac_val2; d++;
while (--len) *d++ += Pico.snd.dac_val, d++;
} else {
short *d = PicoIn.sndOut + pos;
s16 *d = PicoIn.sndOut + pos;
*d++ += Pico.snd.dac_val2;
while (--len) *d++ += Pico.snd.dac_val;
}
@ -200,7 +203,7 @@ PICO_INTERNAL void PsndDoYM2413(int cyc_to)
{
int pos, len;
int stereo = 0;
short *buf;
s16 *buf;
// nothing to do if sound is off
if (!PicoIn.sndOut) return;
@ -268,14 +271,11 @@ PICO_INTERNAL void PsndDoFM(int cyc_to)
}
// cdda
static void cdda_raw_update(int *buffer, int length)
static void cdda_raw_update(s32 *buffer, int length, int stereo)
{
int ret, cdda_bytes, mult = 1;
int ret, cdda_bytes;
cdda_bytes = length*4;
if (PicoIn.sndRate <= 22050 + 100) mult = 2;
if (PicoIn.sndRate < 22050 - 100) mult = 4;
cdda_bytes *= mult;
cdda_bytes = (length * Pico.snd.cdda_mult >> 16) * 4;
ret = pm_read_audio(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream);
if (ret < cdda_bytes) {
@ -285,11 +285,13 @@ static void cdda_raw_update(int *buffer, int length)
}
// now mix
switch (mult) {
case 1: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;
case 2: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;
case 4: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;
}
if (stereo) switch (Pico.snd.cdda_mult) {
case 0x10000: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;
case 0x20000: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;
case 0x40000: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;
default: mix_16h_to_32_resample_stereo(buffer, cdda_out_buffer, length, Pico.snd.cdda_mult);
} else
mix_16h_to_32_resample_mono(buffer, cdda_out_buffer, length, Pico.snd.cdda_mult);
}
void cdda_start_play(int lba_base, int lba_offset, int lb_len)
@ -326,7 +328,7 @@ PICO_INTERNAL void PsndClear(void)
if (PicoIn.opt & POPT_EN_STEREO)
memset32((int *) PicoIn.sndOut, 0, len); // assume PicoIn.sndOut to be aligned
else {
short *out = PicoIn.sndOut;
s16 *out = PicoIn.sndOut;
if ((uintptr_t)out & 2) { *out++ = 0; len--; }
memset32((int *) out, 0, len/2);
if (len & 1) out[len-1] = 0;
@ -350,14 +352,14 @@ static int PsndRender(int offset, int length)
if (PicoIn.AHW & PAHW_PICO) {
// XXX ugly hack, need to render sound for interrupts
s16 *buf16 = PicoIn.sndOut ? PicoIn.sndOut : (short *)PsndBuffer;
s16 *buf16 = PicoIn.sndOut ? PicoIn.sndOut : (s16 *)PsndBuffer;
PicoPicoPCMUpdate(buf16+(offset<<stereo), length-offset, stereo);
return length;
}
// Fill up DAC output in case of missing samples (Q rounding errors)
if (length-daclen > 0 && PicoIn.sndOut) {
short *dacbuf = PicoIn.sndOut + (daclen << stereo);
s16 *dacbuf = PicoIn.sndOut + (daclen << stereo);
Pico.snd.dac_pos += (length-daclen) << 20;
*dacbuf++ += Pico.snd.dac_val2;
if (stereo) dacbuf++;
@ -370,7 +372,7 @@ static int PsndRender(int offset, int length)
// Add in parts of the PSG output not yet done
if (length-psglen > 0 && PicoIn.sndOut) {
short *psgbuf = PicoIn.sndOut + (psglen << stereo);
s16 *psgbuf = PicoIn.sndOut + (psglen << stereo);
Pico.snd.psg_pos += (length-psglen) << 20;
if (PicoIn.opt & POPT_EN_PSG)
SN76496Update(psgbuf, length-psglen, stereo);
@ -378,7 +380,7 @@ static int PsndRender(int offset, int length)
// Add in parts of the FM buffer not yet done
if (length-fmlen > 0 && PicoIn.sndOut) {
int *fmbuf = buf32 + ((fmlen-offset) << stereo);
s32 *fmbuf = buf32 + ((fmlen-offset) << stereo);
Pico.snd.fm_pos += (length-fmlen) << 20;
if (PicoIn.opt & POPT_EN_FM)
YM2612UpdateOne(fmbuf, length-fmlen, stereo, 1);
@ -395,11 +397,10 @@ static int PsndRender(int offset, int length)
&& Pico_mcd->cdda_stream != NULL
&& !(Pico_mcd->s68k_regs[0x36] & 1))
{
// note: only 44, 22 and 11 kHz supported, with forced stereo
if (Pico_mcd->cdda_type == CT_MP3)
mp3_update(buf32, length-offset, stereo);
else
cdda_raw_update(buf32, length-offset);
cdda_raw_update(buf32, length-offset, stereo);
}
if ((PicoIn.AHW & PAHW_32X) && (PicoIn.opt & POPT_EN_PWM))
@ -439,14 +440,14 @@ static int PsndRenderMS(int offset, int length)
// Add in parts of the PSG output not yet done
if (length-psglen > 0) {
short *psgbuf = PicoIn.sndOut + (psglen << stereo);
s16 *psgbuf = PicoIn.sndOut + (psglen << stereo);
Pico.snd.psg_pos += (length-psglen) << 20;
if (PicoIn.opt & POPT_EN_PSG)
SN76496Update(psgbuf, length-psglen, stereo);
}
if (length-ym2413len > 0) {
short *ym2413buf = PicoIn.sndOut + (ym2413len << stereo);
s16 *ym2413buf = PicoIn.sndOut + (ym2413len << stereo);
Pico.snd.ym2413_pos += (length-ym2413len) << 20;
int len = (length-ym2413len);
if (PicoIn.opt & POPT_EN_YM2413){
@ -461,8 +462,8 @@ static int PsndRenderMS(int offset, int length)
// upmix to "stereo" if needed
if (PicoIn.opt & POPT_EN_STEREO) {
int i;
short *p;
for (i = length, p = (short *)PicoIn.sndOut; i > 0; i--, p+=2)
s16 *p;
for (i = length, p = (s16 *)PicoIn.sndOut; i > 0; i--, p+=2)
*(p + 1) = *p;
}

View file

@ -1127,7 +1127,7 @@ static int update_algo_channel(chan_rend_context *ct, unsigned int eg_out, unsig
return smp;
}
static void chan_render_loop(chan_rend_context *ct, int *buffer, int length)
static void chan_render_loop(chan_rend_context *ct, s32 *buffer, int length)
{
int scounter; /* sample counter */
@ -1244,7 +1244,7 @@ disabled:
}
}
#else
void chan_render_loop(chan_rend_context *ct, int *buffer, unsigned short length);
void chan_render_loop(chan_rend_context *ct, s32 *buffer, unsigned short length);
#endif
static chan_rend_context crct;
@ -1255,7 +1255,7 @@ static void chan_render_prep(void)
crct.lfo_inc = ym2612.OPN.lfo_inc;
}
static void chan_render_finish(int *buffer, unsigned short length, int active_chans)
static void chan_render_finish(s32 *buffer, unsigned short length, int active_chans)
{
ym2612.OPN.eg_cnt = crct.eg_cnt;
ym2612.OPN.eg_timer = crct.eg_timer;
@ -1290,7 +1290,7 @@ static UINT32 update_lfo_phase(FM_SLOT *SLOT, UINT32 block_fnum)
return SLOT->Incr;
}
static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: stereo, ?, disabled, ?, pan_r, pan_l
static int chan_render(s32 *buffer, int length, int c, UINT32 flags) // flags: stereo, ?, disabled, ?, pan_r, pan_l
{
crct.CH = &ym2612.CH[c];
crct.mem = crct.CH->mem_value; /* one sample delay memory */
@ -1781,7 +1781,7 @@ static int OPNWriteReg(int r, int v)
/*******************************************************************************/
/* Generate samples for YM2612 */
int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty)
int YM2612UpdateOne_(s32 *buffer, int length, int stereo, int is_buf_empty)
{
int pan;
int active_chs = 0;

View file

@ -167,7 +167,7 @@ extern YM2612 ym2612;
void YM2612Init_(int baseclock, int rate, int flags);
void YM2612ResetChip_(void);
int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty);
int YM2612UpdateOne_(s32 *buffer, int length, int stereo, int is_buf_empty);
int YM2612Write_(unsigned int a, unsigned int v);
//unsigned char YM2612Read_(void);