The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2115 lines
65KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #define WIN32_LEAN_AND_MEAN
  24. #define STRICT
  25. #ifdef _MSC_VER
  26. #pragma warning (disable: 4514)
  27. #pragma warning (push)
  28. #endif
  29. #include "win32_headers.h"
  30. #include <stddef.h>
  31. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  32. BEGIN_JUCE_NAMESPACE
  33. #include "../../../src/juce_appframework/audio/audio_file_formats/juce_AudioCDReader.h"
  34. #include "../../../src/juce_appframework/events/juce_Timer.h"
  35. #include "../../../src/juce_appframework/application/juce_DeletedAtShutdown.h"
  36. #ifdef _MSC_VER
  37. #pragma warning (pop)
  38. #endif
  39. //***************************************************************************
  40. // %%% TARGET STATUS VALUES %%%
  41. //***************************************************************************
  42. #define STATUS_GOOD 0x00 // Status Good
  43. #define STATUS_CHKCOND 0x02 // Check Condition
  44. #define STATUS_CONDMET 0x04 // Condition Met
  45. #define STATUS_BUSY 0x08 // Busy
  46. #define STATUS_INTERM 0x10 // Intermediate
  47. #define STATUS_INTCDMET 0x14 // Intermediate-condition met
  48. #define STATUS_RESCONF 0x18 // Reservation conflict
  49. #define STATUS_COMTERM 0x22 // Command Terminated
  50. #define STATUS_QFULL 0x28 // Queue full
  51. //***************************************************************************
  52. // %%% SCSI MISCELLANEOUS EQUATES %%%
  53. //***************************************************************************
  54. #define MAXLUN 7 // Maximum Logical Unit Id
  55. #define MAXTARG 7 // Maximum Target Id
  56. #define MAX_SCSI_LUNS 64 // Maximum Number of SCSI LUNs
  57. #define MAX_NUM_HA 8 // Maximum Number of SCSI HA's
  58. //***************************************************************************
  59. // %%% Commands for all Device Types %%%
  60. //***************************************************************************
  61. #define SCSI_CHANGE_DEF 0x40 // Change Definition (Optional)
  62. #define SCSI_COMPARE 0x39 // Compare (O)
  63. #define SCSI_COPY 0x18 // Copy (O)
  64. #define SCSI_COP_VERIFY 0x3A // Copy and Verify (O)
  65. #define SCSI_INQUIRY 0x12 // Inquiry (MANDATORY)
  66. #define SCSI_LOG_SELECT 0x4C // Log Select (O)
  67. #define SCSI_LOG_SENSE 0x4D // Log Sense (O)
  68. #define SCSI_MODE_SEL6 0x15 // Mode Select 6-byte (Device Specific)
  69. #define SCSI_MODE_SEL10 0x55 // Mode Select 10-byte (Device Specific)
  70. #define SCSI_MODE_SEN6 0x1A // Mode Sense 6-byte (Device Specific)
  71. #define SCSI_MODE_SEN10 0x5A // Mode Sense 10-byte (Device Specific)
  72. #define SCSI_READ_BUFF 0x3C // Read Buffer (O)
  73. #define SCSI_REQ_SENSE 0x03 // Request Sense (MANDATORY)
  74. #define SCSI_SEND_DIAG 0x1D // Send Diagnostic (O)
  75. #define SCSI_TST_U_RDY 0x00 // Test Unit Ready (MANDATORY)
  76. #define SCSI_WRITE_BUFF 0x3B // Write Buffer (O)
  77. //***************************************************************************
  78. // %%% Commands Unique to Direct Access Devices %%%
  79. //***************************************************************************
  80. #define SCSI_COMPARE 0x39 // Compare (O)
  81. #define SCSI_FORMAT 0x04 // Format Unit (MANDATORY)
  82. #define SCSI_LCK_UN_CAC 0x36 // Lock Unlock Cache (O)
  83. #define SCSI_PREFETCH 0x34 // Prefetch (O)
  84. #define SCSI_MED_REMOVL 0x1E // Prevent/Allow medium Removal (O)
  85. #define SCSI_READ6 0x08 // Read 6-byte (MANDATORY)
  86. #define SCSI_READ10 0x28 // Read 10-byte (MANDATORY)
  87. #define SCSI_RD_CAPAC 0x25 // Read Capacity (MANDATORY)
  88. #define SCSI_RD_DEFECT 0x37 // Read Defect Data (O)
  89. #define SCSI_READ_LONG 0x3E // Read Long (O)
  90. #define SCSI_REASS_BLK 0x07 // Reassign Blocks (O)
  91. #define SCSI_RCV_DIAG 0x1C // Receive Diagnostic Results (O)
  92. #define SCSI_RELEASE 0x17 // Release Unit (MANDATORY)
  93. #define SCSI_REZERO 0x01 // Rezero Unit (O)
  94. #define SCSI_SRCH_DAT_E 0x31 // Search Data Equal (O)
  95. #define SCSI_SRCH_DAT_H 0x30 // Search Data High (O)
  96. #define SCSI_SRCH_DAT_L 0x32 // Search Data Low (O)
  97. #define SCSI_SEEK6 0x0B // Seek 6-Byte (O)
  98. #define SCSI_SEEK10 0x2B // Seek 10-Byte (O)
  99. #define SCSI_SEND_DIAG 0x1D // Send Diagnostics (MANDATORY)
  100. #define SCSI_SET_LIMIT 0x33 // Set Limits (O)
  101. #define SCSI_START_STP 0x1B // Start/Stop Unit (O)
  102. #define SCSI_SYNC_CACHE 0x35 // Synchronize Cache (O)
  103. #define SCSI_VERIFY 0x2F // Verify (O)
  104. #define SCSI_WRITE6 0x0A // Write 6-Byte (MANDATORY)
  105. #define SCSI_WRITE10 0x2A // Write 10-Byte (MANDATORY)
  106. #define SCSI_WRT_VERIFY 0x2E // Write and Verify (O)
  107. #define SCSI_WRITE_LONG 0x3F // Write Long (O)
  108. #define SCSI_WRITE_SAME 0x41 // Write Same (O)
  109. //***************************************************************************
  110. // %%% Commands Unique to Sequential Access Devices %%%
  111. //***************************************************************************
  112. #define SCSI_ERASE 0x19 // Erase (MANDATORY)
  113. #define SCSI_LOAD_UN 0x1b // Load/Unload (O)
  114. #define SCSI_LOCATE 0x2B // Locate (O)
  115. #define SCSI_RD_BLK_LIM 0x05 // Read Block Limits (MANDATORY)
  116. #define SCSI_READ_POS 0x34 // Read Position (O)
  117. #define SCSI_READ_REV 0x0F // Read Reverse (O)
  118. #define SCSI_REC_BF_DAT 0x14 // Recover Buffer Data (O)
  119. #define SCSI_RESERVE 0x16 // Reserve Unit (MANDATORY)
  120. #define SCSI_REWIND 0x01 // Rewind (MANDATORY)
  121. #define SCSI_SPACE 0x11 // Space (MANDATORY)
  122. #define SCSI_VERIFY_T 0x13 // Verify (Tape) (O)
  123. #define SCSI_WRT_FILE 0x10 // Write Filemarks (MANDATORY)
  124. //***************************************************************************
  125. // %%% Commands Unique to Printer Devices %%%
  126. //***************************************************************************
  127. #define SCSI_PRINT 0x0A // Print (MANDATORY)
  128. #define SCSI_SLEW_PNT 0x0B // Slew and Print (O)
  129. #define SCSI_STOP_PNT 0x1B // Stop Print (O)
  130. #define SCSI_SYNC_BUFF 0x10 // Synchronize Buffer (O)
  131. //***************************************************************************
  132. // %%% Commands Unique to Processor Devices %%%
  133. //***************************************************************************
  134. #define SCSI_RECEIVE 0x08 // Receive (O)
  135. #define SCSI_SEND 0x0A // Send (O)
  136. //***************************************************************************
  137. // %%% Commands Unique to Write-Once Devices %%%
  138. //***************************************************************************
  139. #define SCSI_MEDIUM_SCN 0x38 // Medium Scan (O)
  140. #define SCSI_SRCHDATE10 0x31 // Search Data Equal 10-Byte (O)
  141. #define SCSI_SRCHDATE12 0xB1 // Search Data Equal 12-Byte (O)
  142. #define SCSI_SRCHDATH10 0x30 // Search Data High 10-Byte (O)
  143. #define SCSI_SRCHDATH12 0xB0 // Search Data High 12-Byte (O)
  144. #define SCSI_SRCHDATL10 0x32 // Search Data Low 10-Byte (O)
  145. #define SCSI_SRCHDATL12 0xB2 // Search Data Low 12-Byte (O)
  146. #define SCSI_SET_LIM_10 0x33 // Set Limits 10-Byte (O)
  147. #define SCSI_SET_LIM_12 0xB3 // Set Limits 10-Byte (O)
  148. #define SCSI_VERIFY10 0x2F // Verify 10-Byte (O)
  149. #define SCSI_VERIFY12 0xAF // Verify 12-Byte (O)
  150. #define SCSI_WRITE12 0xAA // Write 12-Byte (O)
  151. #define SCSI_WRT_VER10 0x2E // Write and Verify 10-Byte (O)
  152. #define SCSI_WRT_VER12 0xAE // Write and Verify 12-Byte (O)
  153. //***************************************************************************
  154. // %%% Commands Unique to CD-ROM Devices %%%
  155. //***************************************************************************
  156. #define SCSI_PLAYAUD_10 0x45 // Play Audio 10-Byte (O)
  157. #define SCSI_PLAYAUD_12 0xA5 // Play Audio 12-Byte 12-Byte (O)
  158. #define SCSI_PLAYAUDMSF 0x47 // Play Audio MSF (O)
  159. #define SCSI_PLAYA_TKIN 0x48 // Play Audio Track/Index (O)
  160. #define SCSI_PLYTKREL10 0x49 // Play Track Relative 10-Byte (O)
  161. #define SCSI_PLYTKREL12 0xA9 // Play Track Relative 12-Byte (O)
  162. #define SCSI_READCDCAP 0x25 // Read CD-ROM Capacity (MANDATORY)
  163. #define SCSI_READHEADER 0x44 // Read Header (O)
  164. #define SCSI_SUBCHANNEL 0x42 // Read Subchannel (O)
  165. #define SCSI_READ_TOC 0x43 // Read TOC (O)
  166. //***************************************************************************
  167. // %%% Commands Unique to Scanner Devices %%%
  168. //***************************************************************************
  169. #define SCSI_GETDBSTAT 0x34 // Get Data Buffer Status (O)
  170. #define SCSI_GETWINDOW 0x25 // Get Window (O)
  171. #define SCSI_OBJECTPOS 0x31 // Object Postion (O)
  172. #define SCSI_SCAN 0x1B // Scan (O)
  173. #define SCSI_SETWINDOW 0x24 // Set Window (MANDATORY)
  174. //***************************************************************************
  175. // %%% Commands Unique to Optical Memory Devices %%%
  176. //***************************************************************************
  177. #define SCSI_UpdateBlk 0x3D // Update Block (O)
  178. //***************************************************************************
  179. // %%% Commands Unique to Medium Changer Devices %%%
  180. //***************************************************************************
  181. #define SCSI_EXCHMEDIUM 0xA6 // Exchange Medium (O)
  182. #define SCSI_INITELSTAT 0x07 // Initialize Element Status (O)
  183. #define SCSI_POSTOELEM 0x2B // Position to Element (O)
  184. #define SCSI_REQ_VE_ADD 0xB5 // Request Volume Element Address (O)
  185. #define SCSI_SENDVOLTAG 0xB6 // Send Volume Tag (O)
  186. //***************************************************************************
  187. // %%% Commands Unique to Communication Devices %%%
  188. //***************************************************************************
  189. #define SCSI_GET_MSG_6 0x08 // Get Message 6-Byte (MANDATORY)
  190. #define SCSI_GET_MSG_10 0x28 // Get Message 10-Byte (O)
  191. #define SCSI_GET_MSG_12 0xA8 // Get Message 12-Byte (O)
  192. #define SCSI_SND_MSG_6 0x0A // Send Message 6-Byte (MANDATORY)
  193. #define SCSI_SND_MSG_10 0x2A // Send Message 10-Byte (O)
  194. #define SCSI_SND_MSG_12 0xAA // Send Message 12-Byte (O)
  195. //***************************************************************************
  196. // %%% Request Sense Data Format %%%
  197. //***************************************************************************
  198. typedef struct {
  199. BYTE ErrorCode; // Error Code (70H or 71H)
  200. BYTE SegmentNum; // Number of current segment descriptor
  201. BYTE SenseKey; // Sense Key(See bit definitions too)
  202. BYTE InfoByte0; // Information MSB
  203. BYTE InfoByte1; // Information MID
  204. BYTE InfoByte2; // Information MID
  205. BYTE InfoByte3; // Information LSB
  206. BYTE AddSenLen; // Additional Sense Length
  207. BYTE ComSpecInf0; // Command Specific Information MSB
  208. BYTE ComSpecInf1; // Command Specific Information MID
  209. BYTE ComSpecInf2; // Command Specific Information MID
  210. BYTE ComSpecInf3; // Command Specific Information LSB
  211. BYTE AddSenseCode; // Additional Sense Code
  212. BYTE AddSenQual; // Additional Sense Code Qualifier
  213. BYTE FieldRepUCode; // Field Replaceable Unit Code
  214. BYTE SenKeySpec15; // Sense Key Specific 15th byte
  215. BYTE SenKeySpec16; // Sense Key Specific 16th byte
  216. BYTE SenKeySpec17; // Sense Key Specific 17th byte
  217. BYTE AddSenseBytes; // Additional Sense Bytes
  218. } SENSE_DATA_FMT;
  219. //***************************************************************************
  220. // %%% REQUEST SENSE ERROR CODE %%%
  221. //***************************************************************************
  222. #define SERROR_CURRENT 0x70 // Current Errors
  223. #define SERROR_DEFERED 0x71 // Deferred Errors
  224. //***************************************************************************
  225. // %%% REQUEST SENSE BIT DEFINITIONS %%%
  226. //***************************************************************************
  227. #define SENSE_VALID 0x80 // Byte 0 Bit 7
  228. #define SENSE_FILEMRK 0x80 // Byte 2 Bit 7
  229. #define SENSE_EOM 0x40 // Byte 2 Bit 6
  230. #define SENSE_ILI 0x20 // Byte 2 Bit 5
  231. //***************************************************************************
  232. // %%% REQUEST SENSE SENSE KEY DEFINITIONS %%%
  233. //***************************************************************************
  234. #define KEY_NOSENSE 0x00 // No Sense
  235. #define KEY_RECERROR 0x01 // Recovered Error
  236. #define KEY_NOTREADY 0x02 // Not Ready
  237. #define KEY_MEDIUMERR 0x03 // Medium Error
  238. #define KEY_HARDERROR 0x04 // Hardware Error
  239. #define KEY_ILLGLREQ 0x05 // Illegal Request
  240. #define KEY_UNITATT 0x06 // Unit Attention
  241. #define KEY_DATAPROT 0x07 // Data Protect
  242. #define KEY_BLANKCHK 0x08 // Blank Check
  243. #define KEY_VENDSPEC 0x09 // Vendor Specific
  244. #define KEY_COPYABORT 0x0A // Copy Abort
  245. #define KEY_EQUAL 0x0C // Equal (Search)
  246. #define KEY_VOLOVRFLW 0x0D // Volume Overflow
  247. #define KEY_MISCOMP 0x0E // Miscompare (Search)
  248. #define KEY_RESERVED 0x0F // Reserved
  249. //***************************************************************************
  250. // %%% PERIPHERAL DEVICE TYPE DEFINITIONS %%%
  251. //***************************************************************************
  252. #define DTYPE_DASD 0x00 // Disk Device
  253. #define DTYPE_SEQD 0x01 // Tape Device
  254. #define DTYPE_PRNT 0x02 // Printer
  255. #define DTYPE_PROC 0x03 // Processor
  256. #define DTYPE_WORM 0x04 // Write-once read-multiple
  257. #define DTYPE_CROM 0x05 // CD-ROM device
  258. #define DTYPE_SCAN 0x06 // Scanner device
  259. #define DTYPE_OPTI 0x07 // Optical memory device
  260. #define DTYPE_JUKE 0x08 // Medium Changer device
  261. #define DTYPE_COMM 0x09 // Communications device
  262. #define DTYPE_RESL 0x0A // Reserved (low)
  263. #define DTYPE_RESH 0x1E // Reserved (high)
  264. #define DTYPE_UNKNOWN 0x1F // Unknown or no device type
  265. //***************************************************************************
  266. // %%% ANSI APPROVED VERSION DEFINITIONS %%%
  267. //***************************************************************************
  268. #define ANSI_MAYBE 0x0 // Device may or may not be ANSI approved stand
  269. #define ANSI_SCSI1 0x1 // Device complies to ANSI X3.131-1986 (SCSI-1)
  270. #define ANSI_SCSI2 0x2 // Device complies to SCSI-2
  271. #define ANSI_RESLO 0x3 // Reserved (low)
  272. #define ANSI_RESHI 0x7 // Reserved (high)
  273. typedef struct
  274. {
  275. USHORT Length;
  276. UCHAR ScsiStatus;
  277. UCHAR PathId;
  278. UCHAR TargetId;
  279. UCHAR Lun;
  280. UCHAR CdbLength;
  281. UCHAR SenseInfoLength;
  282. UCHAR DataIn;
  283. ULONG DataTransferLength;
  284. ULONG TimeOutValue;
  285. ULONG DataBufferOffset;
  286. ULONG SenseInfoOffset;
  287. UCHAR Cdb[16];
  288. } SCSI_PASS_THROUGH, *PSCSI_PASS_THROUGH;
  289. typedef struct
  290. {
  291. USHORT Length;
  292. UCHAR ScsiStatus;
  293. UCHAR PathId;
  294. UCHAR TargetId;
  295. UCHAR Lun;
  296. UCHAR CdbLength;
  297. UCHAR SenseInfoLength;
  298. UCHAR DataIn;
  299. ULONG DataTransferLength;
  300. ULONG TimeOutValue;
  301. PVOID DataBuffer;
  302. ULONG SenseInfoOffset;
  303. UCHAR Cdb[16];
  304. } SCSI_PASS_THROUGH_DIRECT, *PSCSI_PASS_THROUGH_DIRECT;
  305. typedef struct
  306. {
  307. SCSI_PASS_THROUGH_DIRECT spt;
  308. ULONG Filler;
  309. UCHAR ucSenseBuf[32];
  310. } SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, *PSCSI_PASS_THROUGH_DIRECT_WITH_BUFFER;
  311. typedef struct
  312. {
  313. ULONG Length;
  314. UCHAR PortNumber;
  315. UCHAR PathId;
  316. UCHAR TargetId;
  317. UCHAR Lun;
  318. } SCSI_ADDRESS, *PSCSI_ADDRESS;
  319. #define METHOD_BUFFERED 0
  320. #define METHOD_IN_DIRECT 1
  321. #define METHOD_OUT_DIRECT 2
  322. #define METHOD_NEITHER 3
  323. #define FILE_ANY_ACCESS 0
  324. #ifndef FILE_READ_ACCESS
  325. #define FILE_READ_ACCESS (0x0001)
  326. #endif
  327. #ifndef FILE_WRITE_ACCESS
  328. #define FILE_WRITE_ACCESS (0x0002)
  329. #endif
  330. #define IOCTL_SCSI_BASE 0x00000004
  331. #define SCSI_IOCTL_DATA_OUT 0
  332. #define SCSI_IOCTL_DATA_IN 1
  333. #define SCSI_IOCTL_DATA_UNSPECIFIED 2
  334. #define CTL_CODE2( DevType, Function, Method, Access ) ( \
  335. ((DevType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
  336. )
  337. #define IOCTL_SCSI_PASS_THROUGH CTL_CODE2( IOCTL_SCSI_BASE, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS )
  338. #define IOCTL_SCSI_GET_CAPABILITIES CTL_CODE2( IOCTL_SCSI_BASE, 0x0404, METHOD_BUFFERED, FILE_ANY_ACCESS)
  339. #define IOCTL_SCSI_PASS_THROUGH_DIRECT CTL_CODE2( IOCTL_SCSI_BASE, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS )
  340. #define IOCTL_SCSI_GET_ADDRESS CTL_CODE2( IOCTL_SCSI_BASE, 0x0406, METHOD_BUFFERED, FILE_ANY_ACCESS )
  341. #define SENSE_LEN 14
  342. #define SRB_DIR_SCSI 0x00
  343. #define SRB_POSTING 0x01
  344. #define SRB_ENABLE_RESIDUAL_COUNT 0x04
  345. #define SRB_DIR_IN 0x08
  346. #define SRB_DIR_OUT 0x10
  347. #define SRB_EVENT_NOTIFY 0x40
  348. #define RESIDUAL_COUNT_SUPPORTED 0x02
  349. #define MAX_SRB_TIMEOUT 1080001u
  350. #define DEFAULT_SRB_TIMEOUT 1080001u
  351. #define SC_HA_INQUIRY 0x00
  352. #define SC_GET_DEV_TYPE 0x01
  353. #define SC_EXEC_SCSI_CMD 0x02
  354. #define SC_ABORT_SRB 0x03
  355. #define SC_RESET_DEV 0x04
  356. #define SC_SET_HA_PARMS 0x05
  357. #define SC_GET_DISK_INFO 0x06
  358. #define SC_RESCAN_SCSI_BUS 0x07
  359. #define SC_GETSET_TIMEOUTS 0x08
  360. #define SS_PENDING 0x00
  361. #define SS_COMP 0x01
  362. #define SS_ABORTED 0x02
  363. #define SS_ABORT_FAIL 0x03
  364. #define SS_ERR 0x04
  365. #define SS_INVALID_CMD 0x80
  366. #define SS_INVALID_HA 0x81
  367. #define SS_NO_DEVICE 0x82
  368. #define SS_INVALID_SRB 0xE0
  369. #define SS_OLD_MANAGER 0xE1
  370. #define SS_BUFFER_ALIGN 0xE1
  371. #define SS_ILLEGAL_MODE 0xE2
  372. #define SS_NO_ASPI 0xE3
  373. #define SS_FAILED_INIT 0xE4
  374. #define SS_ASPI_IS_BUSY 0xE5
  375. #define SS_BUFFER_TO_BIG 0xE6
  376. #define SS_BUFFER_TOO_BIG 0xE6
  377. #define SS_MISMATCHED_COMPONENTS 0xE7
  378. #define SS_NO_ADAPTERS 0xE8
  379. #define SS_INSUFFICIENT_RESOURCES 0xE9
  380. #define SS_ASPI_IS_SHUTDOWN 0xEA
  381. #define SS_BAD_INSTALL 0xEB
  382. #define HASTAT_OK 0x00
  383. #define HASTAT_SEL_TO 0x11
  384. #define HASTAT_DO_DU 0x12
  385. #define HASTAT_BUS_FREE 0x13
  386. #define HASTAT_PHASE_ERR 0x14
  387. #define HASTAT_TIMEOUT 0x09
  388. #define HASTAT_COMMAND_TIMEOUT 0x0B
  389. #define HASTAT_MESSAGE_REJECT 0x0D
  390. #define HASTAT_BUS_RESET 0x0E
  391. #define HASTAT_PARITY_ERROR 0x0F
  392. #define HASTAT_REQUEST_SENSE_FAILED 0x10
  393. #define PACKED
  394. #pragma pack(1)
  395. typedef struct
  396. {
  397. BYTE SRB_Cmd;
  398. BYTE SRB_Status;
  399. BYTE SRB_HaID;
  400. BYTE SRB_Flags;
  401. DWORD SRB_Hdr_Rsvd;
  402. BYTE HA_Count;
  403. BYTE HA_SCSI_ID;
  404. BYTE HA_ManagerId[16];
  405. BYTE HA_Identifier[16];
  406. BYTE HA_Unique[16];
  407. WORD HA_Rsvd1;
  408. BYTE pad[20];
  409. } PACKED SRB_HAInquiry, *PSRB_HAInquiry, FAR *LPSRB_HAInquiry;
  410. typedef struct
  411. {
  412. BYTE SRB_Cmd;
  413. BYTE SRB_Status;
  414. BYTE SRB_HaID;
  415. BYTE SRB_Flags;
  416. DWORD SRB_Hdr_Rsvd;
  417. BYTE SRB_Target;
  418. BYTE SRB_Lun;
  419. BYTE SRB_DeviceType;
  420. BYTE SRB_Rsvd1;
  421. BYTE pad[68];
  422. } PACKED SRB_GDEVBlock, *PSRB_GDEVBlock, FAR *LPSRB_GDEVBlock;
  423. typedef struct
  424. {
  425. BYTE SRB_Cmd;
  426. BYTE SRB_Status;
  427. BYTE SRB_HaID;
  428. BYTE SRB_Flags;
  429. DWORD SRB_Hdr_Rsvd;
  430. BYTE SRB_Target;
  431. BYTE SRB_Lun;
  432. WORD SRB_Rsvd1;
  433. DWORD SRB_BufLen;
  434. BYTE FAR *SRB_BufPointer;
  435. BYTE SRB_SenseLen;
  436. BYTE SRB_CDBLen;
  437. BYTE SRB_HaStat;
  438. BYTE SRB_TargStat;
  439. VOID FAR *SRB_PostProc;
  440. BYTE SRB_Rsvd2[20];
  441. BYTE CDBByte[16];
  442. BYTE SenseArea[SENSE_LEN+2];
  443. } PACKED SRB_ExecSCSICmd, *PSRB_ExecSCSICmd, FAR *LPSRB_ExecSCSICmd;
  444. typedef struct
  445. {
  446. BYTE SRB_Cmd;
  447. BYTE SRB_Status;
  448. BYTE SRB_HaId;
  449. BYTE SRB_Flags;
  450. DWORD SRB_Hdr_Rsvd;
  451. } PACKED SRB, *PSRB, FAR *LPSRB;
  452. #pragma pack()
  453. //==============================================================================
  454. struct CDDeviceInfo
  455. {
  456. char vendor[9];
  457. char productId[17];
  458. char rev[5];
  459. char vendorSpec[21];
  460. BYTE ha;
  461. BYTE tgt;
  462. BYTE lun;
  463. char scsiDriveLetter; // will be 0 if not using scsi
  464. };
  465. //==============================================================================
  466. class CDReadBuffer
  467. {
  468. public:
  469. int startFrame;
  470. int numFrames;
  471. int dataStartOffset;
  472. int dataLength;
  473. BYTE* buffer;
  474. int bufferSize;
  475. int index;
  476. bool wantsIndex;
  477. //==============================================================================
  478. CDReadBuffer (const int numberOfFrames)
  479. : startFrame (0),
  480. numFrames (0),
  481. dataStartOffset (0),
  482. dataLength (0),
  483. index (0),
  484. wantsIndex (false)
  485. {
  486. bufferSize = 2352 * numberOfFrames;
  487. buffer = (BYTE*) malloc (bufferSize);
  488. }
  489. ~CDReadBuffer()
  490. {
  491. free (buffer);
  492. }
  493. bool isZero() const
  494. {
  495. BYTE* p = buffer + dataStartOffset;
  496. for (int i = dataLength; --i >= 0;)
  497. if (*p++ != 0)
  498. return false;
  499. return true;
  500. }
  501. };
  502. class CDDeviceHandle;
  503. class CDController
  504. {
  505. public:
  506. CDController();
  507. virtual ~CDController();
  508. virtual bool read (CDReadBuffer* t) = 0;
  509. virtual void shutDown();
  510. bool readAudio (CDReadBuffer* t, CDReadBuffer* overlapBuffer = 0);
  511. int getLastIndex();
  512. public:
  513. bool initialised;
  514. CDDeviceHandle* deviceInfo;
  515. int framesToCheck, framesOverlap;
  516. void prepare (SRB_ExecSCSICmd& s);
  517. void perform (SRB_ExecSCSICmd& s);
  518. void setPaused (bool paused);
  519. };
  520. //==============================================================================
  521. #pragma pack(1)
  522. struct TOCTRACK
  523. {
  524. BYTE rsvd;
  525. BYTE ADR;
  526. BYTE trackNumber;
  527. BYTE rsvd2;
  528. BYTE addr[4];
  529. };
  530. struct TOC
  531. {
  532. WORD tocLen;
  533. BYTE firstTrack;
  534. BYTE lastTrack;
  535. TOCTRACK tracks[100];
  536. };
  537. #pragma pack()
  538. enum
  539. {
  540. READTYPE_ANY = 0,
  541. READTYPE_ATAPI1 = 1,
  542. READTYPE_ATAPI2 = 2,
  543. READTYPE_READ6 = 3,
  544. READTYPE_READ10 = 4,
  545. READTYPE_READ_D8 = 5,
  546. READTYPE_READ_D4 = 6,
  547. READTYPE_READ_D4_1 = 7,
  548. READTYPE_READ10_2 = 8
  549. };
  550. //==============================================================================
  551. class CDDeviceHandle
  552. {
  553. public:
  554. CDDeviceHandle (const CDDeviceInfo* const device)
  555. : scsiHandle (0),
  556. readType (READTYPE_ANY),
  557. controller (0)
  558. {
  559. memcpy (&info, device, sizeof (info));
  560. }
  561. ~CDDeviceHandle()
  562. {
  563. if (controller != 0)
  564. {
  565. controller->shutDown();
  566. delete controller;
  567. }
  568. if (scsiHandle != 0)
  569. CloseHandle (scsiHandle);
  570. }
  571. bool readTOC (TOC* lpToc, bool useMSF);
  572. bool readAudio (CDReadBuffer* buffer, CDReadBuffer* overlapBuffer = 0);
  573. void openDrawer (bool shouldBeOpen);
  574. CDDeviceInfo info;
  575. HANDLE scsiHandle;
  576. BYTE readType;
  577. private:
  578. CDController* controller;
  579. bool testController (const int readType,
  580. CDController* const newController,
  581. CDReadBuffer* const bufferToUse);
  582. };
  583. //==============================================================================
  584. DWORD (*fGetASPI32SupportInfo)(void);
  585. DWORD (*fSendASPI32Command)(LPSRB);
  586. //==============================================================================
  587. static HINSTANCE winAspiLib = 0;
  588. static bool usingScsi = false;
  589. static bool initialised = false;
  590. static bool InitialiseCDRipper()
  591. {
  592. if (! initialised)
  593. {
  594. initialised = true;
  595. OSVERSIONINFO info;
  596. info.dwOSVersionInfoSize = sizeof (info);
  597. GetVersionEx (&info);
  598. usingScsi = (info.dwPlatformId == VER_PLATFORM_WIN32_NT) && (info.dwMajorVersion > 4);
  599. if (! usingScsi)
  600. {
  601. fGetASPI32SupportInfo = 0;
  602. fSendASPI32Command = 0;
  603. winAspiLib = LoadLibrary (_T("WNASPI32.DLL"));
  604. if (winAspiLib != 0)
  605. {
  606. fGetASPI32SupportInfo = (DWORD(*)(void)) GetProcAddress (winAspiLib, "GetASPI32SupportInfo");
  607. fSendASPI32Command = (DWORD(*)(LPSRB)) GetProcAddress (winAspiLib, "SendASPI32Command");
  608. if (fGetASPI32SupportInfo == 0 || fSendASPI32Command == 0)
  609. return false;
  610. }
  611. else
  612. {
  613. usingScsi = true;
  614. }
  615. }
  616. }
  617. return true;
  618. }
  619. static void DeinitialiseCDRipper()
  620. {
  621. if (winAspiLib != 0)
  622. {
  623. fGetASPI32SupportInfo = 0;
  624. fSendASPI32Command = 0;
  625. FreeLibrary (winAspiLib);
  626. winAspiLib = 0;
  627. }
  628. initialised = false;
  629. }
  630. //==============================================================================
  631. static HANDLE CreateSCSIDeviceHandle (char driveLetter)
  632. {
  633. TCHAR devicePath[8];
  634. devicePath[0] = '\\';
  635. devicePath[1] = '\\';
  636. devicePath[2] = '.';
  637. devicePath[3] = '\\';
  638. devicePath[4] = driveLetter;
  639. devicePath[5] = ':';
  640. devicePath[6] = 0;
  641. OSVERSIONINFO info;
  642. info.dwOSVersionInfoSize = sizeof (info);
  643. GetVersionEx (&info);
  644. DWORD flags = GENERIC_READ;
  645. if ((info.dwPlatformId == VER_PLATFORM_WIN32_NT) && (info.dwMajorVersion > 4))
  646. flags = GENERIC_READ | GENERIC_WRITE;
  647. HANDLE h = CreateFile (devicePath, flags, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  648. if (h == INVALID_HANDLE_VALUE)
  649. {
  650. flags ^= GENERIC_WRITE;
  651. h = CreateFile (devicePath, flags, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  652. }
  653. return h;
  654. }
  655. static DWORD performScsiPassThroughCommand (const LPSRB_ExecSCSICmd srb,
  656. const char driveLetter,
  657. HANDLE& deviceHandle,
  658. const bool retryOnFailure = true)
  659. {
  660. SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER s;
  661. zerostruct (s);
  662. s.spt.Length = sizeof (SCSI_PASS_THROUGH);
  663. s.spt.CdbLength = srb->SRB_CDBLen;
  664. s.spt.DataIn = (BYTE) ((srb->SRB_Flags & SRB_DIR_IN)
  665. ? SCSI_IOCTL_DATA_IN
  666. : ((srb->SRB_Flags & SRB_DIR_OUT)
  667. ? SCSI_IOCTL_DATA_OUT
  668. : SCSI_IOCTL_DATA_UNSPECIFIED));
  669. s.spt.DataTransferLength = srb->SRB_BufLen;
  670. s.spt.TimeOutValue = 5;
  671. s.spt.DataBuffer = srb->SRB_BufPointer;
  672. s.spt.SenseInfoOffset = offsetof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, ucSenseBuf);
  673. memcpy (s.spt.Cdb, srb->CDBByte, srb->SRB_CDBLen);
  674. srb->SRB_Status = SS_ERR;
  675. srb->SRB_TargStat = 0x0004;
  676. DWORD bytesReturned = 0;
  677. if (DeviceIoControl (deviceHandle, IOCTL_SCSI_PASS_THROUGH_DIRECT,
  678. &s, sizeof (s),
  679. &s, sizeof (s),
  680. &bytesReturned, 0) != 0)
  681. {
  682. srb->SRB_Status = SS_COMP;
  683. }
  684. else if (retryOnFailure)
  685. {
  686. const DWORD error = GetLastError();
  687. if ((error == ERROR_MEDIA_CHANGED) || (error == ERROR_INVALID_HANDLE))
  688. {
  689. if (error != ERROR_INVALID_HANDLE)
  690. CloseHandle (deviceHandle);
  691. deviceHandle = CreateSCSIDeviceHandle (driveLetter);
  692. return performScsiPassThroughCommand (srb, driveLetter, deviceHandle, false);
  693. }
  694. }
  695. return srb->SRB_Status;
  696. }
  697. //==============================================================================
  698. // Controller types..
  699. class ControllerType1 : public CDController
  700. {
  701. public:
  702. ControllerType1() {}
  703. ~ControllerType1() {}
  704. bool read (CDReadBuffer* rb)
  705. {
  706. if (rb->numFrames * 2352 > rb->bufferSize)
  707. return false;
  708. SRB_ExecSCSICmd s;
  709. prepare (s);
  710. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  711. s.SRB_BufLen = rb->bufferSize;
  712. s.SRB_BufPointer = rb->buffer;
  713. s.SRB_CDBLen = 12;
  714. s.CDBByte[0] = 0xBE;
  715. s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
  716. s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
  717. s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
  718. s.CDBByte[8] = (BYTE)(rb->numFrames & 0xFF);
  719. s.CDBByte[9] = (BYTE)((deviceInfo->readType == READTYPE_ATAPI1) ? 0x10 : 0xF0);
  720. perform (s);
  721. if (s.SRB_Status != SS_COMP)
  722. return false;
  723. rb->dataLength = rb->numFrames * 2352;
  724. rb->dataStartOffset = 0;
  725. return true;
  726. }
  727. };
  728. //==============================================================================
  729. class ControllerType2 : public CDController
  730. {
  731. public:
  732. ControllerType2() {}
  733. ~ControllerType2() {}
  734. void shutDown()
  735. {
  736. if (initialised)
  737. {
  738. BYTE bufPointer[] = { 0, 0, 0, 8, 83, 0, 0, 0, 0, 0, 8, 0 };
  739. SRB_ExecSCSICmd s;
  740. prepare (s);
  741. s.SRB_Flags = SRB_EVENT_NOTIFY | SRB_ENABLE_RESIDUAL_COUNT;
  742. s.SRB_BufLen = 0x0C;
  743. s.SRB_BufPointer = bufPointer;
  744. s.SRB_CDBLen = 6;
  745. s.CDBByte[0] = 0x15;
  746. s.CDBByte[4] = 0x0C;
  747. perform (s);
  748. }
  749. }
  750. bool init()
  751. {
  752. SRB_ExecSCSICmd s;
  753. s.SRB_Status = SS_ERR;
  754. if (deviceInfo->readType == READTYPE_READ10_2)
  755. {
  756. BYTE bufPointer1[] = { 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 9, 48, 35, 6, 0, 0, 0, 0, 0, 128 };
  757. BYTE bufPointer2[] = { 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 9, 48, 1, 6, 32, 7, 0, 0, 0, 0 };
  758. for (int i = 0; i < 2; ++i)
  759. {
  760. prepare (s);
  761. s.SRB_Flags = SRB_EVENT_NOTIFY;
  762. s.SRB_BufLen = 0x14;
  763. s.SRB_BufPointer = (i == 0) ? bufPointer1 : bufPointer2;
  764. s.SRB_CDBLen = 6;
  765. s.CDBByte[0] = 0x15;
  766. s.CDBByte[1] = 0x10;
  767. s.CDBByte[4] = 0x14;
  768. perform (s);
  769. if (s.SRB_Status != SS_COMP)
  770. return false;
  771. }
  772. }
  773. else
  774. {
  775. BYTE bufPointer[] = { 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 9, 48 };
  776. prepare (s);
  777. s.SRB_Flags = SRB_EVENT_NOTIFY;
  778. s.SRB_BufLen = 0x0C;
  779. s.SRB_BufPointer = bufPointer;
  780. s.SRB_CDBLen = 6;
  781. s.CDBByte[0] = 0x15;
  782. s.CDBByte[4] = 0x0C;
  783. perform (s);
  784. }
  785. return s.SRB_Status == SS_COMP;
  786. }
  787. bool read (CDReadBuffer* rb)
  788. {
  789. if (rb->numFrames * 2352 > rb->bufferSize)
  790. return false;
  791. if (!initialised)
  792. {
  793. initialised = init();
  794. if (!initialised)
  795. return false;
  796. }
  797. SRB_ExecSCSICmd s;
  798. prepare (s);
  799. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  800. s.SRB_BufLen = rb->bufferSize;
  801. s.SRB_BufPointer = rb->buffer;
  802. s.SRB_CDBLen = 10;
  803. s.CDBByte[0] = 0x28;
  804. s.CDBByte[1] = (BYTE)(deviceInfo->info.lun << 5);
  805. s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
  806. s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
  807. s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
  808. s.CDBByte[8] = (BYTE)(rb->numFrames & 0xFF);
  809. perform (s);
  810. if (s.SRB_Status != SS_COMP)
  811. return false;
  812. rb->dataLength = rb->numFrames * 2352;
  813. rb->dataStartOffset = 0;
  814. return true;
  815. }
  816. };
  817. //==============================================================================
  818. class ControllerType3 : public CDController
  819. {
  820. public:
  821. ControllerType3() {}
  822. ~ControllerType3() {}
  823. bool read (CDReadBuffer* rb)
  824. {
  825. if (rb->numFrames * 2352 > rb->bufferSize)
  826. return false;
  827. if (!initialised)
  828. {
  829. setPaused (false);
  830. initialised = true;
  831. }
  832. SRB_ExecSCSICmd s;
  833. prepare (s);
  834. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  835. s.SRB_BufLen = rb->numFrames * 2352;
  836. s.SRB_BufPointer = rb->buffer;
  837. s.SRB_CDBLen = 12;
  838. s.CDBByte[0] = 0xD8;
  839. s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
  840. s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
  841. s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
  842. s.CDBByte[9] = (BYTE)(rb->numFrames & 0xFF);
  843. perform (s);
  844. if (s.SRB_Status != SS_COMP)
  845. return false;
  846. rb->dataLength = rb->numFrames * 2352;
  847. rb->dataStartOffset = 0;
  848. return true;
  849. }
  850. };
  851. //==============================================================================
  852. class ControllerType4 : public CDController
  853. {
  854. public:
  855. ControllerType4() {}
  856. ~ControllerType4() {}
  857. bool selectD4Mode()
  858. {
  859. BYTE bufPointer[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 48 };
  860. SRB_ExecSCSICmd s;
  861. prepare (s);
  862. s.SRB_Flags = SRB_EVENT_NOTIFY;
  863. s.SRB_CDBLen = 6;
  864. s.SRB_BufLen = 12;
  865. s.SRB_BufPointer = bufPointer;
  866. s.CDBByte[0] = 0x15;
  867. s.CDBByte[1] = 0x10;
  868. s.CDBByte[4] = 0x08;
  869. perform (s);
  870. return s.SRB_Status == SS_COMP;
  871. }
  872. bool read (CDReadBuffer* rb)
  873. {
  874. if (rb->numFrames * 2352 > rb->bufferSize)
  875. return false;
  876. if (!initialised)
  877. {
  878. setPaused (true);
  879. if (deviceInfo->readType == READTYPE_READ_D4_1)
  880. selectD4Mode();
  881. initialised = true;
  882. }
  883. SRB_ExecSCSICmd s;
  884. prepare (s);
  885. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  886. s.SRB_BufLen = rb->bufferSize;
  887. s.SRB_BufPointer = rb->buffer;
  888. s.SRB_CDBLen = 10;
  889. s.CDBByte[0] = 0xD4;
  890. s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
  891. s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
  892. s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
  893. s.CDBByte[8] = (BYTE)(rb->numFrames & 0xFF);
  894. perform (s);
  895. if (s.SRB_Status != SS_COMP)
  896. return false;
  897. rb->dataLength = rb->numFrames * 2352;
  898. rb->dataStartOffset = 0;
  899. return true;
  900. }
  901. };
  902. //==============================================================================
  903. CDController::CDController() : initialised (false)
  904. {
  905. }
  906. CDController::~CDController()
  907. {
  908. }
  909. void CDController::prepare (SRB_ExecSCSICmd& s)
  910. {
  911. zerostruct (s);
  912. s.SRB_Cmd = SC_EXEC_SCSI_CMD;
  913. s.SRB_HaID = deviceInfo->info.ha;
  914. s.SRB_Target = deviceInfo->info.tgt;
  915. s.SRB_Lun = deviceInfo->info.lun;
  916. s.SRB_SenseLen = SENSE_LEN;
  917. }
  918. void CDController::perform (SRB_ExecSCSICmd& s)
  919. {
  920. HANDLE event = CreateEvent (0, TRUE, FALSE, 0);
  921. s.SRB_PostProc = (void*)event;
  922. ResetEvent (event);
  923. DWORD status = (usingScsi) ? performScsiPassThroughCommand ((LPSRB_ExecSCSICmd)&s,
  924. deviceInfo->info.scsiDriveLetter,
  925. deviceInfo->scsiHandle)
  926. : fSendASPI32Command ((LPSRB)&s);
  927. if (status == SS_PENDING)
  928. WaitForSingleObject (event, 4000);
  929. CloseHandle (event);
  930. }
  931. void CDController::setPaused (bool paused)
  932. {
  933. SRB_ExecSCSICmd s;
  934. prepare (s);
  935. s.SRB_Flags = SRB_EVENT_NOTIFY;
  936. s.SRB_CDBLen = 10;
  937. s.CDBByte[0] = 0x4B;
  938. s.CDBByte[8] = (BYTE) (paused ? 0 : 1);
  939. perform (s);
  940. }
  941. void CDController::shutDown()
  942. {
  943. }
  944. bool CDController::readAudio (CDReadBuffer* rb, CDReadBuffer* overlapBuffer)
  945. {
  946. if (overlapBuffer != 0)
  947. {
  948. const bool canDoJitter = (overlapBuffer->bufferSize >= 2352 * framesToCheck);
  949. const bool doJitter = canDoJitter && ! overlapBuffer->isZero();
  950. if (doJitter
  951. && overlapBuffer->startFrame > 0
  952. && overlapBuffer->numFrames > 0
  953. && overlapBuffer->dataLength > 0)
  954. {
  955. const int numFrames = rb->numFrames;
  956. if (overlapBuffer->startFrame == (rb->startFrame - framesToCheck))
  957. {
  958. rb->startFrame -= framesOverlap;
  959. if (framesToCheck < framesOverlap
  960. && numFrames + framesOverlap <= rb->bufferSize / 2352)
  961. rb->numFrames += framesOverlap;
  962. }
  963. else
  964. {
  965. overlapBuffer->dataLength = 0;
  966. overlapBuffer->startFrame = 0;
  967. overlapBuffer->numFrames = 0;
  968. }
  969. }
  970. if (! read (rb))
  971. return false;
  972. if (doJitter)
  973. {
  974. const int checkLen = framesToCheck * 2352;
  975. const int maxToCheck = rb->dataLength - checkLen;
  976. if (overlapBuffer->dataLength == 0 || overlapBuffer->isZero())
  977. return true;
  978. BYTE* const p = overlapBuffer->buffer + overlapBuffer->dataStartOffset;
  979. bool found = false;
  980. for (int i = 0; i < maxToCheck; ++i)
  981. {
  982. if (!memcmp (p, rb->buffer + i, checkLen))
  983. {
  984. i += checkLen;
  985. rb->dataStartOffset = i;
  986. rb->dataLength -= i;
  987. rb->startFrame = overlapBuffer->startFrame + framesToCheck;
  988. found = true;
  989. break;
  990. }
  991. }
  992. rb->numFrames = rb->dataLength / 2352;
  993. rb->dataLength = 2352 * rb->numFrames;
  994. if (!found)
  995. return false;
  996. }
  997. if (canDoJitter)
  998. {
  999. memcpy (overlapBuffer->buffer,
  1000. rb->buffer + rb->dataStartOffset + 2352 * (rb->numFrames - framesToCheck),
  1001. 2352 * framesToCheck);
  1002. overlapBuffer->startFrame = rb->startFrame + rb->numFrames - framesToCheck;
  1003. overlapBuffer->numFrames = framesToCheck;
  1004. overlapBuffer->dataLength = 2352 * framesToCheck;
  1005. overlapBuffer->dataStartOffset = 0;
  1006. }
  1007. else
  1008. {
  1009. overlapBuffer->startFrame = 0;
  1010. overlapBuffer->numFrames = 0;
  1011. overlapBuffer->dataLength = 0;
  1012. }
  1013. return true;
  1014. }
  1015. else
  1016. {
  1017. return read (rb);
  1018. }
  1019. }
  1020. int CDController::getLastIndex()
  1021. {
  1022. char qdata[100];
  1023. SRB_ExecSCSICmd s;
  1024. prepare (s);
  1025. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  1026. s.SRB_BufLen = sizeof (qdata);
  1027. s.SRB_BufPointer = (BYTE*)qdata;
  1028. s.SRB_CDBLen = 12;
  1029. s.CDBByte[0] = 0x42;
  1030. s.CDBByte[1] = (BYTE)(deviceInfo->info.lun << 5);
  1031. s.CDBByte[2] = 64;
  1032. s.CDBByte[3] = 1; // get current position
  1033. s.CDBByte[7] = 0;
  1034. s.CDBByte[8] = (BYTE)sizeof (qdata);
  1035. perform (s);
  1036. if (s.SRB_Status == SS_COMP)
  1037. return qdata[7];
  1038. return 0;
  1039. }
  1040. //==============================================================================
  1041. bool CDDeviceHandle::readTOC (TOC* lpToc, bool useMSF)
  1042. {
  1043. HANDLE event = CreateEvent (0, TRUE, FALSE, 0);
  1044. SRB_ExecSCSICmd s;
  1045. zerostruct (s);
  1046. s.SRB_Cmd = SC_EXEC_SCSI_CMD;
  1047. s.SRB_HaID = info.ha;
  1048. s.SRB_Target = info.tgt;
  1049. s.SRB_Lun = info.lun;
  1050. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  1051. s.SRB_BufLen = 0x324;
  1052. s.SRB_BufPointer = (BYTE*)lpToc;
  1053. s.SRB_SenseLen = 0x0E;
  1054. s.SRB_CDBLen = 0x0A;
  1055. s.SRB_PostProc = (void*)event;
  1056. s.CDBByte[0] = 0x43;
  1057. s.CDBByte[1] = (BYTE)(useMSF ? 0x02 : 0x00);
  1058. s.CDBByte[7] = 0x03;
  1059. s.CDBByte[8] = 0x24;
  1060. ResetEvent (event);
  1061. DWORD status = (usingScsi) ? performScsiPassThroughCommand ((LPSRB_ExecSCSICmd)&s, info.scsiDriveLetter, scsiHandle)
  1062. : fSendASPI32Command ((LPSRB)&s);
  1063. if (status == SS_PENDING)
  1064. WaitForSingleObject (event, 4000);
  1065. CloseHandle (event);
  1066. return (s.SRB_Status == SS_COMP);
  1067. }
  1068. bool CDDeviceHandle::readAudio (CDReadBuffer* const buffer,
  1069. CDReadBuffer* const overlapBuffer)
  1070. {
  1071. if (controller == 0)
  1072. {
  1073. testController (READTYPE_ATAPI2, new ControllerType1(), buffer)
  1074. || testController (READTYPE_ATAPI1, new ControllerType1(), buffer)
  1075. || testController (READTYPE_READ10_2, new ControllerType2(), buffer)
  1076. || testController (READTYPE_READ10, new ControllerType2(), buffer)
  1077. || testController (READTYPE_READ_D8, new ControllerType3(), buffer)
  1078. || testController (READTYPE_READ_D4, new ControllerType4(), buffer)
  1079. || testController (READTYPE_READ_D4_1, new ControllerType4(), buffer);
  1080. }
  1081. buffer->index = 0;
  1082. if ((controller != 0)
  1083. && controller->readAudio (buffer, overlapBuffer))
  1084. {
  1085. if (buffer->wantsIndex)
  1086. buffer->index = controller->getLastIndex();
  1087. return true;
  1088. }
  1089. return false;
  1090. }
  1091. void CDDeviceHandle::openDrawer (bool shouldBeOpen)
  1092. {
  1093. if (shouldBeOpen)
  1094. {
  1095. if (controller != 0)
  1096. {
  1097. controller->shutDown();
  1098. delete controller;
  1099. controller = 0;
  1100. }
  1101. if (scsiHandle != 0)
  1102. {
  1103. CloseHandle (scsiHandle);
  1104. scsiHandle = 0;
  1105. }
  1106. }
  1107. SRB_ExecSCSICmd s;
  1108. zerostruct (s);
  1109. s.SRB_Cmd = SC_EXEC_SCSI_CMD;
  1110. s.SRB_HaID = info.ha;
  1111. s.SRB_Target = info.tgt;
  1112. s.SRB_Lun = info.lun;
  1113. s.SRB_SenseLen = SENSE_LEN;
  1114. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  1115. s.SRB_BufLen = 0;
  1116. s.SRB_BufPointer = 0;
  1117. s.SRB_CDBLen = 12;
  1118. s.CDBByte[0] = 0x1b;
  1119. s.CDBByte[1] = (BYTE)(info.lun << 5);
  1120. s.CDBByte[4] = (BYTE)((shouldBeOpen) ? 2 : 3);
  1121. HANDLE event = CreateEvent (0, TRUE, FALSE, 0);
  1122. s.SRB_PostProc = (void*)event;
  1123. ResetEvent (event);
  1124. DWORD status = (usingScsi) ? performScsiPassThroughCommand ((LPSRB_ExecSCSICmd)&s, info.scsiDriveLetter, scsiHandle)
  1125. : fSendASPI32Command ((LPSRB)&s);
  1126. if (status == SS_PENDING)
  1127. WaitForSingleObject (event, 4000);
  1128. CloseHandle (event);
  1129. }
  1130. bool CDDeviceHandle::testController (const int type,
  1131. CDController* const newController,
  1132. CDReadBuffer* const rb)
  1133. {
  1134. controller = newController;
  1135. readType = (BYTE)type;
  1136. controller->deviceInfo = this;
  1137. controller->framesToCheck = 1;
  1138. controller->framesOverlap = 3;
  1139. bool passed = false;
  1140. memset (rb->buffer, 0xcd, rb->bufferSize);
  1141. if (controller->read (rb))
  1142. {
  1143. passed = true;
  1144. int* p = (int*) (rb->buffer + rb->dataStartOffset);
  1145. int wrong = 0;
  1146. for (int i = rb->dataLength / 4; --i >= 0;)
  1147. {
  1148. if (*p++ == (int) 0xcdcdcdcd)
  1149. {
  1150. if (++wrong == 4)
  1151. {
  1152. passed = false;
  1153. break;
  1154. }
  1155. }
  1156. else
  1157. {
  1158. wrong = 0;
  1159. }
  1160. }
  1161. }
  1162. if (! passed)
  1163. {
  1164. controller->shutDown();
  1165. delete controller;
  1166. controller = 0;
  1167. }
  1168. return passed;
  1169. }
  1170. //==============================================================================
  1171. static void GetAspiDeviceInfo (CDDeviceInfo* dev, BYTE ha, BYTE tgt, BYTE lun)
  1172. {
  1173. HANDLE event = CreateEvent (0, TRUE, FALSE, 0);
  1174. const int bufSize = 128;
  1175. BYTE buffer[bufSize];
  1176. zeromem (buffer, bufSize);
  1177. SRB_ExecSCSICmd s;
  1178. zerostruct (s);
  1179. s.SRB_Cmd = SC_EXEC_SCSI_CMD;
  1180. s.SRB_HaID = ha;
  1181. s.SRB_Target = tgt;
  1182. s.SRB_Lun = lun;
  1183. s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
  1184. s.SRB_BufLen = bufSize;
  1185. s.SRB_BufPointer = buffer;
  1186. s.SRB_SenseLen = SENSE_LEN;
  1187. s.SRB_CDBLen = 6;
  1188. s.SRB_PostProc = (void*)event;
  1189. s.CDBByte[0] = SCSI_INQUIRY;
  1190. s.CDBByte[4] = 100;
  1191. ResetEvent (event);
  1192. if (fSendASPI32Command ((LPSRB)&s) == SS_PENDING)
  1193. WaitForSingleObject (event, 4000);
  1194. CloseHandle (event);
  1195. if (s.SRB_Status == SS_COMP)
  1196. {
  1197. memcpy (dev->vendor, &buffer[8], 8);
  1198. memcpy (dev->productId, &buffer[16], 16);
  1199. memcpy (dev->rev, &buffer[32], 4);
  1200. memcpy (dev->vendorSpec, &buffer[36], 20);
  1201. }
  1202. }
  1203. static int FindCDDevices (CDDeviceInfo* const list,
  1204. int maxItems)
  1205. {
  1206. int count = 0;
  1207. if (usingScsi)
  1208. {
  1209. for (char driveLetter = 'b'; driveLetter <= 'z'; ++driveLetter)
  1210. {
  1211. TCHAR drivePath[8];
  1212. drivePath[0] = driveLetter;
  1213. drivePath[1] = ':';
  1214. drivePath[2] = '\\';
  1215. drivePath[3] = 0;
  1216. if (GetDriveType (drivePath) == DRIVE_CDROM)
  1217. {
  1218. HANDLE h = CreateSCSIDeviceHandle (driveLetter);
  1219. if (h != INVALID_HANDLE_VALUE)
  1220. {
  1221. BYTE buffer[100], passThroughStruct[1024];
  1222. zeromem (buffer, sizeof (buffer));
  1223. zeromem (passThroughStruct, sizeof (passThroughStruct));
  1224. PSCSI_PASS_THROUGH_DIRECT_WITH_BUFFER p = (PSCSI_PASS_THROUGH_DIRECT_WITH_BUFFER)passThroughStruct;
  1225. p->spt.Length = sizeof (SCSI_PASS_THROUGH);
  1226. p->spt.CdbLength = 6;
  1227. p->spt.SenseInfoLength = 24;
  1228. p->spt.DataIn = SCSI_IOCTL_DATA_IN;
  1229. p->spt.DataTransferLength = 100;
  1230. p->spt.TimeOutValue = 2;
  1231. p->spt.DataBuffer = buffer;
  1232. p->spt.SenseInfoOffset = offsetof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, ucSenseBuf);
  1233. p->spt.Cdb[0] = 0x12;
  1234. p->spt.Cdb[4] = 100;
  1235. DWORD bytesReturned = 0;
  1236. if (DeviceIoControl (h, IOCTL_SCSI_PASS_THROUGH_DIRECT,
  1237. p, sizeof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER),
  1238. p, sizeof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER),
  1239. &bytesReturned, 0) != 0)
  1240. {
  1241. zeromem (&list[count], sizeof (CDDeviceInfo));
  1242. list[count].scsiDriveLetter = driveLetter;
  1243. memcpy (list[count].vendor, &buffer[8], 8);
  1244. memcpy (list[count].productId, &buffer[16], 16);
  1245. memcpy (list[count].rev, &buffer[32], 4);
  1246. memcpy (list[count].vendorSpec, &buffer[36], 20);
  1247. zeromem (passThroughStruct, sizeof (passThroughStruct));
  1248. PSCSI_ADDRESS scsiAddr = (PSCSI_ADDRESS)passThroughStruct;
  1249. scsiAddr->Length = sizeof (SCSI_ADDRESS);
  1250. if (DeviceIoControl (h, IOCTL_SCSI_GET_ADDRESS,
  1251. 0, 0, scsiAddr, sizeof (SCSI_ADDRESS),
  1252. &bytesReturned, 0) != 0)
  1253. {
  1254. list[count].ha = scsiAddr->PortNumber;
  1255. list[count].tgt = scsiAddr->TargetId;
  1256. list[count].lun = scsiAddr->Lun;
  1257. ++count;
  1258. }
  1259. }
  1260. CloseHandle (h);
  1261. }
  1262. }
  1263. }
  1264. }
  1265. else
  1266. {
  1267. const DWORD d = fGetASPI32SupportInfo();
  1268. BYTE status = HIBYTE (LOWORD (d));
  1269. if (status != SS_COMP || status == SS_NO_ADAPTERS)
  1270. return 0;
  1271. const int numAdapters = LOBYTE (LOWORD (d));
  1272. for (BYTE ha = 0; ha < numAdapters; ++ha)
  1273. {
  1274. SRB_HAInquiry s;
  1275. zerostruct (s);
  1276. s.SRB_Cmd = SC_HA_INQUIRY;
  1277. s.SRB_HaID = ha;
  1278. fSendASPI32Command ((LPSRB)&s);
  1279. if (s.SRB_Status == SS_COMP)
  1280. {
  1281. maxItems = (int)s.HA_Unique[3];
  1282. if (maxItems == 0)
  1283. maxItems = 8;
  1284. for (BYTE tgt = 0; tgt < maxItems; ++tgt)
  1285. {
  1286. for (BYTE lun = 0; lun < 8; ++lun)
  1287. {
  1288. SRB_GDEVBlock s;
  1289. zerostruct (s);
  1290. s.SRB_Cmd = SC_GET_DEV_TYPE;
  1291. s.SRB_HaID = ha;
  1292. s.SRB_Target = tgt;
  1293. s.SRB_Lun = lun;
  1294. fSendASPI32Command ((LPSRB)&s);
  1295. if (s.SRB_Status == SS_COMP
  1296. && s.SRB_DeviceType == DTYPE_CROM)
  1297. {
  1298. zeromem (&list[count], sizeof (CDDeviceInfo));
  1299. list[count].ha = ha;
  1300. list[count].tgt = tgt;
  1301. list[count].lun = lun;
  1302. GetAspiDeviceInfo (&(list[count]), ha, tgt, lun);
  1303. ++count;
  1304. }
  1305. }
  1306. }
  1307. }
  1308. }
  1309. }
  1310. return count;
  1311. }
  1312. //==============================================================================
  1313. static int ripperUsers = 0;
  1314. static bool initialisedOk = false;
  1315. class DeinitialiseTimer : private Timer,
  1316. private DeletedAtShutdown
  1317. {
  1318. DeinitialiseTimer (const DeinitialiseTimer&);
  1319. const DeinitialiseTimer& operator= (const DeinitialiseTimer&);
  1320. public:
  1321. DeinitialiseTimer()
  1322. {
  1323. startTimer (4000);
  1324. }
  1325. ~DeinitialiseTimer()
  1326. {
  1327. if (--ripperUsers == 0)
  1328. DeinitialiseCDRipper();
  1329. }
  1330. void timerCallback()
  1331. {
  1332. delete this;
  1333. }
  1334. juce_UseDebuggingNewOperator
  1335. };
  1336. static void incUserCount()
  1337. {
  1338. if (ripperUsers++ == 0)
  1339. initialisedOk = InitialiseCDRipper();
  1340. }
  1341. static void decUserCount()
  1342. {
  1343. new DeinitialiseTimer();
  1344. }
  1345. //==============================================================================
  1346. struct CDDeviceWrapper
  1347. {
  1348. CDDeviceHandle* cdH;
  1349. CDReadBuffer* overlapBuffer;
  1350. bool jitter;
  1351. };
  1352. //==============================================================================
  1353. static int getAddressOf (const TOCTRACK* const t)
  1354. {
  1355. return (((DWORD)t->addr[0]) << 24) + (((DWORD)t->addr[1]) << 16) +
  1356. (((DWORD)t->addr[2]) << 8) + ((DWORD)t->addr[3]);
  1357. }
  1358. static int getMSFAddressOf (const TOCTRACK* const t)
  1359. {
  1360. return 60 * t->addr[1] + t->addr[2];
  1361. }
  1362. static const int samplesPerFrame = 44100 / 75;
  1363. static const int bytesPerFrame = samplesPerFrame * 4;
  1364. //==============================================================================
  1365. const StringArray AudioCDReader::getAvailableCDNames()
  1366. {
  1367. StringArray results;
  1368. incUserCount();
  1369. if (initialisedOk)
  1370. {
  1371. CDDeviceInfo list[8];
  1372. const int num = FindCDDevices (list, 8);
  1373. decUserCount();
  1374. for (int i = 0; i < num; ++i)
  1375. {
  1376. String s;
  1377. if (list[i].scsiDriveLetter > 0)
  1378. s << String::charToString (list[i].scsiDriveLetter).toUpperCase() << T(": ");
  1379. s << String (list[i].vendor).trim()
  1380. << T(" ") << String (list[i].productId).trim()
  1381. << T(" ") << String (list[i].rev).trim();
  1382. results.add (s);
  1383. }
  1384. }
  1385. return results;
  1386. }
  1387. static CDDeviceHandle* openHandle (const CDDeviceInfo* const device)
  1388. {
  1389. SRB_GDEVBlock s;
  1390. zerostruct (s);
  1391. s.SRB_Cmd = SC_GET_DEV_TYPE;
  1392. s.SRB_HaID = device->ha;
  1393. s.SRB_Target = device->tgt;
  1394. s.SRB_Lun = device->lun;
  1395. if (usingScsi)
  1396. {
  1397. HANDLE h = CreateSCSIDeviceHandle (device->scsiDriveLetter);
  1398. if (h != INVALID_HANDLE_VALUE)
  1399. {
  1400. CDDeviceHandle* cdh = new CDDeviceHandle (device);
  1401. cdh->scsiHandle = h;
  1402. return cdh;
  1403. }
  1404. }
  1405. else
  1406. {
  1407. if (fSendASPI32Command ((LPSRB)&s) == SS_COMP
  1408. && s.SRB_DeviceType == DTYPE_CROM)
  1409. {
  1410. return new CDDeviceHandle (device);
  1411. }
  1412. }
  1413. return 0;
  1414. }
  1415. AudioCDReader* AudioCDReader::createReaderForCD (const int deviceIndex)
  1416. {
  1417. incUserCount();
  1418. if (initialisedOk)
  1419. {
  1420. CDDeviceInfo list[8];
  1421. const int num = FindCDDevices (list, 8);
  1422. if (deviceIndex >= 0 && deviceIndex < num)
  1423. {
  1424. CDDeviceHandle* const handle = openHandle (&(list[deviceIndex]));
  1425. if (handle != 0)
  1426. {
  1427. CDDeviceWrapper* const d = new CDDeviceWrapper();
  1428. d->cdH = handle;
  1429. d->overlapBuffer = new CDReadBuffer(3);
  1430. return new AudioCDReader (d);
  1431. }
  1432. }
  1433. }
  1434. decUserCount();
  1435. return 0;
  1436. }
  1437. AudioCDReader::AudioCDReader (void* handle_)
  1438. : AudioFormatReader (0, T("CD Audio")),
  1439. handle (handle_),
  1440. indexingEnabled (false),
  1441. lastIndex (0),
  1442. firstFrameInBuffer (0),
  1443. samplesInBuffer (0)
  1444. {
  1445. jassert (handle_ != 0);
  1446. refreshTrackLengths();
  1447. sampleRate = 44100.0;
  1448. bitsPerSample = 16;
  1449. lengthInSamples = getPositionOfTrackStart (numTracks);
  1450. numChannels = 2;
  1451. usesFloatingPointData = false;
  1452. buffer.setSize (4 * bytesPerFrame, true);
  1453. }
  1454. AudioCDReader::~AudioCDReader()
  1455. {
  1456. CDDeviceWrapper* const device = (CDDeviceWrapper*)handle;
  1457. delete device->cdH;
  1458. delete device->overlapBuffer;
  1459. delete device;
  1460. decUserCount();
  1461. }
  1462. bool AudioCDReader::read (int** destSamples,
  1463. int64 startSampleInFile,
  1464. int numSamples)
  1465. {
  1466. CDDeviceWrapper* const device = (CDDeviceWrapper*)handle;
  1467. bool ok = true;
  1468. int offset = 0;
  1469. if (startSampleInFile < 0)
  1470. {
  1471. int* l = destSamples[0];
  1472. int* r = destSamples[1];
  1473. numSamples += (int) startSampleInFile;
  1474. offset -= (int) startSampleInFile;
  1475. while (++startSampleInFile <= 0)
  1476. {
  1477. *l++ = 0;
  1478. if (r != 0)
  1479. *r++ = 0;
  1480. }
  1481. }
  1482. while (numSamples > 0)
  1483. {
  1484. const int bufferStartSample = firstFrameInBuffer * samplesPerFrame;
  1485. const int bufferEndSample = bufferStartSample + samplesInBuffer;
  1486. if (startSampleInFile >= bufferStartSample
  1487. && startSampleInFile < bufferEndSample)
  1488. {
  1489. const int toDo = (int) jmin ((int64) numSamples, bufferEndSample - startSampleInFile);
  1490. int* const l = destSamples[0] + offset;
  1491. int* const r = destSamples[1] + offset;
  1492. const short* src = (const short*) buffer.getData();
  1493. src += 2 * (startSampleInFile - bufferStartSample);
  1494. for (int i = 0; i < toDo; ++i)
  1495. {
  1496. l[i] = src [i << 1] << 16;
  1497. if (r != 0)
  1498. r[i] = src [(i << 1) + 1] << 16;
  1499. }
  1500. offset += toDo;
  1501. startSampleInFile += toDo;
  1502. numSamples -= toDo;
  1503. }
  1504. else
  1505. {
  1506. const int framesInBuffer = buffer.getSize() / bytesPerFrame;
  1507. const int frameNeeded = (int) (startSampleInFile / samplesPerFrame);
  1508. if (firstFrameInBuffer + framesInBuffer != frameNeeded)
  1509. {
  1510. device->overlapBuffer->dataLength = 0;
  1511. device->overlapBuffer->startFrame = 0;
  1512. device->overlapBuffer->numFrames = 0;
  1513. device->jitter = false;
  1514. }
  1515. firstFrameInBuffer = frameNeeded;
  1516. lastIndex = 0;
  1517. CDReadBuffer readBuffer (framesInBuffer + 4);
  1518. readBuffer.wantsIndex = indexingEnabled;
  1519. int i;
  1520. for (i = 5; --i >= 0;)
  1521. {
  1522. readBuffer.startFrame = frameNeeded;
  1523. readBuffer.numFrames = framesInBuffer;
  1524. if (device->cdH->readAudio (&readBuffer, (device->jitter) ? device->overlapBuffer : 0))
  1525. break;
  1526. else
  1527. device->overlapBuffer->dataLength = 0;
  1528. }
  1529. if (i >= 0)
  1530. {
  1531. memcpy ((char*) buffer.getData(),
  1532. readBuffer.buffer + readBuffer.dataStartOffset,
  1533. readBuffer.dataLength);
  1534. samplesInBuffer = readBuffer.dataLength >> 2;
  1535. lastIndex = readBuffer.index;
  1536. }
  1537. else
  1538. {
  1539. int* l = destSamples[0] + offset;
  1540. int* r = destSamples[1] + offset;
  1541. while (--numSamples >= 0)
  1542. {
  1543. *l++ = 0;
  1544. if (r != 0)
  1545. *r++ = 0;
  1546. }
  1547. // sometimes the read fails for just the very last couple of blocks, so
  1548. // we'll ignore and errors in the last half-second of the disk..
  1549. ok = startSampleInFile > (trackStarts [numTracks] - 20000);
  1550. break;
  1551. }
  1552. }
  1553. }
  1554. return ok;
  1555. }
  1556. bool AudioCDReader::isCDStillPresent() const
  1557. {
  1558. TOC toc;
  1559. zerostruct (toc);
  1560. return ((CDDeviceWrapper*)handle)->cdH->readTOC (&toc, false);
  1561. }
  1562. int AudioCDReader::getNumTracks() const
  1563. {
  1564. return numTracks;
  1565. }
  1566. int AudioCDReader::getPositionOfTrackStart (int trackNum) const
  1567. {
  1568. return (trackNum >= 0 && trackNum <= numTracks) ? trackStarts [trackNum] * samplesPerFrame
  1569. : 0;
  1570. }
  1571. void AudioCDReader::refreshTrackLengths()
  1572. {
  1573. zeromem (trackStarts, sizeof (trackStarts));
  1574. zeromem (audioTracks, sizeof (audioTracks));
  1575. TOC toc;
  1576. zerostruct (toc);
  1577. if (((CDDeviceWrapper*)handle)->cdH->readTOC (&toc, false))
  1578. {
  1579. numTracks = 1 + toc.lastTrack - toc.firstTrack;
  1580. for (int i = 0; i <= numTracks; ++i)
  1581. {
  1582. trackStarts[i] = getAddressOf (&toc.tracks[i]);
  1583. audioTracks[i] = ((toc.tracks[i].ADR & 4) == 0);
  1584. }
  1585. }
  1586. else
  1587. {
  1588. numTracks = 0;
  1589. }
  1590. }
  1591. bool AudioCDReader::isTrackAudio (int trackNum) const
  1592. {
  1593. return (trackNum >= 0 && trackNum <= numTracks) ? audioTracks [trackNum]
  1594. : false;
  1595. }
  1596. void AudioCDReader::enableIndexScanning (bool b)
  1597. {
  1598. indexingEnabled = b;
  1599. }
  1600. int AudioCDReader::getLastIndex() const
  1601. {
  1602. return lastIndex;
  1603. }
  1604. const int framesPerIndexRead = 4;
  1605. int AudioCDReader::getIndexAt (int samplePos)
  1606. {
  1607. CDDeviceWrapper* const device = (CDDeviceWrapper*) handle;
  1608. const int frameNeeded = samplePos / samplesPerFrame;
  1609. device->overlapBuffer->dataLength = 0;
  1610. device->overlapBuffer->startFrame = 0;
  1611. device->overlapBuffer->numFrames = 0;
  1612. device->jitter = false;
  1613. firstFrameInBuffer = 0;
  1614. lastIndex = 0;
  1615. CDReadBuffer readBuffer (4 + framesPerIndexRead);
  1616. readBuffer.wantsIndex = true;
  1617. int i;
  1618. for (i = 5; --i >= 0;)
  1619. {
  1620. readBuffer.startFrame = frameNeeded;
  1621. readBuffer.numFrames = framesPerIndexRead;
  1622. if (device->cdH->readAudio (&readBuffer, (false) ? device->overlapBuffer : 0))
  1623. break;
  1624. }
  1625. if (i >= 0)
  1626. return readBuffer.index;
  1627. return -1;
  1628. }
  1629. const Array <int> AudioCDReader::findIndexesInTrack (const int trackNumber)
  1630. {
  1631. Array <int> indexes;
  1632. const int trackStart = getPositionOfTrackStart (trackNumber);
  1633. const int trackEnd = getPositionOfTrackStart (trackNumber + 1);
  1634. bool needToScan = true;
  1635. if (trackEnd - trackStart > 20 * 44100)
  1636. {
  1637. // check the end of the track for indexes before scanning the whole thing
  1638. needToScan = false;
  1639. int pos = jmax (trackStart, trackEnd - 44100 * 5);
  1640. bool seenAnIndex = false;
  1641. while (pos <= trackEnd - samplesPerFrame)
  1642. {
  1643. const int index = getIndexAt (pos);
  1644. if (index == 0)
  1645. {
  1646. // lead-out, so skip back a bit if we've not found any indexes yet..
  1647. if (seenAnIndex)
  1648. break;
  1649. pos -= 44100 * 5;
  1650. if (pos < trackStart)
  1651. break;
  1652. }
  1653. else
  1654. {
  1655. if (index > 0)
  1656. seenAnIndex = true;
  1657. if (index > 1)
  1658. {
  1659. needToScan = true;
  1660. break;
  1661. }
  1662. pos += samplesPerFrame * framesPerIndexRead;
  1663. }
  1664. }
  1665. }
  1666. if (needToScan)
  1667. {
  1668. CDDeviceWrapper* const device = (CDDeviceWrapper*) handle;
  1669. int pos = trackStart;
  1670. int last = -1;
  1671. while (pos < trackEnd - samplesPerFrame * 10)
  1672. {
  1673. const int frameNeeded = pos / samplesPerFrame;
  1674. device->overlapBuffer->dataLength = 0;
  1675. device->overlapBuffer->startFrame = 0;
  1676. device->overlapBuffer->numFrames = 0;
  1677. device->jitter = false;
  1678. firstFrameInBuffer = 0;
  1679. CDReadBuffer readBuffer (4);
  1680. readBuffer.wantsIndex = true;
  1681. int i;
  1682. for (i = 5; --i >= 0;)
  1683. {
  1684. readBuffer.startFrame = frameNeeded;
  1685. readBuffer.numFrames = framesPerIndexRead;
  1686. if (device->cdH->readAudio (&readBuffer, (false) ? device->overlapBuffer : 0))
  1687. break;
  1688. }
  1689. if (i < 0)
  1690. break;
  1691. if (readBuffer.index > last && readBuffer.index > 1)
  1692. {
  1693. last = readBuffer.index;
  1694. indexes.add (pos);
  1695. }
  1696. pos += samplesPerFrame * framesPerIndexRead;
  1697. }
  1698. indexes.removeValue (trackStart);
  1699. }
  1700. return indexes;
  1701. }
  1702. int AudioCDReader::getCDDBId()
  1703. {
  1704. refreshTrackLengths();
  1705. if (numTracks > 0)
  1706. {
  1707. TOC toc;
  1708. zerostruct (toc);
  1709. if (((CDDeviceWrapper*) handle)->cdH->readTOC (&toc, true))
  1710. {
  1711. int n = 0;
  1712. for (int i = numTracks; --i >= 0;)
  1713. {
  1714. int j = getMSFAddressOf (&toc.tracks[i]);
  1715. while (j > 0)
  1716. {
  1717. n += (j % 10);
  1718. j /= 10;
  1719. }
  1720. }
  1721. if (n != 0)
  1722. {
  1723. const int t = getMSFAddressOf (&toc.tracks[numTracks])
  1724. - getMSFAddressOf (&toc.tracks[0]);
  1725. return ((n % 0xff) << 24) | (t << 8) | numTracks;
  1726. }
  1727. }
  1728. }
  1729. return 0;
  1730. }
  1731. void AudioCDReader::ejectDisk()
  1732. {
  1733. ((CDDeviceWrapper*) handle)->cdH->openDrawer (true);
  1734. }
  1735. END_JUCE_NAMESPACE