123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- #ifndef SNDFILE_HH
- #define SNDFILE_HH
- #include <sndfile.h>
- #include <string>
- #include <new> // for std::nothrow
- class SndfileHandle
- { private :
- struct SNDFILE_ref
- { SNDFILE_ref (void) ;
- ~SNDFILE_ref (void) ;
- SNDFILE *sf ;
- SF_INFO sfinfo ;
- int ref ;
- } ;
- SNDFILE_ref *p ;
- public :
-
- SndfileHandle (void) : p (NULL) {} ;
- SndfileHandle (const char *path, int mode = SFM_READ,
- int format = 0, int channels = 0, int samplerate = 0) ;
- SndfileHandle (std::string const & path, int mode = SFM_READ,
- int format = 0, int channels = 0, int samplerate = 0) ;
- SndfileHandle (int fd, bool close_desc, int mode = SFM_READ,
- int format = 0, int channels = 0, int samplerate = 0) ;
- SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode = SFM_READ,
- int format = 0, int channels = 0, int samplerate = 0) ;
- #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
- SndfileHandle (LPCWSTR wpath, int mode = SFM_READ,
- int format = 0, int channels = 0, int samplerate = 0) ;
- #endif
- ~SndfileHandle (void) ;
- SndfileHandle (const SndfileHandle &orig) ;
- SndfileHandle & operator = (const SndfileHandle &rhs) ;
-
- int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
- operator bool () const { return (p != NULL) ; }
- bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
- sf_count_t frames (void) const { return p ? p->sfinfo.frames : 0 ; }
- int format (void) const { return p ? p->sfinfo.format : 0 ; }
- int channels (void) const { return p ? p->sfinfo.channels : 0 ; }
- int samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; }
- int error (void) const ;
- const char * strError (void) const ;
- int command (int cmd, void *data, int datasize) ;
- sf_count_t seek (sf_count_t frames, int whence) ;
- void writeSync (void) ;
- int setString (int str_type, const char* str) ;
- const char* getString (int str_type) const ;
- static int formatCheck (int format, int channels, int samplerate) ;
- sf_count_t read (short *ptr, sf_count_t items) ;
- sf_count_t read (int *ptr, sf_count_t items) ;
- sf_count_t read (float *ptr, sf_count_t items) ;
- sf_count_t read (double *ptr, sf_count_t items) ;
- sf_count_t write (const short *ptr, sf_count_t items) ;
- sf_count_t write (const int *ptr, sf_count_t items) ;
- sf_count_t write (const float *ptr, sf_count_t items) ;
- sf_count_t write (const double *ptr, sf_count_t items) ;
- sf_count_t readf (short *ptr, sf_count_t frames) ;
- sf_count_t readf (int *ptr, sf_count_t frames) ;
- sf_count_t readf (float *ptr, sf_count_t frames) ;
- sf_count_t readf (double *ptr, sf_count_t frames) ;
- sf_count_t writef (const short *ptr, sf_count_t frames) ;
- sf_count_t writef (const int *ptr, sf_count_t frames) ;
- sf_count_t writef (const float *ptr, sf_count_t frames) ;
- sf_count_t writef (const double *ptr, sf_count_t frames) ;
- sf_count_t readRaw (void *ptr, sf_count_t bytes) ;
- sf_count_t writeRaw (const void *ptr, sf_count_t bytes) ;
-
- SNDFILE * rawHandle (void) ;
-
- SNDFILE * takeOwnership (void) ;
- } ;
- inline
- SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
- : sf (NULL), sfinfo (), ref (1)
- {}
- inline
- SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
- { if (sf != NULL) sf_close (sf) ; }
- inline
- SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
- : p (NULL)
- {
- p = new (std::nothrow) SNDFILE_ref () ;
- if (p != NULL)
- { p->ref = 1 ;
- p->sfinfo.frames = 0 ;
- p->sfinfo.channels = chans ;
- p->sfinfo.format = fmt ;
- p->sfinfo.samplerate = srate ;
- p->sfinfo.sections = 0 ;
- p->sfinfo.seekable = 0 ;
- p->sf = sf_open (path, mode, &p->sfinfo) ;
- } ;
- return ;
- }
- inline
- SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
- : p (NULL)
- {
- p = new (std::nothrow) SNDFILE_ref () ;
- if (p != NULL)
- { p->ref = 1 ;
- p->sfinfo.frames = 0 ;
- p->sfinfo.channels = chans ;
- p->sfinfo.format = fmt ;
- p->sfinfo.samplerate = srate ;
- p->sfinfo.sections = 0 ;
- p->sfinfo.seekable = 0 ;
- p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
- } ;
- return ;
- }
- inline
- SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
- : p (NULL)
- {
- if (fd < 0)
- return ;
- p = new (std::nothrow) SNDFILE_ref () ;
- if (p != NULL)
- { p->ref = 1 ;
- p->sfinfo.frames = 0 ;
- p->sfinfo.channels = chans ;
- p->sfinfo.format = fmt ;
- p->sfinfo.samplerate = srate ;
- p->sfinfo.sections = 0 ;
- p->sfinfo.seekable = 0 ;
- p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ;
- } ;
- return ;
- }
- inline
- SndfileHandle::SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode, int fmt, int chans, int srate)
- : p (NULL)
- {
- p = new (std::nothrow) SNDFILE_ref () ;
- if (p != NULL)
- { p->ref = 1 ;
- p->sfinfo.frames = 0 ;
- p->sfinfo.channels = chans ;
- p->sfinfo.format = fmt ;
- p->sfinfo.samplerate = srate ;
- p->sfinfo.sections = 0 ;
- p->sfinfo.seekable = 0 ;
- p->sf = sf_open_virtual (&sfvirtual, mode, &p->sfinfo, user_data) ;
- } ;
- return ;
- }
- inline
- SndfileHandle::~SndfileHandle (void)
- { if (p != NULL && --p->ref == 0)
- delete p ;
- }
- inline
- SndfileHandle::SndfileHandle (const SndfileHandle &orig)
- : p (orig.p)
- { if (p != NULL)
- ++p->ref ;
- }
- inline SndfileHandle &
- SndfileHandle::operator = (const SndfileHandle &rhs)
- {
- if (&rhs == this)
- return *this ;
- if (p != NULL && --p->ref == 0)
- delete p ;
- p = rhs.p ;
- if (p != NULL)
- ++p->ref ;
- return *this ;
- }
- inline int
- SndfileHandle::error (void) const
- { return sf_error (p->sf) ; }
- inline const char *
- SndfileHandle::strError (void) const
- { return sf_strerror (p->sf) ; }
- inline int
- SndfileHandle::command (int cmd, void *data, int datasize)
- { return sf_command (p->sf, cmd, data, datasize) ; }
- inline sf_count_t
- SndfileHandle::seek (sf_count_t frame_count, int whence)
- { return sf_seek (p->sf, frame_count, whence) ; }
- inline void
- SndfileHandle::writeSync (void)
- { sf_write_sync (p->sf) ; }
- inline int
- SndfileHandle::setString (int str_type, const char* str)
- { return sf_set_string (p->sf, str_type, str) ; }
- inline const char*
- SndfileHandle::getString (int str_type) const
- { return sf_get_string (p->sf, str_type) ; }
- inline int
- SndfileHandle::formatCheck (int fmt, int chans, int srate)
- {
- SF_INFO sfinfo ;
- sfinfo.frames = 0 ;
- sfinfo.channels = chans ;
- sfinfo.format = fmt ;
- sfinfo.samplerate = srate ;
- sfinfo.sections = 0 ;
- sfinfo.seekable = 0 ;
- return sf_format_check (&sfinfo) ;
- }
- inline sf_count_t
- SndfileHandle::read (short *ptr, sf_count_t items)
- { return sf_read_short (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::read (int *ptr, sf_count_t items)
- { return sf_read_int (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::read (float *ptr, sf_count_t items)
- { return sf_read_float (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::read (double *ptr, sf_count_t items)
- { return sf_read_double (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::write (const short *ptr, sf_count_t items)
- { return sf_write_short (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::write (const int *ptr, sf_count_t items)
- { return sf_write_int (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::write (const float *ptr, sf_count_t items)
- { return sf_write_float (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::write (const double *ptr, sf_count_t items)
- { return sf_write_double (p->sf, ptr, items) ; }
- inline sf_count_t
- SndfileHandle::readf (short *ptr, sf_count_t frame_count)
- { return sf_readf_short (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::readf (int *ptr, sf_count_t frame_count)
- { return sf_readf_int (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::readf (float *ptr, sf_count_t frame_count)
- { return sf_readf_float (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::readf (double *ptr, sf_count_t frame_count)
- { return sf_readf_double (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
- { return sf_writef_short (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
- { return sf_writef_int (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
- { return sf_writef_float (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
- { return sf_writef_double (p->sf, ptr, frame_count) ; }
- inline sf_count_t
- SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
- { return sf_read_raw (p->sf, ptr, bytes) ; }
- inline sf_count_t
- SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
- { return sf_write_raw (p->sf, ptr, bytes) ; }
- inline SNDFILE *
- SndfileHandle::rawHandle (void)
- { return (p ? p->sf : NULL) ; }
- inline SNDFILE *
- SndfileHandle::takeOwnership (void)
- {
- if (p == NULL || (p->ref != 1))
- return NULL ;
- SNDFILE * sf = p->sf ;
- p->sf = NULL ;
- delete p ;
- p = NULL ;
- return sf ;
- }
- #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
- inline
- SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate)
- : p (NULL)
- {
- p = new (std::nothrow) SNDFILE_ref () ;
- if (p != NULL)
- { p->ref = 1 ;
- p->sfinfo.frames = 0 ;
- p->sfinfo.channels = chans ;
- p->sfinfo.format = fmt ;
- p->sfinfo.samplerate = srate ;
- p->sfinfo.sections = 0 ;
- p->sfinfo.seekable = 0 ;
- p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
- } ;
- return ;
- }
- #endif
- #endif
|