commit 48652b6c0cb314c72beab9029bd59598c74188b0
parent 31e2c7e6f831a591244ba81569074b1ef79e6522
Author: sin <sin@2f30.org>
Date: Fri, 26 Dec 2014 14:02:59 +0000
Refactor decoder
Diffstat:
4 files changed, 27 insertions(+), 21 deletions(-)
diff --git a/cmd.c b/cmd.c
@@ -50,10 +50,11 @@ cmdnext(int fd, int argc, char **argv)
return;
}
- next = getnextsong();
- matchdecoder(s->path)->close();
+ decoder->close();
s->state = NONE;
+ next = getnextsong();
next->state = PREPARE;
+ setdecoder(next->path);
putcursong(next);
dprintf(fd, "OK\n");
}
@@ -116,11 +117,12 @@ cmdplay(int fd, int argc, char **argv)
cur = getcursong();
if (cur) {
- matchdecoder(cur->path)->close();
+ decoder->close();
cur->state = NONE;
}
s->state = PREPARE;
+ setdecoder(s->path);
putcursong(s);
printf("Song %s with %d playing\n",
s->path, s->id);
@@ -142,10 +144,11 @@ cmdprev(int fd, int argc, char **argv)
return;
}
- prev = getprevsong();
- matchdecoder(s->path)->close();
+ decoder->close();
s->state = NONE;
+ prev = getprevsong();
prev->state = PREPARE;
+ setdecoder(prev->path);
putcursong(prev);
dprintf(fd, "OK\n");
}
@@ -165,7 +168,7 @@ cmdstop(int fd, int argc, char **argv)
dprintf(fd, "ERR \"no song is active\"\n");
return;
}
- matchdecoder(s->path)->close();
+ decoder->close();
s->state = NONE;
dprintf(fd, "OK\n");
}
@@ -201,7 +204,7 @@ cmdclear(int fd, int argc, char **argv)
s = getcursong();
if (s) {
- matchdecoder(s->path)->close();
+ decoder->close();
s->state = NONE;
}
clearplaylist();
diff --git a/decoder.c b/decoder.c
@@ -8,6 +8,8 @@
#include "sad.h"
+Decoder *decoder;
+
static struct {
char *ext;
Decoder *decoder;
@@ -27,17 +29,20 @@ initdecoders(void)
return 0;
}
-Decoder *
-matchdecoder(const char *name)
+int
+setdecoder(const char *name)
{
char *ext;
int i;
ext = strrchr(name, '.');
if (!ext)
- return NULL;
- for (i = 0; i < LEN(Decodermap); i++)
- if (!strcasecmp(Decodermap[i].ext, ext))
- return Decodermap[i].decoder;
- return NULL;
+ return -1;
+ for (i = 0; i < LEN(Decodermap); i++) {
+ if (!strcasecmp(Decodermap[i].ext, ext)) {
+ decoder = Decodermap[i].decoder;
+ break;
+ }
+ }
+ return 0;
}
diff --git a/sad.c b/sad.c
@@ -65,7 +65,6 @@ static void
doaudio(void)
{
Song *s;
- Decoder *d;
short buf[2048];
int nbytes;
@@ -73,19 +72,17 @@ doaudio(void)
if (!s)
return;
- d = matchdecoder(s->path);
-
switch (s->state) {
case PREPARE:
- if (d->open(s->path) < 0) {
+ if (decoder->open(s->path) < 0) {
s->state = NONE;
return;
}
s->state = PLAYING;
break;
case PLAYING:
- if ((nbytes = d->decode(buf, sizeof(buf))) <= 0) {
- d->close();
+ if ((nbytes = decoder->decode(buf, sizeof(buf))) <= 0) {
+ decoder->close();
s->state = NONE;
} else {
output->play(buf, nbytes);
diff --git a/sad.h b/sad.h
@@ -76,5 +76,6 @@ int gettokens(char *, char **, int, char *);
int tokenize(char *, char **, int);
/* decoder.c */
+extern Decoder *decoder;
int initdecoders(void);
-Decoder *matchdecoder(const char *);
+int setdecoder(const char *);