annotate osx/include/sndfile.hh @ 169:223a55898ab9 tip default

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