annotate win64-mingw/include/sndfile.hh @ 122:62723f530572

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