Audio plugin host https://kx.studio/carla
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.

262 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. reader (0)
  109. {
  110. sampleRate = 44100.0;
  111. bitsPerSample = 16;
  112. numChannels = 2;
  113. usesFloatingPointData = false;
  114. refreshTrackLengths();
  115. }
  116. AudioCDReader::~AudioCDReader()
  117. {
  118. }
  119. void AudioCDReader::refreshTrackLengths()
  120. {
  121. tracks.clear();
  122. trackStartSamples.clear();
  123. lengthInSamples = 0;
  124. volumeDir.findChildFiles (tracks, File::findFiles | File::ignoreHiddenFiles, false, "*.aiff");
  125. CDReaderHelpers::TrackSorter sorter;
  126. tracks.sort (sorter);
  127. const File toc (volumeDir.getChildFile (".TOC.plist"));
  128. if (toc.exists())
  129. {
  130. XmlDocument doc (toc);
  131. const char* error = CDReaderHelpers::getTrackOffsets (doc, trackStartSamples);
  132. (void) error; // could be logged..
  133. lengthInSamples = trackStartSamples.getLast() - trackStartSamples.getFirst();
  134. }
  135. }
  136. bool AudioCDReader::readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
  137. int64 startSampleInFile, int numSamples)
  138. {
  139. while (numSamples > 0)
  140. {
  141. int track = -1;
  142. for (int i = 0; i < trackStartSamples.size() - 1; ++i)
  143. {
  144. if (startSampleInFile < trackStartSamples.getUnchecked (i + 1))
  145. {
  146. track = i;
  147. break;
  148. }
  149. }
  150. if (track < 0)
  151. return false;
  152. if (track != currentReaderTrack)
  153. {
  154. reader = nullptr;
  155. if (FileInputStream* const in = tracks [track].createInputStream())
  156. {
  157. BufferedInputStream* const bin = new BufferedInputStream (in, 65536, true);
  158. AiffAudioFormat format;
  159. reader = format.createReaderFor (bin, true);
  160. if (reader == nullptr)
  161. currentReaderTrack = -1;
  162. else
  163. currentReaderTrack = track;
  164. }
  165. }
  166. if (reader == nullptr)
  167. return false;
  168. const int startPos = (int) (startSampleInFile - trackStartSamples.getUnchecked (track));
  169. const int numAvailable = (int) jmin ((int64) numSamples, reader->lengthInSamples - startPos);
  170. reader->readSamples (destSamples, numDestChannels, startOffsetInDestBuffer, startPos, numAvailable);
  171. numSamples -= numAvailable;
  172. startSampleInFile += numAvailable;
  173. }
  174. return true;
  175. }
  176. bool AudioCDReader::isCDStillPresent() const
  177. {
  178. return volumeDir.exists();
  179. }
  180. void AudioCDReader::ejectDisk()
  181. {
  182. JUCE_AUTORELEASEPOOL
  183. {
  184. [[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath: juceStringToNS (volumeDir.getFullPathName())];
  185. }
  186. }
  187. bool AudioCDReader::isTrackAudio (int trackNum) const
  188. {
  189. return tracks [trackNum].hasFileExtension (".aiff");
  190. }
  191. void AudioCDReader::enableIndexScanning (bool)
  192. {
  193. // any way to do this on a Mac??
  194. }
  195. int AudioCDReader::getLastIndex() const
  196. {
  197. return 0;
  198. }
  199. Array<int> AudioCDReader::findIndexesInTrack (const int /*trackNumber*/)
  200. {
  201. return Array<int>();
  202. }