ref: d2618d03f5e17c3deed916e56b5e4f282dc093f2
parent: 4ae2015d865a481829b949a171c055c3ec22fa98
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Apr 12 18:25:26 EDT 2014
games/snes: upsample audio to 44100 hz instead of setting audio device frequency used to set audio device frequency thru /dev/volume tho only ac97 driver supports this. as a quick work arround, upsample the 32000 hz audio signal to 44100 hz (without any interpolation). move the sample buffer room check from audiosample() into dspstep() so that when the buffer is full (shouldnt happen), we wont advance dspstate so samples will not get dropped.
--- a/sys/src/games/snes/dsp.c
+++ b/sys/src/games/snes/dsp.c
@@ -42,21 +42,17 @@
enum { RELEASE, ATTACK, DECAY, SUSTAIN };
+enum { Freq = 44100 };
static s16int sbuf[2*2000], *sbufp;
+static int stime;
static int fd;
void
audioinit(void)
{
- int cfd;
- static char c[] = "speed 32000";
-
fd = open("/dev/audio", OWRITE);
if(fd < 0)
- return;
- cfd = open("/dev/volume", OWRITE);
- if(cfd >= 0)
- write(cfd, c, sizeof(c));
+ sysfatal("open: %r");
sbufp = sbuf;
}
@@ -63,10 +59,13 @@
static void
audiosample(s16int *s)
{
- if(sbufp < sbuf + nelem(sbuf)){
- *sbufp++ = s[0];
- *sbufp++ = s[1];
- }
+ stime -= 1<<16;
+ do {
+ sbufp[0] = s[0];
+ sbufp[1] = s[1];
+ sbufp += 2;
+ stime += (32000<<16)/Freq;
+ } while(stime < 0);
}
int
@@ -459,7 +458,7 @@
void
dspstep(void)
{
- if(sbufp == nil)
+ if(sbufp == nil || sbufp >= sbuf+nelem(sbuf)-2)
return;
switch(dspstate++ & 31){
case 0: voice(0, 5); voice(1, 2); break;