CSL  6.0
SoundFileL.h
Go to the documentation of this file.
1 ///
2 /// SoundFileL.h -- concrete sound file class using libsndfile
3 ///
4 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT
5 ///
6 /// The MP3 file playback is a mild hack; it first converts each MP3 file to a temp AIFF file and uses
7 /// that for playback. There is no MP3 writing (encoding) at present.
8 ///
9 
10 #ifndef CSL_SoundFileL_H
11 #define CSL_SoundFileL_H
12 
13 #include "SoundFile.h" // abstract class header
14 #include <sndfile.h> // libsndfile header file
15  // NB: set these as compile-time flags now
16 #ifndef CSL_WINDOWS
17 // #define USE_libMAD // Support MP3 file reading using libMAD
18 // #define USE_libFAAD // Support AAC/MP4 file reading using libFAAD
19 #endif
20 
21 #include <sndfile.h> // libsndfile header file
22 #include <string.h>
23 
24 // Temp name macro -- also used by other services
25 // NB: This is UNIX-specific - ToDo: use run-time file sep
26 // These are no longer used
27 
28 #if 0
29 
30 #define MP3_TEMP_NAME(in_path, out_path, temp_dir) { \
31  sprintf(out_path, "%s%s", temp_dir, in_path); \
32  char * lastDot = strrchr(out_path, (int) '.'); \
33  if (lastDot) sprintf(lastDot, MP3_TEMP_EXT); \
34  char * pos = out_path; \
35  pos += strlen(temp_dir); \
36  pos = strchr(pos, (int) '/'); \
37  while (pos) { \
38  *pos = '_'; \
39  pos = strchr(pos, (int) '/'); \
40  }}
41 
42 // AIFF name macro -- also used by other services
43 // This assumes the in_path ends with ".mp3"
44 
45 #define AIFF_TEMP_NAME(in_path, out_path) { \
46  strcpy(out_path, in_path); \
47  if (strcasestr(in_path, ".mp3")) \
48  strcpy((out_path + strlen(out_path) - 4), ".aiff"); }
49 
50 #endif // 0
51 
52 namespace csl {
53 
54 // Call-back functions for SF_VIRTUAL_IO struct
55 
56 //sf_vio_get_filelen lsf_getFileLen(void * vBuffer);
57 //sf_vio_seek lsf_seek(sf_count_t offset, int whence, void * vBuffer);
58 //sf_vio_tell lsf_tell(void * vBuffer);
59 //sf_vio_read lsf_read(void *ptr, sf_count_t count, void * vBuffer);
60 //sf_vio_write lsf_write(const void *ptr, sf_count_t count, void * vBuffer);
61 
62 ///
63 /// Here's the sound file reader/writer class; it assumes libSndFile and interleaved sample buffers
64 ///
65 
66 class LSoundFile : public Abst_SoundFile {
67 public: /// Constructor with defaults
68  LSoundFile(std::string path, int start = -1, int stop = -1, bool doRead = true, float maxDurInSecs = 0.0);
69  LSoundFile(float maxDurInSecs, std::string path); ///< this version sets maxSize and always reads
70 // LSoundFile(char * buffer); ///< virtual version uses a sample buffer
71  LSoundFile(LSoundFile & otherSndFile); ///< Copy constructor -- shares sample buffer
72  ~LSoundFile();
73  /// Factory methods
74  static LSoundFile * openSndfile(string path, int start = -1, int stop = -1, bool doRead = true);
75  static LSoundFile * openSndfile(float maxDurInSecs, string path);
76 
77  SoundFileFormat format(); ///< get format
78 
79  virtual void openForRead(bool load = true) throw (CException); ///< open file and get stats
80  /// Open a file for write.
81  /// Default values are some common format.
83  unsigned channels = 1,
84  unsigned rate = 44100,
85  unsigned bitDepth = 16) throw (CException);
86  void openForReadWrite() throw (CException); ///< open r/w
87  void close(); ///< close file
88  /// seek to some position
89  unsigned seekTo(int position, SeekPosition whence = kPositionStart) throw(CException);
90  /// read a buffer from the file (possibly all of it)
91  void readBufferFromFile(unsigned numFrames);
92 
93  /// UGen operations
94  void nextBuffer(Buffer &outB) throw (CException); ///< copy next buffer from cache
95  void writeBuffer(Buffer &inputBuffer) throw (CException); ///< write a buffer of data into the file
96  void writeBuffer(Buffer &inB, unsigned fromFrame, unsigned toFrame) throw (CException);
97  bool isCached(); ///< answer if file has all of its samples in RAM
98  bool isCached(unsigned samps); ///< answer if file has X samples in RAM
99 
100  SF_INFO * sfInfo() { return mSFInfo; } ///< libsndfile sf-info struct
101  SNDFILE * sndFile() { return mSndfile; } ///< libsndfile handle
102 
103 protected:
104  SF_INFO * mSFInfo; ///< libsndfile sf-info struct
105  SNDFILE * mSndfile; ///< libsndfile handle
106  Interleaver mInterleaver; ///< File IO interleaver/deinterleaver
107  float mMaxDurInSecs; ///< max size to read from file. In seconds so it can deal with varying sample rates.
108 
109 //#ifdef CSL_USE_SRConv
110 // Buffer mSRConvBuffer; ///< used by the sample rate convertor
111 // SRC_STATE * mSRateConv; ///< sample rate convertor (SRC) state struct
112 // SRC_DATA mSRateData; ///< SRC call data struct
113 // int mSRCReturn; ///< SRC error flag
114 //#endif
115 
116  void initFromSndfile(); ///< read SF header
117 
118 // void checkBuffer(unsigned numFrames); ///< allocate buffer lazily
119 // void checkBuffer(unsigned numChans, unsigned numFrames);
120 };
121 
122 }
123 
124 #endif
SeekPosition
Enumeration for seek flags.
Definition: CSL_Core.h:560
#define kSoundFileFormatAIFF
Definition: SoundFile.h:60
SF_INFO * mSFInfo
libsndfile sf-info struct
Definition: SoundFileL.h:104
float mMaxDurInSecs
max size to read from file. In seconds so it can deal with varying sample rates.
Definition: SoundFileL.h:107
void readBufferFromFile(unsigned numFrames)
read a buffer from the file (possibly all of it)
Definition: SoundFileL.cpp:408
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
Interleaver handles copying interleaved sample buffers (like sound files and inter-process sockets) t...
Definition: CSL_Core.h:672
bool isCached()
answer if file has all of its samples in RAM
Definition: SoundFileL.cpp:170
int SoundFileFormat
Definition: SoundFile.h:65
void nextBuffer(Buffer &outB)
UGen operations.
Definition: SoundFileL.cpp:288
SoundFileFormat format()
get format
Definition: SoundFileL.cpp:180
string path()
file name
Definition: SoundFile.h:158
void openForWrite(SoundFileFormat format=kSoundFileFormatAIFF, unsigned channels=1, unsigned rate=44100, unsigned bitDepth=16)
Open a file for writing. Default values are some common format.
Definition: SoundFileL.cpp:220
virtual void openForRead(bool load=true)
open file and get stats Open a file for write. Default values are some common format.
Definition: SoundFileL.cpp:198
void close()
close file seek to some position
Definition: SoundFileL.cpp:260
void writeBuffer(Buffer &inputBuffer)
write a buffer of data into the file
Definition: SoundFileL.cpp:371
static LSoundFile * openSndfile(string path, int start=-1, int stop=-1, bool doRead=true)
Factory methods.
Definition: SoundFileL.cpp:18
SNDFILE * sndFile()
libsndfile handle
Definition: SoundFileL.h:101
Interleaver mInterleaver
File IO interleaver/deinterleaver.
Definition: SoundFileL.h:106
void openForReadWrite()
open r/w
Definition: SoundFileL.cpp:254
void initFromSndfile()
read SF header
Definition: SoundFileL.cpp:124
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
LSoundFile(std::string path, int start=-1, int stop=-1, bool doRead=true, float maxDurInSecs=0.0)
Constructor with defaults.
unsigned seekTo(int position, SeekPosition whence=kPositionStart)
seek to some position relative to "whence"
Definition: SoundFileL.cpp:268
Here's the abstract sound file reader/writer class, a sample player UGen. The concrete subclasses rep...
Definition: SoundFile.h:101
Here's the sound file reader/writer class; it assumes libSndFile and interleaved sample buffers...
Definition: SoundFileL.h:66
SF_INFO * sfInfo()
libsndfile sf-info struct
Definition: SoundFileL.h:100
SNDFILE * mSndfile
libsndfile handle
Definition: SoundFileL.h:105
unsigned channels() const
accessors
Definition: SoundFile.cpp:157
Base class of CSL exceptions (written upper-case). Has a string message.