/project/jni/sndfile/include/sndfile.hh

https://github.com/aichunyu/FFPlayer · C++ Header · 422 lines · 270 code · 96 blank · 56 comment · 33 complexity · aa9bd1cb9b1369ae36ccd3e396cec3fa MD5 · raw file

  1. /*
  2. ** Copyright (C) 2005-2011 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. #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
  74. SndfileHandle (LPCWSTR wpath, int mode = SFM_READ,
  75. int format = 0, int channels = 0, int samplerate = 0) ;
  76. #endif
  77. ~SndfileHandle (void) ;
  78. SndfileHandle (const SndfileHandle &orig) ;
  79. SndfileHandle & operator = (const SndfileHandle &rhs) ;
  80. /* Mainly for debugging/testing. */
  81. int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
  82. operator bool () const { return (p != NULL) ; }
  83. bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
  84. sf_count_t frames (void) const { return p ? p->sfinfo.frames : 0 ; }
  85. int format (void) const { return p ? p->sfinfo.format : 0 ; }
  86. int channels (void) const { return p ? p->sfinfo.channels : 0 ; }
  87. int samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; }
  88. int error (void) const ;
  89. const char * strError (void) const ;
  90. int command (int cmd, void *data, int datasize) ;
  91. sf_count_t seek (sf_count_t frames, int whence) ;
  92. void writeSync (void) ;
  93. int setString (int str_type, const char* str) ;
  94. const char* getString (int str_type) const ;
  95. static int formatCheck (int format, int channels, int samplerate) ;
  96. sf_count_t read (short *ptr, sf_count_t items) ;
  97. sf_count_t read (int *ptr, sf_count_t items) ;
  98. sf_count_t read (float *ptr, sf_count_t items) ;
  99. sf_count_t read (double *ptr, sf_count_t items) ;
  100. sf_count_t write (const short *ptr, sf_count_t items) ;
  101. sf_count_t write (const int *ptr, sf_count_t items) ;
  102. sf_count_t write (const float *ptr, sf_count_t items) ;
  103. sf_count_t write (const double *ptr, sf_count_t items) ;
  104. sf_count_t readf (short *ptr, sf_count_t frames) ;
  105. sf_count_t readf (int *ptr, sf_count_t frames) ;
  106. sf_count_t readf (float *ptr, sf_count_t frames) ;
  107. sf_count_t readf (double *ptr, sf_count_t frames) ;
  108. sf_count_t writef (const short *ptr, sf_count_t frames) ;
  109. sf_count_t writef (const int *ptr, sf_count_t frames) ;
  110. sf_count_t writef (const float *ptr, sf_count_t frames) ;
  111. sf_count_t writef (const double *ptr, sf_count_t frames) ;
  112. sf_count_t readRaw (void *ptr, sf_count_t bytes) ;
  113. sf_count_t writeRaw (const void *ptr, sf_count_t bytes) ;
  114. /**< Raw access to the handle. SndfileHandle keeps ownership. */
  115. SNDFILE * rawHandle (void) ;
  116. /**< Take ownership of handle, iff reference count is 1. */
  117. SNDFILE * takeOwnership (void) ;
  118. } ;
  119. /*==============================================================================
  120. ** Nothing but implementation below.
  121. */
  122. inline
  123. SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
  124. : ref (1)
  125. {}
  126. inline
  127. SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
  128. { if (sf != NULL) sf_close (sf) ; }
  129. inline
  130. SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
  131. : p (NULL)
  132. {
  133. p = new (std::nothrow) SNDFILE_ref () ;
  134. if (p != NULL)
  135. { p->ref = 1 ;
  136. p->sfinfo.frames = 0 ;
  137. p->sfinfo.channels = chans ;
  138. p->sfinfo.format = fmt ;
  139. p->sfinfo.samplerate = srate ;
  140. p->sfinfo.sections = 0 ;
  141. p->sfinfo.seekable = 0 ;
  142. p->sf = sf_open (path, mode, &p->sfinfo) ;
  143. } ;
  144. return ;
  145. } /* SndfileHandle const char * constructor */
  146. inline
  147. SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
  148. : p (NULL)
  149. {
  150. p = new (std::nothrow) SNDFILE_ref () ;
  151. if (p != NULL)
  152. { p->ref = 1 ;
  153. p->sfinfo.frames = 0 ;
  154. p->sfinfo.channels = chans ;
  155. p->sfinfo.format = fmt ;
  156. p->sfinfo.samplerate = srate ;
  157. p->sfinfo.sections = 0 ;
  158. p->sfinfo.seekable = 0 ;
  159. p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
  160. } ;
  161. return ;
  162. } /* SndfileHandle std::string constructor */
  163. inline
  164. SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
  165. : p (NULL)
  166. {
  167. if (fd < 0)
  168. return ;
  169. p = new (std::nothrow) SNDFILE_ref () ;
  170. if (p != NULL)
  171. { p->ref = 1 ;
  172. p->sfinfo.frames = 0 ;
  173. p->sfinfo.channels = chans ;
  174. p->sfinfo.format = fmt ;
  175. p->sfinfo.samplerate = srate ;
  176. p->sfinfo.sections = 0 ;
  177. p->sfinfo.seekable = 0 ;
  178. p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ;
  179. } ;
  180. return ;
  181. } /* SndfileHandle fd constructor */
  182. inline
  183. SndfileHandle::~SndfileHandle (void)
  184. { if (p != NULL && --p->ref == 0)
  185. delete p ;
  186. } /* SndfileHandle destructor */
  187. inline
  188. SndfileHandle::SndfileHandle (const SndfileHandle &orig)
  189. : p (orig.p)
  190. { if (p != NULL)
  191. ++p->ref ;
  192. } /* SndfileHandle copy constructor */
  193. inline SndfileHandle &
  194. SndfileHandle::operator = (const SndfileHandle &rhs)
  195. {
  196. if (&rhs == this)
  197. return *this ;
  198. if (p != NULL && --p->ref == 0)
  199. delete p ;
  200. p = rhs.p ;
  201. if (p != NULL)
  202. ++p->ref ;
  203. return *this ;
  204. } /* SndfileHandle assignment operator */
  205. inline int
  206. SndfileHandle::error (void) const
  207. { return sf_error (p->sf) ; }
  208. inline const char *
  209. SndfileHandle::strError (void) const
  210. { return sf_strerror (p->sf) ; }
  211. inline int
  212. SndfileHandle::command (int cmd, void *data, int datasize)
  213. { return sf_command (p->sf, cmd, data, datasize) ; }
  214. inline sf_count_t
  215. SndfileHandle::seek (sf_count_t frame_count, int whence)
  216. { return sf_seek (p->sf, frame_count, whence) ; }
  217. inline void
  218. SndfileHandle::writeSync (void)
  219. { sf_write_sync (p->sf) ; }
  220. inline int
  221. SndfileHandle::setString (int str_type, const char* str)
  222. { return sf_set_string (p->sf, str_type, str) ; }
  223. inline const char*
  224. SndfileHandle::getString (int str_type) const
  225. { return sf_get_string (p->sf, str_type) ; }
  226. inline int
  227. SndfileHandle::formatCheck (int fmt, int chans, int srate)
  228. {
  229. SF_INFO sfinfo ;
  230. sfinfo.frames = 0 ;
  231. sfinfo.channels = chans ;
  232. sfinfo.format = fmt ;
  233. sfinfo.samplerate = srate ;
  234. sfinfo.sections = 0 ;
  235. sfinfo.seekable = 0 ;
  236. return sf_format_check (&sfinfo) ;
  237. }
  238. /*---------------------------------------------------------------------*/
  239. inline sf_count_t
  240. SndfileHandle::read (short *ptr, sf_count_t items)
  241. { return sf_read_short (p->sf, ptr, items) ; }
  242. inline sf_count_t
  243. SndfileHandle::read (int *ptr, sf_count_t items)
  244. { return sf_read_int (p->sf, ptr, items) ; }
  245. inline sf_count_t
  246. SndfileHandle::read (float *ptr, sf_count_t items)
  247. { return sf_read_float (p->sf, ptr, items) ; }
  248. inline sf_count_t
  249. SndfileHandle::read (double *ptr, sf_count_t items)
  250. { return sf_read_double (p->sf, ptr, items) ; }
  251. inline sf_count_t
  252. SndfileHandle::write (const short *ptr, sf_count_t items)
  253. { return sf_write_short (p->sf, ptr, items) ; }
  254. inline sf_count_t
  255. SndfileHandle::write (const int *ptr, sf_count_t items)
  256. { return sf_write_int (p->sf, ptr, items) ; }
  257. inline sf_count_t
  258. SndfileHandle::write (const float *ptr, sf_count_t items)
  259. { return sf_write_float (p->sf, ptr, items) ; }
  260. inline sf_count_t
  261. SndfileHandle::write (const double *ptr, sf_count_t items)
  262. { return sf_write_double (p->sf, ptr, items) ; }
  263. inline sf_count_t
  264. SndfileHandle::readf (short *ptr, sf_count_t frame_count)
  265. { return sf_readf_short (p->sf, ptr, frame_count) ; }
  266. inline sf_count_t
  267. SndfileHandle::readf (int *ptr, sf_count_t frame_count)
  268. { return sf_readf_int (p->sf, ptr, frame_count) ; }
  269. inline sf_count_t
  270. SndfileHandle::readf (float *ptr, sf_count_t frame_count)
  271. { return sf_readf_float (p->sf, ptr, frame_count) ; }
  272. inline sf_count_t
  273. SndfileHandle::readf (double *ptr, sf_count_t frame_count)
  274. { return sf_readf_double (p->sf, ptr, frame_count) ; }
  275. inline sf_count_t
  276. SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
  277. { return sf_writef_short (p->sf, ptr, frame_count) ; }
  278. inline sf_count_t
  279. SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
  280. { return sf_writef_int (p->sf, ptr, frame_count) ; }
  281. inline sf_count_t
  282. SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
  283. { return sf_writef_float (p->sf, ptr, frame_count) ; }
  284. inline sf_count_t
  285. SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
  286. { return sf_writef_double (p->sf, ptr, frame_count) ; }
  287. inline sf_count_t
  288. SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
  289. { return sf_read_raw (p->sf, ptr, bytes) ; }
  290. inline sf_count_t
  291. SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
  292. { return sf_write_raw (p->sf, ptr, bytes) ; }
  293. inline SNDFILE *
  294. SndfileHandle::rawHandle (void)
  295. { return (p ? p->sf : NULL) ; }
  296. inline SNDFILE *
  297. SndfileHandle::takeOwnership (void)
  298. {
  299. if (p == NULL || (p->ref != 1))
  300. return NULL ;
  301. SNDFILE * sf = p->sf ;
  302. p->sf = NULL ;
  303. delete p ;
  304. p = NULL ;
  305. return sf ;
  306. }
  307. #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
  308. inline
  309. SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate)
  310. : p (NULL)
  311. {
  312. p = new (std::nothrow) SNDFILE_ref () ;
  313. if (p != NULL)
  314. { p->ref = 1 ;
  315. p->sfinfo.frames = 0 ;
  316. p->sfinfo.channels = chans ;
  317. p->sfinfo.format = fmt ;
  318. p->sfinfo.samplerate = srate ;
  319. p->sfinfo.sections = 0 ;
  320. p->sfinfo.seekable = 0 ;
  321. p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
  322. } ;
  323. return ;
  324. } /* SndfileHandle const wchar_t * constructor */
  325. #endif
  326. #endif /* SNDFILE_HH */