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