ref: dbfc9b6d9bc6aa9f337eadc21d0d6dfbdbc3e3d7
parent: ca3289c29673b914d8ca64b08954ef0afccdf3c9
author: rodri <[email protected]>
date: Tue Oct 10 10:56:17 EDT 2023
get rid of duplicate AudioSource init procedures. also changed the name of some of the mixer functions.
--- a/bts.c
+++ b/bts.c
@@ -244,9 +244,9 @@
game.state = Waiting0;
conclusion.s = nil;
csetcursor(mctl, nil);
- audio_stop(conclusion.snd);
+ stopaudio(conclusion.snd);
conclusion.snd = nil;
- audio_play(playlist[SBG0]);
+ playaudio(playlist[SBG0]);
}
Point
@@ -547,7 +547,7 @@
}
void
-initsound(void)
+initsfx(void)
{
struct {
char *path;
@@ -564,18 +564,18 @@
};
int i;
- audio_init(44100);
+ initaudio(44100);
audio_set_master_gain(0.5);
for(i = 0; i < NSOUNDS; i++){
- playlist[i] = audio_new_source_from_file(sndtab[i].path);
+ playlist[i] = loadaudiosource(sndtab[i].path);
if(playlist[i] == nil)
- sysfatal("audio_new_source_from_file: %r");
+ sysfatal("loadaudiosource: %r");
audio_set_gain(playlist[i], sndtab[i].gain);
audio_set_loop(playlist[i], sndtab[i].loops);
}
- audio_play(playlist[SBG0]);
+ playaudio(playlist[SBG0]);
}
int
@@ -639,7 +639,7 @@
if(!ptinrect(mc->xy, alienboard.bbox))
break;
- audio_play(playlist[SCANNON]);
+ playaudio(playlist[SCANNON]);
cell = toboard(&alienboard, mc->xy);
cell2coords(buf, sizeof buf, cell);
if(gettile(&alienboard, cell) == Twater){
@@ -823,8 +823,8 @@
conclusion.s = s;
conclusion.snd = playlist[SVICTORY];
- audio_stop(playlist[SBG2]);
- audio_play(conclusion.snd);
+ stopaudio(playlist[SBG2]);
+ playaudio(conclusion.snd);
}
void
@@ -836,8 +836,8 @@
conclusion.s = s;
conclusion.snd = playlist[SDEFEAT];
- audio_stop(playlist[SBG2]);
- audio_play(conclusion.snd);
+ stopaudio(playlist[SBG2]);
+ playaudio(conclusion.snd);
}
void
@@ -853,8 +853,8 @@
conclusion.s = s;
conclusion.snd = playlist[SVICTORY];
- audio_stop(playlist[SBG2]);
- audio_play(conclusion.snd);
+ stopaudio(playlist[SBG2]);
+ playaudio(conclusion.snd);
}
void
@@ -904,8 +904,8 @@
match.bl[0] = &localboard;
match.bl[1] = &alienboard;
game.state = Watching;
- audio_stop(playlist[SBG0]);
- audio_play(playlist[SBG2]);
+ stopaudio(playlist[SBG0]);
+ playaudio(playlist[SBG2]);
}
break;
case Ready:
@@ -912,8 +912,8 @@
if(ct->index == CMlayout){
game.state = Outlaying;
curship = &armada[0];
- audio_stop(playlist[SBG0]);
- audio_play(playlist[SBG2]);
+ stopaudio(playlist[SBG0]);
+ playaudio(playlist[SBG2]);
}else if(ct->index == CMoid)
snprint(oid, sizeof oid, "%s", cb->f[1]);
break;
@@ -955,7 +955,7 @@
}else if(ct->index == CMwehit)
settile(&alienboard, lastshot, Thit);
else if(ct->index == CMwemiss){
- audio_play(playlist[SWATER]);
+ playaudio(playlist[SWATER]);
settile(&alienboard, lastshot, Tmiss);
}
break;
@@ -994,7 +994,7 @@
sysfatal("Bopen: %r");
for(;;){
- audio_process((void*)buf, sizeof(buf)/2);
+ processaudio((void*)buf, sizeof(buf)/2);
Bwrite(aout, buf, sizeof buf);
}
}
@@ -1108,7 +1108,7 @@
matches = newmenulist(14*font->height, "ongoing matches");
game.state = Waiting0;
- initsound();
+ initsfx();
proccreate(soundproc, nil, mainstacksize);
addr = netmkaddr(argv[0], "tcp", "3047");
--- a/mixer.c
+++ b/mixer.c
@@ -9,9 +9,6 @@
static Mixer mixer;
-static int wav_init(AudioSourceInfo*, int);
-static int mp3_init(AudioSourceInfo*, int);
-
static int
min(int a, int b)
{
@@ -72,8 +69,138 @@
//
//}
+static void
+pcm_handler(AudioEvent *e)
+{
+ Pcm *pcm;
+ s16int *dst;
+ int len, i, n;
+
+ pcm = e->udata;
+
+ switch(e->type){
+ case AUDIO_EVENT_DESTROY:
+ free(pcm->data);
+ free(pcm);
+ break;
+ case AUDIO_EVENT_SAMPLES:
+ dst = e->buffer;
+ len = e->length/2;
+Fillbuf:
+ n = min(len, pcm->len - pcm->off);
+ len -= n;
+ while(n--){
+ i = 2*pcm->off;
+ dst[0] = ((s16int*)pcm->data)[i];
+ dst[1] = ((s16int*)pcm->data)[i+1];
+ dst += 2;
+ pcm->off++;
+ }
+ if(len > 0){
+ pcm->off = 0;
+ goto Fillbuf;
+ }
+ break;
+ case AUDIO_EVENT_REWIND:
+ pcm->off = 0;
+ break;
+ }
+}
+
+/* TODO generalize the *decproc procedures */
+static void
+wavdecproc(void *arg)
+{
+ int *pfd, fd;
+
+ pfd = arg;
+ fd = pfd[2];
+
+ close(pfd[0]);
+ dup(fd, 0);
+ close(fd);
+ dup(pfd[1], 1);
+ close(pfd[1]);
+
+ execl("/bin/audio/wavdec", "wavdec", nil);
+ threadexitsall("execl: %r");
+}
+
+static void
+mp3decproc(void *arg)
+{
+ int *pfd, fd;
+
+ pfd = arg;
+ fd = pfd[2];
+
+ close(pfd[0]);
+ dup(fd, 0);
+ close(fd);
+ dup(pfd[1], 1);
+ close(pfd[1]);
+
+ execl("/bin/audio/mp3dec", "mp3dec", nil);
+ threadexitsall("execl: %r");
+}
+
+static int
+loadaudio(AudioSourceInfo *info, int fd, void (*decfn)(void*))
+{
+ Pcm *pcm;
+ void *data;
+ uchar buf[1024];
+ int pfd[3], n, len;
+
+ data = nil;
+ len = 0;
+
+ if(pipe(pfd) < 0){
+ werrstr("pipe: %r");
+ return -1;
+ }
+ pfd[2] = fd;
+
+ procrfork(decfn, pfd, mainstacksize, RFFDG|RFNAMEG|RFNOTEG);
+ close(pfd[1]);
+ while((n = read(pfd[0], buf, sizeof buf)) > 0){
+ data = realloc(data, len+n);
+ if(data == nil){
+ werrstr("realloc: %r");
+ return -1;
+ }
+ memmove((uchar*)data+len, buf, n);
+ len += n;
+ }
+ close(pfd[0]);
+
+ pcm = malloc(sizeof *pcm);
+ if(pcm == nil){
+ free(data);
+ werrstr("malloc: %r");
+ return -1;
+ }
+ pcm->depth = 16;
+ pcm->chans = 2;
+ pcm->rate = 44100;
+ pcm->data = data;
+ pcm->len = len/(pcm->depth/8)/pcm->chans;
+
+ info->udata = pcm;
+ info->handler = pcm_handler;
+ info->samplerate = pcm->rate;
+ info->length = pcm->len;
+
+// fprint(2, "pcm 0x%p:\ndata 0x%p\nlen %d\ndepth %d\nchans %d\nrate %d\n",
+// pcm, pcm->data, pcm->len, pcm->depth, pcm->chans, pcm->rate);
+// fprint(2, "info 0x%p:\nudata 0x%p\nhandler 0x%p\nsamplerate %d\nlength %d\n",
+// info, info->udata, info->handler, info->samplerate, info->length);
+
+ return 0;
+}
+
void
-audio_init(int samplerate)
+initaudio(int samplerate)
{
mixer.samplerate = samplerate;
mixer.sources = nil;
@@ -191,7 +318,7 @@
}
void
-audio_process(s16int *dst, int len)
+processaudio(s16int *dst, int len)
{
int i, x;
AudioSource **s;
@@ -198,7 +325,7 @@
/* Process in chunks of MIXBUFSIZE if `len` is larger than MIXBUFSIZE */
while(len > MIXBUFSIZE){
- audio_process(dst, MIXBUFSIZE);
+ processaudio(dst, MIXBUFSIZE);
dst += MIXBUFSIZE;
len -= MIXBUFSIZE;
}
@@ -226,7 +353,7 @@
}
AudioSource *
-audio_new_source(AudioSourceInfo *info)
+newaudiosource(AudioSourceInfo *info)
{
AudioSource *src;
@@ -245,12 +372,12 @@
audio_set_pan(src, 0);
audio_set_pitch(src, 1);
audio_set_loop(src, 0);
- audio_stop(src);
+ stopaudio(src);
return src;
}
AudioSource *
-audio_new_source_from_file(char *path)
+loadaudiosource(char *path)
{
AudioSourceInfo info;
uchar buf[12];
@@ -264,12 +391,12 @@
readn(fd, buf, sizeof buf);
seek(fd, 0, 0);
if(memcmp(buf, "ID3", 3) == 0 || (buf[0] == 0xFF && buf[1] == 0xFB)){
- if(mp3_init(&info, fd) < 0){
+ if(loadaudio(&info, fd, mp3decproc) < 0){
close(fd);
return nil;
}
}else if(memcmp(buf+8, "WAVE", 4) == 0){
- if(wav_init(&info, fd) < 0){
+ if(loadaudio(&info, fd, wavdecproc) < 0){
close(fd);
return nil;
}
@@ -280,11 +407,11 @@
}
close(fd);
- return audio_new_source(&info);
+ return newaudiosource(&info);
}
void
-audio_destroy_source(AudioSource *src)
+delaudiosource(AudioSource *src)
{
AudioSource **s;
AudioEvent e;
@@ -368,7 +495,7 @@
}
void
-audio_play(AudioSource *src)
+playaudio(AudioSource *src)
{
src->state = AUDIO_STATE_PLAYING;
if(!src->active){
@@ -379,199 +506,14 @@
}
void
-audio_pause(AudioSource *src)
+pauseaudio(AudioSource *src)
{
src->state = AUDIO_STATE_PAUSED;
}
void
-audio_stop(AudioSource *src)
+stopaudio(AudioSource *src)
{
src->state = AUDIO_STATE_STOPPED;
src->rewind = 1;
-}
-
-static void
-pcm_handler(AudioEvent *e)
-{
- Pcm *pcm;
- s16int *dst;
- int len, i, n;
-
- pcm = e->udata;
-
- switch(e->type){
- case AUDIO_EVENT_DESTROY:
- free(pcm->data);
- free(pcm);
- break;
- case AUDIO_EVENT_SAMPLES:
- dst = e->buffer;
- len = e->length/2;
-Fillbuf:
- n = min(len, pcm->len - pcm->off);
- len -= n;
- while(n--){
- i = 2*pcm->off;
- dst[0] = ((s16int*)pcm->data)[i];
- dst[1] = ((s16int*)pcm->data)[i+1];
- dst += 2;
- pcm->off++;
- }
- if(len > 0){
- pcm->off = 0;
- goto Fillbuf;
- }
- break;
- case AUDIO_EVENT_REWIND:
- pcm->off = 0;
- break;
- }
-}
-
-static void
-mp3decproc(void *arg)
-{
- int *pfd, fd;
-
- pfd = arg;
- fd = pfd[2];
-
- close(pfd[0]);
- dup(fd, 0);
- close(fd);
- dup(pfd[1], 1);
- close(pfd[1]);
-
- execl("/bin/audio/mp3dec", "mp3dec", nil);
- threadexitsall("execl: %r");
-}
-
-static int
-mp3_init(AudioSourceInfo *info, int fd)
-{
- Pcm *pcm;
- void *data;
- uchar buf[1024];
- int pfd[3], n, len;
-
- data = nil;
- len = 0;
-
- if(pipe(pfd) < 0){
- werrstr("pipe: %r");
- return -1;
- }
- pfd[2] = fd;
-
- procrfork(mp3decproc, pfd, mainstacksize, RFFDG|RFNAMEG|RFNOTEG);
- close(pfd[1]);
- while((n = read(pfd[0], buf, sizeof buf)) > 0){
- data = realloc(data, len+n);
- if(data == nil){
- werrstr("realloc: %r");
- return -1;
- }
- memmove((uchar*)data+len, buf, n);
- len += n;
- }
- close(pfd[0]);
-
- pcm = malloc(sizeof *pcm);
- if(pcm == nil){
- free(data);
- werrstr("malloc: %r");
- return -1;
- }
- pcm->depth = 16;
- pcm->chans = 2;
- pcm->rate = 44100;
- pcm->data = data;
- pcm->len = len/(pcm->depth/8)/pcm->chans;
-
- info->udata = pcm;
- info->handler = pcm_handler;
- info->samplerate = pcm->rate;
- info->length = pcm->len;
-
-// fprint(2, "pcm 0x%p:\ndata 0x%p\nlen %d\ndepth %d\nchans %d\nrate %d\n",
-// pcm, pcm->data, pcm->len, pcm->depth, pcm->chans, pcm->rate);
-// fprint(2, "info 0x%p:\nudata 0x%p\nhandler 0x%p\nsamplerate %d\nlength %d\n",
-// info, info->udata, info->handler, info->samplerate, info->length);
-
- return 0;
-}
-
-/* TODO generalize the *decproc and *_init procedures */
-static void
-wavdecproc(void *arg)
-{
- int *pfd, fd;
-
- pfd = arg;
- fd = pfd[2];
-
- close(pfd[0]);
- dup(fd, 0);
- close(fd);
- dup(pfd[1], 1);
- close(pfd[1]);
-
- execl("/bin/audio/wavdec", "wavdec", nil);
- threadexitsall("execl: %r");
-}
-
-static int
-wav_init(AudioSourceInfo *info, int fd)
-{
- Pcm *pcm;
- void *data;
- uchar buf[1024];
- int pfd[3], n, len;
-
- data = nil;
- len = 0;
-
- if(pipe(pfd) < 0){
- werrstr("pipe: %r");
- return -1;
- }
- pfd[2] = fd;
-
- procrfork(wavdecproc, pfd, mainstacksize, RFFDG|RFNAMEG|RFNOTEG);
- close(pfd[1]);
- while((n = read(pfd[0], buf, sizeof buf)) > 0){
- data = realloc(data, len+n);
- if(data == nil){
- werrstr("realloc: %r");
- return -1;
- }
- memmove((uchar*)data+len, buf, n);
- len += n;
- }
- close(pfd[0]);
-
- pcm = malloc(sizeof *pcm);
- if(pcm == nil){
- free(data);
- werrstr("malloc: %r");
- return -1;
- }
- pcm->depth = 16;
- pcm->chans = 2;
- pcm->rate = 44100;
- pcm->data = data;
- pcm->len = len/(pcm->depth/8)/pcm->chans;
-
- info->udata = pcm;
- info->handler = pcm_handler;
- info->samplerate = pcm->rate;
- info->length = pcm->len;
-
-// fprint(2, "pcm 0x%p:\ndata 0x%p\nlen %d\ndepth %d\nchans %d\nrate %d\n",
-// pcm, pcm->data, pcm->len, pcm->depth, pcm->chans, pcm->rate);
-// fprint(2, "info 0x%p:\nudata 0x%p\nhandler 0x%p\nsamplerate %d\nlength %d\n",
-// info, info->udata, info->handler, info->samplerate, info->length);
-
- return 0;
}
--- a/mixer.h
+++ b/mixer.h
@@ -83,13 +83,13 @@
};
-void audio_init(int);
+void initaudio(int);
void audio_set_master_gain(double);
-void audio_process(s16int*, int);
+void processaudio(s16int*, int);
-AudioSource *audio_new_source(AudioSourceInfo*);
-AudioSource *audio_new_source_from_file(char*);
-void audio_destroy_source(AudioSource*);
+AudioSource *newaudiosource(AudioSourceInfo*);
+AudioSource *loadaudiosource(char*);
+void delaudiosource(AudioSource*);
double audio_get_length(AudioSource*);
double audio_get_position(AudioSource*);
int audio_get_state(AudioSource*);
@@ -97,6 +97,6 @@
void audio_set_pan(AudioSource*, double);
void audio_set_pitch(AudioSource*, double);
void audio_set_loop(AudioSource*, int);
-void audio_play(AudioSource*);
-void audio_pause(AudioSource*);
-void audio_stop(AudioSource*);
+void playaudio(AudioSource*);
+void pauseaudio(AudioSource*);
+void stopaudio(AudioSource*);