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