annotate win64-mingw/include/sndfile.hh @ 42:2cd0e3b3e1fd

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