\r
static void updateSound(int len)\r
{\r
+ len <<= 1;\r
if (PicoOpt & POPT_EN_STEREO)\r
len <<= 1;\r
\r
+ if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))\r
+ return;\r
+\r
/* avoid writing audio when lagging behind to prevent audio lag */\r
if (PicoSkipFrame != 2)\r
- sndout_oss_write(PsndOut, len<<1);\r
+ sndout_oss_write(PsndOut, len);\r
}\r
\r
void pemu_sound_start(void)\r
{\r
int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
int target_fps = Pico.m.pal ? 50 : 60;\r
- int snd_excess_add;\r
+ int frame_samples, snd_excess_add;\r
gp2x_soc_t soc;\r
\r
+ soc = soc_detect();\r
+\r
#define SOUND_RERATE_FLAGS (POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO|POPT_EXT_FM|POPT_EN_MCD_CDDA)\r
if (PsndRate != PsndRate_old || Pico.m.pal != pal_old || ((PicoOpt & POPT_EXT_FM) && crashed_940) ||\r
((PicoOpt ^ PicoOpt_old) & SOUND_RERATE_FLAGS)) {\r
PsndRerate(Pico.m.frame_count ? 1 : 0);\r
}\r
- snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;\r
- printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
- PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);\r
- sndout_oss_start(PsndRate, 16, is_stereo);\r
- sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
- PicoWriteSound = updateSound;\r
- plat_update_volume(0, 0);\r
+\r
memset(sndBuffer, 0, sizeof(sndBuffer));\r
PsndOut = sndBuffer;\r
+ PicoWriteSound = updateSound;\r
PsndRate_old = PsndRate;\r
PicoOpt_old = PicoOpt;\r
pal_old = Pico.m.pal;\r
+ plat_update_volume(0, 0);\r
+\r
+ frame_samples = PsndLen;\r
+ snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;\r
+ if (snd_excess_add != 0)\r
+ frame_samples++;\r
+ if (soc == SOCID_POLLUX)\r
+ frame_samples *= 2; /* force larger buffer */\r
+\r
+ printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
+ PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);\r
+ sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
+ sndout_oss_start(PsndRate, frame_samples, is_stereo);\r
\r
/* Wiz's sound hardware needs more prebuffer */\r
- soc = soc_detect();\r
if (soc == SOCID_POLLUX)\r
- updateSound(PsndLen);\r
+ updateSound(frame_samples);\r
}\r
}\r
\r
#include "sndout_oss.h"
static int sounddev = -1, mixerdev = -1;
+static int can_write_safe;
+
int sndout_oss_init(void)
{
return 0;
}
-
-int sndout_oss_start(int rate, int bits, int stereo)
+int sndout_oss_start(int rate, int frame_samples, int stereo)
{
- static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
- int frag = 0, bsize, buffers, ret;
+ static int s_oldrate = 0, s_old_fsamples = 0, s_oldstereo = 0;
+ int frag, bsize, bits, ret;
// if no settings change, we don't need to do anything,
// since audio is never stopped
- if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo)
+ if (rate == s_oldrate && s_old_fsamples == frame_samples && s_oldstereo == stereo)
return 0;
- if (sounddev >= 0) close(sounddev);
+ if (sounddev >= 0) {
+ ioctl(sounddev, SOUND_PCM_SYNC, 0);
+ close(sounddev);
+ }
+
sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
if (sounddev == -1)
{
return -1;
}
- // calculate buffer size
- // this is tuned for GP2X
- buffers = 16;
- bsize = rate / 32;
- if (rate > 22050) { bsize*=4; buffers*=2; }
- while ((bsize>>=1)) frag++;
- frag |= buffers<<16; // 16 buffers
- ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
- if (ret) perror("SNDCTL_DSP_SETFRAGMENT failed");
+ // calculate buffer size. We one to fit 1 frame worth of sound data.
+ // Also ignore mono because both GP2X and Wiz mixes mono to stereo anyway.
+ bsize = frame_samples << 2;
+
+ for (frag = 0; bsize; bsize >>= 1, frag++)
+ ;
- ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
- ret |= ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
- ret |= ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
- if (ret) printf("failed to set audio format\n");
+ frag |= 16 << 16; // fragment count
+ ret = ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
+ if (ret < 0)
+ perror("SNDCTL_DSP_SETFRAGMENT failed");
+
+ bits = 16;
+ ret = ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
+ if (ret == 0)
+ ret = ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
+ if (ret == 0)
+ ret = ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
+ if (ret < 0)
+ perror("failed to set audio format");
+
+ // not sure if this is still needed (avoiding driver bugs?)
usleep(192*1024);
- printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
- rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
+ printf("sndout_oss_start: %d/%dbit/%s, %d buffers of %i bytes\n",
+ rate, bits, stereo ? "stereo" : "mono", frag >> 16, 1 << (frag & 0xffff));
- s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
+ s_oldrate = rate; s_old_fsamples = frame_samples; s_oldstereo = stereo;
+ can_write_safe = 0;
return 0;
}
-
int sndout_oss_write(const void *buff, int len)
{
return write(sounddev, buff, len);
}
+int sndout_oss_can_write(int bytes)
+{
+ audio_buf_info bi;
+ int ret;
+
+#ifdef __GP2X__
+ // note: SNDCTL_DSP_GETOSPACE crashes F100 kernel for some reason
+ // if called too early, so we work around here
+ if (can_write_safe++ < 8)
+ return 1;
+#endif
+ ret = ioctl(sounddev, SNDCTL_DSP_GETOSPACE, &bi);
+ if (ret < 0)
+ return 1;
+
+ // have enough bytes to write + 4 extra frags
+ return bi.bytes - bi.fragsize * 4 >= bytes ? 1 : 0;
+}
void sndout_oss_sync(void)
{
ioctl(sounddev, SOUND_PCM_SYNC, 0);
}
-
void sndout_oss_setvol(int l, int r)
{
if (mixerdev < 0) return;
ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
}
-
void sndout_oss_exit(void)
{
if (sounddev >= 0) close(sounddev); sounddev = -1;