cannam@120: /* cannam@120: ** Copyright (C) 2005-2011 Erik de Castro Lopo cannam@120: ** cannam@120: ** All rights reserved. cannam@120: ** cannam@120: ** Redistribution and use in source and binary forms, with or without cannam@120: ** modification, are permitted provided that the following conditions are cannam@120: ** met: cannam@120: ** cannam@120: ** * Redistributions of source code must retain the above copyright cannam@120: ** notice, this list of conditions and the following disclaimer. cannam@120: ** * Redistributions in binary form must reproduce the above copyright cannam@120: ** notice, this list of conditions and the following disclaimer in cannam@120: ** the documentation and/or other materials provided with the cannam@120: ** distribution. cannam@120: ** * Neither the author nor the names of any contributors may be used cannam@120: ** to endorse or promote products derived from this software without cannam@120: ** specific prior written permission. cannam@120: ** cannam@120: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@120: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED cannam@120: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR cannam@120: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR cannam@120: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@120: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@120: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; cannam@120: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, cannam@120: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR cannam@120: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF cannam@120: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@120: */ cannam@120: cannam@120: /* cannam@120: ** The above modified BSD style license (GPL and LGPL compatible) applies to cannam@120: ** this file. It does not apply to libsndfile itself which is released under cannam@120: ** the GNU LGPL or the libsndfile test suite which is released under the GNU cannam@120: ** GPL. cannam@120: ** This means that this header file can be used under this modified BSD style cannam@120: ** license, but the LGPL still holds for the libsndfile library itself. cannam@120: */ cannam@120: cannam@120: /* cannam@120: ** sndfile.hh -- A lightweight C++ wrapper for the libsndfile API. cannam@120: ** cannam@120: ** All the methods are inlines and all functionality is contained in this cannam@120: ** file. There is no separate implementation file. cannam@120: ** cannam@120: ** API documentation is in the doc/ directory of the source code tarball cannam@120: ** and at http://www.mega-nerd.com/libsndfile/api.html. cannam@120: */ cannam@120: cannam@120: #ifndef SNDFILE_HH cannam@120: #define SNDFILE_HH cannam@120: cannam@120: #include cannam@120: cannam@120: #include cannam@120: #include // for std::nothrow cannam@120: cannam@120: class SndfileHandle cannam@120: { private : cannam@120: struct SNDFILE_ref cannam@120: { SNDFILE_ref (void) ; cannam@120: ~SNDFILE_ref (void) ; cannam@120: cannam@120: SNDFILE *sf ; cannam@120: SF_INFO sfinfo ; cannam@120: int ref ; cannam@120: } ; cannam@120: cannam@120: SNDFILE_ref *p ; cannam@120: cannam@120: public : cannam@120: /* Default constructor */ cannam@120: SndfileHandle (void) : p (NULL) {} ; cannam@120: SndfileHandle (const char *path, int mode = SFM_READ, cannam@120: int format = 0, int channels = 0, int samplerate = 0) ; cannam@120: SndfileHandle (std::string const & path, int mode = SFM_READ, cannam@120: int format = 0, int channels = 0, int samplerate = 0) ; cannam@120: SndfileHandle (int fd, bool close_desc, int mode = SFM_READ, cannam@120: int format = 0, int channels = 0, int samplerate = 0) ; cannam@120: cannam@120: #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES cannam@120: SndfileHandle (LPCWSTR wpath, int mode = SFM_READ, cannam@120: int format = 0, int channels = 0, int samplerate = 0) ; cannam@120: #endif cannam@120: cannam@120: ~SndfileHandle (void) ; cannam@120: cannam@120: SndfileHandle (const SndfileHandle &orig) ; cannam@120: SndfileHandle & operator = (const SndfileHandle &rhs) ; cannam@120: cannam@120: /* Mainly for debugging/testing. */ cannam@120: int refCount (void) const { return (p == NULL) ? 0 : p->ref ; } cannam@120: cannam@120: operator bool () const { return (p != NULL) ; } cannam@120: cannam@120: bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; } cannam@120: cannam@120: sf_count_t frames (void) const { return p ? p->sfinfo.frames : 0 ; } cannam@120: int format (void) const { return p ? p->sfinfo.format : 0 ; } cannam@120: int channels (void) const { return p ? p->sfinfo.channels : 0 ; } cannam@120: int samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; } cannam@120: cannam@120: int error (void) const ; cannam@120: const char * strError (void) const ; cannam@120: cannam@120: int command (int cmd, void *data, int datasize) ; cannam@120: cannam@120: sf_count_t seek (sf_count_t frames, int whence) ; cannam@120: cannam@120: void writeSync (void) ; cannam@120: cannam@120: int setString (int str_type, const char* str) ; cannam@120: cannam@120: const char* getString (int str_type) const ; cannam@120: cannam@120: static int formatCheck (int format, int channels, int samplerate) ; cannam@120: cannam@120: sf_count_t read (short *ptr, sf_count_t items) ; cannam@120: sf_count_t read (int *ptr, sf_count_t items) ; cannam@120: sf_count_t read (float *ptr, sf_count_t items) ; cannam@120: sf_count_t read (double *ptr, sf_count_t items) ; cannam@120: cannam@120: sf_count_t write (const short *ptr, sf_count_t items) ; cannam@120: sf_count_t write (const int *ptr, sf_count_t items) ; cannam@120: sf_count_t write (const float *ptr, sf_count_t items) ; cannam@120: sf_count_t write (const double *ptr, sf_count_t items) ; cannam@120: cannam@120: sf_count_t readf (short *ptr, sf_count_t frames) ; cannam@120: sf_count_t readf (int *ptr, sf_count_t frames) ; cannam@120: sf_count_t readf (float *ptr, sf_count_t frames) ; cannam@120: sf_count_t readf (double *ptr, sf_count_t frames) ; cannam@120: cannam@120: sf_count_t writef (const short *ptr, sf_count_t frames) ; cannam@120: sf_count_t writef (const int *ptr, sf_count_t frames) ; cannam@120: sf_count_t writef (const float *ptr, sf_count_t frames) ; cannam@120: sf_count_t writef (const double *ptr, sf_count_t frames) ; cannam@120: cannam@120: sf_count_t readRaw (void *ptr, sf_count_t bytes) ; cannam@120: sf_count_t writeRaw (const void *ptr, sf_count_t bytes) ; cannam@120: cannam@120: /**< Raw access to the handle. SndfileHandle keeps ownership. */ cannam@120: SNDFILE * rawHandle (void) ; cannam@120: cannam@120: /**< Take ownership of handle, iff reference count is 1. */ cannam@120: SNDFILE * takeOwnership (void) ; cannam@120: } ; cannam@120: cannam@120: /*============================================================================== cannam@120: ** Nothing but implementation below. cannam@120: */ cannam@120: cannam@120: inline cannam@120: SndfileHandle::SNDFILE_ref::SNDFILE_ref (void) cannam@120: : ref (1) cannam@120: {} cannam@120: cannam@120: inline cannam@120: SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void) cannam@120: { if (sf != NULL) sf_close (sf) ; } cannam@120: cannam@120: inline cannam@120: SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate) cannam@120: : p (NULL) cannam@120: { cannam@120: p = new (std::nothrow) SNDFILE_ref () ; cannam@120: cannam@120: if (p != NULL) cannam@120: { p->ref = 1 ; cannam@120: cannam@120: p->sfinfo.frames = 0 ; cannam@120: p->sfinfo.channels = chans ; cannam@120: p->sfinfo.format = fmt ; cannam@120: p->sfinfo.samplerate = srate ; cannam@120: p->sfinfo.sections = 0 ; cannam@120: p->sfinfo.seekable = 0 ; cannam@120: cannam@120: p->sf = sf_open (path, mode, &p->sfinfo) ; cannam@120: } ; cannam@120: cannam@120: return ; cannam@120: } /* SndfileHandle const char * constructor */ cannam@120: cannam@120: inline cannam@120: SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate) cannam@120: : p (NULL) cannam@120: { cannam@120: p = new (std::nothrow) SNDFILE_ref () ; cannam@120: cannam@120: if (p != NULL) cannam@120: { p->ref = 1 ; cannam@120: cannam@120: p->sfinfo.frames = 0 ; cannam@120: p->sfinfo.channels = chans ; cannam@120: p->sfinfo.format = fmt ; cannam@120: p->sfinfo.samplerate = srate ; cannam@120: p->sfinfo.sections = 0 ; cannam@120: p->sfinfo.seekable = 0 ; cannam@120: cannam@120: p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ; cannam@120: } ; cannam@120: cannam@120: return ; cannam@120: } /* SndfileHandle std::string constructor */ cannam@120: cannam@120: inline cannam@120: SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate) cannam@120: : p (NULL) cannam@120: { cannam@120: if (fd < 0) cannam@120: return ; cannam@120: cannam@120: p = new (std::nothrow) SNDFILE_ref () ; cannam@120: cannam@120: if (p != NULL) cannam@120: { p->ref = 1 ; cannam@120: cannam@120: p->sfinfo.frames = 0 ; cannam@120: p->sfinfo.channels = chans ; cannam@120: p->sfinfo.format = fmt ; cannam@120: p->sfinfo.samplerate = srate ; cannam@120: p->sfinfo.sections = 0 ; cannam@120: p->sfinfo.seekable = 0 ; cannam@120: cannam@120: p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ; cannam@120: } ; cannam@120: cannam@120: return ; cannam@120: } /* SndfileHandle fd constructor */ cannam@120: cannam@120: inline cannam@120: SndfileHandle::~SndfileHandle (void) cannam@120: { if (p != NULL && --p->ref == 0) cannam@120: delete p ; cannam@120: } /* SndfileHandle destructor */ cannam@120: cannam@120: cannam@120: inline cannam@120: SndfileHandle::SndfileHandle (const SndfileHandle &orig) cannam@120: : p (orig.p) cannam@120: { if (p != NULL) cannam@120: ++p->ref ; cannam@120: } /* SndfileHandle copy constructor */ cannam@120: cannam@120: inline SndfileHandle & cannam@120: SndfileHandle::operator = (const SndfileHandle &rhs) cannam@120: { cannam@120: if (&rhs == this) cannam@120: return *this ; cannam@120: if (p != NULL && --p->ref == 0) cannam@120: delete p ; cannam@120: cannam@120: p = rhs.p ; cannam@120: if (p != NULL) cannam@120: ++p->ref ; cannam@120: cannam@120: return *this ; cannam@120: } /* SndfileHandle assignment operator */ cannam@120: cannam@120: inline int cannam@120: SndfileHandle::error (void) const cannam@120: { return sf_error (p->sf) ; } cannam@120: cannam@120: inline const char * cannam@120: SndfileHandle::strError (void) const cannam@120: { return sf_strerror (p->sf) ; } cannam@120: cannam@120: inline int cannam@120: SndfileHandle::command (int cmd, void *data, int datasize) cannam@120: { return sf_command (p->sf, cmd, data, datasize) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::seek (sf_count_t frame_count, int whence) cannam@120: { return sf_seek (p->sf, frame_count, whence) ; } cannam@120: cannam@120: inline void cannam@120: SndfileHandle::writeSync (void) cannam@120: { sf_write_sync (p->sf) ; } cannam@120: cannam@120: inline int cannam@120: SndfileHandle::setString (int str_type, const char* str) cannam@120: { return sf_set_string (p->sf, str_type, str) ; } cannam@120: cannam@120: inline const char* cannam@120: SndfileHandle::getString (int str_type) const cannam@120: { return sf_get_string (p->sf, str_type) ; } cannam@120: cannam@120: inline int cannam@120: SndfileHandle::formatCheck (int fmt, int chans, int srate) cannam@120: { cannam@120: SF_INFO sfinfo ; cannam@120: cannam@120: sfinfo.frames = 0 ; cannam@120: sfinfo.channels = chans ; cannam@120: sfinfo.format = fmt ; cannam@120: sfinfo.samplerate = srate ; cannam@120: sfinfo.sections = 0 ; cannam@120: sfinfo.seekable = 0 ; cannam@120: cannam@120: return sf_format_check (&sfinfo) ; cannam@120: } cannam@120: cannam@120: /*---------------------------------------------------------------------*/ cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::read (short *ptr, sf_count_t items) cannam@120: { return sf_read_short (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::read (int *ptr, sf_count_t items) cannam@120: { return sf_read_int (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::read (float *ptr, sf_count_t items) cannam@120: { return sf_read_float (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::read (double *ptr, sf_count_t items) cannam@120: { return sf_read_double (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::write (const short *ptr, sf_count_t items) cannam@120: { return sf_write_short (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::write (const int *ptr, sf_count_t items) cannam@120: { return sf_write_int (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::write (const float *ptr, sf_count_t items) cannam@120: { return sf_write_float (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::write (const double *ptr, sf_count_t items) cannam@120: { return sf_write_double (p->sf, ptr, items) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::readf (short *ptr, sf_count_t frame_count) cannam@120: { return sf_readf_short (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::readf (int *ptr, sf_count_t frame_count) cannam@120: { return sf_readf_int (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::readf (float *ptr, sf_count_t frame_count) cannam@120: { return sf_readf_float (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::readf (double *ptr, sf_count_t frame_count) cannam@120: { return sf_readf_double (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::writef (const short *ptr, sf_count_t frame_count) cannam@120: { return sf_writef_short (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::writef (const int *ptr, sf_count_t frame_count) cannam@120: { return sf_writef_int (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::writef (const float *ptr, sf_count_t frame_count) cannam@120: { return sf_writef_float (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::writef (const double *ptr, sf_count_t frame_count) cannam@120: { return sf_writef_double (p->sf, ptr, frame_count) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::readRaw (void *ptr, sf_count_t bytes) cannam@120: { return sf_read_raw (p->sf, ptr, bytes) ; } cannam@120: cannam@120: inline sf_count_t cannam@120: SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes) cannam@120: { return sf_write_raw (p->sf, ptr, bytes) ; } cannam@120: cannam@120: inline SNDFILE * cannam@120: SndfileHandle::rawHandle (void) cannam@120: { return (p ? p->sf : NULL) ; } cannam@120: cannam@120: inline SNDFILE * cannam@120: SndfileHandle::takeOwnership (void) cannam@120: { cannam@120: if (p == NULL || (p->ref != 1)) cannam@120: return NULL ; cannam@120: cannam@120: SNDFILE * sf = p->sf ; cannam@120: p->sf = NULL ; cannam@120: delete p ; cannam@120: p = NULL ; cannam@120: return sf ; cannam@120: } cannam@120: cannam@120: #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES cannam@120: cannam@120: inline cannam@120: SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate) cannam@120: : p (NULL) cannam@120: { cannam@120: p = new (std::nothrow) SNDFILE_ref () ; cannam@120: cannam@120: if (p != NULL) cannam@120: { p->ref = 1 ; cannam@120: cannam@120: p->sfinfo.frames = 0 ; cannam@120: p->sfinfo.channels = chans ; cannam@120: p->sfinfo.format = fmt ; cannam@120: p->sfinfo.samplerate = srate ; cannam@120: p->sfinfo.sections = 0 ; cannam@120: p->sfinfo.seekable = 0 ; cannam@120: cannam@120: p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ; cannam@120: } ; cannam@120: cannam@120: return ; cannam@120: } /* SndfileHandle const wchar_t * constructor */ cannam@120: cannam@120: #endif cannam@120: cannam@120: #endif /* SNDFILE_HH */ cannam@120: