File: splay.cc

package info (click to toggle)
splay 0.9.5.2-14
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 848 kB
  • ctags: 1,479
  • sloc: cpp: 7,680; sh: 330; makefile: 35
file content (307 lines) | stat: -rw-r--r-- 6,609 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* Sound Player

   Copyright (C) 1997 by Woo-jae Jung */

// It's an example of using MPEG Sound library

// Anyone can use MPEG Sound library under GPL

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>

#ifdef TAGLIB
#include <taglib/tag.h>
#include <taglib/mpegfile.h>
#endif

#ifdef HAVE_LIBID3
#include <id3/tag.h>
#include <id3/misc_support.h>
#endif /* HAVE_LIBID3 */

#include <iomanip>

#include "mpegsound.h"

#include "splay.h"

using namespace std;

static const char *help=
"\t-2 : playing with half frequency.\n"
"\t-e : exit when playing is done. (only XSPLAY)\n"
"\t-f : display frame and time info (played and remaining).\n"
"\t-m : force to mono.\n"
"\t-r : repeat forever.\n"
"\t-s : shuffle play.\n"
"\t-v : verbose, Very verbose, Very very verbose.\n"
"\t-M : playing MPEG-Audio file as standard input.\n"
"\t-V : display version.\n"
"\n"
"\t-k num : start playing at frame num.\n"
"\t-d dev  : Set dev as device or file.\n"
"\t-l list : list file.\n"
"\t-t buf  : Set number of buffer. (default : 50)\n"
"\n"
"For detail, see man pages.\n";

/***********************/
/* Command line player */
/***********************/
inline void error(int n)
{
  fprintf(stderr,"%s: %s\n",splay_progname,splay_Sounderrors[n-1]);
  return;
}

#ifdef PTHREADEDMPEG
static void playingthread(Mpegfileplayer *player)
{
  if(player->geterrorcode()>0)error(player->geterrorcode());
  else
  {
    player->setforcetomono(splay_forcetomonoflag);
    player->playingwiththread(splay_verbose-1,splay_frameinfo,
			      splay_threadnum, splay_startframe); 
    if(player->geterrorcode()>0)error(player->geterrorcode());
  }
}
#endif

static void playing(Fileplayer *player)
{
  if(player->geterrorcode()>0)error(player->geterrorcode());
  else
  {
    player->setforcetomono(splay_forcetomonoflag);
    player->playing(splay_verbose-1,splay_frameinfo,splay_startframe);
    if(player->geterrorcode()>0)error(player->geterrorcode());
  }
}

//Wrapper to convert null-string to the empty string
inline const char * nn(const char* str)
{
  return str?str:"";
}

#ifdef TAGLIB
std::ostream& operator<<(std::ostream& s, const TagLib::Tag& tag )
{
  s.setf(std::ios::left);
  s << "Title : " << tag.title() << std::endl
    << "Artist: " << std::setw(30) << tag.artist()
    << "Album: " << tag.album() << std::endl
    << "Genre : " << std::setw(18) << tag.genre()
    << "Track: " << std::setw(5) << tag.track()
    << "Year: " <<  std::setw(6) << tag.year()
    << std::endl << std::flush; 
  return s;
}
#endif /* TAGLIB */

#ifdef HAVE_LIBID3

ostream& operator<<(ostream& s, const ID3_Tag* tag )
{
  // Print just what we want...
  s.setf(ios::left);  // The filled fields get the text to the left
  s << 
    "Title : " << nn(ID3_GetTitle(tag)) << endl;
  s << 
    "Artist: " << setw(30) << nn(ID3_GetArtist(tag)) << 
    "Album: " << nn(ID3_GetAlbum(tag)) << 
    endl ; 
  

  s << 
    "Genre : " << setw(18) << nn(ID3_GetGenre(tag)) << 
    "Track: " << setw(5) << ID3_GetTrackNum(tag) << 
    "Year: " <<  setw(6) << nn(ID3_GetYear(tag)); 

  return s;
}

#endif /* HAVE_LIBID3 */

static void play(char *filename)
{
  if( splay_verbose-- )
    cout << filename << ":" << endl;

#ifdef TAGLIB
  if( splay_verbose > 0) // cant do ID3 stuff on stdin
  {
    std::cout << *TagLib::MPEG::File(filename).tag();
  }
#endif /* TAGLIB */        
  
#ifdef HAVE_LIBID3

  if( splay_verbose>0)
    {
      //    cerr << getpid() << endl;
      try {
	const ID3_Tag*  mytag = new ID3_Tag(filename);
	if ( mytag->HasV1Tag() || mytag->HasV2Tag()  )
	  cout << mytag << endl;
	delete mytag;
      }
//      catch(ID3_Error &err){
      catch(...){
	cout << "Error found (GetError functions disabled)" << endl;
//	cout << err.GetErrorFile() << " (" << err.GetErrorLine() << "): "
//	     << err.GetErrorType() << ": " << err.GetErrorDesc() << endl;
      }
    }

#endif /* HAVE_LIBID3 */        
  
  Mpegfileplayer *player;
  
  player=new Mpegfileplayer;
  if(!player->openfile(filename,splay_devicename))
    {
      error(player->geterrorcode());
      delete player;
      return;
    }
  player->setdownfrequency(splay_downfrequency);
#ifdef PTHREADEDMPEG
  playingthread(player);
#else
  playing(player);
#endif
  delete player;
}

#include <signal.h>

void mtest(int)
{
  //  cerr << "mtest got nr " << i << endl;
}

void mstop(int)
{
  //  cerr << "mstop got nr " << i << endl;
  exit(1);
}


int main(int argc,char *argv[])
{
  int c;

  splay_progname=argv[0];

  while((c=getopt(argc,argv,
		  "VM2mfrsvd:k:l:"
#ifdef PTHREADEDMPEG
		  "t:"
#endif
		  ))>=0)
  {
    switch(c)
    {
      case 'V':printf("%s %s"
#ifdef PTHREADEDMPEG
		      " with pthread"
#endif
		      "\n",PACKAGE,VERSION);
               return 0;
      case 'M':
	{
	  Mpegfileplayer player;

	  player.openfile(NULL,splay_devicename);
	  playing(&player);
	  return 0;
	}
	
    case '2':splay_downfrequency  =   1;break;
    case 'f':splay_frameinfo      =true;break;
    case 'm':splay_forcetomonoflag=true;break;
    case 'r':splay_repeatflag     =true;break;
    case 's':splay_shuffleflag    =true;break;
    case 'v':splay_verbose++;           break;
      
    case 'd':splay_devicename=optarg;   break;
    case 'k':splay_startframe=atoi(optarg);   break;
    case 'l':if(splay_verbose)
		fprintf(stderr,"List file : %s\n",optarg);
	       readlist(optarg);
	       break;
#ifdef PTHREADEDMPEG
      case 't':
	{
	  int a;

	  sscanf(optarg,"%d",&a);
	  splay_threadnum=a;
	}
	break;
#endif
      default:fprintf(stderr,"Bad argument.\n");
    }
  }

  if(argc<=optind && splay_listsize==0)
  {
    printf("%s %s"
#ifdef PTHREADEDMPEG
	   " with pthread"
#endif
	   "\n"
	   "Usage : splay [-2mrsvMVW] [-d device] [-l listfile] "
#ifdef PTHREADEDMPEG
	   "[-t number] "
#endif
	   "files ...\n"
	   "\n"
	   "%s"
	   ,PACKAGE,VERSION,help);
    return 0;
  }

  if(splay_listsize==0)    // Make list by arguments
    arglist(argc,argv,optind);

  do
  {
    if(splay_shuffleflag)shufflelist();

    for(int i=0;i<splay_listsize;i++){
      signal(SIGINT,&mtest);
      if ( fork() ) {
	//	pid_t pid=getpid();
	//	cout << "parent process " << pid << endl;
	int a;
	wait(&a);
	signal(SIGINT,&mstop);
	//	cerr << "stoppable "<<endl;
	usleep (500*1000);
	//	cerr << "not stoppable "<<endl;
	signal(SIGINT,&mtest);
      }
      else{
	//	cout << "child process " << getpid() << endl;
	signal(SIGINT,&mstop);
	play(splay_list[i]);
	exit(0);
      }
    }
    
  }while(splay_repeatflag);

  return 0;
}