bin_to_cso_mp3.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * bin_to_cso_mp3
  3. * originally written by Exophase as "bin_to_iso_ogg"
  4. * updated for cso/mp3 by notaz
  5. * v2
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sys/stat.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #ifndef MAX_PATH
  14. #define MAX_PATH 1024
  15. #endif
  16. #ifdef _WIN32
  17. #include <windows.h>
  18. #define DIR_SEPARATOR_CHAR '\\'
  19. #define PATH_SEPARATOR_CHAR ';'
  20. #define LAME_BINARY "lame.exe"
  21. #define CISO_BINARY "ciso.exe"
  22. #define NULL_REDIR "> NUL 2>&1"
  23. #else
  24. #define DIR_SEPARATOR_CHAR '/'
  25. #define PATH_SEPARATOR_CHAR ':'
  26. #define LAME_BINARY "lame"
  27. #define CISO_BINARY "ciso"
  28. #define NULL_REDIR "> /dev/null 2>&1"
  29. #define mkdir(x) mkdir(x, S_IRWXU)
  30. #endif
  31. #define LAME_OPTIONS "-h --cbr"
  32. typedef unsigned char u8;
  33. typedef unsigned short int u16;
  34. typedef unsigned int u32;
  35. typedef unsigned long long int u64;
  36. typedef signed char s8;
  37. typedef signed short int s16;
  38. typedef signed int s32;
  39. typedef signed long long int s64;
  40. typedef enum
  41. {
  42. TRACK_FILE_TYPE_BINARY,
  43. TRACK_FILE_TYPE_WAVE,
  44. } track_file_type_enum;
  45. typedef struct
  46. {
  47. u32 file_number;
  48. u32 physical_offset;
  49. u32 sector_offset;
  50. u32 sector_count;
  51. u32 pregap_offset;
  52. u32 sector_size;
  53. u32 format_type;
  54. } cd_track_struct;
  55. typedef struct
  56. {
  57. track_file_type_enum type;
  58. FILE *file_handle;
  59. u32 current_offset;
  60. } cd_track_file_struct;
  61. typedef struct
  62. {
  63. FILE *bin_file;
  64. cd_track_file_struct track_files[100];
  65. u32 num_files;
  66. s32 first_track;
  67. s32 last_track;
  68. u32 num_physical_tracks;
  69. u32 num_sectors;
  70. s32 last_seek_track;
  71. cd_track_struct physical_tracks[100];
  72. cd_track_struct *logical_tracks[100];
  73. } cd_bin_struct;
  74. cd_bin_struct cd_bin;
  75. int opt_use_mp3 = 1;
  76. int opt_mp3_bitrate = 128;
  77. int opt_use_cso = 1;
  78. static void myexit(int code)
  79. {
  80. #ifdef _WIN32
  81. system("pause");
  82. #endif
  83. exit(code);
  84. }
  85. char *skip_whitespace(char *str)
  86. {
  87. while (isspace(*str))
  88. str++;
  89. return str;
  90. }
  91. char *skip_whitespace_rev(char *str)
  92. {
  93. while (isspace(*str))
  94. str--;
  95. return str;
  96. }
  97. char *skip_nonspace_rev(char *str)
  98. {
  99. while (!isspace(*str))
  100. str--;
  101. return str;
  102. }
  103. s32 load_bin_cue(char *cue_file_name)
  104. {
  105. FILE *cue_file = fopen(cue_file_name, "rb");
  106. printf("loading cue file %s\n", cue_file_name);
  107. if(cue_file)
  108. {
  109. char line_buffer[256];
  110. char *line_buffer_ptr;
  111. char *tmp;
  112. char bin_file_name[MAX_PATH];
  113. char *separator_pos;
  114. s32 current_physical_track_number = -1;
  115. u32 current_physical_offset;
  116. u32 current_pregap = 0;
  117. u32 bin_file_size;
  118. cd_track_struct *current_physical_track = NULL;
  119. u32 i;
  120. // First, get filename. Only support binary right now.
  121. tmp = fgets(line_buffer, 255, cue_file);
  122. if (tmp == NULL) goto invalid;
  123. separator_pos = line_buffer + strlen(line_buffer) - 1;
  124. separator_pos = skip_whitespace_rev(separator_pos);
  125. if (separator_pos <= line_buffer) goto invalid;
  126. separator_pos = skip_nonspace_rev(separator_pos);
  127. if (separator_pos <= line_buffer) goto invalid;
  128. separator_pos = skip_whitespace_rev(separator_pos);
  129. if (separator_pos <= line_buffer) goto invalid;
  130. // see if what's there is a quote.
  131. if(*separator_pos == '"')
  132. {
  133. separator_pos[0] = 0;
  134. separator_pos = strrchr(line_buffer, '"');
  135. if (separator_pos == NULL) goto invalid;
  136. strcpy(bin_file_name, separator_pos + 1);
  137. }
  138. else
  139. {
  140. // Otherwise go to the next space.
  141. separator_pos[1] = 0;
  142. separator_pos = strrchr(line_buffer, ' ');
  143. if (separator_pos == NULL) goto invalid;
  144. strcpy(bin_file_name, separator_pos + 1);
  145. }
  146. // Might have to change directory first.
  147. separator_pos = strrchr(cue_file_name, DIR_SEPARATOR_CHAR);
  148. if(separator_pos)
  149. {
  150. char current_dir[MAX_PATH];
  151. getcwd(current_dir, MAX_PATH);
  152. *separator_pos = 0;
  153. chdir(cue_file_name);
  154. #ifdef GP2X_BUILD
  155. cd_bin.bin_file = open(bin_file_name, O_RDONLY);
  156. #else
  157. cd_bin.bin_file = fopen(bin_file_name, "rb");
  158. #endif
  159. *separator_pos = DIR_SEPARATOR_CHAR;
  160. chdir(current_dir);
  161. }
  162. else
  163. {
  164. #ifdef GP2X_BUILD
  165. cd_bin.bin_file = open(bin_file_name, O_RDONLY);
  166. #else
  167. cd_bin.bin_file = fopen(bin_file_name, "rb");
  168. #endif
  169. }
  170. if (cd_bin.bin_file == NULL)
  171. {
  172. printf("can't open bin file: \"%s\"\n", bin_file_name);
  173. return -1;
  174. }
  175. else
  176. {
  177. printf("found bin file: %s\n", bin_file_name);
  178. }
  179. for(i = 0; i < 100; i++)
  180. {
  181. cd_bin.logical_tracks[i] = NULL;
  182. }
  183. cd_bin.first_track = -1;
  184. cd_bin.last_track = -1;
  185. cd_bin.num_physical_tracks = 0;
  186. cd_bin.num_sectors = 0;
  187. // Get line
  188. while(fgets(line_buffer, 256, cue_file))
  189. {
  190. // Skip trailing whitespace
  191. line_buffer_ptr = skip_whitespace(line_buffer);
  192. // Dirty, but should work - switch on first character.
  193. switch(line_buffer_ptr[0])
  194. {
  195. // New track number
  196. case 'T':
  197. {
  198. u32 new_track_number;
  199. char track_type[64];
  200. sscanf(line_buffer_ptr, "TRACK %d %s", &new_track_number,
  201. track_type);
  202. current_physical_track_number++;
  203. current_physical_track =
  204. cd_bin.physical_tracks + current_physical_track_number;
  205. current_physical_track->sector_size = 2352;
  206. if(!strcmp(track_type, "AUDIO"))
  207. {
  208. current_physical_track->format_type = 0;
  209. current_physical_track->sector_size = 2352;
  210. }
  211. if(!strcmp(track_type, "MODE1/2352"))
  212. {
  213. current_physical_track->format_type = 4;
  214. current_physical_track->sector_size = 2352;
  215. }
  216. if(!strcmp(track_type, "MODE1/2048"))
  217. {
  218. current_physical_track->format_type = 4;
  219. current_physical_track->sector_size = 2048;
  220. }
  221. cd_bin.logical_tracks[new_track_number] = current_physical_track;
  222. cd_bin.num_physical_tracks++;
  223. if((cd_bin.first_track == -1) ||
  224. (new_track_number < cd_bin.first_track))
  225. {
  226. cd_bin.first_track = new_track_number;
  227. }
  228. if((cd_bin.last_track == -1) ||
  229. (new_track_number > cd_bin.last_track))
  230. {
  231. cd_bin.last_track = new_track_number;
  232. }
  233. break;
  234. }
  235. // Pregap
  236. case 'P':
  237. {
  238. u32 minutes, seconds, frames;
  239. sscanf(line_buffer_ptr, "PREGAP %d:%d:%d", &minutes,
  240. &seconds, &frames);
  241. current_pregap += frames + (seconds * 75) + (minutes * 75 * 60);
  242. break;
  243. }
  244. // Index
  245. case 'I':
  246. {
  247. u32 index_number;
  248. u32 minutes, seconds, frames;
  249. u32 sector_offset;
  250. sscanf(line_buffer_ptr, "INDEX %d %d:%d:%d", &index_number,
  251. &minutes, &seconds, &frames);
  252. sector_offset = frames + (seconds * 75) + (minutes * 75 * 60);
  253. if(index_number == 1)
  254. {
  255. current_physical_track->pregap_offset = current_pregap;
  256. current_physical_track->sector_offset = sector_offset;
  257. }
  258. break;
  259. }
  260. }
  261. }
  262. current_physical_offset = 0;
  263. for(i = 0; i < cd_bin.num_physical_tracks - 1; i++)
  264. {
  265. cd_bin.physical_tracks[i].sector_count =
  266. cd_bin.physical_tracks[i + 1].sector_offset -
  267. cd_bin.physical_tracks[i].sector_offset;
  268. cd_bin.physical_tracks[i].physical_offset = current_physical_offset;
  269. current_physical_offset += (cd_bin.physical_tracks[i].sector_count *
  270. cd_bin.physical_tracks[i].sector_size);
  271. cd_bin.physical_tracks[i].sector_offset +=
  272. cd_bin.physical_tracks[i].pregap_offset;
  273. cd_bin.num_sectors += cd_bin.physical_tracks[i].sector_count;
  274. }
  275. #ifdef GP2X_BUILD
  276. bin_file_size = lseek(cd_bin.bin_file, 0, SEEK_END);
  277. lseek(cd_bin.bin_file, 0, SEEK_SET);
  278. #else
  279. fseek(cd_bin.bin_file, 0, SEEK_END);
  280. bin_file_size = ftell(cd_bin.bin_file);
  281. fseek(cd_bin.bin_file, 0, SEEK_SET);
  282. #endif
  283. // Set the last track data
  284. cd_bin.physical_tracks[i].physical_offset = current_physical_offset;
  285. cd_bin.physical_tracks[i].sector_offset +=
  286. cd_bin.physical_tracks[i].pregap_offset;
  287. cd_bin.physical_tracks[i].sector_count =
  288. (bin_file_size - current_physical_offset) /
  289. cd_bin.physical_tracks[i].sector_size;
  290. cd_bin.num_sectors += cd_bin.physical_tracks[i].sector_count;
  291. printf("finished loading cue %s\n", cue_file_name);
  292. printf("bin file: %s (%p)\n", bin_file_name, cd_bin.bin_file);
  293. printf("first track: %d, last track: %d\n", cd_bin.first_track,
  294. cd_bin.last_track);
  295. for(i = cd_bin.first_track; i <= cd_bin.last_track; i++)
  296. {
  297. printf("track %d (%p):\n", i, cd_bin.logical_tracks[i]);
  298. if(cd_bin.logical_tracks[i] == NULL)
  299. {
  300. printf(" (invalid)\n");
  301. }
  302. else
  303. {
  304. printf(" physical offset 0x%x\n",
  305. cd_bin.logical_tracks[i]->physical_offset);
  306. printf(" sector offset 0x%x\n",
  307. cd_bin.logical_tracks[i]->sector_offset);
  308. printf(" sector size %d\n",
  309. cd_bin.logical_tracks[i]->sector_size);
  310. }
  311. }
  312. cd_bin.last_seek_track = 0;
  313. fclose(cue_file);
  314. return 0;
  315. }
  316. return -1;
  317. invalid:
  318. printf("error: invalid/unsupported .cue file\n");
  319. return -1;
  320. }
  321. #define address8(base, offset) \
  322. *((u8 *)((u8 *)base + (offset))) \
  323. #define address16(base, offset) \
  324. *((u16 *)((u8 *)base + (offset))) \
  325. #define address32(base, offset) \
  326. *((u32 *)((u8 *)base + (offset))) \
  327. // This will only work on little endian platforms for now.
  328. s32 convert_bin_to_wav(FILE *bin_file, char *output_dir, char *wav_file_name,
  329. u32 sector_count)
  330. {
  331. FILE *wav_file;
  332. u8 wav_header[36];
  333. u8 *riff_header = wav_header + 0;
  334. u8 *fmt_header = wav_header + 0x0C;
  335. u8 sector_buffer[2352];
  336. u32 byte_length = sector_count * 2352;
  337. u32 i;
  338. chdir(output_dir);
  339. wav_file = fopen(wav_file_name, "wb");
  340. printf("writing wav %s, %x sectors\n", wav_file_name, sector_count);
  341. // RIFF type chunk
  342. memcpy(riff_header + 0x00, "RIFF", 4);
  343. address32(riff_header, 0x04) = byte_length + 44 - 8;
  344. memcpy(riff_header + 0x08, "WAVE", 4);
  345. // WAVE file chunk: format
  346. memcpy(fmt_header + 0x00, "fmt ", 4);
  347. // Chunk data size
  348. address32(fmt_header, 0x04) = 16;
  349. // Compression code: PCM
  350. address16(fmt_header, 0x08) = 1;
  351. // Number of channels: Stereo
  352. address16(fmt_header, 0x0a) = 2;
  353. // Sample rate: 44100Hz
  354. address32(fmt_header, 0x0c) = 44100;
  355. // Average bytes per second: sample rate * 4
  356. address32(fmt_header, 0x10) = 44100 * 4;
  357. // Block align (bytes per sample)
  358. address16(fmt_header, 0x14) = 4;
  359. // Bit depth
  360. address16(fmt_header, 0x16) = 16;
  361. // Write out header
  362. fwrite(wav_header, 36, 1, wav_file);
  363. // DATA chunk
  364. fprintf(wav_file, "data");
  365. // length
  366. fwrite(&byte_length, 4, 1, wav_file);
  367. // Write out sectors
  368. for(i = 0; i < sector_count; i++)
  369. {
  370. printf("\b\b\b%3i", i*100 / sector_count);
  371. fflush(stdout);
  372. fread(sector_buffer, 2352, 1, bin_file);
  373. fwrite(sector_buffer, 2352, 1, wav_file);
  374. }
  375. printf("\b\b\b100\n");
  376. fclose(wav_file);
  377. chdir("..");
  378. return 0;
  379. }
  380. void convert_wav_to_ogg(char *wav_file_name, char *output_dir,
  381. char *ogg_file_name)
  382. {
  383. char cmd_string[(MAX_PATH * 2) + 16];
  384. chdir(output_dir);
  385. sprintf(cmd_string, "oggenc %s", wav_file_name);
  386. system(cmd_string);
  387. unlink(wav_file_name);
  388. chdir("..");
  389. }
  390. void convert_wav_to_mp3(char *wav_file_name, char *output_dir,
  391. char *mp3_file_name)
  392. {
  393. char cmd_string[(MAX_PATH * 2) + 16];
  394. chdir(output_dir);
  395. sprintf(cmd_string, LAME_BINARY " " LAME_OPTIONS " -b %i \"%s\" \"%s\"",
  396. opt_mp3_bitrate, wav_file_name, mp3_file_name);
  397. if (system(cmd_string) != 0)
  398. {
  399. printf("failed to encode mp3\n");
  400. myexit(1);
  401. }
  402. unlink(wav_file_name);
  403. chdir("..");
  404. }
  405. s32 convert_bin_to_iso(FILE *bin_file, char *output_dir, char *iso_file_name,
  406. u32 sector_count)
  407. {
  408. FILE *iso_file;
  409. u8 sector_buffer[2352];
  410. u32 i;
  411. chdir(output_dir);
  412. iso_file = fopen(iso_file_name, "wb");
  413. if (iso_file == NULL)
  414. {
  415. printf("failed to open: %s\n", iso_file_name);
  416. myexit(1);
  417. }
  418. printf("writing iso %s, %x sectors\n", iso_file_name, sector_count);
  419. for(i = 0; i < sector_count; i++)
  420. {
  421. printf("\b\b\b%3i", i*100 / sector_count);
  422. fflush(stdout);
  423. fread(sector_buffer, 2352, 1, bin_file);
  424. fwrite(sector_buffer + 16, 2048, 1, iso_file);
  425. }
  426. printf("\b\b\b100\n");
  427. fclose(iso_file);
  428. chdir("..");
  429. return 0;
  430. }
  431. void convert_iso_to_cso(char *output_dir, char *iso_file_name, char *cso_file_name)
  432. {
  433. char cmd_string[(MAX_PATH * 2) + 16];
  434. chdir(output_dir);
  435. sprintf(cmd_string, CISO_BINARY " 9 \"%s\" \"%s\"", iso_file_name, cso_file_name);
  436. if (system(cmd_string) != 0)
  437. {
  438. printf("failed to convert iso to cso\n");
  439. myexit(1);
  440. }
  441. unlink(iso_file_name);
  442. chdir("..");
  443. }
  444. #define sector_offset_to_msf(offset, minutes, seconds, frames) \
  445. { \
  446. u32 _offset = offset; \
  447. minutes = (_offset / 75) / 60; \
  448. seconds = (_offset / 75) % 60; \
  449. frames = _offset % 75; \
  450. } \
  451. s32 convert_bin_cue(char *output_name_base)
  452. {
  453. char output_file_name[MAX_PATH];
  454. FILE *output_cue_file;
  455. FILE *bin_file = cd_bin.bin_file;
  456. cd_track_struct *current_track;
  457. u32 m, s, f;
  458. u32 current_pregap = 0;
  459. u32 last_pregap = 0;
  460. u32 i;
  461. struct stat sb;
  462. if(stat(output_name_base, &sb))
  463. mkdir(output_name_base);
  464. sprintf(output_file_name, "%s.cue", output_name_base);
  465. chdir(output_name_base);
  466. output_cue_file = fopen(output_file_name, "wb");
  467. chdir("..");
  468. // Every track gets its own file. It's either going to be of type ISO
  469. // or of type WAV.
  470. for(i = 0; i < 100; i++)
  471. {
  472. current_track = cd_bin.logical_tracks[i];
  473. if(current_track != NULL)
  474. {
  475. switch(current_track->format_type)
  476. {
  477. char output_name_tmp[MAX_PATH];
  478. // Audio
  479. case 0:
  480. {
  481. sprintf(output_file_name, "%s_%02d.mp3", output_name_base, i);
  482. sprintf(output_name_tmp, "%s_%02d.wav", output_name_base, i);
  483. fprintf(output_cue_file, "FILE \"%s\" %s\n",
  484. opt_use_mp3 ? output_file_name : output_name_tmp,
  485. opt_use_mp3 ? "MP3" : "WAVE");
  486. fprintf(output_cue_file, " TRACK %02d AUDIO\n", i);
  487. current_pregap = current_track->pregap_offset - last_pregap;
  488. last_pregap = current_track->pregap_offset;
  489. if(current_pregap > 0)
  490. {
  491. sector_offset_to_msf(current_pregap, m, s, f);
  492. fprintf(output_cue_file, " PREGAP %02d:%02d:%02d\n", m, s, f);
  493. }
  494. fprintf(output_cue_file, " INDEX 01 00:00:00\n");
  495. sector_offset_to_msf(current_track->sector_count, m, s, f);
  496. fprintf(output_cue_file, " REM LENGTH %02d:%02d:%02d\n", m, s, f);
  497. fseek(bin_file, current_track->physical_offset, SEEK_SET);
  498. convert_bin_to_wav(bin_file, output_name_base, output_name_tmp,
  499. current_track->sector_count);
  500. if(opt_use_mp3)
  501. {
  502. convert_wav_to_mp3(output_name_tmp, output_name_base,
  503. output_file_name);
  504. }
  505. break;
  506. }
  507. // Data
  508. default:
  509. sprintf(output_file_name, "%s_%02d.cso", output_name_base, i);
  510. sprintf(output_name_tmp, "%s_%02d.iso", output_name_base, i);
  511. fprintf(output_cue_file, "FILE \"%s\" BINARY\n",
  512. opt_use_cso ? output_file_name : output_name_tmp);
  513. fprintf(output_cue_file, " TRACK %02d MODE1/2048\n", i);
  514. current_pregap = current_track->pregap_offset - last_pregap;
  515. last_pregap = current_track->pregap_offset;
  516. if(current_pregap > 0)
  517. {
  518. sector_offset_to_msf(current_pregap, m, s, f);
  519. fprintf(output_cue_file, " PREGAP %02d:%02d:%02d\n", m, s, f);
  520. }
  521. fprintf(output_cue_file, " INDEX 01 00:00:00\n");
  522. fseek(bin_file, current_track->physical_offset, SEEK_SET);
  523. convert_bin_to_iso(bin_file, output_name_base, output_name_tmp,
  524. current_track->sector_count);
  525. if(opt_use_cso)
  526. {
  527. convert_iso_to_cso(output_name_base, output_name_tmp, output_file_name);
  528. }
  529. break;
  530. }
  531. }
  532. }
  533. fclose(output_cue_file);
  534. return 0;
  535. }
  536. #ifdef _WIN32
  537. static void update_path(void)
  538. {
  539. char buff1[MAX_PATH*4], *buff2;
  540. char *path;
  541. int size, i;
  542. path = getenv("PATH");
  543. GetModuleFileNameA(NULL, buff1, sizeof(buff1));
  544. for (i = strlen(buff1)-1; i > 0; i--)
  545. if (buff1[i] == '\\') break;
  546. buff1[i] = 0;
  547. size = strlen(path) + strlen(buff1) + 3;
  548. buff2 = malloc(size);
  549. if (buff2 == NULL) return;
  550. snprintf(buff2, size, "%s;%s", path, buff1);
  551. SetEnvironmentVariableA("PATH", buff2);
  552. free(buff2);
  553. }
  554. #endif
  555. int main(int argc, char *argv[])
  556. {
  557. char out_buff[MAX_PATH], *cue_file, *out_base;
  558. int a;
  559. if(argc < 2)
  560. {
  561. printf("bin/cue to cso/mp3 converter\n");
  562. printf("usage: %s [options] <input cue> [output base]\n", argv[0]);
  563. printf("options:\n"
  564. " -m output mp3 files for audio (default) (lame required)\n"
  565. " -b <rate> mp3 bitrate to use (default is 128)\n"
  566. " -w output wav files for audio\n"
  567. " -c output cso as data track (default) (ciso required)\n"
  568. " -i output iso as data track\n");
  569. return 0;
  570. }
  571. for (a = 1; a < argc - 1; a++)
  572. {
  573. if (strcmp(argv[a], "-m") == 0)
  574. opt_use_mp3 = 1;
  575. else if (strcmp(argv[a], "-w") == 0)
  576. opt_use_mp3 = 0;
  577. else if (strcmp(argv[a], "-c") == 0)
  578. opt_use_cso = 1;
  579. else if (strcmp(argv[a], "-i") == 0)
  580. opt_use_cso = 0;
  581. else if (strcmp(argv[a], "-b") == 0)
  582. {
  583. opt_mp3_bitrate = atoi(argv[++a]);
  584. }
  585. else
  586. break;
  587. }
  588. cue_file = argv[a];
  589. out_base = argv[a+1];
  590. /* some sanity checks */
  591. if(strlen(cue_file) < 4 || strcasecmp(cue_file + strlen(cue_file) - 4, ".cue") != 0)
  592. {
  593. printf("error: not a cue file specified?\n");
  594. myexit(1);
  595. }
  596. #ifdef _WIN32
  597. update_path();
  598. #endif
  599. if(opt_use_mp3 && system(LAME_BINARY " --help " NULL_REDIR) != 0)
  600. {
  601. printf("LAME seems to be missing.\n"
  602. #ifdef _WIN32
  603. "Download from http://lame.sourceforge.net/links.php#Binaries and extract\n"
  604. "lame.exe to the same directory as %s\n", argv[0]
  605. #else
  606. "Install lame using your packet manager, obtain binaries or build from\n"
  607. "sources at http://lame.sourceforge.net/\n"
  608. #endif
  609. );
  610. myexit(1);
  611. }
  612. if(opt_use_cso && system(CISO_BINARY " " NULL_REDIR) != 0)
  613. {
  614. printf("CISO seems to be missing.\n"
  615. #ifdef _WIN32
  616. "Download ciso.exe and extract to the same directory as %s\n"
  617. "You can take ciso.exe from yacc at http://yacc.pspgen.com/\n", argv[0]
  618. #else
  619. "Install ciso using your packet manager, obtain binaries or build from\n"
  620. "sources at http://ciso.tenshu.fr/\n"
  621. #endif
  622. );
  623. myexit(1);
  624. }
  625. if(load_bin_cue(cue_file) == 0)
  626. {
  627. if(out_base == NULL)
  628. {
  629. char *p;
  630. strncpy(out_buff, cue_file, sizeof(out_buff));
  631. out_buff[sizeof(out_buff)-1] = 0;
  632. p = strrchr(out_buff, DIR_SEPARATOR_CHAR);
  633. if (p != NULL)
  634. {
  635. *p++ = 0;
  636. chdir(out_buff);
  637. memmove(out_buff, p, strlen(p)+1);
  638. }
  639. out_buff[strlen(out_buff)-4] = 0;
  640. out_base = out_buff;
  641. }
  642. if(convert_bin_cue(out_base) != 0)
  643. myexit(1);
  644. }
  645. else
  646. {
  647. printf("error: could not load cue file %s\n", cue_file);
  648. myexit(1);
  649. }
  650. return 0;
  651. }