Commit Diff
Commit:
2139c525dbcd33e08c153dd8dbe75cd1fddf6b30
Date:
Wed Mar 9 09:13:06 2022
UTC
Message:
refactor the player_shouldstop/sio_write dance in a function
--- amused.h
+++ amused.h
@@ -140,6 +140,7 @@ int player(int, int);
void player_senderr(void);
void player_sendeof(void);
int player_shouldstop(void);
+int play(const void *, size_t);
int player(int, int);
void play_oggvorbis(int fd);
--- player.c
+++ player.c
@@ -251,6 +251,15 @@ player(int debug, int verbose)
}
int
+play(const void *buf, size_t len)
+{
+ if (player_shouldstop())
+ return 0;
+ sio_write(hdl, buf, len);
+ return 1;
+}
+
+int
player(int debug, int verbose)
{
int flags;
--- player_123.c
+++ player_123.c
@@ -70,9 +70,6 @@ play_mp3(int fd)
goto done;
for (;;) {
- if (player_shouldstop())
- break;
-
err = mpg123_read(mh, buf, sizeof(buf), &len);
switch (err) {
case MPG123_DONE:
@@ -82,7 +79,8 @@ play_mp3(int fd)
goto done;
break;
case MPG123_OK:
- sio_write(hdl, buf, len);
+ if (!play(buf, len))
+ goto done;
break;
default:
log_warnx("error %d decoding mp3", err);
--- player_flac.c
+++ player_flac.c
@@ -41,13 +41,10 @@ writecb(const FLAC__StreamDecoder *decoder, const FLAC
int i;
size_t len;
- if (player_shouldstop())
- return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
-
for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
if (len+4 >= sizeof(buf)) {
- sio_write(hdl, buf, len);
- len = 0;
+ if (!play(buf, len))
+ goto quit;
}
buf[len++] = buffer[0][i] & 0xff;
@@ -57,10 +54,12 @@ writecb(const FLAC__StreamDecoder *decoder, const FLAC
buf[len++] = (buffer[1][i] >> 8) & 0xff;
}
- if (len != 0)
- sio_write(hdl, buf, len);
+ if (len != 0 && !play(buf, len))
+ goto quit;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+quit:
+ return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
static void
--- player_oggvorbis.c
+++ player_oggvorbis.c
@@ -69,16 +69,14 @@ play_oggvorbis(int fd)
while (!eof) {
long ret;
- if (player_shouldstop())
- break;
-
ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
¤t_section);
if (ret == 0)
eof = 1;
else if (ret > 0) {
/* TODO: deal with sample rate changes */
- sio_write(hdl, pcmout, ret);
+ if (!play(pcmout, ret))
+ break;
}
}
--- player_opus.c
+++ player_opus.c
@@ -60,9 +60,6 @@ play_opus(int fd)
}
for (;;) {
- if (player_shouldstop())
- break;
-
/* NB: will downmix multichannels files into two channels */
ret = op_read_stereo(of, pcm, nitems(pcm));
if (ret == OP_HOLE) /* corrupt file segment? */
@@ -90,7 +87,9 @@ play_opus(int fd)
out[2*i+0] = pcm[i] & 0xFF;
out[2*i+1] = (pcm[i] >> 8) & 0xFF;
}
- sio_write(hdl, out, 4*ret);
+
+ if (!play(out, 4*ret))
+ break;
}
op_free(of);
Omar Polo