~ubuntu-branches/debian/experimental/mednafen/experimental

« back to all changes in this revision

Viewing changes to src/cdplay.cpp

  • Committer: Package Import Robot
  • Author(s): Stephen Kitt
  • Date: 2012-01-31 07:21:35 UTC
  • mfrom: (1.2.8)
  • Revision ID: package-import@ubuntu.com-20120131072135-es3dj12y00xcnrsk
Tags: 0.9.19-1
* New upstream WIP version.
* Update copyright information.
* Refresh use-system-tremor.patch and remove psx-big-endian-only.patch.
* Add spelling-fixes.patch based on Lintian's recommendations.
* Build-depend on debhelper 9 or later and remove corresponding Lintian
  override.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <vector>
25
25
#include <math.h>
26
26
 
 
27
using namespace CDUtility;
27
28
 
28
29
#include <mednafen/resampler/resampler.h>
29
30
 
30
 
SpeexResamplerState *resampler = NULL;
 
31
namespace MDFN_IEN_CDPLAY
 
32
{
 
33
 
 
34
static std::vector<double> sqrt_lut; //[65536];
 
35
static std::vector<double> sin_lut; //[65536];
 
36
static SpeexResamplerState *resampler = NULL;
31
37
 
32
38
static uint8 *controller_ptr;
33
39
static uint8 last_controller;
34
 
static CD_TOC toc;
35
40
 
36
41
enum
37
42
{
49
54
static int16 ResampBuffer[588 * 2][2];  // Resampler input buffer, * 2 for resampler leftovers
50
55
static uint32 ResampBufferPos;
51
56
static uint32 PrevRate;
 
57
static unsigned int CurrentDisc;
 
58
 
 
59
static std::vector<CDIF *> *cdifs;
52
60
 
53
61
static int32 CurrentATLI;
54
62
 
55
 
static std::vector<int32> AudioTrackList;
 
63
struct AudioTrackInfo
 
64
{
 
65
 inline AudioTrackInfo(unsigned disc_, int32 track_, int32 lba_, int32 final_lba_)
 
66
 {
 
67
  disc = disc_;
 
68
  track = track_;
 
69
  lba = lba_;
 
70
  final_lba = final_lba_;
 
71
 }
 
72
 
 
73
 unsigned disc;
 
74
 int32 track;
 
75
 int32 lba;
 
76
 int32 final_lba;       // Inclusive.
 
77
};
 
78
 
 
79
static std::vector<AudioTrackInfo> AudioTrackList;
56
80
 
57
81
static void InitLUT(void);
58
82
 
59
 
static int LoadCD(void)
 
83
static int LoadCD(std::vector<CDIF *> *CDInterfaces)
60
84
{
61
 
 if(!CDIF_ReadTOC(&toc))
62
 
 {
63
 
  puts("Error reading TOC");
64
 
  return(0);
65
 
 }
 
85
 cdifs = CDInterfaces;
66
86
 
67
87
 AudioTrackList.clear();
68
88
 
69
 
 for(int32 track = toc.first_track; track <= toc.last_track; track++)
 
89
 for(unsigned disc = 0; disc < cdifs->size(); disc++)
70
90
 {
71
 
  if(!(toc.tracks[track].control & 0x4))
72
 
   AudioTrackList.push_back(track);
 
91
  TOC toc;
 
92
 
 
93
  (*cdifs)[disc]->ReadTOC(&toc);
 
94
 
 
95
  for(int32 track = toc.first_track; track <= toc.last_track; track++)
 
96
  {
 
97
   if(!(toc.tracks[track].control & 0x4))
 
98
   {
 
99
    AudioTrackList.push_back(AudioTrackInfo(disc, track, toc.tracks[track].lba, toc.tracks[track + 1].lba - 1));
 
100
   }
 
101
  }
73
102
 }
74
103
 
75
104
 if(!AudioTrackList.size())
78
107
  return(0);
79
108
 }
80
109
 
81
 
 
82
110
 CurrentATLI = 0;
83
 
 PlaySector = toc.tracks[AudioTrackList[CurrentATLI]].lba;
 
111
 PlaySector = AudioTrackList[CurrentATLI].lba;
84
112
 PlayMode = PLAYMODE_PLAY;   //STOP;
85
113
 
86
114
 {
97
125
 return(1);
98
126
}
99
127
 
100
 
static bool TestMagicCD(void)
 
128
static bool TestMagicCD(std::vector<CDIF *> *CDInterfaces)
101
129
{
102
 
 CD_TOC magic_toc;
103
 
 
104
 
 if(!CDIF_ReadTOC(&magic_toc))
105
 
  return(false);
106
 
 
107
 
 // If any audio track is found, return true.
108
 
 for(int32 track = magic_toc.first_track; track <= magic_toc.last_track; track++)
109
 
  if(!(magic_toc.tracks[track].control & 0x4))
110
 
   return(true);
 
130
 CDUtility::TOC magic_toc;
 
131
 
 
132
 for(unsigned i = 0; i < CDInterfaces->size(); i++)
 
133
 {
 
134
  (*CDInterfaces)[i]->ReadTOC(&magic_toc);
 
135
 
 
136
  // If any audio track is found, return true.
 
137
  for(int32 track = magic_toc.first_track; track <= magic_toc.last_track; track++)
 
138
   if(!(magic_toc.tracks[track].control & 0x4))
 
139
    return(true);
 
140
 }
111
141
 
112
142
 return(false);
113
143
}
119
149
  speex_resampler_destroy(resampler);
120
150
  resampler = NULL;
121
151
 }
 
152
 sin_lut.resize(0);
 
153
 sqrt_lut.resize(0);
122
154
}
123
155
 
124
156
static uint8 SubQBuf[3][0xC];
132
164
 for(int i = 0; i < 96; i++)
133
165
  sq[i >> 3] |= ((SubPWBuf[i] & 0x40) >> 6) << (7 - (i & 7));
134
166
 
135
 
 if(!CDIF_CheckSubQChecksum(sq))
 
167
 if(!subq_check_checksum(sq))
136
168
  puts("SubQ checksum error!");
137
169
 else
138
170
 {
147
179
static const int oversample = 1 << oversample_shift;
148
180
static const int oversample_mo = oversample - 1;
149
181
 
150
 
static double sqrt_lut[65536];
151
 
static double sin_lut[65536];
152
 
 
153
182
static void InitLUT(void)
154
183
{
155
 
   for(int i = 0; i < 65536; i++)
156
 
    sqrt_lut[i] = sqrt((double)i / 65536);
157
 
 
158
 
   for(int i = 0; i < 65536; i++)
159
 
    sin_lut[i] = sin((double)i * M_PI * 2 / 65536);
 
184
 sqrt_lut.resize(65536);
 
185
 sin_lut.resize(65536);
 
186
 
 
187
 for(int i = 0; i < 65536; i++)
 
188
  sqrt_lut[i] = sqrt((double)i / 65536);
 
189
 
 
190
 for(int i = 0; i < 65536; i++)
 
191
  sin_lut[i] = sin((double)i * M_PI * 2 / 65536);
160
192
}
161
193
 
162
194
static void Emulate(EmulateSpecStruct *espec)
168
200
 
169
201
 //printf("%d %d\n", toc.tracks[100].lba, AudioTrackList[AudioTrackList.size() - 1] + 1);
170
202
 
171
 
 if(PlaySector >= toc.tracks[100].lba || PlaySector >= toc.tracks[AudioTrackList[AudioTrackList.size() - 1] + 1].lba)
 
203
 if(PlaySector > AudioTrackList[CurrentATLI].final_lba)
172
204
 {
173
 
  puts("STOP");
174
 
  PlayMode = PLAYMODE_STOP;
 
205
  if((CurrentATLI + 1) < AudioTrackList.size())
 
206
   CurrentATLI++;
 
207
  else
 
208
  {
 
209
   CurrentATLI = 0;
 
210
   PlayMode = PLAYMODE_STOP;
 
211
  }   
175
212
 
176
 
  CurrentATLI = 0;
177
 
  PlaySector = toc.tracks[AudioTrackList[CurrentATLI]].lba;
178
 
 }
179
 
 else if(AudioTrackList[CurrentATLI] < toc.last_track && PlaySector >= toc.tracks[AudioTrackList[CurrentATLI] + 1].lba)
180
 
 {
181
 
  puts("CurrentATLI++");
182
 
  CurrentATLI++;
183
 
  PlaySector = toc.tracks[AudioTrackList[CurrentATLI]].lba;
 
213
  PlaySector = AudioTrackList[CurrentATLI].lba;
184
214
 }
185
215
 
186
 
 
187
216
 if(PlayMode == PLAYMODE_STOP || PlayMode == PLAYMODE_PAUSE)
188
217
 {
189
218
  //memset(CDDABuffer, 0, sizeof(CDDABuffer));
196
225
 }
197
226
 else
198
227
 {
199
 
  CDIF_ReadRawSector(sector_buffer, PlaySector);
 
228
  (*cdifs)[AudioTrackList[CurrentATLI].disc]->ReadRawSector(sector_buffer, PlaySector);
200
229
  GenSubQFromSubPW(sector_buffer + 2352);
201
230
 
202
231
  for(int i = 0; i < 588 * 2; i++)
228
257
   }
229
258
 
230
259
   in_len = ResampBufferPos;
231
 
   out_len = 2048;      // FIXME, real size.
 
260
   out_len = 524288;    // FIXME, real size.
232
261
 
233
262
   speex_resampler_process_interleaved_int(resampler, (const spx_int16_t *)ResampBuffer, &in_len, (spx_int16_t *)espec->SoundBuf, &out_len);
234
263
 
239
268
 
240
269
   ResampBufferPos -= in_len;
241
270
 
 
271
   //printf("%d\n", ResampBufferPos);
 
272
   assert((ResampBufferPos + 588) <= (sizeof(ResampBuffer) / sizeof(int16) / 2));
 
273
 
242
274
   espec->SoundBufSize = out_len;
243
275
  }
244
276
 }
253
285
  uint32 text_color = format.MakeColor(0xE0, 0xE0, 0xE0);
254
286
  uint32 text_shadow_color = format.MakeColor(0x20, 0x20, 0x20);
255
287
  uint32 cur_sector = PlaySector;
256
 
  int cur_track;
257
 
 
258
 
  cur_track = CDIF_FindTrackByLBA(cur_sector);
259
288
 
260
289
  espec->DisplayRect.x = 0;
261
290
  espec->DisplayRect.y = 0;
332
361
  }
333
362
  }
334
363
 
335
 
  trio_snprintf(tmpbuf, 256, "Track: %d/%d", cur_track, toc.last_track);
336
 
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
337
 
  pixels += 22 * espec->surface->pitch32;
338
 
 
339
 
  trio_snprintf(tmpbuf, 256, "Sector: %d/%d", cur_sector, toc.tracks[100].lba - 1);
340
 
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
341
 
  pixels += 22 * espec->surface->pitch32;
342
 
 
 
364
  {
 
365
   TOC toc;
 
366
 
 
367
   (*cdifs)[AudioTrackList[CurrentATLI].disc]->ReadTOC(&toc);
 
368
 
 
369
   trio_snprintf(tmpbuf, 256, "Disc: %d/%d", AudioTrackList[CurrentATLI].disc + 1, cdifs->size());
 
370
   DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
 
371
   pixels += 22 * espec->surface->pitch32;
 
372
 
 
373
   trio_snprintf(tmpbuf, 256, "Track: %d/%d", AudioTrackList[CurrentATLI].track, toc.last_track);
 
374
   DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
 
375
   pixels += 22 * espec->surface->pitch32;
 
376
 
 
377
   trio_snprintf(tmpbuf, 256, "Sector: %d/%d", cur_sector, toc.tracks[100].lba - 1);
 
378
   DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
 
379
   pixels += 22 * espec->surface->pitch32;
 
380
  }
343
381
 
344
382
  pixels += 22 * espec->surface->pitch32;
345
383
 
351
389
  //DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
352
390
  //pixels += 22 * espec->surface->pitch32;
353
391
 
354
 
  trio_snprintf(tmpbuf, 256, "Track: %d", BCD_TO_INT(SubQBuf[1][1]));
355
 
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
356
 
  pixels += 22 * espec->surface->pitch32;
357
 
 
358
 
  trio_snprintf(tmpbuf, 256, "Index: %d", BCD_TO_INT(SubQBuf[1][2]));
359
 
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
360
 
  pixels += 22 * espec->surface->pitch32;
361
 
 
362
 
 
363
 
  trio_snprintf(tmpbuf, 256, "Relative: %02d:%02d:%02d", BCD_TO_INT(SubQBuf[1][3]), BCD_TO_INT(SubQBuf[1][4]), BCD_TO_INT(SubQBuf[1][5]));
364
 
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
365
 
  pixels += 22 * espec->surface->pitch32;
366
 
 
367
 
  trio_snprintf(tmpbuf, 256, "Absolute: %02d:%02d:%02d", BCD_TO_INT(SubQBuf[1][7]), BCD_TO_INT(SubQBuf[1][8]), BCD_TO_INT(SubQBuf[1][9]));
 
392
  trio_snprintf(tmpbuf, 256, "Track: %d", BCD_to_U8(SubQBuf[1][1]));
 
393
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
 
394
  pixels += 22 * espec->surface->pitch32;
 
395
 
 
396
  trio_snprintf(tmpbuf, 256, "Index: %d", BCD_to_U8(SubQBuf[1][2]));
 
397
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
 
398
  pixels += 22 * espec->surface->pitch32;
 
399
 
 
400
 
 
401
  trio_snprintf(tmpbuf, 256, "Relative: %02d:%02d:%02d", BCD_to_U8(SubQBuf[1][3]), BCD_to_U8(SubQBuf[1][4]), BCD_to_U8(SubQBuf[1][5]));
 
402
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
 
403
  pixels += 22 * espec->surface->pitch32;
 
404
 
 
405
  trio_snprintf(tmpbuf, 256, "Absolute: %02d:%02d:%02d", BCD_to_U8(SubQBuf[1][7]), BCD_to_U8(SubQBuf[1][8]), BCD_to_U8(SubQBuf[1][9]));
368
406
  DrawTextTransShadow(pixels, espec->surface->pitch32 * 4, 320, (UTF8 *)tmpbuf, text_color, text_shadow_color, 0, MDFN_FONT_9x18_18x18);
369
407
  pixels += 22 * espec->surface->pitch32;
370
408
 }
380
418
 if(!(last_controller & 0x2) && (new_controller & 2)) // Stop
381
419
 {
382
420
  PlayMode = PLAYMODE_STOP;
383
 
  PlaySector = toc.tracks[AudioTrackList[CurrentATLI]].lba;
 
421
  PlaySector = AudioTrackList[CurrentATLI].lba;
384
422
 }
385
423
 
386
424
 if(!(last_controller & 0x4) && (new_controller & 4))
388
426
  if(CurrentATLI < (AudioTrackList.size() - 1))
389
427
   CurrentATLI++;
390
428
 
391
 
  PlaySector = toc.tracks[AudioTrackList[CurrentATLI]].lba;
 
429
  PlaySector = AudioTrackList[CurrentATLI].lba;
392
430
 }
393
431
 
394
432
 if(!(last_controller & 0x8) && (new_controller & 8))
396
434
  if(CurrentATLI)
397
435
   CurrentATLI--;
398
436
 
399
 
  PlaySector = toc.tracks[AudioTrackList[CurrentATLI]].lba;
400
 
 }
 
437
  PlaySector = AudioTrackList[CurrentATLI].lba;
 
438
 }
 
439
 
 
440
 if(!(last_controller & 0x10) && (new_controller & 0x10))
 
441
 {
 
442
  CurrentATLI = std::min<int>(CurrentATLI + 10, AudioTrackList.size() - 1);
 
443
  PlaySector = AudioTrackList[CurrentATLI].lba;
 
444
 }
 
445
 
 
446
 if(!(last_controller & 0x20) && (new_controller & 0x20))
 
447
 {
 
448
  CurrentATLI = std::max<int>(CurrentATLI - 10, 0);
 
449
  PlaySector = AudioTrackList[CurrentATLI].lba;
 
450
 }
 
451
 
401
452
 
402
453
 last_controller = new_controller;
403
454
}
466
517
 sizeof(PortInfo) / sizeof(InputPortInfoStruct),
467
518
 PortInfo
468
519
};
 
520
}
 
521
 
 
522
using namespace MDFN_IEN_CDPLAY;
469
523
 
470
524
MDFNGI EmulatedCDPlay =
471
525
{
507
561
 2,     // Number of output sound channels
508
562
};
509
563
 
510