1 /* Sound Player
2 
3    Copyright (C) 1997 by Woo-jae Jung */
4 
5 // Functions to support player
6 // Anyone can use MPEG/WAVE Sound library under GPL
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 
16 #include <mpegsound.h>
17 #include <stdarg.h>
18 #include "splay.h"
19 
20 int  splay_verbose=0;
21 char *splay_progname;
22 const char *splay_devicename = SOUND_DEVICE;
23 
24 char *splay_list[MAXLISTSIZE];
25 int  splay_listsize=0;
26 int  splay_downfrequency=0;
27 bool splay_shuffleflag=false,
28      splay_repeatflag=false,
29      splay_forcetomonoflag=false;
30 
31 #ifdef PTHREADEDMPEG
32 int splay_threadnum=50;
33 #endif
34 
35 const char *splay_Sounderrors[SOUND_ERROR_UNKNOWN]=
36 { "Failed to open sound device.",
37   "Sound device is busy.",
38   "Buffersize of sound device is wrong.",
39   "Sound device control error.",
40 
41   "Failed to open file for reading.",    // 5
42   "Failed to read file.",
43 
44   "Unkown proxy.",
45   "Unkown host.",
46   "Socket error.",
47   "Connection failed.",                  // 10
48   "Fdopen error.",
49   "Http request failed.",
50   "Http write failed.",
51   "Too many relocation",
52 
53   "Memory is not enough.",               // 15
54   "Unexpected EOF.",
55   "Bad sound file format.",
56 
57   "Cannot make thread.",
58 
59   "Unknown error.",
60 };
61 
62 
63 /***************/
64 /* Manage list */
65 /***************/
66 static bool argsflag=false;
arglist(int argc,char * argv[],int start)67 void arglist(int argc,char *argv[],int start)
68 {
69   register int i;
70 
71   argsflag=true;
72   for(i=start;(i<argc) && (splay_listsize<MAXLISTSIZE); i++)
73     splay_list[splay_listsize++]=argv[i];
74 }
75 
killlist(void)76 void killlist(void)
77 {
78   if(splay_listsize)
79   {
80     if(!argsflag)
81     {
82       for(int i=0;i<splay_listsize;i++)free(splay_list[i]);
83     }
84     argsflag=false;
85   }
86   splay_listsize=0;
87 }
88 
addlist(const char * path,const char * filename)89 void addlist(const char *path,const char *filename)
90 {
91   char songfilename[MAXFILENAMELENGTH*2];
92   int p=0;
93 
94   if(path)
95   {
96     for(;path[p];p++)
97       songfilename[p]=path[p];
98     songfilename[p++]='/';
99   }
100 
101   for(int i=0;filename[i];i++,p++)
102     songfilename[p]=filename[i];
103 
104   songfilename[p]=0;
105   splay_list[splay_listsize]=strdup(songfilename);
106   splay_listsize++;
107 }
108 
readlist(char * filename)109 void readlist(char *filename)
110 {
111   char songfile[MAXFILENAMELENGTH];
112   Soundinputstream *fp;
113   int c;
114 
115   killlist();
116 
117   {
118     int err;
119 
120     if((fp=Soundinputstream::hopen(filename,&err))==NULL)
121     {
122       fprintf(stderr,"%s: No list file found\n",splay_progname);
123       return;
124     }
125   }
126 
127   for(splay_listsize=0;splay_listsize<MAXLISTSIZE;)
128   {
129     c=fp->getbytedirect();
130     if(c==EOF)break;
131     if(c=='#')
132       for(;;)
133       {
134 	c=fp->getbytedirect();
135 	if(c==EOF)break;
136 	if(c=='\n')break;
137       }
138     else
139       for(;;)
140       {
141 	if((c==EOF) || (c=='\n'))break;
142 	if(c!=' ')
143 	{
144 	  int p;
145 
146 	  for(p=0;;p++)
147 	  {
148 	    songfile[p]=c;
149 	    c=fp->getbytedirect();
150 	    if((c==EOF) || (c=='\n'))break;
151 	  }
152 
153 	  songfile[++p]='\0';
154 	  splay_list[splay_listsize]=strdup(songfile);
155 	  splay_listsize++;
156 	  break;
157 	}
158 
159 	c=fp->getbytedirect();
160       }
161   }
162 
163   if(splay_verbose>1)
164   {
165     for(int i=0;i<splay_listsize;i++)
166       fprintf(stderr,"%d : %s\n",i+1,splay_list[i]);
167   }
168 
169   delete fp;
170 }
171 
shufflelist(void)172 void shufflelist(void)
173 {
174   char *tmp;
175   int i,p;
176   time_t t;
177 
178   srandom(time(&t));
179 
180   for(i=splay_listsize-1;i>0;i--)
181   {
182     p=random()%i;
183 
184     tmp=splay_list[p];
185     splay_list[p]=splay_list[i];
186     splay_list[i]=tmp;
187   }
188 }
189 
190 void
debug(const char * fmt,...)191 debug(const char *fmt, ... )
192 {
193 	va_list ap;
194 	va_start(ap, fmt);
195 	vfprintf(stderr,fmt,ap);
196 	va_end(ap);
197 }
198 
199