annotate osx/include/sndfile.hh @ 79:91c729825bca pa_catalina

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