sndfile.hh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. ** Copyright (C) 2005-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** All rights reserved.
  5. **
  6. ** Redistribution and use in source and binary forms, with or without
  7. ** modification, are permitted provided that the following conditions are
  8. ** met:
  9. **
  10. ** * Redistributions of source code must retain the above copyright
  11. ** notice, this list of conditions and the following disclaimer.
  12. ** * Redistributions in binary form must reproduce the above copyright
  13. ** notice, this list of conditions and the following disclaimer in
  14. ** the documentation and/or other materials provided with the
  15. ** distribution.
  16. ** * Neither the author nor the names of any contributors may be used
  17. ** to endorse or promote products derived from this software without
  18. ** specific prior written permission.
  19. **
  20. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. ** The above modified BSD style license (GPL and LGPL compatible) applies to
  34. ** this file. It does not apply to libsndfile itself which is released under
  35. ** the GNU LGPL or the libsndfile test suite which is released under the GNU
  36. ** GPL.
  37. ** This means that this header file can be used under this modified BSD style
  38. ** license, but the LGPL still holds for the libsndfile library itself.
  39. */
  40. /*
  41. ** sndfile.hh -- A lightweight C++ wrapper for the libsndfile API.
  42. **
  43. ** All the methods are inlines and all functionality is contained in this
  44. ** file. There is no separate implementation file.
  45. **
  46. ** API documentation is in the doc/ directory of the source code tarball
  47. ** and at http://www.mega-nerd.com/libsndfile/api.html.
  48. */
  49. #ifndef SNDFILE_HH
  50. #define SNDFILE_HH
  51. #include <sndfile.h>
  52. #include <string>
  53. #include <new> // for std::nothrow
  54. class SndfileHandle
  55. { private :
  56. struct SNDFILE_ref
  57. { SNDFILE_ref (void) ;
  58. ~SNDFILE_ref (void) ;
  59. SNDFILE *sf ;
  60. SF_INFO sfinfo ;
  61. int ref ;
  62. } ;
  63. SNDFILE_ref *p ;
  64. public :
  65. /* Default constructor */
  66. SndfileHandle (void) : p (NULL) {} ;
  67. SndfileHandle (const char *path, int mode = SFM_READ,
  68. int format = 0, int channels = 0, int samplerate = 0) ;
  69. SndfileHandle (std::string const & path, int mode = SFM_READ,
  70. int format = 0, int channels = 0, int samplerate = 0) ;
  71. SndfileHandle (int fd, bool close_desc, int mode = SFM_READ,
  72. int format = 0, int channels = 0, int samplerate = 0) ;
  73. SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode = SFM_READ,
  74. int format = 0, int channels = 0, int samplerate = 0) ;
  75. #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
  76. SndfileHandle (LPCWSTR wpath, int mode = SFM_READ,
  77. int format = 0, int channels = 0, int samplerate = 0) ;
  78. #endif
  79. ~SndfileHandle (void) ;
  80. SndfileHandle (const SndfileHandle &orig) ;
  81. SndfileHandle & operator = (const SndfileHandle &rhs) ;
  82. /* Mainly for debugging/testing. */
  83. int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
  84. operator bool () const { return (p != NULL) ; }
  85. bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
  86. sf_count_t frames (void) const { return p ? p->sfinfo.frames : 0 ; }
  87. int format (void) const { return p ? p->sfinfo.format : 0 ; }
  88. int channels (void) const { return p ? p->sfinfo.channels : 0 ; }
  89. int samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; }
  90. int error (void) const ;
  91. const char * strError (void) const ;
  92. int command (int cmd, void *data, int datasize) ;
  93. sf_count_t seek (sf_count_t frames, int whence) ;
  94. void writeSync (void) ;
  95. int setString (int str_type, const char* str) ;
  96. const char* getString (int str_type) const ;
  97. static int formatCheck (int format, int channels, int samplerate) ;
  98. sf_count_t read (short *ptr, sf_count_t items) ;
  99. sf_count_t read (int *ptr, sf_count_t items) ;
  100. sf_count_t read (float *ptr, sf_count_t items) ;
  101. sf_count_t read (double *ptr, sf_count_t items) ;
  102. sf_count_t write (const short *ptr, sf_count_t items) ;
  103. sf_count_t write (const int *ptr, sf_count_t items) ;
  104. sf_count_t write (const float *ptr, sf_count_t items) ;
  105. sf_count_t write (const double *ptr, sf_count_t items) ;
  106. sf_count_t readf (short *ptr, sf_count_t frames) ;
  107. sf_count_t readf (int *ptr, sf_count_t frames) ;
  108. sf_count_t readf (float *ptr, sf_count_t frames) ;
  109. sf_count_t readf (double *ptr, sf_count_t frames) ;
  110. sf_count_t writef (const short *ptr, sf_count_t frames) ;
  111. sf_count_t writef (const int *ptr, sf_count_t frames) ;
  112. sf_count_t writef (const float *ptr, sf_count_t frames) ;
  113. sf_count_t writef (const double *ptr, sf_count_t frames) ;
  114. sf_count_t readRaw (void *ptr, sf_count_t bytes) ;
  115. sf_count_t writeRaw (const void *ptr, sf_count_t bytes) ;
  116. /**< Raw access to the handle. SndfileHandle keeps ownership. */
  117. SNDFILE * rawHandle (void) ;
  118. /**< Take ownership of handle, if reference count is 1. */
  119. SNDFILE * takeOwnership (void) ;
  120. } ;
  121. /*==============================================================================
  122. ** Nothing but implementation below.
  123. */
  124. inline
  125. SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
  126. : sf (NULL), sfinfo (), ref (1)
  127. {}
  128. inline
  129. SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
  130. { if (sf != NULL) sf_close (sf) ; }
  131. inline
  132. SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
  133. : p (NULL)
  134. {
  135. p = new (std::nothrow) SNDFILE_ref () ;
  136. if (p != NULL)
  137. { p->ref = 1 ;
  138. p->sfinfo.frames = 0 ;
  139. p->sfinfo.channels = chans ;
  140. p->sfinfo.format = fmt ;
  141. p->sfinfo.samplerate = srate ;
  142. p->sfinfo.sections = 0 ;
  143. p->sfinfo.seekable = 0 ;
  144. p->sf = sf_open (path, mode, &p->sfinfo) ;
  145. } ;
  146. return ;
  147. } /* SndfileHandle const char * constructor */
  148. inline
  149. SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
  150. : p (NULL)
  151. {
  152. p = new (std::nothrow) SNDFILE_ref () ;
  153. if (p != NULL)
  154. { p->ref = 1 ;
  155. p->sfinfo.frames = 0 ;
  156. p->sfinfo.channels = chans ;
  157. p->sfinfo.format = fmt ;
  158. p->sfinfo.samplerate = srate ;
  159. p->sfinfo.sections = 0 ;
  160. p->sfinfo.seekable = 0 ;
  161. p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
  162. } ;
  163. return ;
  164. } /* SndfileHandle std::string constructor */
  165. inline
  166. SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
  167. : p (NULL)
  168. {
  169. if (fd < 0)
  170. return ;
  171. p = new (std::nothrow) SNDFILE_ref () ;
  172. if (p != NULL)
  173. { p->ref = 1 ;
  174. p->sfinfo.frames = 0 ;
  175. p->sfinfo.channels = chans ;
  176. p->sfinfo.format = fmt ;
  177. p->sfinfo.samplerate = srate ;
  178. p->sfinfo.sections = 0 ;
  179. p->sfinfo.seekable = 0 ;
  180. p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ;
  181. } ;
  182. return ;
  183. } /* SndfileHandle fd constructor */
  184. inline
  185. SndfileHandle::SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode, int fmt, int chans, int srate)
  186. : p (NULL)
  187. {
  188. p = new (std::nothrow) SNDFILE_ref () ;
  189. if (p != NULL)
  190. { p->ref = 1 ;
  191. p->sfinfo.frames = 0 ;
  192. p->sfinfo.channels = chans ;
  193. p->sfinfo.format = fmt ;
  194. p->sfinfo.samplerate = srate ;
  195. p->sfinfo.sections = 0 ;
  196. p->sfinfo.seekable = 0 ;
  197. p->sf = sf_open_virtual (&sfvirtual, mode, &p->sfinfo, user_data) ;
  198. } ;
  199. return ;
  200. } /* SndfileHandle std::string constructor */
  201. inline
  202. SndfileHandle::~SndfileHandle (void)
  203. { if (p != NULL && --p->ref == 0)
  204. delete p ;
  205. } /* SndfileHandle destructor */
  206. inline
  207. SndfileHandle::SndfileHandle (const SndfileHandle &orig)
  208. : p (orig.p)
  209. { if (p != NULL)
  210. ++p->ref ;
  211. } /* SndfileHandle copy constructor */
  212. inline SndfileHandle &
  213. SndfileHandle::operator = (const SndfileHandle &rhs)
  214. {
  215. if (&rhs == this)
  216. return *this ;
  217. if (p != NULL && --p->ref == 0)
  218. delete p ;
  219. p = rhs.p ;
  220. if (p != NULL)
  221. ++p->ref ;
  222. return *this ;
  223. } /* SndfileHandle assignment operator */
  224. inline int
  225. SndfileHandle::error (void) const
  226. { return sf_error (p->sf) ; }
  227. inline const char *
  228. SndfileHandle::strError (void) const
  229. { return sf_strerror (p->sf) ; }
  230. inline int
  231. SndfileHandle::command (int cmd, void *data, int datasize)
  232. { return sf_command (p->sf, cmd, data, datasize) ; }
  233. inline sf_count_t
  234. SndfileHandle::seek (sf_count_t frame_count, int whence)
  235. { return sf_seek (p->sf, frame_count, whence) ; }
  236. inline void
  237. SndfileHandle::writeSync (void)
  238. { sf_write_sync (p->sf) ; }
  239. inline int
  240. SndfileHandle::setString (int str_type, const char* str)
  241. { return sf_set_string (p->sf, str_type, str) ; }
  242. inline const char*
  243. SndfileHandle::getString (int str_type) const
  244. { return sf_get_string (p->sf, str_type) ; }
  245. inline int
  246. SndfileHandle::formatCheck (int fmt, int chans, int srate)
  247. {
  248. SF_INFO sfinfo ;
  249. sfinfo.frames = 0 ;
  250. sfinfo.channels = chans ;
  251. sfinfo.format = fmt ;
  252. sfinfo.samplerate = srate ;
  253. sfinfo.sections = 0 ;
  254. sfinfo.seekable = 0 ;
  255. return sf_format_check (&sfinfo) ;
  256. }
  257. /*---------------------------------------------------------------------*/
  258. inline sf_count_t
  259. SndfileHandle::read (short *ptr, sf_count_t items)
  260. { return sf_read_short (p->sf, ptr, items) ; }
  261. inline sf_count_t
  262. SndfileHandle::read (int *ptr, sf_count_t items)
  263. { return sf_read_int (p->sf, ptr, items) ; }
  264. inline sf_count_t
  265. SndfileHandle::read (float *ptr, sf_count_t items)
  266. { return sf_read_float (p->sf, ptr, items) ; }
  267. inline sf_count_t
  268. SndfileHandle::read (double *ptr, sf_count_t items)
  269. { return sf_read_double (p->sf, ptr, items) ; }
  270. inline sf_count_t
  271. SndfileHandle::write (const short *ptr, sf_count_t items)
  272. { return sf_write_short (p->sf, ptr, items) ; }
  273. inline sf_count_t
  274. SndfileHandle::write (const int *ptr, sf_count_t items)
  275. { return sf_write_int (p->sf, ptr, items) ; }
  276. inline sf_count_t
  277. SndfileHandle::write (const float *ptr, sf_count_t items)
  278. { return sf_write_float (p->sf, ptr, items) ; }
  279. inline sf_count_t
  280. SndfileHandle::write (const double *ptr, sf_count_t items)
  281. { return sf_write_double (p->sf, ptr, items) ; }
  282. inline sf_count_t
  283. SndfileHandle::readf (short *ptr, sf_count_t frame_count)
  284. { return sf_readf_short (p->sf, ptr, frame_count) ; }
  285. inline sf_count_t
  286. SndfileHandle::readf (int *ptr, sf_count_t frame_count)
  287. { return sf_readf_int (p->sf, ptr, frame_count) ; }
  288. inline sf_count_t
  289. SndfileHandle::readf (float *ptr, sf_count_t frame_count)
  290. { return sf_readf_float (p->sf, ptr, frame_count) ; }
  291. inline sf_count_t
  292. SndfileHandle::readf (double *ptr, sf_count_t frame_count)
  293. { return sf_readf_double (p->sf, ptr, frame_count) ; }
  294. inline sf_count_t
  295. SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
  296. { return sf_writef_short (p->sf, ptr, frame_count) ; }
  297. inline sf_count_t
  298. SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
  299. { return sf_writef_int (p->sf, ptr, frame_count) ; }
  300. inline sf_count_t
  301. SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
  302. { return sf_writef_float (p->sf, ptr, frame_count) ; }
  303. inline sf_count_t
  304. SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
  305. { return sf_writef_double (p->sf, ptr, frame_count) ; }
  306. inline sf_count_t
  307. SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
  308. { return sf_read_raw (p->sf, ptr, bytes) ; }
  309. inline sf_count_t
  310. SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
  311. { return sf_write_raw (p->sf, ptr, bytes) ; }
  312. inline SNDFILE *
  313. SndfileHandle::rawHandle (void)
  314. { return (p ? p->sf : NULL) ; }
  315. inline SNDFILE *
  316. SndfileHandle::takeOwnership (void)
  317. {
  318. if (p == NULL || (p->ref != 1))
  319. return NULL ;
  320. SNDFILE * sf = p->sf ;
  321. p->sf = NULL ;
  322. delete p ;
  323. p = NULL ;
  324. return sf ;
  325. }
  326. #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
  327. inline
  328. SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate)
  329. : p (NULL)
  330. {
  331. p = new (std::nothrow) SNDFILE_ref () ;
  332. if (p != NULL)
  333. { p->ref = 1 ;
  334. p->sfinfo.frames = 0 ;
  335. p->sfinfo.channels = chans ;
  336. p->sfinfo.format = fmt ;
  337. p->sfinfo.samplerate = srate ;
  338. p->sfinfo.sections = 0 ;
  339. p->sfinfo.seekable = 0 ;
  340. p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
  341. } ;
  342. return ;
  343. } /* SndfileHandle const wchar_t * constructor */
  344. #endif
  345. #endif /* SNDFILE_HH */