summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/decoder/tplayPlugin.cpp
blob: 01c44cbcbc9dfe3a00d81ce1672cb41362d0c806 (plain)
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
/*
  tplay player plugin
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "tplayPlugin.h"

#include "../tplay/tplayfunctions.h"

#include <iostream>

using namespace std;

TplayPlugin::TplayPlugin() {
  info=new info_struct();

  info->progname = NULL;
  info->loop = FALSE;
  info->in_seconds = FALSE;
  info->speed = DEFAULT_SPEED;
  info->bits = DEFAULT_BITS;
  info->channels = DEFAULT_CHANNELS;
  info->buffer_size = BUFFER_SIZE;
  info->show_usage = FALSE;
  info->swap = FALSE;
  info->forceraw = FALSE;
  info->force = FALSE;
  info->device = NULL;
  info->verbose = FALSE;
  info->optind = 0;
  info->buffer = NULL;
  info->firstblock = NULL;
  info->number_of_blocks = 0;
  info->readblock = 0;
  info->writeblock = 0;
  info->readcount = 0;
  info->writecount = 0;
  info->alldone = FALSE;
  info->overflow = FALSE;
  info->underflow = FALSE;
  info->audioset = FALSE;
  info->headerskip = 0;
  info->blocksize = 4096;
  info->bytes_on_last_block = 0;

  startStamp=new TimeStamp();
  endStamp=new TimeStamp();
}


TplayPlugin::~TplayPlugin() {
  delete startStamp;
  delete endStamp;
  delete info;
}



void TplayPlugin::decoder_loop() {
  int bytesread, count, stereo;
  char *p, *bufferp;

  if (input == NULL) {
    cout << "TplayPlugin::decoder_loop input is NULL"<<endl;
    exit(0);
  }
  if (output == NULL) {
    cout << "TplayPlugin::decoder_loop output is NULL"<<endl;
    exit(0);
  }

  TimeStamp* stamp;
  long pos;

  info->buffer = (char*) malloc(info->buffer_size * sizeof(char));
 
  while(runCheck()) {
    switch(streamState) {
    case _STREAM_STATE_FIRST_INIT : {
       read_header();
       
       if (info->channels == 1)
	 stereo = FALSE;
       else
	 stereo = TRUE;

       info->number_of_blocks = 0;
       bufferp = info->buffer;
       pluginInfo->setLength(getTotalLength());
       output->writeInfo(pluginInfo);
       lhasLength=true;

       setStreamState(_STREAM_STATE_INIT);
       break;
    }
    case _STREAM_STATE_INIT :
      setStreamState(_STREAM_STATE_PLAY);
      cout << "audioSetup call"<<endl;
      output->audioOpen();
      output->audioSetup(info->speed, stereo, 1, 0, info->bits);
      break;   
    case _STREAM_STATE_PLAY :
      bytesread = 0;
      count = 0;
      p = bufferp;
      while ((bytesread < info->blocksize) && (count != -1) &&
	     ((count = input->read(p,info->blocksize-bytesread))!=0)){
	p += count;
	bytesread += count;
      }
      if (info->swap) {
	swap_block(bufferp, bytesread);
      }
      
      if (bytesread > 0) {
	pos=input->getBytePosition();
	stamp=input->getTimeStamp(pos-bytesread);
      	output->audioPlay(stamp,endStamp,(char *) bufferp, bytesread);
      }

      if (bytesread < info->blocksize) {
	info->alldone = TRUE;

      }
      break;
    case _STREAM_STATE_WAIT_FOR_END:
      // exit while loop
      lDecoderLoop=false;
      break;
    default:
      cout << "unknown stream state:"<<streamState<<endl;
    }
  }
  cout << "tplay exit"<<endl;
  free(info->buffer);
  info->buffer = NULL;
  output->audioFlush();
}


int TplayPlugin::seek_impl(int second) {
  int bytes_per_second;
  int seconds;
  int back;
  

  bytes_per_second = info->speed * info->channels * (info->bits / 8);


  
  seconds = bytes_per_second * second;
  back=input->seek(seconds);

  return back;
}


void TplayPlugin::config(const char* key,const char* value,void* user_data) {
  DecoderPlugin::config(key,value,user_data);
}


void TplayPlugin::swap_block(char * buffer, int blocksize) {
  register int i;
  char c, *p;

  p = buffer;
  for (i = 0; i < (blocksize / 2); i++) {
    c = *p;
    *p = *(p + 1);
    *++p = c;
    p++;
  }
}


void TplayPlugin::read_header() {
  int bytesread, count;
  char *p, *bufferp;

  info->firstblock = (char*) malloc(info->blocksize * sizeof(char));
  bufferp = info->firstblock;
  if (!info->forceraw) {
    bytesread = 0;
    count = 0;
    p = bufferp;
    while ((bytesread < info->blocksize) && (count != -1) &&
	   ((count=input->read(p,info->blocksize-bytesread)) != 0)){
      p += count;
      bytesread += count;
    }

    if (bytesread < SUN_HDRSIZE)
      cout << "Sample size is too small"<<endl;
    
    if (read_au(info,info->firstblock) && read_wav(info,info->firstblock)) {
      if (info->verbose)
	printf("Playing raw data: %ld samples/s, %d bits, %d channels.\n",
	       info->speed, info->bits, info->channels);
    }
    
    if (info->swap)
      swap_block(bufferp, bytesread);
    
    if (bytesread < info->blocksize) {
      info->alldone = TRUE;
      info->bytes_on_last_block = bytesread;
      return;
    }
    
    if (info->headerskip) {
      count = 0;
      bytesread = info->blocksize - info->headerskip;
      p = info->firstblock + (info->blocksize - info->headerskip);
      while ((bytesread < info->blocksize) && (count != -1) &&
	     ((count = input->read(p,info->blocksize - bytesread)) != 0)) {
	p += count;
	bytesread += count;
      }
    }
    
    info->writeblock++;
    info->writecount++;
  }
  else {
    if (info->verbose)
      printf("Playing raw data: %ld samples/s, %d bits, %d channels\n",
	     info->speed, info->bits, info->channels);
  }
}

/**
   Nobody is perfect I'm too tired to fix this race
*/

int TplayPlugin::getTotalLength() {
  float wavfilesize = input->getByteLength();
  int back=0;


  float frequence=info->speed;

  int bits=info->bits;


  if (bits == 16) {
    wavfilesize=wavfilesize/2;
  }
  if (info->channels==2) {
    wavfilesize=wavfilesize/2;
  }
  if (frequence != 0) {
    back=(int)(wavfilesize/frequence);
  }
  return back;
}