annotate src/libsndfile-1.0.25/src/sndfile.hh @ 95:89f5e221ed7b

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