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