CSL  6.0
SoundFile.h
Go to the documentation of this file.
1 ///
2 /// SoundFile.h -- CSL's abstract sound file class, a sample player UGen, SoundCue & SoundFileBuffer.
3 /// The concrete subclasses represent sound file APIs, not individual formats.
4 ///
5 /// Classes: SoundFileMetadata, Abst_SoundFile, SoundCue, SoundFileBuffer (unfinished)
6 /// We read ID3-style tags from MP4/3, AAC, FLAC, etc. files using TagLib, http://developer.kde.org/~wheeler/taglib.html
7 ///
8 /// The sound file player is actually a powerful sampler, supporting arbitrary transposition
9 /// using wavetable interpolation or a sample rate convertor; Sound files are seekable and also writeable.
10 ///
11 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT
12 ///
13 
14 #ifndef CSL_SoundFile_H
15 #define CSL_SoundFile_H
16 
17 #include "CSL_Core.h" // the core of CSL 5
18 #include "Oscillator.h" // the superclass of sound files
19 
20 #include <string.h>
21 
22 //#define USE_TAGS
23 
24 namespace csl {
25 
26 /// Sound file constants
27 
28 #ifdef CSL_ENUMS // use enumerations or integers?
29 
30 typedef enum { ///< File I/O r/w mode
31  kSoundFileClosed,
36 
37 typedef enum { /// File format
38  kSoundFileFormatAIFF, ///< AIFF
39  kSoundFileFormatWAV, ///< WAV
40  kSoundFileFormatMP3, ///< MP3
41  kSoundFileFormatMP4, ///< MP4, M4A
42  kSoundFileFormatAAC, ///< AAC
43  kSoundFileFormatFLAC, ///< FLAC
44  kSoundFileFormatCAF, ///< CoreAudio format
45  kSoundFileFormatOGG, ///< Ogg/Vorbis
46  kSoundFileFormatSHN, ///< Shorten
47  kSoundFileFormatSND, ///< Sun/NeXT SND/AU
48  kSoundFileFormatEBICSF, ///< Extended BIC (Berkeley/IRCAM/CARL) data
49  kSoundFileFormatRaw, ///< Raw data
50  kSoundFileFormatOther, ///< Other formats
52 
53 #else
54  #define kSoundFileRead 0
55  #define kSoundFileWrite 1
56  #define kSoundFileReadWrite 2
57  typedef int SoundFileMode;
58 
59  #define kSoundFileFormatWAV 0
60  #define kSoundFileFormatAIFF 1
61  #define kSoundFileFormatSND 2
62  #define kSoundFileFormatEBICSF 3
63  #define kSoundFileFormatRaw 4
64  #define kSoundFileFormatOther 5
65  typedef int SoundFileFormat;
66 #endif
67 
68 ///
69 /// Class SndFileMetadata holds the ID3 tags of a sound file
70 ///
71 
73 public:
74  std::string mTitle; ///< ID3 tag fields
75  std::string mArtist;
76  std::string mAlbum;
77  unsigned mYear;
78  std::string mComment;
79  unsigned mTrack;
80  std::string mGenre;
81 
82  unsigned mBitRate; ///< encoding data
83  unsigned mSampleRate;
84  unsigned mChannels;
85  unsigned mLength;
86 
89 
90  void dump(); ///< pretty-print the receiver
91 };
92 
93 ///
94 /// Here's the abstract sound file reader/writer class, a sample player UGen.
95 /// The concrete subclasses represent sound file APIs, not individual formats.
96 ///
97 /// The sound file player is actually a powerful sampler, supporting arbitrary transposition
98 /// using wavetable interpolation or a sample rate convertor; Sound files are seekable and also writeable.
99 ///
100 
101 class Abst_SoundFile : public WavetableOscillator, public Writeable, public Seekable {
102 public: /// Constructor. Values not passed default to null.
103  Abst_SoundFile(string path, int start = -1, int stop = -1);
104  Abst_SoundFile(Abst_SoundFile & otherSndFile); ///< Copy constructor -- shares sample buffer
105  ~Abst_SoundFile();
106 
107 
108  static bool isSndfileName(const char * path); ///< Answer whether the given name looks like a snd file
109  static SoundFileFormat sndfileNameType(const char * path); ///< Answer the snd file type
110  static const char * mimeType(const char * path); ///< Answer the MIME type based on the file name
111 
112  ///< open file and get stats; read it if "load"
113  virtual void openForRead(bool load = true) throw (CException) = 0;
114  /// Open a file for writing.
115  /// Default values are some common format.
116  virtual void openForWrite(SoundFileFormat format = kSoundFileFormatAIFF,
117  unsigned channels = 1,
118  unsigned rate = 44100,
119  unsigned bitDepth = 16) throw (CException) = 0;
120  /// seek to some position relative to "whence"
121  virtual unsigned seekTo(int position, SeekPosition whence) throw(CException) = 0;
122  unsigned seekTo(int position) throw(CException) { return seekTo(position, kPositionStart); }
123 
124  ///< read a buffer from the file (possibly all of it)
125  virtual void readBufferFromFile(unsigned numFr) = 0;
126 
127  /// UGen operations are implemented here
128  /// copy next buffer from file cache
129  virtual void nextBuffer(Buffer &outB) throw (CException);
130  /// write a buffer of data into the file
131  virtual void writeBuffer(Buffer &inB) throw (CException) = 0;
132  virtual void writeBuffer(Buffer &inB, unsigned fromFrame, unsigned toFrame)
133  throw (CException) = 0;
134  virtual SampleBuffer buffer(unsigned bufNum) { return mWavetable.buffer(bufNum); }
135 
136  /// accessors
137  unsigned channels() const; ///< # chans
138  unsigned duration() { return mNumFrames; }; ///< number of frames in the sound file
139  float durationInSecs(); ///< actual duration of the selected portion in sec
140 // virtual SoundFileFormat format() = 0; ///< get format
141  unsigned sampleSize() { return mBytesPerSample; } ///< get the bytes-per-sample
142  SoundFileMode mode() { return mMode; } ///< r/w mode
143  unsigned cacheSize() { return mWavetable.mNumAlloc; } ///< size in frames of cached portion
144 
145  void mergeToMono(); ///< average all the channels to mono
146  virtual void setToEnd(); ///< set to end position
147  virtual void trigger(); ///< reset to start
148  virtual void close() = 0; ///< close file
149  virtual void freeBuffer(); ///< free the file cache
150  void convertRate(int fromRate, int toRate); ///< perform sample-rate conversion
151 
152  bool isValid() { return mIsValid; } ///< answer if a valid file/buffer
153  bool isActive(); ///< answer if currently active
154  virtual bool isCached(); ///< answer if file is loaded into RAM
155  virtual bool isCached(unsigned samps); ///< answer if file has X samples in RAM
156 
157  virtual void setPath(string path); ///< set file name path string
158  string path() { return mPath; } ///< file name
159  virtual void dump(); ///< log snd file props
160 
161  int startFrame() { return mStart; } ///< get/set start frame
162  void setStart(int val);
163  void setStartSec(float val);
164  void setStartRatio(float val);
165  int stopFrame() { return mStop; } ///< get/set stop frame
166  void setStop(int val);
167  void setStopSec(float val);
168  void setStopRatio(float val);
169  void setBase(int val);
170  unsigned int base() { return mBase; };
171 
172  double playbackRate() { return mRate; } ///< playback rate (pitch ratio)
173  void setRate(UnitGenerator & frequency); ///< set the receiver's playback rate (pitch ratio)
174  void setRate(float frequency);
175 
176  bool isLooping() { return mIsLooping; } ///< get/set looping state
177  void setIsLooping(bool tLooping) { mIsLooping = tLooping; }
178 
179  SoundFileMetadata * mProperties; ///< the ID3 tags properties
180 
181 protected:
182  string mPath; ///< file name
183  SoundFileMode mMode; ///< r/w mode
184  SoundFileFormat mFormat; ///< sf format
185  bool mIsValid; ///< is my file valid?
186  bool mIsLooping; ///< am i looping start-stop?
187  int mStart, mStop; ///< starting/ending frames (or -1 if not used)
188  double mRate; ///< sample rate ratio
189  unsigned mNumFrames; ///< # sample frames
190  unsigned mBytesPerSample; ///< the # of bytes per sample
191  unsigned mBase; ///< starting frame in file of buffer
192 
193  virtual void initFromSndfile() = 0; ///< read SF header
194  void checkBuffer(unsigned numFrames); ///< allocate buffer lazily
195  void checkBuffer(unsigned numChans, unsigned numFrames);
196  bool readTags() throw (CException); ///< read the ID3 or other tags. Returns true if able to read them.
197 };
198 
199 ///
200 /// SoundCue -- a pointer to a segment of a sound file
201 ///
202 
203 class SoundCue : public UnitGenerator {
204 
205 public:
206  SoundCue(); /// Constructor
207  SoundCue(string name, Abst_SoundFile *file = 0, int start = 1, int stop = -1);
208 // SoundCue(string name, int start = 1, int stop = -1);
209  ~SoundCue();
210 
211  string mName; ///< my name
212  Abst_SoundFile * mFile; ///< the file I point into
213  int mStart, mStop, mCurrent; ///< the start/stop samples I represent
214  UnitGenerator *mReadRate; ///< my playback rate
215 
216  void readFrom(FILE *input); ///< for loading file lists
217  void dump(void); ///< pretty-print me
218  /// UGen operations
219  void nextBuffer(Buffer & outputBuffer) throw(CException); ///< copy next buffer from cache
220 
221  bool isActive();
222  unsigned channels() const { return (mFile->channels()); }
223  void setToEnd(void);
224  void trigger(void);
225  float duration() const { return (float) (mStop - mStart); } ///< duration in frames
226 
227 protected:
228  float mFloatCurrent; ///< current pointer as a float
229 };
230 
231 #ifdef USE_SNDFILEBUFFER
232 
233 ///
234 /// SoundFileBuffer is a buffer that streams a sound file
235 ///
236 
237 class SoundFileBuffer : public Buffer {
238 public: /// Constructor. Values not passed default to null.
239  SoundFileBuffer(string path, unsigned numFrames = CSL_mSndFileFrames);
240  SoundFileBuffer(Abst_SoundFile & otherSndFile); ///< Copy constructor -- shares sample buffer
241  ~SoundFileBuffer();
242 
243  /// answer a samp ptr with offset
244  virtual SampleBuffer samplePtrFor(unsigned channel, unsigned offset);
245  /// answer a samp ptr tested for extent (offset + maxFrame)
246  virtual SampleBuffer samplePtrFor(unsigned channel, unsigned offset, unsigned maxFrame);
247 
248  /// convenience accessors for sample buffers
249  virtual SampleBuffer buffer(unsigned bufNum) { return mBuffers[bufNum]; }
250 
251  /// Set the buffer pointer (rare; used in joiners)
252  virtual void setBuffer(unsigned bufNum, SampleBuffer sPtr) {
253  throw LogicError("Cannot set pointer to file buffer"); };
254  virtual void setBuffer(unsigned bufNum, unsigned offset, sample samp) {
255  throw LogicError("Cannot set pointer to file buffer"); };
256 
257 protected:
258 // ///< check if the buffer's big enough -- allocate it lazily
259 // virtual void checkBuffer(unsigned numFrames);
260 // virtual void checkBuffer(unsigned numChans, unsigned numFrames);
261 
262  Abst_SoundFile * mFile; ///< the file I point into
263 
264 };
265 
266 #endif
267 
268 #ifdef UNDEFINED // this was never completed...
269 
270 class SampleFile : public Abst_SoundFile {
271 public:
272  SampleFile(); /// Constructor
273  SampleFile(string name, Abst_SoundFile *file = 0, int start = 1, int stop = -1);
274  ~SampleFile();
275  /// Data members
276  unsigned mMIDIKey; // sample's MIDI key #
277  double mFrequency; // sample's actual frequency
278  double mMinRatio; // min transp.
279  double mMaxRatio; // max transp. (often 1.0)
280 
281  double ratioForKey(int desiredMIDI);
282  double ratioForPitch(int desiredMIDI);
283 };
284 
285 #endif
286 
287 }
288 
289 #endif
sample * SampleBuffer
1-channel buffer data type, vector of (sample)
Definition: CSL_Types.h:194
UnitGenerator * mReadRate
my playback rate
Definition: SoundFile.h:214
SeekPosition
Enumeration for seek flags.
Definition: CSL_Core.h:560
void setStart(int val)
Definition: SoundFile.cpp:248
SoundFileFormat mFormat
sf format
Definition: SoundFile.h:184
#define kSoundFileFormatAIFF
Definition: SoundFile.h:60
virtual void close()=0
close file
unsigned mNumFrames
sample frames
Definition: SoundFile.h:189
#define CSL_mSndFileFrames
default file cache size = 20 MFrames (~ 2 min)
Definition: CSL_Types.h:101
double mRate
sample rate ratio
Definition: SoundFile.h:188
bool isActive()
answer if currently active
Definition: SoundFile.cpp:303
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
SoundFileMode mMode
r/w mode
Definition: SoundFile.h:183
static const char * mimeType(const char *path)
Answer the MIME type based on the file name.
Definition: SoundFile.cpp:98
WavetableOscillator – Oscillator with a stored wave table that does table look-up. The default wave table is an 8192-sample sine. (perhaps accept a vector of freqs and a multichannel buffer?)
Definition: Oscillator.h:66
static SoundFileFormat sndfileNameType(const char *path)
Answer the snd file type.
Definition: SoundFile.cpp:60
#define kSoundFileRead
Sound file constants.
Definition: SoundFile.h:54
unsigned cacheSize()
size in frames of cached portion
Definition: SoundFile.h:143
unsigned mBase
starting frame in file of buffer
Definition: SoundFile.h:191
void mergeToMono()
average all the channels to mono
Definition: SoundFile.cpp:208
virtual void readBufferFromFile(unsigned numFr)=0
int SoundFileFormat
Definition: SoundFile.h:65
#define kSoundFileReadWrite
Definition: SoundFile.h:56
virtual unsigned seekTo(int position, SeekPosition whence)=0
seek to some position relative to "whence"
virtual void setToEnd()
set to end position
Definition: SoundFile.cpp:334
unsigned mBitRate
encoding data
Definition: SoundFile.h:82
virtual SampleBuffer buffer(unsigned bufNum)
convenience accessors for sample buffers
Definition: CSL_Core.cpp:66
unsigned int base()
Definition: SoundFile.h:170
virtual void setPath(string path)
set file name path string
Definition: SoundFile.cpp:153
Abst_SoundFile * mFile
the file I point into
Definition: SoundFile.h:212
string path()
file name
Definition: SoundFile.h:158
float mFloatCurrent
current pointer as a float
Definition: SoundFile.h:228
std::string mComment
Definition: SoundFile.h:78
virtual void initFromSndfile()=0
read SF header
void setStop(int val)
Definition: SoundFile.cpp:266
virtual void trigger()
reset to start
Definition: SoundFile.cpp:322
Class SndFileMetadata holds the ID3 tags of a sound file.
Definition: SoundFile.h:72
int SoundFileMode
Definition: SoundFile.h:57
#define kSoundFileFormatRaw
Definition: SoundFile.h:63
#define kSoundFileFormatEBICSF
Definition: SoundFile.h:62
void setStopRatio(float val)
Definition: SoundFile.cpp:279
bool mIsValid
is my file valid?
Definition: SoundFile.h:185
virtual void openForWrite(SoundFileFormat format=kSoundFileFormatAIFF, unsigned channels=1, unsigned rate=44100, unsigned bitDepth=16)=0
Open a file for writing. Default values are some common format.
Abst_SoundFile(string path, int start=-1, int stop=-1)
Constructor. Values not passed default to null.
Definition: SoundFile.cpp:111
#define kSoundFileFormatSND
Definition: SoundFile.h:61
bool isLooping()
get/set looping state
Definition: SoundFile.h:176
unsigned mBytesPerSample
the # of bytes per sample
Definition: SoundFile.h:190
#define kSoundFileFormatOther
Definition: SoundFile.h:64
unsigned seekTo(int position)
read a buffer from the file (possibly all of it)
Definition: SoundFile.h:122
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
#define kSoundFileWrite
Definition: SoundFile.h:55
#define kSoundFileFormatWAV
Definition: SoundFile.h:59
virtual void freeBuffer()
free the file cache
Definition: SoundFile.cpp:165
virtual void openForRead(bool load=true)=0
unsigned mNumAlloc
num frames in each buffer
Definition: CSL_Core.h:114
std::string mAlbum
Definition: SoundFile.h:76
Writeable – a mix-in for buffers and streams that one can write to.
Definition: CSL_Core.h:546
bool readTags()
read the ID3 or other tags. Returns true if able to read them.
Definition: SoundFile.cpp:340
bool mIsLooping
am i looping start-stop?
Definition: SoundFile.h:186
static bool isSndfileName(const char *path)
Answer whether the given name looks like a snd file.
Definition: SoundFile.cpp:44
int stopFrame()
get/set stop frame
Definition: SoundFile.h:165
SoundCue – a pointer to a segment of a sound file.
Definition: SoundFile.h:203
int mStop
starting/ending frames (or -1 if not used)
Definition: SoundFile.h:187
virtual void nextBuffer(Buffer &outB)
UGen operations are implemented here copy next buffer from file cache.
Definition: SoundFile.cpp:382
virtual SampleBuffer buffer(unsigned bufNum)
Definition: SoundFile.h:134
Buffer mWavetable
the stored wave form
Definition: Oscillator.h:84
void setStartRatio(float val)
Definition: SoundFile.cpp:262
void setIsLooping(bool tLooping)
Definition: SoundFile.h:177
string mPath
file name
Definition: SoundFile.h:182
bool isValid()
answer if a valid file/buffer
Definition: SoundFile.h:152
std::string mTitle
ID3 tag fields.
Definition: SoundFile.h:74
unsigned duration()
Definition: SoundFile.h:138
virtual void writeBuffer(Buffer &inB)=0
write a buffer of data into the file
void setBase(int val)
Definition: SoundFile.cpp:283
unsigned channels() const
Definition: SoundFile.h:222
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
virtual void dump()
log snd file props
Definition: SoundFile.cpp:370
void setStartSec(float val)
Definition: SoundFile.cpp:258
void convertRate(int fromRate, int toRate)
perform sample-rate conversion
Definition: SoundFile.cpp:236
void setRate(UnitGenerator &frequency)
set the receiver's playback rate (pitch ratio)
Definition: SoundFile.cpp:289
void dump()
pretty-print the receiver
Definition: SoundFile.cpp:17
double playbackRate()
playback rate (pitch ratio)
Definition: SoundFile.h:172
SoundFileMetadata * mProperties
the ID3 tags properties
Definition: SoundFile.h:179
SoundFileMode mode()
r/w mode
Definition: SoundFile.h:142
forward declaration
Definition: CSL_Core.h:241
void checkBuffer(unsigned numFrames)
allocate buffer lazily
Definition: SoundFile.cpp:172
unsigned sampleSize()
get the bytes-per-sample
Definition: SoundFile.h:141
string mName
my name
Definition: SoundFile.h:211
virtual bool isCached()
answer if file is loaded into RAM
Definition: SoundFile.cpp:309
std::string mGenre
Definition: SoundFile.h:80
std::string mArtist
Definition: SoundFile.h:75
Here's the abstract sound file reader/writer class, a sample player UGen. The concrete subclasses rep...
Definition: SoundFile.h:101
float duration() const
duration in frames
Definition: SoundFile.h:225
unsigned channels() const
accessors
Definition: SoundFile.cpp:157
int startFrame()
get/set start frame
Definition: SoundFile.h:161
Seekable – a mix-in for positionable streams.
Definition: CSL_Core.h:577
float durationInSecs()
number of frames in the sound file
Definition: SoundFile.cpp:161
Base class of CSL exceptions (written upper-case). Has a string message.
void setStopSec(float val)
Definition: SoundFile.cpp:275