sad

simple audio daemon
git clone git://git.2f30.org/sad
Log | Files | Refs | LICENSE

commit 14aa84edc4b44aa18713157ed120f12f8d78d8cb
parent be5658cc64ace57bb6193e35b5d88f844e71588e
Author: sin <sin@2f30.org>
Date:   Mon, 29 Dec 2014 02:20:36 +0000

Add repeat/random modes

Diffstat:
Mplaylist.c | 39++++++++++++++++++++-------------------
Msad.c | 4+++-
Msad.h | 10++++++++--
3 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/playlist.c b/playlist.c @@ -6,6 +6,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include "sad.h" @@ -110,28 +111,22 @@ clearplaylist(void) } Song * -playnextsong(void) +picknextsong(void) { Song *s; - stopsong(playlist.cursong); - /* default to a repeat/cycle through mode */ - s = getnextsong(); - s->state = PREPARE; - playlist.cursong = s; - return s; -} - -Song * -playprevsong(void) -{ - Song *s; - - stopsong(playlist.cursong); - /* default to a repeat/cycle through mode */ - s = getprevsong(); - s->state = PREPARE; - playlist.cursong = s; + switch (playlist.mode) { + case REPEAT: + s = getnextsong(); + break; + case RANDOM: + srand(time(NULL)); + s = playlist.songs[rand() % playlist.nsongs]; + break; + default: + s = NULL; + break; + } return s; } @@ -151,3 +146,9 @@ stopsong(Song *s) s->state = NONE; } } + +void +playlistmode(int mode) +{ + playlist.mode = mode; +} diff --git a/sad.c b/sad.c @@ -83,7 +83,7 @@ playaudio(void) break; case PLAYING: if ((nbytes = s->decoder->decode(buf, sizeof(buf))) <= 0) { - playnextsong(); + playsong(picknextsong()); notify(EVSONGFINISHED); } else { playoutput(buf, nbytes); @@ -125,6 +125,8 @@ main(int argc, char *argv[]) openoutputs(); initnotifier(); + playlistmode(REPEAT); + while (1) { rfds = master; tv.tv_sec = 0; diff --git a/sad.h b/sad.h @@ -18,6 +18,11 @@ enum { }; enum { + REPEAT, + RANDOM +}; + +enum { EVSONGFINISHED, }; @@ -48,6 +53,7 @@ typedef struct { Song *cursong; size_t nsongs; size_t maxsongs; + int mode; } Playlist; typedef struct { @@ -77,10 +83,10 @@ Song *getcursong(void); void putcursong(Song *); void dumpplaylist(int); void clearplaylist(void); -Song *playnextsong(void); -Song *playprevsong(void); +Song *picknextsong(void); void playsong(Song *); void stopsong(Song *); +void playlistmode(int); /* library.c */ Song *addlibrary(const char *);