CSL  5.2
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 WIN32
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 method
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  static bool isSndfileName(const char * path); ///< Answer whether the given name looks like a snd file
78  static SoundFileFormat sndfileNameType(const char * path); ///< Answer the snd file type
79  static const char * mimeType(const char * path); ///< Answer the MIME type based on the file name
80 
81  SoundFileFormat format(); ///< get format
82 
83  virtual void openForRead(bool load = true) throw (CException); ///< open file and get stats
84  /// Open a file for write.
85  /// Default values are some common format.
87  unsigned channels = 1,
88  unsigned rate = 44100,
89  unsigned bitDepth = 16) throw (CException);
90  void openForReadWrite() throw (CException); ///< open r/w
91  void close(); ///< close file
92  /// seek to some position
93  unsigned seekTo(int position, SeekPosition whence = kPositionStart) throw(CException);
94  /// read a buffer from the file (possibly all of it)
95  void readBufferFromFile(unsigned numFrames);
96 
97  /// UGen operations
98  void nextBuffer(Buffer &outB) throw (CException); ///< copy next buffer from cache
99  void writeBuffer(Buffer &inputBuffer) throw (CException); ///< write a buffer of data into the file
100  bool isCached(); ///< answer if file has all of its samples in RAM
101  bool isCached(unsigned samps); ///< answer if file has X samples in RAM
102 
103  SF_INFO * sfInfo() { return mSFInfo; } ///< libsndfile sf-info struct
104  SNDFILE * sndFile() { return mSndfile; } ///< libsndfile handle
105 
106 protected:
107  SF_INFO * mSFInfo; ///< libsndfile sf-info struct
108  SNDFILE * mSndfile; ///< libsndfile handle
109  Interleaver mInterleaver; ///< File IO interleaver/deinterleaver
110  float mMaxDurInSecs; ///< max size to read from file. In seconds so it can deal with varying sample rates.
111 
112 //#ifdef CSL_USE_SRConv
113 // Buffer mSRConvBuffer; ///< used by the sample rate convertor
114 // SRC_STATE * mSRateConv; ///< sample rate convertor (SRC) state struct
115 // SRC_DATA mSRateData; ///< SRC call data struct
116 // int mSRCReturn; ///< SRC error flag
117 //#endif
118 
119  void initFromSndfile(); ///< read SF header
120 
121 // void checkBuffer(unsigned numFrames); ///< allocate buffer lazily
122 // void checkBuffer(unsigned numChans, unsigned numFrames);
123 };
124 
125 }
126 
127 #endif