2 * mpd_random_playlist.c 19.01.2009
4 * Copyright 2009 Max Christian Pohle <webmaster@coderonline.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 #define PROGRAM_NAME "mpd_random_playlist"
25 #include <libmpd/libmpd.h>
29 void help(char *program
);
30 void error_callback(MpdObj
*mi
,int errorid
, char *msg
, void *userdata
);
32 int main(int argc
, char **argv
)
38 char *hostname
= getenv("MPD_HOST");
39 char *port
= getenv("MPD_PORT");
40 char *password
= getenv("MPD_PASSWORD");
41 char *count
= argv
[1];
44 if(!hostname
) { hostname
= "localhost"; }
45 if(!password
) { password
= ""; }
46 if(count
) { icount
= atoi(count
); }
47 if(port
) { iport
= atoi(port
); }
50 printf("==== " PROGRAM_NAME
"====\n\n");
57 MpdObj
*mo
= mpd_new(hostname
, iport
, password
);
60 { mpd_send_password(mo
); }
62 mpd_signal_connect_error(mo
, (ErrorCallback
)error_callback
, NULL
);
66 if (!mpd_check_connected(mo
))
69 printf("\tERROR: failed to connect to %s\n", hostname
);
75 printf("\t> connection to host `%s` estamblished.\n", hostname
);
78 MpdData
*listItems
= mpd_database_get_complete(mo
);
80 int total_songs
= mpd_stats_get_total_songs(mo
);
81 int total_playtime
= mpd_stats_get_db_playtime(mo
);
84 printf("\t> found %d songs in database.\n", total_songs
);
85 printf("\t> with a total playtime of %d\n", total_playtime
);
86 printf("\t> random selection of %d files starts now...\n", icount
);
88 srand( (unsigned)time( NULL
) );
90 for(i
=1;i
<=icount
; i
++)
92 float random_song
= ((float) rand() / RAND_MAX
) * total_songs
;
93 int songNr
= (int) random_song
;
96 for(q
=0; q
<=songNr
; q
++)
98 listItems
= mpd_data_get_next(listItems
);
99 if (mpd_data_is_last(listItems
))
100 { listItems
= mpd_data_get_first(listItems
); }
102 if(listItems
->type
!= MPD_DATA_TYPE_SONG
)
107 printf("\t\t> adding %s\n", listItems
->song
->title
);
108 mpd_playlist_queue_add(mo
, listItems
->song
->file
);
111 printf("\t> random selection successfully done.\n");
113 mpd_playlist_clear(mo
);
115 printf("\t> cleared playlist.\n");
116 mpd_playlist_queue_commit(mo
);
118 printf("\t> submitted new playlist with random songs.\n");
121 printf("\t> started playback.\n\n");
128 void error_callback(MpdObj
*mi
,int errorid
, char *msg
, void *userdata
)
131 printf("ERROR: %s\n...maybe wrong password?", msg
);
135 void help(char *program
)
140 "\t./%s [--help] [<NUMBER_OF_RANDOM_FILES>]\n\n"
141 "\tENVIROMENT-VARIABLES\n\n"
142 "\tMPD_HOST\thostname of mpd (optional)\n"
143 "\tMPD_PORT\tport to connect with mpd (optional)\n"
144 "\tMPD_PASSWORD\tpassword for the mpd-server (optional, but recommend)\n\n"
145 "\t...to set these use export <VARIABLE>=<VALUE>-syntax\n\n"