Extract audio and cue sheet from an NRG audio CD image.
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.

161 lines
5.0 KiB

  1. // This file is part of the NRGrip project.
  2. //
  3. // Copyright (c) 2016, 2018 Matteo Cypriani <mcy@lm7.fr>
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //! NRG CUEX chunk data structure and associated functions.
  23. use std::fmt;
  24. use std::fs::File;
  25. use ::error::NrgError;
  26. use super::readers::*;
  27. #[derive(Debug)]
  28. pub struct NrgCuex {
  29. pub size: u32,
  30. pub tracks: Vec<NrgCuexTrack>,
  31. }
  32. impl NrgCuex {
  33. pub fn new() -> NrgCuex {
  34. NrgCuex {
  35. size: 0,
  36. tracks: Vec::new(),
  37. }
  38. }
  39. }
  40. impl fmt::Display for NrgCuex {
  41. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  42. try!(write!(f, "Chunk ID: CUEX\n\
  43. Chunk description: Cue Sheet\n\
  44. Chunk size: {} Bytes", self.size));
  45. if self.tracks.is_empty() {
  46. try!(write!(f, "\nNo CUEX tracks!"));
  47. } else {
  48. for track in &self.tracks {
  49. try!(write!(f, "\n\
  50. Track:\n\
  51. {}", track));
  52. }
  53. }
  54. Ok(())
  55. }
  56. }
  57. #[derive(Copy, Clone, Debug)]
  58. pub struct NrgCuexTrack {
  59. pub mode: u8,
  60. pub track_number: u8,
  61. pub index_number: u8,
  62. pub padding: u8,
  63. pub position_sectors: i32,
  64. }
  65. impl NrgCuexTrack {
  66. pub fn new() -> NrgCuexTrack {
  67. NrgCuexTrack {
  68. mode: 0,
  69. track_number: 0,
  70. index_number: 0,
  71. padding: 0,
  72. position_sectors: 0,
  73. }
  74. }
  75. }
  76. impl fmt::Display for NrgCuexTrack {
  77. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  78. try!(writeln!(f, "\tMode: 0x{:02X}", self.mode));
  79. try!(write!(f, "\tTrack number: "));
  80. if self.track_number == 0 {
  81. try!(writeln!(f, "0 (lead-in area)"));
  82. } else if self.track_number == 0xAA {
  83. try!(writeln!(f, "0xAA (lead-out area)"));
  84. } else {
  85. try!(writeln!(f, "{}", self.track_number));
  86. }
  87. try!(writeln!(f, "\tIndex number: {}", self.index_number));
  88. if self.padding != 0 {
  89. try!(writeln!(f, "\tPadding: {} (Warning: should be 0!)",
  90. self.padding));
  91. }
  92. // Audio CDs are played at a 75 sectors per second rate:
  93. let position_seconds: f64 = (self.position_sectors as f64) / 75.0;
  94. write!(f, "\tPosition: {} sectors ({:.2} seconds)",
  95. self.position_sectors, position_seconds)
  96. }
  97. }
  98. /// Reads the NRG Cue Sheet chunk (CUEX) from `fd`.
  99. ///
  100. /// The CUEX is constituted of the following data:
  101. ///
  102. /// - 4 B: Chunk size (in bytes): size to be read *after* this chunk size
  103. /// (should be a multiple of 8)
  104. ///
  105. /// - one or more pairs of 8-byte track blocks composed of:
  106. /// + 1 B: Mode (values found: 0x01 for audio; 0x21 for non
  107. /// copyright-protected audio; 0x41 for data)
  108. /// + 1 B: Track number (BCD coded; 0xAA for the lead-out area)
  109. /// + 1 B: Index number (probably BCD coded): 0 or 1
  110. /// + 1 B: Unknown (padding?), always 0
  111. /// + 4 B: Position in sectors (signed integer value)
  112. ///
  113. /// - one last track block like the ones above, for the lead-out area
  114. /// (optional?)
  115. pub fn read_nrg_cuex(fd: &mut File) -> Result<NrgCuex, NrgError> {
  116. let mut chunk = NrgCuex::new();
  117. chunk.size = try!(read_u32(fd));
  118. let mut bytes_read = 0;
  119. // Read all the 8-byte track info
  120. while bytes_read < chunk.size {
  121. chunk.tracks.push(try!(read_nrg_cuex_track(fd)));
  122. bytes_read += 8;
  123. }
  124. assert_eq!(bytes_read, chunk.size);
  125. Ok(chunk)
  126. }
  127. /// Reads a 8-byte track block from the NRG cue sheet.
  128. ///
  129. /// See the documentation for read_nrg_cuex() for the format of the track
  130. /// blocks.
  131. fn read_nrg_cuex_track(fd: &mut File) -> Result<NrgCuexTrack, NrgError> {
  132. let mut track = NrgCuexTrack::new();
  133. track.mode = try!(read_u8(fd));
  134. track.track_number = try!(read_u8_bcd(fd));
  135. track.index_number = try!(read_u8_bcd(fd));
  136. track.padding = try!(read_u8(fd));
  137. track.position_sectors = try!(read_u32(fd)) as i32;
  138. Ok(track)
  139. }