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