CSL  5.2
SoundFileMP3.h
Go to the documentation of this file.
1 ///
2 /// SoundFileMP3.h -- concrete sound file class using libMAD for mp3 and libFAAD for mp4
3 ///
4 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT
5 ///
6 /// These classes load their target file into a buffer on openForRead().
7 /// There is no MP3/4 writing (encoding) at present.
8 ///
9 
10 #ifndef CSL_SoundFileMP3_H
11 #define CSL_SoundFileMP3_H
12 
13 #include "SoundFileL.h" // abstract class header
14 #include <sndfile.h> // libsndfile header file
15 #include <string.h>
16  // NB: set these as compile-time flags now
17 //#define CSL_USE_SRConv // Support sample rate conversion
18 //#define DEFAULT_MP3_RATE 44100 // default sample rate for MP3 files
19 
20 namespace csl {
21 
22 ///
23 /// DecodedFile - abstract class for MP3/4/AAC - read-only decoded files
24 /// Provides a BufferVector for reading files of unknown size.
25 ///
26 
27 class DecodedFile : public LSoundFile {
28 public: /// Constructor.
29  DecodedFile(string path = "", int start = -1, int stop = -1, float maxDurInSecs = 0.0);
30  ~DecodedFile();
31 // virtual void openForRead() throw (CException); ///< open file and get stats
32  /// Open a file for write (an error for decoded files).
34  unsigned channels = 1,
35  unsigned rate = 44100,
36  unsigned bitDepth = 16) throw (CException);
37  void openForReadWrite() throw (CException); ///< open r/w (error)
38  /// seek to some position
39 // unsigned seekTo(int position, SeekPosition whence = kPositionStart) throw(CException);
40  void nextBuffer(Buffer &outB) throw (CException); ///< copy next buffer from cache
41 
42  /// store samples into the receiver, adding another buffer if necessary. Returns true if we should do so, false if there are no more samples.
43  bool writeBuffer (float * stereo_buffer, unsigned num_samples);
44  bool writeBuffer (float * L_buffer, float * R_buffer, unsigned num_samples);
45  bool writeBuffer (int * L_buffer, int * R_buffer, unsigned num_samples);
46 
47 protected:
48  BufferVector mBuffers; ///< temp decoder buffers
49  /// merge (concatenate) the buffers into 1 after loading
50  void mergeBuffers();
51  bool checkBufferStack(unsigned numFrames);
52  bool addBuffer(unsigned numChannels, unsigned numFrames);
53  unsigned totalSize(); ///< answer the total size of the buffer list
54 };
55 
56 //-----------------------------------------------------------------------------//
57 
58 #ifdef USE_libMAD // Support MP3 file reading using libMAD
59 
60 #include "math.h"
61 #include <fcntl.h>
62 #include <sys/mman.h>
63 #include <sys/stat.h>
64 #include "mad.h"
65 
66 ///
67 /// MP3File - decodes MP3s into a buffer upon openForRead()
68 ///
69 
70 class MP3File : public DecodedFile {
71 public: /// Constructor.
72  MP3File(string path = "", int start = -1, int stop = -1);
73  MP3File(float maxDurInSecs, string path);
74  ~MP3File();
75 
76  void openForRead() throw (CException); ///< open file and get stats
77 
78  unsigned mMP3Rate; ///< the actual rate of the MP3 file
79 
80 protected:
81  int decodeMP3() throw (CException); ///< decode an MP3 file into a buffer
82 };
83 
84 #endif // USE_libMAD
85 
86 //-----------------------------------------------------------------------------//
87 
88 #ifdef USE_libFAAD // Support AAC/MP4 file reading using libFAAD
89 
90 #define HAVE_STDINT_H
91 
92 #include <faad.h>
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <time.h>
96 #include <fcntl.h>
97 #include <neaacdec.h>
98 #include <mp4ff.h>
99 
100 #define MAX_CHANNELS 6 // make this higher to support files with more channels
101 
102 /// FAAD file/buffer struct
103 
104 typedef struct {
105  long bytes_into_buffer; ///< read pos
106  long bytes_consumed; ///< write pos
107  long file_offset; ///< offset in file
108  unsigned char *buffer; ///< storage
109  int at_eof; ///< if at EOF
110  FILE *infile; ///< input FILE ptr
111 } aac_buffer;
112 
113 ///
114 /// MP4File - decodes AAC & MP4 into a buffer upon openForRead()
115 ///
116 
117 class MP4File : public DecodedFile {
118 public: /// Constructor.
119  MP4File(string path = "", int start = -1, int stop = -1);
120  MP4File(float maxDurInSecs, string path);
121  ~MP4File();
122 
123  void openForRead() throw (CException); ///< open file and get stats
124 
125 protected:
126  csl::Status decodeAACfile(FILE * inFile) throw (CException); ///< read and decode an AAC file
127  csl::Status decodeMP4file(FILE * inFile) throw (CException); ///< read and decode an MP4 file
128  void printMP4(mp4ff_t * infile, int track, unsigned *song_length);
129 
130  /// inst vars
131  NeAACDecHandle hDecoder;
132  NeAACDecFrameInfo frameInfo;
133  NeAACDecConfigurationPtr config;
134  mp4AudioSpecificConfig mp4ASC;
135  aac_buffer aacBuff;
136 };
137 
138 #endif // USE_libFAAD
139 
140 } // end of namespace
141 
142 #endif