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.

261 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace CDReaderHelpers
  18. {
  19. inline const XmlElement* getElementForKey (const XmlElement& xml, const String& key)
  20. {
  21. forEachXmlChildElementWithTagName (xml, child, "key")
  22. if (child->getAllSubText().trim() == key)
  23. return child->getNextElement();
  24. return nullptr;
  25. }
  26. static int getIntValueForKey (const XmlElement& xml, const String& key, int defaultValue = -1)
  27. {
  28. const XmlElement* const block = getElementForKey (xml, key);
  29. return block != nullptr ? block->getAllSubText().trim().getIntValue() : defaultValue;
  30. }
  31. // Get the track offsets for a CD given an XmlElement representing its TOC.Plist.
  32. // Returns NULL on success, otherwise a const char* representing an error.
  33. static const char* getTrackOffsets (XmlDocument& xmlDocument, Array<int>& offsets)
  34. {
  35. const ScopedPointer<XmlElement> xml (xmlDocument.getDocumentElement());
  36. if (xml == nullptr)
  37. return "Couldn't parse XML in file";
  38. const XmlElement* const dict = xml->getChildByName ("dict");
  39. if (dict == nullptr)
  40. return "Couldn't get top level dictionary";
  41. const XmlElement* const sessions = getElementForKey (*dict, "Sessions");
  42. if (sessions == nullptr)
  43. return "Couldn't find sessions key";
  44. const XmlElement* const session = sessions->getFirstChildElement();
  45. if (session == nullptr)
  46. return "Couldn't find first session";
  47. const int leadOut = getIntValueForKey (*session, "Leadout Block");
  48. if (leadOut < 0)
  49. return "Couldn't find Leadout Block";
  50. const XmlElement* const trackArray = getElementForKey (*session, "Track Array");
  51. if (trackArray == nullptr)
  52. return "Couldn't find Track Array";
  53. forEachXmlChildElement (*trackArray, track)
  54. {
  55. const int trackValue = getIntValueForKey (*track, "Start Block");
  56. if (trackValue < 0)
  57. return "Couldn't find Start Block in the track";
  58. offsets.add (trackValue * AudioCDReader::samplesPerFrame - 88200);
  59. }
  60. offsets.add (leadOut * AudioCDReader::samplesPerFrame - 88200);
  61. return nullptr;
  62. }
  63. static void findDevices (Array<File>& cds)
  64. {
  65. File volumes ("/Volumes");
  66. volumes.findChildFiles (cds, File::findDirectories, false);
  67. for (int i = cds.size(); --i >= 0;)
  68. if (! cds.getReference(i).getChildFile (".TOC.plist").exists())
  69. cds.remove (i);
  70. }
  71. struct TrackSorter
  72. {
  73. static int getCDTrackNumber (const File& file)
  74. {
  75. return file.getFileName().initialSectionContainingOnly ("0123456789").getIntValue();
  76. }
  77. static int compareElements (const File& first, const File& second)
  78. {
  79. const int firstTrack = getCDTrackNumber (first);
  80. const int secondTrack = getCDTrackNumber (second);
  81. jassert (firstTrack > 0 && secondTrack > 0);
  82. return firstTrack - secondTrack;
  83. }
  84. };
  85. }
  86. //==============================================================================
  87. StringArray AudioCDReader::getAvailableCDNames()
  88. {
  89. Array<File> cds;
  90. CDReaderHelpers::findDevices (cds);
  91. StringArray names;
  92. for (int i = 0; i < cds.size(); ++i)
  93. names.add (cds.getReference(i).getFileName());
  94. return names;
  95. }
  96. AudioCDReader* AudioCDReader::createReaderForCD (const int index)
  97. {
  98. Array<File> cds;
  99. CDReaderHelpers::findDevices (cds);
  100. if (cds[index].exists())
  101. return new AudioCDReader (cds[index]);
  102. return nullptr;
  103. }
  104. AudioCDReader::AudioCDReader (const File& volume)
  105. : AudioFormatReader (0, "CD Audio"),
  106. volumeDir (volume),
  107. currentReaderTrack (-1)
  108. {
  109. sampleRate = 44100.0;
  110. bitsPerSample = 16;
  111. numChannels = 2;
  112. usesFloatingPointData = false;
  113. refreshTrackLengths();
  114. }
  115. AudioCDReader::~AudioCDReader()
  116. {
  117. }
  118. void AudioCDReader::refreshTrackLengths()
  119. {
  120. tracks.clear();
  121. trackStartSamples.clear();
  122. lengthInSamples = 0;
  123. volumeDir.findChildFiles (tracks, File::findFiles | File::ignoreHiddenFiles, false, "*.aiff");
  124. CDReaderHelpers::TrackSorter sorter;
  125. tracks.sort (sorter);
  126. const File toc (volumeDir.getChildFile (".TOC.plist"));
  127. if (toc.exists())
  128. {
  129. XmlDocument doc (toc);
  130. const char* error = CDReaderHelpers::getTrackOffsets (doc, trackStartSamples);
  131. ignoreUnused (error); // could be logged..
  132. lengthInSamples = trackStartSamples.getLast() - trackStartSamples.getFirst();
  133. }
  134. }
  135. bool AudioCDReader::readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
  136. int64 startSampleInFile, int numSamples)
  137. {
  138. while (numSamples > 0)
  139. {
  140. int track = -1;
  141. for (int i = 0; i < trackStartSamples.size() - 1; ++i)
  142. {
  143. if (startSampleInFile < trackStartSamples.getUnchecked (i + 1))
  144. {
  145. track = i;
  146. break;
  147. }
  148. }
  149. if (track < 0)
  150. return false;
  151. if (track != currentReaderTrack)
  152. {
  153. reader = nullptr;
  154. if (FileInputStream* const in = tracks [track].createInputStream())
  155. {
  156. BufferedInputStream* const bin = new BufferedInputStream (in, 65536, true);
  157. AiffAudioFormat format;
  158. reader = format.createReaderFor (bin, true);
  159. if (reader == nullptr)
  160. currentReaderTrack = -1;
  161. else
  162. currentReaderTrack = track;
  163. }
  164. }
  165. if (reader == nullptr)
  166. return false;
  167. const int startPos = (int) (startSampleInFile - trackStartSamples.getUnchecked (track));
  168. const int numAvailable = (int) jmin ((int64) numSamples, reader->lengthInSamples - startPos);
  169. reader->readSamples (destSamples, numDestChannels, startOffsetInDestBuffer, startPos, numAvailable);
  170. numSamples -= numAvailable;
  171. startSampleInFile += numAvailable;
  172. }
  173. return true;
  174. }
  175. bool AudioCDReader::isCDStillPresent() const
  176. {
  177. return volumeDir.exists();
  178. }
  179. void AudioCDReader::ejectDisk()
  180. {
  181. JUCE_AUTORELEASEPOOL
  182. {
  183. [[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath: juceStringToNS (volumeDir.getFullPathName())];
  184. }
  185. }
  186. bool AudioCDReader::isTrackAudio (int trackNum) const
  187. {
  188. return tracks [trackNum].hasFileExtension (".aiff");
  189. }
  190. void AudioCDReader::enableIndexScanning (bool)
  191. {
  192. // any way to do this on a Mac??
  193. }
  194. int AudioCDReader::getLastIndex() const
  195. {
  196. return 0;
  197. }
  198. Array<int> AudioCDReader::findIndexesInTrack (const int /*trackNumber*/)
  199. {
  200. return Array<int>();
  201. }