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.

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