1
0

WindacPlay.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "WindacPlay.h"
  2. #include "api__in_cdda.h"
  3. int WindacPlay::threadProc2()
  4. {
  5. while (1)
  6. {
  7. if (need_seek != -1)
  8. {
  9. current_sector = start_sector;
  10. current_sector += ((need_seek * 75) / 1000);
  11. bytes_in_sbuf = 0;
  12. line.outMod->Flush(need_seek);
  13. decode_pos_ms = need_seek;
  14. need_seek = -1;
  15. }
  16. if (!killswitch && bytes_in_sbuf <= 0 && current_sector.GetHSG() < end_sector.GetHSG())
  17. {
  18. if (!scsi->Get_DriveStatus().CDPresent)
  19. {
  20. //infos->error("No CD present!");
  21. PostMessage(line.hMainWindow, WM_WA_MPEG_EOF, 0, 0);
  22. return 0;
  23. }
  24. unsigned char *s = sbuf;
  25. while ((bytes_in_sbuf < buf_size*2352) && (current_sector.GetHSG() < end_sector.GetHSG()) && !killswitch)
  26. {
  27. int n = min((int)16, (int)(end_sector.GetHSG() - current_sector.GetHSG()));
  28. memset(s, 0, n*2352);
  29. scsi->ReadCDDA(current_sector, n, s);
  30. while (!scsi->WaitCDDA() && !killswitch) Sleep(66);
  31. bytes_in_sbuf += n * 2352;
  32. s += n * 2352;
  33. current_sector += n;
  34. }
  35. }
  36. if (!bytes_in_sbuf && !killswitch)
  37. {
  38. //wait for output to be finished
  39. line.outMod->Write(NULL, 0);
  40. while (!killswitch && line.outMod->IsPlaying()) Sleep(10);
  41. if (!killswitch)
  42. PostMessage(line.hMainWindow, WM_WA_MPEG_EOF, 0, 0);
  43. return 0;
  44. }
  45. if (killswitch) return 0;
  46. char sample_buffer[576*4*2] = {0};
  47. int bytes = sizeof(sample_buffer) / 2; // enough room for dsp bullcrap
  48. bytes = min((int)bytes_in_sbuf, (int)bytes);
  49. memcpy(sample_buffer, sbuf, bytes);
  50. if (bytes_in_sbuf > bytes) memcpy(sbuf, sbuf + bytes, bytes_in_sbuf - bytes);
  51. bytes_in_sbuf -= bytes;
  52. int obytes = bytes;
  53. line.VSAAddPCMData(sample_buffer, g_nch, 16, line.outMod->GetWrittenTime() /*decode_pos_ms*/);
  54. line.SAAddPCMData(sample_buffer, g_nch, 16, line.outMod->GetWrittenTime() /*decode_pos_ms*/);
  55. if (line.dsp_isactive())
  56. bytes = line.dsp_dosamples((short *)sample_buffer, bytes / g_nch / 2, 16, g_nch, 44100) * (g_nch * 2);
  57. while (line.outMod->CanWrite() < bytes && !killswitch) Sleep(66);
  58. if (killswitch) return 0;
  59. line.outMod->Write(sample_buffer, bytes);
  60. decode_pos_ms += ((obytes / g_nch / 2) * 1000) / 44100;
  61. }
  62. return 0;
  63. }
  64. void WindacPlay::stop()
  65. {
  66. if (hThread)
  67. {
  68. killswitch = 1;
  69. WaitForSingleObject(hThread, INFINITE);
  70. }
  71. if (needsToClose)
  72. {
  73. needsToClose = false;
  74. }
  75. line.outMod->Close();
  76. }
  77. int WindacPlay::open(wchar_t drive, int track) //called by winampGetExtendedRead
  78. {
  79. g_drive = drive;
  80. if (!inited && !LoadASPI())
  81. {
  82. g_drive = 0;
  83. return 1;
  84. }
  85. inited = 1;
  86. int drivenum = 0;
  87. getTrackInfos(&drivenum, (char)drive);
  88. m_pMapDrive = new CMapDrive(TRUE);
  89. int nbdrives = m_pMapDrive->GetMaxDrives();
  90. if (!nbdrives) return 0;
  91. int host = -1, id = -1, lun = -1;
  92. if (getSCSIIDFromDrive((char)drive, &host, &id, &lun))
  93. {
  94. int found = 0;
  95. for (int i = 0;i < nbdrives;i++)
  96. {
  97. drive_info = m_pMapDrive->GetInfo(i);
  98. if (drive_info.HostAdapterNumber == host && drive_info.ID == id && drive_info.LUN == lun)
  99. {
  100. found = 1;
  101. break;
  102. }
  103. }
  104. if (!found)
  105. {
  106. s_last_error = "Drive not found";
  107. return 1;
  108. }
  109. }
  110. else
  111. {
  112. // can't figure out the SCSI ID, oh well, try the gay method
  113. TDriveInfo *tdi = &m_pMapDrive->GetInfo(drivenum);
  114. if (!tdi)
  115. {
  116. s_last_error = "Drive not found";
  117. return 1;
  118. }
  119. drive_info = *tdi;
  120. }
  121. scsi = new CSCSICD((char)drive, drive_info);
  122. TDriveStatus status = scsi->Get_DriveStatus();
  123. if (!status.CDPresent)
  124. {
  125. s_last_error = "CD not present";
  126. //infos->warning("No CD present!");
  127. g_drive=0;
  128. return 1;
  129. }
  130. TTrackList track_info = {0};
  131. track_info.TrackNummer = track;
  132. scsi->ReadTrackInfo(track_info);
  133. if (track_info.Flags.DataTrack)
  134. {
  135. s_last_error = "Cannot play track";
  136. //infos->warning("Can't play data tracks");
  137. g_drive=0;
  138. return 1;
  139. }
  140. start_sector = track_info.StartSektor;
  141. current_sector = start_sector;
  142. end_sector = start_sector;
  143. slength = track_info.Laenge;
  144. end_sector += slength;
  145. g_playlength = (slength / 75) * 1000;
  146. g_nch = track_info.Flags.AudioChannels;
  147. g_srate = 44100;
  148. g_bps = 16;
  149. scsi->PrepareCDDA();
  150. if (!sbuf)
  151. sbuf = (unsigned char *)malloc(2352 * buf_size);
  152. bytes_in_sbuf = 0;
  153. last_eject_scan = 0;
  154. return 0;
  155. }
  156. int WindacPlay::play(wchar_t drive, int track) //called by winamp2's normal(old) play() interface
  157. {
  158. if (open(drive, track)) return 1;
  159. // do this here as it helps to prevent an audio glitch on first playback and volume is set low
  160. setvolume(a_v, a_p);
  161. int maxlat = line.outMod->Open(44100, g_nch, 16, -1, -1);
  162. if (maxlat < 0)
  163. {
  164. g_drive=0;
  165. return 1;
  166. }
  167. line.SetInfo(1411, 44, g_nch, 1);
  168. line.SAVSAInit(maxlat, 44100);
  169. line.VSASetInfo(g_nch, 44100);
  170. line.is_seekable = 1;
  171. killswitch = 0;
  172. DWORD thread_id;
  173. hThread = CreateThread(NULL, NULL, &threadProc, (LPVOID)this, NULL, &thread_id);
  174. SetThreadPriority(hThread, AGAVE_API_CONFIG->GetInt(playbackConfigGroupGUID, L"priority", THREAD_PRIORITY_HIGHEST));
  175. //open the device thru MCI (for getfileinfo to work properly)
  176. g_playtrack = track;
  177. needsToClose = true;
  178. return 0;
  179. }
  180. int WindacPlay::read(char *dest, int len, int *killswitch) //called by winampGetExtendedRead_getData
  181. {
  182. int l = 0;
  183. while (l < len && !*killswitch)
  184. {
  185. if (!*killswitch && bytes_in_sbuf <= 0 && current_sector.GetHSG() < end_sector.GetHSG())
  186. {
  187. //scan for ejected CD only every 2 seconds
  188. if (last_eject_scan + 5000 < GetTickCount())
  189. {
  190. int cnt = 5;
  191. while (!scsi->Get_DriveStatus().CDPresent && cnt--)
  192. {
  193. Sleep(100);
  194. }
  195. if (cnt < 0 && !scsi->Get_DriveStatus().CDPresent)
  196. {
  197. //infos->error("No CD present!");
  198. return -1;
  199. }
  200. last_eject_scan = GetTickCount();
  201. }
  202. unsigned char *s = sbuf;
  203. while ((bytes_in_sbuf < buf_size*2352) && (current_sector.GetHSG() < end_sector.GetHSG()))
  204. {
  205. int n = min((int)16, (int)(end_sector.GetHSG() - current_sector.GetHSG()));
  206. memset(s, 0, n*2352);
  207. scsi->ReadCDDA(current_sector, n, s);
  208. while (!scsi->WaitCDDA() && !*killswitch) Sleep(66);
  209. if (*killswitch) break;
  210. bytes_in_sbuf += n * 2352;
  211. s += n * 2352;
  212. current_sector += n;
  213. }
  214. }
  215. if (!bytes_in_sbuf) break;
  216. int bytes = min(bytes_in_sbuf, len - l);
  217. memcpy(dest + l, sbuf, bytes);
  218. if (bytes_in_sbuf > bytes) memcpy(sbuf, sbuf + bytes, bytes_in_sbuf - bytes);
  219. bytes_in_sbuf -= bytes;
  220. l += bytes;
  221. }
  222. return l;
  223. }
  224. void WindacPlay::getTrackInfos(int *drivenum, char driveletter)
  225. {
  226. //finds first cdrom drive letter
  227. char firstcd = 'D';
  228. {
  229. DWORD drives = GetLogicalDrives();
  230. int nb = 0;
  231. for (int drivemask = 0; (drivemask < 32) && (nb < 4); drivemask++)
  232. {
  233. if (drives&(1 << drivemask))
  234. {
  235. wchar_t tmp[16] = {0};
  236. wsprintf(tmp, L"%c:\\", L'A' + drivemask);
  237. if (GetDriveType(tmp) == DRIVE_CDROM)
  238. {
  239. firstcd = 'A' + drivemask;
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. *drivenum = driveletter - (unsigned char)firstcd;
  246. }
  247. WindacPlay::WindacPlay()
  248. {
  249. scsi = NULL;
  250. sbuf = NULL;
  251. m_pMapDrive = NULL;
  252. buf_size = 64; //make it configitem
  253. hThread = NULL;
  254. decode_pos_ms = 0;
  255. inited = 0;
  256. need_seek = -1;
  257. needsToClose = false;
  258. }
  259. WindacPlay::~WindacPlay()
  260. {
  261. if (scsi)
  262. {
  263. scsi->FinishCDDA();
  264. delete(scsi);
  265. }
  266. delete(m_pMapDrive);
  267. free(sbuf);
  268. if (inited) FreeASPI();
  269. if (needsToClose)
  270. {
  271. needsToClose = false;
  272. }
  273. }