00001 #include "common.h"
00002 #include <curses.h>
00003 #include <limits.h>
00004 #include <signal.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <sys/types.h>
00008 #include <unistd.h>
00009
00010
00011 int repeat = 1;
00012
00013 static void stopplay (int signo)
00014 {
00015 repeat = 0;
00016 }
00017
00018 static void hhmmss (u_int16_t seconds, u_int16_t *hh, u_int16_t *mm, u_int16_t *ss)
00019 {
00020 *hh = *mm = 0;
00021 if ( seconds >= 3600 ) *hh= seconds/3600;
00022 seconds -= 3600*(*hh);
00023 if ( seconds >= 60 ) *mm= seconds/60;
00024 *ss= seconds-(60*(*mm));
00025 }
00026
00027 static void usage (void) {
00028 fprintf(stderr, "usage: play [ -D debuglvl ] <trackid> ...\n");
00029 exit(1);
00030 }
00031
00032 int main (int argc, char **argv)
00033 {
00034 njb_t njbs[NJB_MAX_DEVICES], *njb;
00035 int i, n, status, change, opt, debug;
00036 u_int16_t sec, hh, mm, ss;
00037 u_int32_t trackid;
00038 extern char *optarg;
00039 extern int optind;
00040 WINDOW * root;
00041
00042 debug = 0;
00043
00044 while ( (opt = getopt(argc, argv, "D:")) != -1 ) {
00045 switch (opt) {
00046 case 'D':
00047 debug = atoi(optarg);
00048 break;
00049 default:
00050 break;
00051 }
00052 }
00053 argc -= optind;
00054 argv += optind;
00055
00056 if ( ! argc ) usage();
00057
00058 if ( debug ) NJB_Set_Debug(debug);
00059
00060 signal(SIGINT, stopplay);
00061
00062 if ( NJB_Discover(njbs, 0, &n) == -1 ) {
00063 perror("could not probe for NJB devices\n");
00064 return 1;
00065 }
00066 if ( n == 0 ) {
00067 fprintf(stderr, "no NJB devices found\n");
00068 return 0;
00069 }
00070
00071 njb = njbs;
00072
00073 if ( NJB_Open(njb) == -1 ) {
00074 NJB_Error_Dump(njb, stderr);
00075 return 1;
00076 }
00077
00078 if ( NJB_Capture(njb) == -1 ) {
00079 NJB_Error_Dump(njb, stderr);
00080 return 1;
00081 }
00082
00083 for (i= 0; i< argc; i++) {
00084 trackid= strtoul(argv[i], NULL, 10);
00085
00086 if ( i == 0 ) {
00087 status= NJB_Play_Track(njb, trackid);
00088 } else {
00089 status= NJB_Queue_Track(njb, trackid);
00090 }
00091
00092 if ( status == -1 ) NJB_Error_Dump(njb, stdout);
00093 }
00094
00095 root = initscr();
00096 cbreak();
00097 noecho();
00098 halfdelay (1);
00099
00100 printw("+---------------+\n"
00101 "| x : exit |\n"
00102 "| p : pause |\n"
00103 "| r : resume |\n"
00104 "| <> : seek 10s |\n"
00105 "+---------------+\n");
00106
00107 change = 0;
00108 i = 0;
00109 mvprintw(6, 0, "Track ID %10.10s: 00:00:00", argv[i]);
00110 fflush(stdout);
00111 while ( repeat ) {
00112 int c;
00113
00114 if ((c = getch ()) != ERR) {
00115 switch (c) {
00116 case 'p':
00117 NJB_Pause_Play (njb);
00118 break;
00119 case 'r':
00120 NJB_Resume_Play (njb);
00121 break;
00122 case 'x':
00123 repeat = 0;
00124 break;
00125 case '>':
00126 NJB_Seek_Track (njb, sec*1000+10000);
00127 break;
00128 case '<':
00129 NJB_Seek_Track (njb, sec > 10 ? sec*1000-10000 : 0);
00130 break;
00131 }
00132 }
00133 if ( change ) {
00134 i++;
00135 if ( i == argc ) {
00136 repeat= 0;
00137 } else {
00138 mvprintw(6, 0, "Track ID %10.10s: 00:00:00", argv[i]);
00139 refresh ();
00140 }
00141 change= 0;
00142 } else
00143 NJB_Elapsed_Time(njb, &sec, &change);
00144
00145 hhmmss(sec, &hh, &mm, &ss);
00146 mvprintw (6, 21, "%02u:%02u:%02u", hh, mm, ss);
00147 refresh ();
00148 }
00149 echo ();
00150 endwin ();
00151
00152 NJB_Stop_Play(njb);
00153
00154 NJB_Release(njb);
00155
00156 NJB_Close(njb);
00157 return 0;
00158 }