make OSS detect blocking, adjust audio API

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@898 be3aeb3a-fb24-0410-a615-afba39da0efa
This commit is contained in:
notaz 2010-09-18 16:40:13 +00:00
parent d40231e29a
commit a4edca53b4
11 changed files with 92 additions and 99 deletions

View file

@ -90,7 +90,7 @@ void PicoLoopPrepare(void);
void PicoFrame(void); void PicoFrame(void);
void PicoFrameDrawOnly(void); void PicoFrameDrawOnly(void);
extern int PicoPad[2]; // Joypads, format is MXYZ SACB RLDU extern int PicoPad[2]; // Joypads, format is MXYZ SACB RLDU
extern void (*PicoWriteSound)(int len); // called once per frame at the best time to send sound buffer (PsndOut) to hardware extern void (*PicoWriteSound)(int bytes); // called once per frame at the best time to send sound buffer (PsndOut) to hardware
extern void (*PicoMessage)(const char *msg); // callback to output text message from emu extern void (*PicoMessage)(const char *msg); // callback to output text message from emu
typedef enum { PI_ROM, PI_ISPAL, PI_IS40_CELL, PI_IS240_LINES } pint_t; typedef enum { PI_ROM, PI_ISPAL, PI_IS40_CELL, PI_IS240_LINES } pint_t;
typedef union { int vint; void *vptr; } pint_ret_t; typedef union { int vint; void *vptr; } pint_ret_t;

View file

@ -375,7 +375,8 @@ PICO_INTERNAL void PsndGetSamples(int y)
#if SIMPLE_WRITE_SOUND #if SIMPLE_WRITE_SOUND
if (y != 224) return; if (y != 224) return;
PsndRender(0, PsndLen); PsndRender(0, PsndLen);
if (PicoWriteSound) PicoWriteSound(PsndLen); if (PicoWriteSound)
PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));
PsndClear(); PsndClear();
#else #else
static int curr_pos = 0; static int curr_pos = 0;
@ -385,8 +386,11 @@ PICO_INTERNAL void PsndGetSamples(int y)
if (emustatus & 2) if (emustatus & 2)
curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2); curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);
else curr_pos = PsndRender(0, PsndLen); else curr_pos = PsndRender(0, PsndLen);
if (emustatus&1) emustatus|=2; else emustatus&=~2; if (emustatus & 1)
if (PicoWriteSound) PicoWriteSound(curr_pos); emustatus |= 2;
else emustatus &= ~2;
if (PicoWriteSound)
PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));
// clear sound buffer // clear sound buffer
PsndClear(); PsndClear();
} }
@ -424,7 +428,7 @@ PICO_INTERNAL void PsndGetSamplesMS(void)
} }
if (PicoWriteSound != NULL) if (PicoWriteSound != NULL)
PicoWriteSound(length); PicoWriteSound(length * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));
PsndClear(); PsndClear();
} }

View file

@ -1350,6 +1350,26 @@ static void mkdir_path(char *path_with_reserve, int pos, const char *name)
lprintf("failed to create: %s\n", path_with_reserve); lprintf("failed to create: %s\n", path_with_reserve);
} }
void emu_cmn_forced_frame(int no_scale, int do_emu)
{
int po_old = PicoOpt;
memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);
PicoOpt |= POPT_ACC_SPRITES;
if (!no_scale)
PicoOpt |= POPT_EN_SOFTSCALE;
PicoDrawSetOutFormat(PDF_RGB555, 1);
Pico.m.dirtyPal = 1;
if (do_emu)
PicoFrame();
else
PicoFrameDrawOnly();
PicoOpt = po_old;
}
void emu_init(void) void emu_init(void)
{ {
char path[512]; char path[512];

View file

@ -146,6 +146,9 @@ void emu_get_game_name(char *str150);
void emu_set_fastforward(int set_on); void emu_set_fastforward(int set_on);
void emu_status_msg(const char *format, ...); void emu_status_msg(const char *format, ...);
/* used by some (but not all) platforms */
void emu_cmn_forced_frame(int no_scale, int do_emu);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View file

@ -273,10 +273,8 @@ static void stdbg(const char *fmt, ...)
static void updateSound(int len) static void updateSound(int len)
{ {
if (PicoOpt&8) len<<=1; snd_all_samples += len / 2;
PsndOut += len / 2;
snd_all_samples += len;
PsndOut += len;
if (PsndOut - snd_cbuff >= snd_cbuf_samples) if (PsndOut - snd_cbuff >= snd_cbuf_samples)
{ {
//if (PsndOut - snd_cbuff != snd_cbuf_samples) //if (PsndOut - snd_cbuff != snd_cbuf_samples)

View file

@ -710,18 +710,13 @@ void plat_update_volume(int has_changed, int is_up)
} }
} }
static void updateSound(int len) static void oss_write_nonblocking(int len)
{ {
len <<= 1; // sndout_oss_can_write() is not reliable, only use with no_frmlimit
if (PicoOpt & POPT_EN_STEREO)
len <<= 1;
if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len)) if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))
return; return;
/* avoid writing audio when lagging behind to prevent audio lag */ sndout_oss_write_nb(PsndOut, len);
if (PicoSkipFrame != 2)
sndout_oss_write(PsndOut, len);
} }
void pemu_sound_start(void) void pemu_sound_start(void)
@ -739,7 +734,7 @@ void pemu_sound_start(void)
memset(sndBuffer, 0, sizeof(sndBuffer)); memset(sndBuffer, 0, sizeof(sndBuffer));
PsndOut = sndBuffer; PsndOut = sndBuffer;
PicoWriteSound = updateSound; PicoWriteSound = oss_write_nonblocking;
plat_update_volume(0, 0); plat_update_volume(0, 0);
printf("starting audio: %i len: %i stereo: %i, pal: %i\n", printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
@ -786,30 +781,16 @@ void pemu_sound_wait(void)
void pemu_forced_frame(int no_scale, int do_emu) void pemu_forced_frame(int no_scale, int do_emu)
{ {
int po_old = PicoOpt; doing_bg_frame = 1;
PicoOpt &= ~POPT_ALT_RENDERER;
PicoOpt |= POPT_ACC_SPRITES;
if (!no_scale)
PicoOpt |= POPT_EN_SOFTSCALE;
memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);
PicoDrawSetOutFormat(PDF_RGB555, 1);
PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2); PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
PicoDraw32xSetFrameMode(0, 0); PicoDraw32xSetFrameMode(0, 0);
PicoDrawSetCallbacks(NULL, NULL); PicoDrawSetCallbacks(NULL, NULL);
Pico.m.dirtyPal = 1; Pico.m.dirtyPal = 1;
doing_bg_frame = 1; emu_cmn_forced_frame(no_scale, do_emu);
if (do_emu)
PicoFrame();
else
PicoFrameDrawOnly();
doing_bg_frame = 0;
g_menubg_src_ptr = g_screen_ptr; g_menubg_src_ptr = g_screen_ptr;
PicoOpt = po_old; doing_bg_frame = 0;
} }
void plat_debug_cat(char *str) void plat_debug_cat(char *str)

View file

@ -210,47 +210,27 @@ void plat_update_volume(int has_changed, int is_up)
void pemu_forced_frame(int no_scale, int do_emu) void pemu_forced_frame(int no_scale, int do_emu)
{ {
int po_old = PicoOpt;
memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);
PicoOpt &= ~POPT_ALT_RENDERER;
PicoOpt |= POPT_ACC_SPRITES;
if (!no_scale)
PicoOpt |= POPT_EN_SOFTSCALE;
PicoDrawSetOutFormat(PDF_RGB555, 1);
PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2); PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
PicoDraw32xSetFrameMode(0, 0); PicoDraw32xSetFrameMode(0, 0);
PicoDrawSetCallbacks(NULL, NULL);
Pico.m.dirtyPal = 1; Pico.m.dirtyPal = 1;
if (do_emu)
PicoFrame(); emu_cmn_forced_frame(no_scale, do_emu);
else
PicoFrameDrawOnly();
g_menubg_src_ptr = g_screen_ptr; g_menubg_src_ptr = g_screen_ptr;
PicoOpt = po_old;
} }
static void updateSound(int len) static void oss_write_nonblocking(int len)
{ {
len <<= 1; // sndout_oss_can_write() is not reliable, only use with no_frmlimit
if (PicoOpt & POPT_EN_STEREO)
len <<= 1;
if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len)) if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))
return; return;
/* avoid writing audio when lagging behind to prevent audio lag */ sndout_oss_write_nb(PsndOut, len);
if (PicoSkipFrame != 2)
sndout_oss_write(PsndOut, len);
} }
void pemu_sound_start(void) void pemu_sound_start(void)
{ {
int target_fps = Pico.m.pal ? 50 : 60;
PsndOut = NULL; PsndOut = NULL;
if (currentConfig.EmuOpt & EOPT_EN_SOUND) if (currentConfig.EmuOpt & EOPT_EN_SOUND)
@ -263,7 +243,7 @@ void pemu_sound_start(void)
PsndRate, PsndLen, is_stereo, Pico.m.pal); PsndRate, PsndLen, is_stereo, Pico.m.pal);
sndout_oss_start(PsndRate, is_stereo, 1); sndout_oss_start(PsndRate, is_stereo, 1);
sndout_oss_setvol(currentConfig.volume, currentConfig.volume); sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
PicoWriteSound = updateSound; PicoWriteSound = oss_write_nonblocking;
plat_update_volume(0, 0); plat_update_volume(0, 0);
memset(sndBuffer, 0, sizeof(sndBuffer)); memset(sndBuffer, 0, sizeof(sndBuffer));
PsndOut = sndBuffer; PsndOut = sndBuffer;

View file

@ -98,6 +98,42 @@ int sndout_oss_write(const void *buff, int len)
return write(sounddev, buff, len); return write(sounddev, buff, len);
} }
#include "../common/plat.h"
/* not really non-blocking, just detects if blocking occurs
* and starts skipping writes in case it does. */
int sndout_oss_write_nb(const void *buff, int len)
{
static int lag_counter, skip_counter;
unsigned int t;
int ret;
if (lag_counter > 2) {
// skip writes if audio starts blocking
lag_counter = 0;
skip_counter = FRAG_COUNT;
}
if (skip_counter > 0) {
skip_counter--;
return len;
}
t = plat_get_ticks_ms();
ret = sndout_oss_write(buff, len);
t = plat_get_ticks_ms() - t;
if (t > 1) {
// this shouldn't really happen, most likely audio is out of sync
lag_counter++;
if (lag_counter > 2)
printf("audio lag %u\n", t);
}
else
lag_counter = 0;
return ret;
}
int sndout_oss_can_write(int bytes) int sndout_oss_can_write(int bytes)
{ {
audio_buf_info bi; audio_buf_info bi;

View file

@ -2,6 +2,7 @@ int sndout_oss_init(void);
int sndout_oss_start(int rate, int stereo, int frames_in_frag); int sndout_oss_start(int rate, int stereo, int frames_in_frag);
void sndout_oss_stop(void); void sndout_oss_stop(void);
int sndout_oss_write(const void *buff, int len); int sndout_oss_write(const void *buff, int len);
int sndout_oss_write_nb(const void *buff, int len);
int sndout_oss_can_write(int bytes); int sndout_oss_can_write(int bytes);
void sndout_oss_sync(void); void sndout_oss_sync(void);
void sndout_oss_setvol(int l, int r); void sndout_oss_setvol(int l, int r);

View file

@ -240,51 +240,22 @@ static void make_bg(int no_scale)
void pemu_forced_frame(int no_scale, int do_emu) void pemu_forced_frame(int no_scale, int do_emu)
{ {
int po_old = PicoOpt;
memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);
PicoOpt |= POPT_ACC_SPRITES;
if (!no_scale)
PicoOpt |= POPT_EN_SOFTSCALE;
PicoDrawSetOutFormat(PDF_RGB555, 1);
Pico.m.dirtyPal = 1;
doing_bg_frame = 1; doing_bg_frame = 1;
if (do_emu) emu_cmn_forced_frame(no_scale, do_emu);
PicoFrame();
else
PicoFrameDrawOnly();
doing_bg_frame = 0; doing_bg_frame = 0;
// making a copy because enabling the layer clears it's mem // making a copy because enabling the layer clears it's mem
memcpy32((void *)fb_copy, g_screen_ptr, sizeof(fb_copy) / 4); memcpy32((void *)fb_copy, g_screen_ptr, sizeof(fb_copy) / 4);
make_bg(no_scale); make_bg(no_scale);
PicoOpt = po_old;
} }
static void updateSound(int len) static void oss_write_nonblocking(int len)
{ {
unsigned int t; // sndout_oss_can_write() is not reliable, only use with no_frmlimit
len <<= 1;
if (PicoOpt & POPT_EN_STEREO)
len <<= 1;
// sndout_oss_can_write() not reliable..
if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len)) if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))
return; return;
/* avoid writing audio when lagging behind to prevent audio lag */ sndout_oss_write_nb(PsndOut, len);
if (PicoSkipFrame == 2)
return;
t = plat_get_ticks_ms();
sndout_oss_write(PsndOut, len);
t = plat_get_ticks_ms() - t;
if (t > 1)
printf("audio lag %u\n", t);
} }
void pemu_sound_start(void) void pemu_sound_start(void)
@ -308,7 +279,7 @@ void pemu_sound_start(void)
PsndRate, PsndLen, is_stereo, Pico.m.pal); PsndRate, PsndLen, is_stereo, Pico.m.pal);
sndout_oss_start(PsndRate, is_stereo, 2); sndout_oss_start(PsndRate, is_stereo, 2);
//sndout_oss_setvol(currentConfig.volume, currentConfig.volume); //sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
PicoWriteSound = updateSound; PicoWriteSound = oss_write_nonblocking;
plat_update_volume(0, 0); plat_update_volume(0, 0);
memset(sndBuffer, 0, sizeof(sndBuffer)); memset(sndBuffer, 0, sizeof(sndBuffer));
PsndOut = sndBuffer; PsndOut = sndBuffer;

View file

@ -637,9 +637,8 @@ static void sound_deinit(void)
static void writeSound(int len) static void writeSound(int len)
{ {
int ret; int ret;
if (PicoOpt&8) len<<=1;
PsndOut += len; PsndOut += len / 2;
/*if (PsndOut > sndBuffer_endptr) { /*if (PsndOut > sndBuffer_endptr) {
memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2); memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2);
PsndOut = &sndBuffer[PsndOut - endptr]; PsndOut = &sndBuffer[PsndOut - endptr];
@ -651,7 +650,7 @@ static void writeSound(int len)
PsndOut = sndBuffer; PsndOut = sndBuffer;
// signal the snd thread // signal the snd thread
samples_made += len; samples_made += len / 2;
if (samples_made - samples_done > samples_block*2) { if (samples_made - samples_done > samples_block*2) {
// lprintf("signal, %i/%i\n", samples_done, samples_made); // lprintf("signal, %i/%i\n", samples_done, samples_made);
ret = sceKernelSignalSema(sound_sem, 1); ret = sceKernelSignalSema(sound_sem, 1);