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.

260 lines
7.5KB

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