2 ** Copyright (C) 2005-2007 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in
14 ** the documentation and/or other materials provided with the
16 ** * Neither the author nor the names of any contributors may be used
17 ** to endorse or promote products derived from this software without
18 ** specific prior written permission.
20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 ** The above modified BSD style license (GPL and LGPL compatible) applies to
35 ** this file. It does not apply to libsndfile itself which is released under
36 ** the GNU LGPL or the libsndfile test suite which is released under the GNU
38 ** This means that this header file can be used under this modified BSD style
39 ** license, but the LGPL still holds for the libsndfile library itself.
43 ** sndfile.hh -- A lightweight C++ wrapper for the libsndfile API.
45 ** All the methods are inlines and all functionality is contained in this
46 ** file. There is no separate implementation file.
48 ** API documentation is in the doc/ directory of the source code tarball
49 ** and at http://www.mega-nerd.com/libsndfile/api.html.
60 #include <new> // for std::nothrow
63 namespace AudioGrapher {
68 { SNDFILE_ref (void) ;
79 /* Default constructor */
80 SndfileHandle (void) : p (NULL) {} ;
81 SndfileHandle (const char *path, int mode = SFM_READ,
82 int format = 0, int channels = 0, int samplerate = 0) ;
83 SndfileHandle (std::string const & path, int mode = SFM_READ,
84 int format = 0, int channels = 0, int samplerate = 0) ;
85 SndfileHandle (int fd, bool close_desc, int mode = SFM_READ,
86 int format = 0, int channels = 0, int samplerate = 0) ;
87 ~SndfileHandle (void) ;
89 SndfileHandle (const SndfileHandle &orig) ;
90 SndfileHandle & operator = (const SndfileHandle &rhs) ;
92 /* Mainly for debugging/testing. */
93 int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
95 operator bool () const { return (p != NULL) ; }
97 bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
99 sf_count_t frames (void) const { return p ? p->sfinfo.frames : 0 ; }
100 int format (void) const { return p ? p->sfinfo.format : 0 ; }
101 int channels (void) const { return p ? p->sfinfo.channels : 0 ; }
102 int samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; }
104 int error (void) const ;
105 const char * strError (void) const ;
107 int command (int cmd, void *data, int datasize) ;
109 sf_count_t seek (sf_count_t frames, int whence) ;
111 void writeSync (void) ;
113 int setString (int str_type, const char* str) ;
115 const char* getString (int str_type) const ;
117 static int formatCheck (int format, int channels, int samplerate) ;
119 sf_count_t read (short *ptr, sf_count_t items) ;
120 sf_count_t read (int *ptr, sf_count_t items) ;
121 sf_count_t read (float *ptr, sf_count_t items) ;
122 sf_count_t read (double *ptr, sf_count_t items) ;
124 sf_count_t write (const short *ptr, sf_count_t items) ;
125 sf_count_t write (const int *ptr, sf_count_t items) ;
126 sf_count_t write (const float *ptr, sf_count_t items) ;
127 sf_count_t write (const double *ptr, sf_count_t items) ;
129 sf_count_t readf (short *ptr, sf_count_t frames) ;
130 sf_count_t readf (int *ptr, sf_count_t frames) ;
131 sf_count_t readf (float *ptr, sf_count_t frames) ;
132 sf_count_t readf (double *ptr, sf_count_t frames) ;
134 sf_count_t writef (const short *ptr, sf_count_t frames) ;
135 sf_count_t writef (const int *ptr, sf_count_t frames) ;
136 sf_count_t writef (const float *ptr, sf_count_t frames) ;
137 sf_count_t writef (const double *ptr, sf_count_t frames) ;
139 sf_count_t readRaw (void *ptr, sf_count_t bytes) ;
140 sf_count_t writeRaw (const void *ptr, sf_count_t bytes) ;
144 /*==============================================================================
145 ** Nothing but implementation below.
149 SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
154 SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
155 { if (sf != NULL) { std::cerr << "sf_closing " << sf << std::endl; sf_close (sf) ; } }
158 SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
161 p = new (std::nothrow) SNDFILE_ref () ;
166 p->sfinfo.frames = 0 ;
167 p->sfinfo.channels = chans ;
168 p->sfinfo.format = fmt ;
169 p->sfinfo.samplerate = srate ;
170 p->sfinfo.sections = 0 ;
171 p->sfinfo.seekable = 0 ;
173 p->sf = sf_open (path, mode, &p->sfinfo) ;
174 std::cerr << "3 attempted to open " << path << " got " << p->sf << std::endl;
178 } /* SndfileHandle const char * constructor */
181 SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
184 p = new (std::nothrow) SNDFILE_ref () ;
189 p->sfinfo.frames = 0 ;
190 p->sfinfo.channels = chans ;
191 p->sfinfo.format = fmt ;
192 p->sfinfo.samplerate = srate ;
193 p->sfinfo.sections = 0 ;
194 p->sfinfo.seekable = 0 ;
196 p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
197 std::cerr << "attempted to open " << path << " got " << p->sf << std::endl;
201 } /* SndfileHandle std::string constructor */
204 SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
210 p = new (std::nothrow) SNDFILE_ref () ;
215 p->sfinfo.frames = 0 ;
216 p->sfinfo.channels = chans ;
217 p->sfinfo.format = fmt ;
218 p->sfinfo.samplerate = srate ;
219 p->sfinfo.sections = 0 ;
220 p->sfinfo.seekable = 0 ;
222 p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ;
223 std::cerr << "2 attempted to open via fd " << fd << " got " << p->sf << std::endl;
227 } /* SndfileHandle fd constructor */
230 SndfileHandle::~SndfileHandle (void)
231 { if (p != NULL && --p->ref == 0)
233 } /* SndfileHandle destructor */
237 SndfileHandle::SndfileHandle (const SndfileHandle &orig)
241 } /* SndfileHandle copy constructor */
243 inline SndfileHandle &
244 SndfileHandle::operator = (const SndfileHandle &rhs)
248 if (p != NULL && --p->ref == 0)
256 } /* SndfileHandle assignment operator */
259 SndfileHandle::error (void) const
260 { return sf_error (p->sf) ; }
263 SndfileHandle::strError (void) const
264 { return sf_strerror (p->sf) ; }
267 SndfileHandle::command (int cmd, void *data, int datasize)
268 { return sf_command (p->sf, cmd, data, datasize) ; }
271 SndfileHandle::seek (sf_count_t frame_count, int whence)
272 { return sf_seek (p->sf, frame_count, whence) ; }
275 SndfileHandle::writeSync (void)
276 { sf_write_sync (p->sf) ; }
279 SndfileHandle::setString (int str_type, const char* str)
280 { return sf_set_string (p->sf, str_type, str) ; }
283 SndfileHandle::getString (int str_type) const
284 { return sf_get_string (p->sf, str_type) ; }
287 SndfileHandle::formatCheck(int fmt, int chans, int srate)
292 sfinfo.channels = chans ;
293 sfinfo.format = fmt ;
294 sfinfo.samplerate = srate ;
295 sfinfo.sections = 0 ;
296 sfinfo.seekable = 0 ;
298 return sf_format_check (&sfinfo) ;
301 /*---------------------------------------------------------------------*/
304 SndfileHandle::read (short *ptr, sf_count_t items)
305 { return sf_read_short (p->sf, ptr, items) ; }
308 SndfileHandle::read (int *ptr, sf_count_t items)
309 { return sf_read_int (p->sf, ptr, items) ; }
312 SndfileHandle::read (float *ptr, sf_count_t items)
313 { return sf_read_float (p->sf, ptr, items) ; }
316 SndfileHandle::read (double *ptr, sf_count_t items)
317 { return sf_read_double (p->sf, ptr, items) ; }
320 SndfileHandle::write (const short *ptr, sf_count_t items)
321 { return sf_write_short (p->sf, ptr, items) ; }
324 SndfileHandle::write (const int *ptr, sf_count_t items)
325 { return sf_write_int (p->sf, ptr, items) ; }
328 SndfileHandle::write (const float *ptr, sf_count_t items)
329 { return sf_write_float (p->sf, ptr, items) ; }
332 SndfileHandle::write (const double *ptr, sf_count_t items)
333 { return sf_write_double (p->sf, ptr, items) ; }
336 SndfileHandle::readf (short *ptr, sf_count_t frame_count)
337 { return sf_readf_short (p->sf, ptr, frame_count) ; }
340 SndfileHandle::readf (int *ptr, sf_count_t frame_count)
341 { return sf_readf_int (p->sf, ptr, frame_count) ; }
344 SndfileHandle::readf (float *ptr, sf_count_t frame_count)
345 { return sf_readf_float (p->sf, ptr, frame_count) ; }
348 SndfileHandle::readf (double *ptr, sf_count_t frame_count)
349 { return sf_readf_double (p->sf, ptr, frame_count) ; }
352 SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
353 { return sf_writef_short (p->sf, ptr, frame_count) ; }
356 SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
357 { return sf_writef_int (p->sf, ptr, frame_count) ; }
360 SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
361 { return sf_writef_float (p->sf, ptr, frame_count) ; }
364 SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
365 { return sf_writef_double (p->sf, ptr, frame_count) ; }
368 SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
369 { return sf_read_raw (p->sf, ptr, bytes) ; }
372 SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
373 { return sf_write_raw (p->sf, ptr, bytes) ; }
376 #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
379 SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate)
382 p = new (std::nothrow) SNDFILE_ref () ;
387 p->sfinfo.frames = 0 ;
388 p->sfinfo.channels = chans ;
389 p->sfinfo.format = fmt ;
390 p->sfinfo.samplerate = srate ;
391 p->sfinfo.sections = 0 ;
392 p->sfinfo.seekable = 0 ;
394 p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
398 } /* SndfileHandle const wchar_t * constructor */
402 } // namespace AudioGrapher
404 #endif /* SNDFILE_HH */