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