CueSharp.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. Title: CueSharp
  3. Version: 0.5
  4. Released: March 24, 2007
  5. Author: Wyatt O'Day
  6. Website: wyday.com/cuesharp
  7. */
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. namespace CueSharp_O
  12. {
  13. /// <summary>
  14. /// A CueSheet class used to create, open, edit, and save cuesheets.
  15. /// </summary>
  16. public class CueSheet
  17. {
  18. #region Private Variables
  19. private string[] cueLines;
  20. private string m_Catalog = "";
  21. private string m_CDTextFile = "";
  22. private string[] m_Comments = new string[0];
  23. // strings that don't belong or were mistyped in the global part of the cue
  24. private string[] m_Garbage = new string[0];
  25. private string m_Performer = "";
  26. private string m_Songwriter = "";
  27. private string m_Title = "";
  28. private Track[] m_Tracks = new Track[0];
  29. #endregion Private Variables
  30. #region Properties
  31. /// <summary>
  32. /// Returns/Sets track in this cuefile.
  33. /// </summary>
  34. /// <param name="tracknumber">The track in this cuefile.</param>
  35. /// <returns>Track at the tracknumber.</returns>
  36. public Track this[int tracknumber]
  37. {
  38. get
  39. {
  40. return m_Tracks[tracknumber];
  41. }
  42. set
  43. {
  44. m_Tracks[tracknumber] = value;
  45. }
  46. }
  47. /// <summary>
  48. /// The catalog number must be 13 digits long and is encoded according to UPC/EAN rules.
  49. /// Example: CATALOG 1234567890123
  50. /// </summary>
  51. public string Catalog
  52. {
  53. get { return m_Catalog; }
  54. set { m_Catalog = value; }
  55. }
  56. /// <summary>
  57. /// This command is used to specify the name of the file that contains the encoded CD-TEXT information for the disc. This command is only used with files that were either created with the graphical CD-TEXT editor or generated automatically by the software when copying a CD-TEXT enhanced disc.
  58. /// </summary>
  59. public string CDTextFile
  60. {
  61. get { return m_CDTextFile; }
  62. set { m_CDTextFile = value; }
  63. }
  64. /// <summary>
  65. /// This command is used to put comments in your CUE SHEET file.
  66. /// </summary>
  67. public string[] Comments
  68. {
  69. get { return m_Comments; }
  70. set { m_Comments = value; }
  71. }
  72. /// <summary>
  73. /// Lines in the cue file that don't belong or have other general syntax errors.
  74. /// </summary>
  75. public string[] Garbage
  76. {
  77. get { return m_Garbage; }
  78. }
  79. /// <summary>
  80. /// This command is used to specify the name of a perfomer for a CD-TEXT enhanced disc.
  81. /// </summary>
  82. public string Performer
  83. {
  84. get { return m_Performer; }
  85. set { m_Performer = value; }
  86. }
  87. /// <summary>
  88. /// This command is used to specify the name of a songwriter for a CD-TEXT enhanced disc.
  89. /// </summary>
  90. public string Songwriter
  91. {
  92. get { return m_Songwriter; }
  93. set { m_Songwriter = value; }
  94. }
  95. /// <summary>
  96. /// The title of the entire disc as a whole.
  97. /// </summary>
  98. public string Title
  99. {
  100. get { return m_Title; }
  101. set { m_Title = value; }
  102. }
  103. /// <summary>
  104. /// The array of tracks on the cuesheet.
  105. /// </summary>
  106. public Track[] Tracks
  107. {
  108. get { return m_Tracks; }
  109. set { m_Tracks = value; }
  110. }
  111. #endregion Properties
  112. #region Constructors
  113. /// <summary>
  114. /// Create a cue sheet from scratch.
  115. /// </summary>
  116. public CueSheet()
  117. { }
  118. /// <summary>
  119. /// Parse a cue sheet string.
  120. /// </summary>
  121. /// <param name="cueString">A string containing the cue sheet data.</param>
  122. /// <param name="lineDelims">Line delimeters; set to "(char[])null" for default delimeters.</param>
  123. public CueSheet(string cueString, char[] lineDelims)
  124. {
  125. if (lineDelims == null)
  126. {
  127. lineDelims = new char[] { '\n' };
  128. }
  129. cueLines = cueString.Split(lineDelims);
  130. RemoveEmptyLines(ref cueLines);
  131. ParseCue(cueLines);
  132. }
  133. /// <summary>
  134. /// Parses a cue sheet file.
  135. /// </summary>
  136. /// <param name="cuefilename">The filename for the cue sheet to open.</param>
  137. public CueSheet(string cuefilename)
  138. {
  139. ReadCueSheet(cuefilename, Encoding.Default);
  140. }
  141. /// <summary>
  142. /// Parses a cue sheet file.
  143. /// </summary>
  144. /// <param name="cuefilename">The filename for the cue sheet to open.</param>
  145. /// <param name="encoding">The encoding used to open the file.</param>
  146. public CueSheet(string cuefilename, Encoding encoding)
  147. {
  148. ReadCueSheet(cuefilename, encoding);
  149. }
  150. private void ReadCueSheet(string filename, Encoding encoding)
  151. {
  152. // array of delimiters to split the sentence with
  153. char[] delimiters = new char[] { '\n' };
  154. // read in the full cue file
  155. TextReader tr = new StreamReader(filename, encoding);
  156. //read in file
  157. cueLines = tr.ReadToEnd().Split(delimiters);
  158. // close the stream
  159. tr.Close();
  160. RemoveEmptyLines(ref cueLines);
  161. ParseCue(cueLines);
  162. }
  163. #endregion Constructors
  164. #region Methods
  165. /// <summary>
  166. /// Removes any empty lines, elimating possible trouble.
  167. /// </summary>
  168. /// <param name="file"></param>
  169. private void RemoveEmptyLines(ref string[] file)
  170. {
  171. int itemsRemoved = 0;
  172. for (int i = 0; i < file.Length; i++)
  173. {
  174. if (file[i].Trim() != "")
  175. {
  176. file[i - itemsRemoved] = file[i];
  177. }
  178. else if (file[i].Trim() == "")
  179. {
  180. itemsRemoved++;
  181. }
  182. }
  183. if (itemsRemoved > 0)
  184. {
  185. file = (string[])ResizeArray(file, file.Length - itemsRemoved);
  186. }
  187. }
  188. private void ParseCue(string[] file)
  189. {
  190. //-1 means still global,
  191. //all others are track specific
  192. int trackOn = -1;
  193. AudioFile currentFile = new AudioFile();
  194. for (int i = 0; i < file.Length; i++)
  195. {
  196. file[i] = file[i].Trim();
  197. switch (file[i].Substring(0, file[i].IndexOf(' ')).ToUpper())
  198. {
  199. case "CATALOG":
  200. ParseString(file[i], trackOn);
  201. break;
  202. case "CDTEXTFILE":
  203. ParseString(file[i], trackOn);
  204. break;
  205. case "FILE":
  206. currentFile = ParseFile(file[i], trackOn);
  207. break;
  208. case "FLAGS":
  209. ParseFlags(file[i], trackOn);
  210. break;
  211. case "INDEX":
  212. ParseIndex(file[i], trackOn);
  213. break;
  214. case "ISRC":
  215. ParseString(file[i], trackOn);
  216. break;
  217. case "PERFORMER":
  218. ParseString(file[i], trackOn);
  219. break;
  220. case "POSTGAP":
  221. ParseIndex(file[i], trackOn);
  222. break;
  223. case "PREGAP":
  224. ParseIndex(file[i], trackOn);
  225. break;
  226. case "REM":
  227. ParseComment(file[i], trackOn);
  228. break;
  229. case "SONGWRITER":
  230. ParseString(file[i], trackOn);
  231. break;
  232. case "TITLE":
  233. ParseString(file[i], trackOn);
  234. break;
  235. case "TRACK":
  236. trackOn++;
  237. ParseTrack(file[i], trackOn);
  238. if (currentFile.Filename != "") //if there's a file
  239. {
  240. m_Tracks[trackOn].DataFile = currentFile;
  241. currentFile = new AudioFile();
  242. }
  243. break;
  244. default:
  245. ParseGarbage(file[i], trackOn);
  246. //save discarded junk and place string[] with track it was found in
  247. break;
  248. }
  249. }
  250. }
  251. private void ParseComment(string line, int trackOn)
  252. {
  253. //remove "REM" (we know the line has already been .Trim()'ed)
  254. line = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' ')).Trim();
  255. if (trackOn == -1)
  256. {
  257. if (line.Trim() != "")
  258. {
  259. m_Comments = (string[])ResizeArray(m_Comments, m_Comments.Length + 1);
  260. m_Comments[m_Comments.Length - 1] = line;
  261. }
  262. }
  263. else
  264. {
  265. m_Tracks[trackOn].AddComment(line);
  266. }
  267. }
  268. private AudioFile ParseFile(string line, int trackOn)
  269. {
  270. string fileType;
  271. line = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' ')).Trim();
  272. fileType = line.Substring(line.LastIndexOf(' '), line.Length - line.LastIndexOf(' ')).Trim();
  273. line = line.Substring(0, line.LastIndexOf(' ')).Trim();
  274. //if quotes around it, remove them.
  275. if (line[0] == '"')
  276. {
  277. line = line.Substring(1, line.LastIndexOf('"') - 1);
  278. }
  279. return new AudioFile(line, fileType);
  280. }
  281. private void ParseFlags(string line, int trackOn)
  282. {
  283. string temp;
  284. if (trackOn != -1)
  285. {
  286. line = line.Trim();
  287. if (line != "")
  288. {
  289. try
  290. {
  291. temp = line.Substring(0, line.IndexOf(' ')).ToUpper();
  292. }
  293. catch (Exception)
  294. {
  295. temp = line.ToUpper();
  296. }
  297. switch (temp)
  298. {
  299. case "FLAGS":
  300. m_Tracks[trackOn].AddFlag(temp);
  301. break;
  302. case "DATA":
  303. m_Tracks[trackOn].AddFlag(temp);
  304. break;
  305. case "DCP":
  306. m_Tracks[trackOn].AddFlag(temp);
  307. break;
  308. case "4CH":
  309. m_Tracks[trackOn].AddFlag(temp);
  310. break;
  311. case "PRE":
  312. m_Tracks[trackOn].AddFlag(temp);
  313. break;
  314. case "SCMS":
  315. m_Tracks[trackOn].AddFlag(temp);
  316. break;
  317. default:
  318. break;
  319. }
  320. //processing for a case when there isn't any more spaces
  321. //i.e. avoiding the "index cannot be less than zero" error
  322. //when calling line.IndexOf(' ')
  323. try
  324. {
  325. temp = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' '));
  326. }
  327. catch (Exception)
  328. {
  329. temp = line.Substring(0, line.Length);
  330. }
  331. //if the flag hasn't already been processed
  332. if (temp.ToUpper().Trim() != line.ToUpper().Trim())
  333. {
  334. ParseFlags(temp, trackOn);
  335. }
  336. }
  337. }
  338. }
  339. private void ParseGarbage(string line, int trackOn)
  340. {
  341. if (trackOn == -1)
  342. {
  343. if (line.Trim() != "")
  344. {
  345. m_Garbage = (string[])ResizeArray(m_Garbage, m_Garbage.Length + 1);
  346. m_Garbage[m_Garbage.Length - 1] = line;
  347. }
  348. }
  349. else
  350. {
  351. m_Tracks[trackOn].AddGarbage(line);
  352. }
  353. }
  354. private void ParseIndex(string line, int trackOn)
  355. {
  356. string indexType;
  357. string tempString;
  358. int number = 0;
  359. int minutes;
  360. int seconds;
  361. int frames;
  362. indexType = line.Substring(0, line.IndexOf(' ')).ToUpper();
  363. tempString = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' ')).Trim();
  364. if (indexType == "INDEX")
  365. {
  366. //read the index number
  367. number = Convert.ToInt32(tempString.Substring(0, tempString.IndexOf(' ')));
  368. tempString = tempString.Substring(tempString.IndexOf(' '), tempString.Length - tempString.IndexOf(' ')).Trim();
  369. }
  370. //extract the minutes, seconds, and frames
  371. minutes = Convert.ToInt32(tempString.Substring(0, tempString.IndexOf(':')));
  372. seconds = Convert.ToInt32(tempString.Substring(tempString.IndexOf(':') + 1, tempString.LastIndexOf(':') - tempString.IndexOf(':') - 1));
  373. frames = Convert.ToInt32(tempString.Substring(tempString.LastIndexOf(':') + 1, tempString.Length - tempString.LastIndexOf(':') - 1));
  374. if (indexType == "INDEX")
  375. {
  376. m_Tracks[trackOn].AddIndex(number, minutes, seconds, frames);
  377. }
  378. else if (indexType == "PREGAP")
  379. {
  380. m_Tracks[trackOn].PreGap = new Index(0, minutes, seconds, frames);
  381. }
  382. else if (indexType == "POSTGAP")
  383. {
  384. m_Tracks[trackOn].PostGap = new Index(0, minutes, seconds, frames);
  385. }
  386. }
  387. private void ParseString(string line, int trackOn)
  388. {
  389. string category = line.Substring(0, line.IndexOf(' ')).ToUpper();
  390. line = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' ')).Trim();
  391. //get rid of the quotes
  392. if (line[0] == '"')
  393. {
  394. line = line.Substring(1, line.LastIndexOf('"') - 1);
  395. }
  396. switch (category)
  397. {
  398. case "CATALOG":
  399. if (trackOn == -1)
  400. {
  401. this.m_Catalog = line;
  402. }
  403. break;
  404. case "CDTEXTFILE":
  405. if (trackOn == -1)
  406. {
  407. this.m_CDTextFile = line;
  408. }
  409. break;
  410. case "ISRC":
  411. if (trackOn != -1)
  412. {
  413. m_Tracks[trackOn].ISRC = line;
  414. }
  415. break;
  416. case "PERFORMER":
  417. if (trackOn == -1)
  418. {
  419. this.m_Performer = line;
  420. }
  421. else
  422. {
  423. m_Tracks[trackOn].Performer = line;
  424. }
  425. break;
  426. case "SONGWRITER":
  427. if (trackOn == -1)
  428. {
  429. this.m_Songwriter = line;
  430. }
  431. else
  432. {
  433. m_Tracks[trackOn].Songwriter = line;
  434. }
  435. break;
  436. case "TITLE":
  437. if (trackOn == -1)
  438. {
  439. this.m_Title = line;
  440. }
  441. else
  442. {
  443. m_Tracks[trackOn].Title = line;
  444. }
  445. break;
  446. default:
  447. break;
  448. }
  449. }
  450. /// <summary>
  451. /// Parses the TRACK command.
  452. /// </summary>
  453. /// <param name="line">The line in the cue file that contains the TRACK command.</param>
  454. /// <param name="trackOn">The track currently processing.</param>
  455. private void ParseTrack(string line, int trackOn)
  456. {
  457. string tempString;
  458. int trackNumber;
  459. tempString = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' ')).Trim();
  460. try
  461. {
  462. trackNumber = Convert.ToInt32(tempString.Substring(0, tempString.IndexOf(' ')));
  463. }
  464. catch (Exception)
  465. { throw; }
  466. //find the data type.
  467. tempString = tempString.Substring(tempString.IndexOf(' '), tempString.Length - tempString.IndexOf(' ')).Trim();
  468. AddTrack(trackNumber, tempString);
  469. }
  470. /// <summary>
  471. /// Reallocates an array with a new size, and copies the contents
  472. /// of the old array to the new array.
  473. /// </summary>
  474. /// <param name="oldArray">The old array, to be reallocated.</param>
  475. /// <param name="newSize">The new array size.</param>
  476. /// <returns>A new array with the same contents.</returns>
  477. /// <remarks >Useage: int[] a = {1,2,3}; a = (int[])ResizeArray(a,5);</remarks>
  478. public static System.Array ResizeArray(System.Array oldArray, int newSize)
  479. {
  480. int oldSize = oldArray.Length;
  481. System.Type elementType = oldArray.GetType().GetElementType();
  482. System.Array newArray = System.Array.CreateInstance(elementType, newSize);
  483. int preserveLength = System.Math.Min(oldSize, newSize);
  484. if (preserveLength > 0)
  485. System.Array.Copy(oldArray, newArray, preserveLength);
  486. return newArray;
  487. }
  488. /// <summary>
  489. /// Add a track to the current cuesheet.
  490. /// </summary>
  491. /// <param name="tracknumber">The number of the said track.</param>
  492. /// <param name="datatype">The datatype of the track.</param>
  493. private void AddTrack(int tracknumber, string datatype)
  494. {
  495. m_Tracks = (Track[])ResizeArray(m_Tracks, m_Tracks.Length + 1);
  496. m_Tracks[m_Tracks.Length - 1] = new Track(tracknumber, datatype);
  497. }
  498. /// <summary>
  499. /// Add a track to the current cuesheet
  500. /// </summary>
  501. /// <param name="title">The title of the track.</param>
  502. /// <param name="performer">The performer of this track.</param>
  503. public void AddTrack(string title, string performer)
  504. {
  505. m_Tracks = (Track[])ResizeArray(m_Tracks, m_Tracks.Length + 1);
  506. m_Tracks[m_Tracks.Length - 1] = new Track(m_Tracks.Length, "");
  507. m_Tracks[m_Tracks.Length - 1].Performer = performer;
  508. m_Tracks[m_Tracks.Length - 1].Title = title;
  509. }
  510. public void AddTrack(string title, string performer, string filename, FileType fType)
  511. {
  512. m_Tracks = (Track[])ResizeArray(m_Tracks, m_Tracks.Length + 1);
  513. m_Tracks[m_Tracks.Length - 1] = new Track(m_Tracks.Length, "");
  514. m_Tracks[m_Tracks.Length - 1].Performer = performer;
  515. m_Tracks[m_Tracks.Length - 1].Title = title;
  516. m_Tracks[m_Tracks.Length - 1].DataFile = new AudioFile(filename, fType);
  517. }
  518. /// <summary>
  519. /// Add a track to the current cuesheet
  520. /// </summary>
  521. /// <param name="title">The title of the track.</param>
  522. /// <param name="performer">The performer of this track.</param>
  523. /// <param name="datatype">The datatype for the track (typically DataType.Audio)</param>
  524. public void AddTrack(string title, string performer, DataType datatype)
  525. {
  526. m_Tracks = (Track[])ResizeArray(m_Tracks, m_Tracks.Length + 1);
  527. m_Tracks[m_Tracks.Length - 1] = new Track(m_Tracks.Length, datatype);
  528. m_Tracks[m_Tracks.Length - 1].Performer = performer;
  529. m_Tracks[m_Tracks.Length - 1].Title = title;
  530. }
  531. /// <summary>
  532. /// Add a track to the current cuesheet
  533. /// </summary>
  534. /// <param name="track">Track object to add to the cuesheet.</param>
  535. public void AddTrack(Track track)
  536. {
  537. m_Tracks = (Track[])ResizeArray(m_Tracks, m_Tracks.Length + 1);
  538. m_Tracks[m_Tracks.Length - 1] = track;
  539. }
  540. /// <summary>
  541. /// Remove a track from the cuesheet.
  542. /// </summary>
  543. /// <param name="trackIndex">The index of the track you wish to remove.</param>
  544. public void RemoveTrack(int trackIndex)
  545. {
  546. for (int i = trackIndex; i < m_Tracks.Length - 1; i++)
  547. {
  548. m_Tracks[i] = m_Tracks[i + 1];
  549. }
  550. m_Tracks = (Track[])ResizeArray(m_Tracks, m_Tracks.Length - 1);
  551. }
  552. /// <summary>
  553. /// Add index information to an existing track.
  554. /// </summary>
  555. /// <param name="trackIndex">The array index number of track to be modified</param>
  556. /// <param name="indexNum">The index number of the new index</param>
  557. /// <param name="minutes">The minute value of the new index</param>
  558. /// <param name="seconds">The seconds value of the new index</param>
  559. /// <param name="frames">The frames value of the new index</param>
  560. public void AddIndex(int trackIndex, int indexNum, int minutes, int seconds, int frames)
  561. {
  562. m_Tracks[trackIndex].AddIndex(indexNum, minutes, seconds, frames);
  563. }
  564. /// <summary>
  565. /// Remove an index from a track.
  566. /// </summary>
  567. /// <param name="trackIndex">The array-index of the track.</param>
  568. /// <param name="indexIndex">The index of the Index you wish to remove.</param>
  569. public void RemoveIndex(int trackIndex, int indexIndex)
  570. {
  571. //Note it is the index of the Index you want to delete,
  572. //which may or may not correspond to the number of the index.
  573. m_Tracks[trackIndex].RemoveIndex(indexIndex);
  574. }
  575. /// <summary>
  576. /// Save the cue sheet file to specified location.
  577. /// </summary>
  578. /// <param name="filename">Filename of destination cue sheet file.</param>
  579. public void SaveCue(string filename)
  580. {
  581. SaveCue(filename, Encoding.Default);
  582. }
  583. /// <summary>
  584. /// Save the cue sheet file to specified location.
  585. /// </summary>
  586. /// <param name="filename">Filename of destination cue sheet file.</param>
  587. /// <param name="encoding">The encoding used to save the file.</param>
  588. public void SaveCue(string filename, Encoding encoding)
  589. {
  590. TextWriter tw = new StreamWriter(filename, false, encoding);
  591. tw.WriteLine(this.ToString());
  592. //close the writer stream
  593. tw.Close();
  594. }
  595. /// <summary>
  596. /// Method to output the cuesheet into a single formatted string.
  597. /// </summary>
  598. /// <returns>The entire cuesheet formatted to specification.</returns>
  599. public override string ToString()
  600. {
  601. StringBuilder output = new StringBuilder();
  602. foreach (string comment in m_Comments)
  603. {
  604. output.Append("REM " + comment + Environment.NewLine);
  605. }
  606. if (m_Catalog.Trim() != "")
  607. {
  608. output.Append("CATALOG " + m_Catalog + Environment.NewLine);
  609. }
  610. if (m_Performer.Trim() != "")
  611. {
  612. output.Append("PERFORMER \"" + m_Performer + "\"" + Environment.NewLine);
  613. }
  614. if (m_Songwriter.Trim() != "")
  615. {
  616. output.Append("SONGWRITER \"" + m_Songwriter + "\"" + Environment.NewLine);
  617. }
  618. if (m_Title.Trim() != "")
  619. {
  620. output.Append("TITLE \"" + m_Title + "\"" + Environment.NewLine);
  621. }
  622. if (m_CDTextFile.Trim() != "")
  623. {
  624. output.Append("CDTEXTFILE \"" + m_CDTextFile.Trim() + "\"" + Environment.NewLine);
  625. }
  626. for (int i = 0; i < m_Tracks.Length; i++)
  627. {
  628. output.Append(m_Tracks[i].ToString());
  629. if (i != m_Tracks.Length - 1)
  630. {
  631. //add line break for each track except last
  632. output.Append(Environment.NewLine);
  633. }
  634. }
  635. return output.ToString();
  636. }
  637. #endregion Methods
  638. //TODO: Fix calculation bugs; currently generates erroneous IDs.
  639. #region CalculateDiscIDs
  640. //For complete CDDB/freedb discID calculation, see:
  641. //http://www.freedb.org/modules.php?name=Sections&sop=viewarticle&artid=6
  642. public string CalculateCDDBdiscID()
  643. {
  644. int i, t = 0, n = 0;
  645. /* For backward compatibility this algorithm must not change */
  646. i = 0;
  647. while (i < m_Tracks.Length)
  648. {
  649. n = n + cddb_sum((lastTrackIndex(m_Tracks[i]).Minutes * 60) + lastTrackIndex(m_Tracks[i]).Seconds);
  650. i++;
  651. }
  652. Console.WriteLine(n.ToString());
  653. t = ((lastTrackIndex(m_Tracks[m_Tracks.Length - 1]).Minutes * 60) + lastTrackIndex(m_Tracks[m_Tracks.Length - 1]).Seconds) -
  654. ((lastTrackIndex(m_Tracks[0]).Minutes * 60) + lastTrackIndex(m_Tracks[0]).Seconds);
  655. ulong lDiscId = (((uint)n % 0xff) << 24 | (uint)t << 8 | (uint)m_Tracks.Length);
  656. return String.Format("{0:x8}", lDiscId);
  657. }
  658. private Index lastTrackIndex(Track track)
  659. {
  660. return track.Indices[track.Indices.Length - 1];
  661. }
  662. private int cddb_sum(int n)
  663. {
  664. int ret;
  665. /* For backward compatibility this algorithm must not change */
  666. ret = 0;
  667. while (n > 0)
  668. {
  669. ret = ret + (n % 10);
  670. n = n / 10;
  671. }
  672. return (ret);
  673. }
  674. #endregion CalculateDiscIDs
  675. }
  676. /// <summary>
  677. ///DCP - Digital copy permitted
  678. ///4CH - Four channel audio
  679. ///PRE - Pre-emphasis enabled (audio tracks only)
  680. ///SCMS - Serial copy management system (not supported by all recorders)
  681. ///There is a fourth subcode flag called "DATA" which is set for all non-audio tracks. This flag is set automatically based on the datatype of the track.
  682. /// </summary>
  683. public enum Flags
  684. {
  685. DCP, CH4, PRE, SCMS, DATA, NONE
  686. }
  687. /// <summary>
  688. /// BINARY - Intel binary file (least significant byte first)
  689. /// MOTOROLA - Motorola binary file (most significant byte first)
  690. /// AIFF - Audio AIFF file
  691. /// WAVE - Audio WAVE file
  692. /// MP3 - Audio MP3 file
  693. /// </summary>
  694. public enum FileType
  695. {
  696. BINARY, MOTOROLA, AIFF, WAVE, MP3
  697. }
  698. /// <summary>
  699. /// <list>
  700. /// <item>AUDIO - Audio/Music (2352)</item>
  701. /// <item>CDG - Karaoke CD+G (2448)</item>
  702. /// <item>MODE1/2048 - CDROM Mode1 Data (cooked)</item>
  703. /// <item>MODE1/2352 - CDROM Mode1 Data (raw)</item>
  704. /// <item>MODE2/2336 - CDROM-XA Mode2 Data</item>
  705. /// <item>MODE2/2352 - CDROM-XA Mode2 Data</item>
  706. /// <item>CDI/2336 - CDI Mode2 Data</item>
  707. /// <item>CDI/2352 - CDI Mode2 Data</item>
  708. /// </list>
  709. /// </summary>
  710. public enum DataType
  711. {
  712. AUDIO, CDG, MODE1_2048, MODE1_2352, MODE2_2336, MODE2_2352, CDI_2336, CDI_2352
  713. }
  714. /// <summary>
  715. /// This command is used to specify indexes (or subindexes) within a track.
  716. /// Syntax:
  717. /// INDEX [number] [mm:ss:ff]
  718. /// </summary>
  719. public struct Index
  720. {
  721. //0-99
  722. private int m_number;
  723. private int m_minutes;
  724. private int m_seconds;
  725. private int m_frames;
  726. /// <summary>
  727. /// Index number (0-99)
  728. /// </summary>
  729. public int Number
  730. {
  731. get { return m_number; }
  732. set
  733. {
  734. if (value > 99)
  735. {
  736. m_number = 99;
  737. }
  738. else if (value < 0)
  739. {
  740. m_number = 0;
  741. }
  742. else
  743. {
  744. m_number = value;
  745. }
  746. }
  747. }
  748. /// <summary>
  749. /// Possible values: 0-99
  750. /// </summary>
  751. public int Minutes
  752. {
  753. get { return m_minutes; }
  754. set
  755. {
  756. if (value > 99)
  757. {
  758. m_minutes = 99;
  759. }
  760. else if (value < 0)
  761. {
  762. m_minutes = 0;
  763. }
  764. else
  765. {
  766. m_minutes = value;
  767. }
  768. }
  769. }
  770. /// <summary>
  771. /// Possible values: 0-59
  772. /// There are 60 seconds/minute
  773. /// </summary>
  774. public int Seconds
  775. {
  776. get { return m_seconds; }
  777. set
  778. {
  779. if (value >= 60)
  780. {
  781. m_seconds = 59;
  782. }
  783. else if (value < 0)
  784. {
  785. m_seconds = 0;
  786. }
  787. else
  788. {
  789. m_seconds = value;
  790. }
  791. }
  792. }
  793. /// <summary>
  794. /// Possible values: 0-74
  795. /// There are 75 frames/second
  796. /// </summary>
  797. public int Frames
  798. {
  799. get { return m_frames; }
  800. set
  801. {
  802. if (value >= 75)
  803. {
  804. m_frames = 74;
  805. }
  806. else if (value < 0)
  807. {
  808. m_frames = 0;
  809. }
  810. else
  811. {
  812. m_frames = value;
  813. }
  814. }
  815. }
  816. /// <summary>
  817. /// The Index of a track.
  818. /// </summary>
  819. /// <param name="number">Index number 0-99</param>
  820. /// <param name="minutes">Minutes (0-99)</param>
  821. /// <param name="seconds">Seconds (0-59)</param>
  822. /// <param name="frames">Frames (0-74)</param>
  823. public Index(int number, int minutes, int seconds, int frames)
  824. {
  825. m_number = number;
  826. m_minutes = minutes;
  827. m_seconds = seconds;
  828. m_frames = frames;
  829. }
  830. }
  831. /// <summary>
  832. /// This command is used to specify a data/audio file that will be written to the recorder.
  833. /// </summary>
  834. public struct AudioFile
  835. {
  836. private string m_Filename;
  837. private FileType m_Filetype;
  838. public string Filename
  839. {
  840. get { return m_Filename; }
  841. set { m_Filename = value; }
  842. }
  843. /// <summary>
  844. /// BINARY - Intel binary file (least significant byte first)
  845. /// MOTOROLA - Motorola binary file (most significant byte first)
  846. /// AIFF - Audio AIFF file
  847. /// WAVE - Audio WAVE file
  848. /// MP3 - Audio MP3 file
  849. /// </summary>
  850. public FileType Filetype
  851. {
  852. get { return m_Filetype; }
  853. set { m_Filetype = value; }
  854. }
  855. public AudioFile(string filename, string filetype)
  856. {
  857. m_Filename = filename;
  858. switch (filetype.Trim().ToUpper())
  859. {
  860. case "BINARY":
  861. m_Filetype = FileType.BINARY;
  862. break;
  863. case "MOTOROLA":
  864. m_Filetype = FileType.MOTOROLA;
  865. break;
  866. case "AIFF":
  867. m_Filetype = FileType.AIFF;
  868. break;
  869. case "WAVE":
  870. m_Filetype = FileType.WAVE;
  871. break;
  872. case "MP3":
  873. m_Filetype = FileType.MP3;
  874. break;
  875. default:
  876. m_Filetype = FileType.BINARY;
  877. break;
  878. }
  879. }
  880. public AudioFile(string filename, FileType filetype)
  881. {
  882. m_Filename = filename;
  883. m_Filetype = filetype;
  884. }
  885. }
  886. /// <summary>
  887. /// Track that contains either data or audio. It can contain Indices and comment information.
  888. /// </summary>
  889. public struct Track
  890. {
  891. #region Private Variables
  892. private string[] m_Comments;
  893. // strings that don't belong or were mistyped in the global part of the cue
  894. private AudioFile m_DataFile;
  895. private string[] m_Garbage;
  896. private Index[] m_Indices;
  897. private string m_ISRC;
  898. private string m_Performer;
  899. private Index m_PostGap;
  900. private Index m_PreGap;
  901. private string m_Songwriter;
  902. private string m_Title;
  903. private Flags[] m_TrackFlags;
  904. private DataType m_TrackDataType;
  905. private int m_TrackNumber;
  906. #endregion Private Variables
  907. #region Properties
  908. /// <summary>
  909. /// Returns/Sets Index in this track.
  910. /// </summary>
  911. /// <param name="indexnumber">Index in the track.</param>
  912. /// <returns>Index at indexnumber.</returns>
  913. public Index this[int indexnumber]
  914. {
  915. get
  916. {
  917. return m_Indices[indexnumber];
  918. }
  919. set
  920. {
  921. m_Indices[indexnumber] = value;
  922. }
  923. }
  924. public string[] Comments
  925. {
  926. get { return m_Comments; }
  927. set { m_Comments = value; }
  928. }
  929. public AudioFile DataFile
  930. {
  931. get { return m_DataFile; }
  932. set { m_DataFile = value; }
  933. }
  934. /// <summary>
  935. /// Lines in the cue file that don't belong or have other general syntax errors.
  936. /// </summary>
  937. public string[] Garbage
  938. {
  939. get { return m_Garbage; }
  940. set { m_Garbage = value; }
  941. }
  942. public Index[] Indices
  943. {
  944. get { return m_Indices; }
  945. set { m_Indices = value; }
  946. }
  947. public string ISRC
  948. {
  949. get { return m_ISRC; }
  950. set { m_ISRC = value; }
  951. }
  952. public string Performer
  953. {
  954. get { return m_Performer; }
  955. set { m_Performer = value; }
  956. }
  957. public Index PostGap
  958. {
  959. get { return m_PostGap; }
  960. set { m_PostGap = value; }
  961. }
  962. public Index PreGap
  963. {
  964. get { return m_PreGap; }
  965. set { m_PreGap = value; }
  966. }
  967. public string Songwriter
  968. {
  969. get { return m_Songwriter; }
  970. set { m_Songwriter = value; }
  971. }
  972. /// <summary>
  973. /// If the TITLE command appears before any TRACK commands, then the string will be encoded as the title of the entire disc.
  974. /// </summary>
  975. public string Title
  976. {
  977. get { return m_Title; }
  978. set { m_Title = value; }
  979. }
  980. public DataType TrackDataType
  981. {
  982. get { return m_TrackDataType; }
  983. set { m_TrackDataType = value; }
  984. }
  985. public Flags[] TrackFlags
  986. {
  987. get { return m_TrackFlags; }
  988. set { m_TrackFlags = value; }
  989. }
  990. public int TrackNumber
  991. {
  992. get { return m_TrackNumber; }
  993. set { m_TrackNumber = value; }
  994. }
  995. #endregion Properties
  996. #region Contructors
  997. public Track(int tracknumber, string datatype)
  998. {
  999. m_TrackNumber = tracknumber;
  1000. switch (datatype.Trim().ToUpper())
  1001. {
  1002. case "AUDIO":
  1003. m_TrackDataType = DataType.AUDIO;
  1004. break;
  1005. case "CDG":
  1006. m_TrackDataType = DataType.CDG;
  1007. break;
  1008. case "MODE1/2048":
  1009. m_TrackDataType = DataType.MODE1_2048;
  1010. break;
  1011. case "MODE1/2352":
  1012. m_TrackDataType = DataType.MODE1_2352;
  1013. break;
  1014. case "MODE2/2336":
  1015. m_TrackDataType = DataType.MODE2_2336;
  1016. break;
  1017. case "MODE2/2352":
  1018. m_TrackDataType = DataType.MODE2_2352;
  1019. break;
  1020. case "CDI/2336":
  1021. m_TrackDataType = DataType.CDI_2336;
  1022. break;
  1023. case "CDI/2352":
  1024. m_TrackDataType = DataType.CDI_2352;
  1025. break;
  1026. default:
  1027. m_TrackDataType = DataType.AUDIO;
  1028. break;
  1029. }
  1030. m_TrackFlags = new Flags[0];
  1031. m_Songwriter = "";
  1032. m_Title = "";
  1033. m_ISRC = "";
  1034. m_Performer = "";
  1035. m_Indices = new Index[0];
  1036. m_Garbage = new string[0];
  1037. m_Comments = new string[0];
  1038. m_PreGap = new Index(-1, 0, 0, 0);
  1039. m_PostGap = new Index(-1, 0, 0, 0);
  1040. m_DataFile = new AudioFile();
  1041. }
  1042. public Track(int tracknumber, DataType datatype)
  1043. {
  1044. m_TrackNumber = tracknumber;
  1045. m_TrackDataType = datatype;
  1046. m_TrackFlags = new Flags[0];
  1047. m_Songwriter = "";
  1048. m_Title = "";
  1049. m_ISRC = "";
  1050. m_Performer = "";
  1051. m_Indices = new Index[0];
  1052. m_Garbage = new string[0];
  1053. m_Comments = new string[0];
  1054. m_PreGap = new Index(-1, 0, 0, 0);
  1055. m_PostGap = new Index(-1, 0, 0, 0);
  1056. m_DataFile = new AudioFile();
  1057. }
  1058. #endregion Contructors
  1059. #region Methods
  1060. public void AddFlag(Flags flag)
  1061. {
  1062. //if it's not a none tag
  1063. //and if the tags hasn't already been added
  1064. if (flag != Flags.NONE && NewFlag(flag) == true)
  1065. {
  1066. m_TrackFlags = (Flags[])CueSheet.ResizeArray(m_TrackFlags, m_TrackFlags.Length + 1);
  1067. m_TrackFlags[m_TrackFlags.Length - 1] = flag;
  1068. }
  1069. }
  1070. public void AddFlag(string flag)
  1071. {
  1072. switch (flag.Trim().ToUpper())
  1073. {
  1074. case "DATA":
  1075. AddFlag(Flags.DATA);
  1076. break;
  1077. case "DCP":
  1078. AddFlag(Flags.DCP);
  1079. break;
  1080. case "4CH":
  1081. AddFlag(Flags.CH4);
  1082. break;
  1083. case "PRE":
  1084. AddFlag(Flags.PRE);
  1085. break;
  1086. case "SCMS":
  1087. AddFlag(Flags.SCMS);
  1088. break;
  1089. default:
  1090. return;
  1091. }
  1092. }
  1093. public void AddGarbage(string garbage)
  1094. {
  1095. if (garbage.Trim() != "")
  1096. {
  1097. m_Garbage = (string[])CueSheet.ResizeArray(m_Garbage, m_Garbage.Length + 1);
  1098. m_Garbage[m_Garbage.Length - 1] = garbage;
  1099. }
  1100. }
  1101. public void AddComment(string comment)
  1102. {
  1103. if (comment.Trim() != "")
  1104. {
  1105. m_Comments = (string[])CueSheet.ResizeArray(m_Comments, m_Comments.Length + 1);
  1106. m_Comments[m_Comments.Length - 1] = comment;
  1107. }
  1108. }
  1109. public void AddIndex(int number, int minutes, int seconds, int frames)
  1110. {
  1111. m_Indices = (Index[])CueSheet.ResizeArray(m_Indices, m_Indices.Length + 1);
  1112. m_Indices[m_Indices.Length - 1] = new Index(number, minutes, seconds, frames);
  1113. }
  1114. public void RemoveIndex(int indexIndex)
  1115. {
  1116. for (int i = indexIndex; i < m_Indices.Length - 1; i++)
  1117. {
  1118. m_Indices[i] = m_Indices[i + 1];
  1119. }
  1120. m_Indices = (Index[])CueSheet.ResizeArray(m_Indices, m_Indices.Length - 1);
  1121. }
  1122. /// <summary>
  1123. /// Checks if the flag is indeed new in this track.
  1124. /// </summary>
  1125. /// <param name="flag">The new flag to be added to the track.</param>
  1126. /// <returns>True if this flag doesn't already exist.</returns>
  1127. private bool NewFlag(Flags new_flag)
  1128. {
  1129. foreach (Flags flag in m_TrackFlags)
  1130. {
  1131. if (flag == new_flag)
  1132. {
  1133. return false;
  1134. }
  1135. }
  1136. return true;
  1137. }
  1138. public override string ToString()
  1139. {
  1140. StringBuilder output = new StringBuilder();
  1141. //write file
  1142. if (m_DataFile.Filename != null && m_DataFile.Filename.Trim() != "")
  1143. {
  1144. output.Append("FILE \"" + m_DataFile.Filename.Trim() + "\" " + m_DataFile.Filetype.ToString() + Environment.NewLine);
  1145. }
  1146. output.Append(" TRACK " + m_TrackNumber.ToString().PadLeft(2, '0') + " " + m_TrackDataType.ToString().Replace('_', '/'));
  1147. //write comments
  1148. foreach (string comment in m_Comments)
  1149. {
  1150. output.Append(Environment.NewLine + " REM " + comment);
  1151. }
  1152. if (m_Performer.Trim() != "")
  1153. {
  1154. output.Append(Environment.NewLine + " PERFORMER \"" + m_Performer + "\"");
  1155. }
  1156. if (m_Songwriter.Trim() != "")
  1157. {
  1158. output.Append(Environment.NewLine + " SONGWRITER \"" + m_Songwriter + "\"");
  1159. }
  1160. if (m_Title.Trim() != "")
  1161. {
  1162. output.Append(Environment.NewLine + " TITLE \"" + m_Title + "\"");
  1163. }
  1164. //write flags
  1165. if (m_TrackFlags.Length > 0)
  1166. {
  1167. output.Append(Environment.NewLine + " FLAGS");
  1168. }
  1169. foreach (Flags flag in m_TrackFlags)
  1170. {
  1171. output.Append(" " + flag.ToString().Replace("CH4", "4CH"));
  1172. }
  1173. //write isrc
  1174. if (m_ISRC.Trim() != "")
  1175. {
  1176. output.Append(Environment.NewLine + " ISRC " + m_ISRC.Trim());
  1177. }
  1178. //write pregap
  1179. if (m_PreGap.Number != -1)
  1180. {
  1181. output.Append(Environment.NewLine + " PREGAP " + m_PreGap.Minutes.ToString().PadLeft(2, '0') + ":" + m_PreGap.Seconds.ToString().PadLeft(2, '0') + ":" + m_PreGap.Frames.ToString().PadLeft(2, '0'));
  1182. }
  1183. //write Indices
  1184. for (int j = 0; j < m_Indices.Length; j++)
  1185. {
  1186. output.Append(Environment.NewLine + " INDEX " + this[j].Number.ToString().PadLeft(2, '0') + " " + this[j].Minutes.ToString().PadLeft(2, '0') + ":" + this[j].Seconds.ToString().PadLeft(2, '0') + ":" + this[j].Frames.ToString().PadLeft(2, '0'));
  1187. }
  1188. //write postgap
  1189. if (m_PostGap.Number != -1)
  1190. {
  1191. output.Append(Environment.NewLine + " POSTGAP " + m_PostGap.Minutes.ToString().PadLeft(2, '0') + ":" + m_PostGap.Seconds.ToString().PadLeft(2, '0') + ":" + m_PostGap.Frames.ToString().PadLeft(2, '0'));
  1192. }
  1193. return output.ToString();
  1194. }
  1195. #endregion Methods
  1196. }
  1197. }