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