1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
29 #if JUCE_USE_CDREADER || DOXYGEN
30 
31 
32 //==============================================================================
33 /**
34     A type of AudioFormatReader that reads from an audio CD.
35 
36     One of these can be used to read a CD as if it's one big audio stream. Use the
37     getPositionOfTrackStart() method to find where the individual tracks are
38     within the stream.
39 
40     @see AudioFormatReader
41 
42     @tags{Audio}
43 */
44 class JUCE_API  AudioCDReader  : public AudioFormatReader
45 {
46 public:
47     //==============================================================================
48     /** Returns a list of names of Audio CDs currently available for reading.
49 
50         If there's a CD drive but no CD in it, this might return an empty list, or
51         possibly a device that can be opened but which has no tracks, depending
52         on the platform.
53 
54         @see createReaderForCD
55     */
56     static StringArray getAvailableCDNames();
57 
58     /** Tries to create an AudioFormatReader that can read from an Audio CD.
59 
60         @param index    the index of one of the available CDs - use getAvailableCDNames()
61                         to find out how many there are.
62         @returns        a new AudioCDReader object, or nullptr if it couldn't be created. The
63                         caller will be responsible for deleting the object returned.
64     */
65     static AudioCDReader* createReaderForCD (const int index);
66 
67     //==============================================================================
68     /** Destructor. */
69     ~AudioCDReader() override;
70 
71     /** Implementation of the AudioFormatReader method. */
72     bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
73                       int64 startSampleInFile, int numSamples) override;
74 
75     /** Checks whether the CD has been removed from the drive. */
76     bool isCDStillPresent() const;
77 
78     /** Returns the total number of tracks (audio + data). */
79     int getNumTracks() const;
80 
81     /** Finds the sample offset of the start of a track.
82         @param trackNum     the track number, where trackNum = 0 is the first track
83                             and trackNum = getNumTracks() means the end of the CD.
84     */
85     int getPositionOfTrackStart (int trackNum) const;
86 
87     /** Returns true if a given track is an audio track.
88         @param trackNum     the track number, where 0 is the first track.
89     */
90     bool isTrackAudio (int trackNum) const;
91 
92     /** Returns an array of sample offsets for the start of each track, followed by
93         the sample position of the end of the CD.
94     */
95     const Array<int>& getTrackOffsets() const;
96 
97     /** Refreshes the object's table of contents.
98 
99         If the disc has been ejected and a different one put in since this
100         object was created, this will cause it to update its idea of how many tracks
101         there are, etc.
102     */
103     void refreshTrackLengths();
104 
105     /** Enables scanning for indexes within tracks.
106         @see getLastIndex
107     */
108     void enableIndexScanning (bool enabled);
109 
110     /** Returns the index number found during the last read() call.
111 
112         Index scanning is turned off by default - turn it on with enableIndexScanning().
113 
114         Then when the read() method is called, if it comes across an index within that
115         block, the index number is stored and returned by this method.
116 
117         Some devices might not support indexes, of course.
118 
119         (If you don't know what CD indexes are, it's unlikely you'll ever need them).
120 
121         @see enableIndexScanning
122     */
123     int getLastIndex() const;
124 
125     /** Scans a track to find the position of any indexes within it.
126         @param trackNumber  the track to look in, where 0 is the first track on the disc
127         @returns    an array of sample positions of any index points found (not including
128                     the index that marks the start of the track)
129     */
130     Array<int> findIndexesInTrack (const int trackNumber);
131 
132     /** Returns the CDDB id number for the CD.
133         It's not a great way of identifying a disc, but it's traditional.
134     */
135     int getCDDBId();
136 
137     /** Tries to eject the disk.
138         Ejecting the disk might not actually be possible, e.g. if some other process is using it.
139     */
140     void ejectDisk();
141 
142     //==============================================================================
143     enum
144     {
145         framesPerSecond = 75,
146         samplesPerFrame = 44100 / framesPerSecond
147     };
148 
149 private:
150     //==============================================================================
151     Array<int> trackStartSamples;
152 
153    #if JUCE_MAC
154     File volumeDir;
155     Array<File> tracks;
156     int currentReaderTrack;
157     std::unique_ptr<AudioFormatReader> reader;
158     AudioCDReader (const File& volume);
159 
160    #elif JUCE_WINDOWS
161     bool audioTracks [100];
162     void* handle;
163     MemoryBlock buffer;
164     bool indexingEnabled;
165     int lastIndex, firstFrameInBuffer, samplesInBuffer;
166     AudioCDReader (void* handle);
167     int getIndexAt (int samplePos);
168 
169    #elif JUCE_LINUX || JUCE_BSD
170     AudioCDReader();
171    #endif
172 
173     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioCDReader)
174 };
175 
176 #endif
177 
178 } // namespace juce
179