File: fileplayer.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 (198 lines) | stat: -rw-r--r-- 4,739 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
/* MPEG Sound library

   (C) 1997 by Jung woo-jae */

// Fileplayer.cc
// It's an example for how to use MPEG Sound library

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

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "mpegsound.h"

using namespace std;

// File player superclass
Fileplayer::Fileplayer()
{
  __errorcode=SOUND_ERROR_OK;
  player=NULL;
};

Fileplayer::~Fileplayer()
{
  delete player;
};

// Mpegfileplayer
Mpegfileplayer::Mpegfileplayer()
{
  loader=NULL;
  server=NULL;
};

Mpegfileplayer::~Mpegfileplayer()
{
  if(loader)delete loader;
  if(server)delete server;
}

bool Mpegfileplayer::openfile(char *filename,const char *device)
{
// Player
  if(device==NULL){
    int fd;
    device="/dev/sound/dsp";
    fd=open(device, O_WRONLY|O_NDELAY);
    if( fd == -1 ) {
      device="/dev/dsp";
      fd=open(device, O_WRONLY|O_NDELAY);
    }
    if ( fd == -1 ) {
      cerr << "Cannot open /dev/dsp or /dev/sound/dsp!" << endl;
      return seterrorcode(SOUND_ERROR_DEVOPENFAIL);
    }
    close(fd);
  }
  
  if(device[0]=='/')player=new Rawplayer;
  else 
  {
    if(device[0]=='-')device=NULL;
    player=new Rawtofile;
  }

  if(player==NULL)
    return seterrorcode(SOUND_ERROR_MEMORYNOTENOUGH);
  if(!player->initialize(device))
    return seterrorcode(player->geterrorcode());

// Loader
  {
    int err;
    if((loader=Soundinputstream::hopen(filename,&err))==NULL)
      return seterrorcode(err);
  }

// Server
  if((server=new Mpegtoraw(loader,player))==NULL)
    return seterrorcode(SOUND_ERROR_MEMORYNOTENOUGH);

// Initialize server
  server->initialize(filename);
  return true;
}

void Mpegfileplayer::setforcetomono(bool flag)
{
  server->setforcetomono(flag);
};

void Mpegfileplayer::setdownfrequency(int value)
{
  server->setdownfrequency(value);
};

#include <math.h>
char * tominsec(double s)
{
  int min=int(floor(s/60));
  double sec=s-60*min;
  char * buff = new char[15];
  sprintf(buff,"%d:%2.2f",min,sec);
  return buff;
}

bool Mpegfileplayer::playing(int verbose, bool frameinfo, int startframe)
{
  if(!server->run(-1))return false;       // Initialize MPEG Layer 3
  if(verbose>0)showverbose(verbose);

  if (startframe) server->setframe(startframe);

  int pcmperframe=server->getpcmperframe();
  int frequency=server->getfrequency();
  int totframes=server->gettotalframe();
  double tottime=1.0*totframes*pcmperframe/frequency;
  if(frameinfo) {
    cout << "Totalframes " <<  totframes;
    cout << "; Totaltime " << tominsec(tottime)  << endl;
  }

  // Playing
  while (server->run(100)) { 
    if(frameinfo) {
      int currframe=server-> getcurrentframe();
      double currtime=1.0*currframe*pcmperframe/frequency;
      cout << "Frame " << currframe << " [" << totframes-currframe << "]; ";
      cout << "Time " << tominsec(currtime) << " [" ;
      cout << tominsec(tottime-currtime) << "]" << endl ;
    }
  }

  seterrorcode(server->geterrorcode());
  if(seterrorcode(SOUND_ERROR_FINISH))return true;
  return false;
}

#ifdef PTHREADEDMPEG
bool Mpegfileplayer::playingwiththread(int verbose,bool frameinfo,
				       int framenumbers, int startframe)
{
  if(framenumbers<20)return playing(verbose,frameinfo,startframe);

  server->makethreadedplayer(framenumbers);

  if(!server->run(-1))return false;       // Initialize MPEG Layer 3
  if(verbose>0)showverbose(verbose);

  if (startframe) server->setframe(startframe);

  int pcmperframe=server->getpcmperframe();
  int frequency=server->getfrequency();
  int totframes=server->gettotalframe();
  double tottime=1.0*totframes*pcmperframe/frequency;
  if(frameinfo) {
    cout << "Totalframes " <<  totframes;
    cout << "; Totaltime " << tominsec(tottime)  << endl;
  }

  // Playing
  while (server->run(100)) {
    if(frameinfo) {
      int currframe=server-> getcurrentframe();
      double currtime=1.0*currframe*pcmperframe/frequency;
      cout << "Frame " << currframe << " [" << totframes-currframe << "]; ";
      cout << "Time " << tominsec(currtime) << " [" ;
      cout << tominsec(tottime-currtime) << "]" << endl ;
    }
  }

  server->freethreadedplayer();
  
  seterrorcode(server->geterrorcode());
  if(seterrorcode(SOUND_ERROR_FINISH))return true;
  return false;
}
#endif

void Mpegfileplayer::showverbose(int )
{
  static const char *modestring[4]={"stereo","joint stereo","dual channel","mono"};

  fprintf(stderr,"\tMPEG-%d Layer %d, %s,\n\t%dHz%s, %dkbit/s, ",
	  server->getversion()+1,
	  server->getlayer(),modestring[server->getmode()],
	  server->getfrequency(),server->getdownfrequency()?"//2":"",
	  server->getbitrate());
  fprintf(stderr,server->getcrccheck() 
	  ? "with crc check\n" 
	  : "without crc check\n");
}