sndfile/export debuggign with ricardus
[ardour.git] / libs / audiographer / private / sndfile.hh
1 /*
2 ** Copyright (C) 2005-2007 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
8 ** met:
9 **
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
15 **       distribution.
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.
19 **
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.
31 */
32
33 /*
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
37 ** GPL.
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.
40 */
41
42 /*
43 ** sndfile.hh -- A lightweight C++ wrapper for the libsndfile API.
44 **
45 ** All the methods are inlines and all functionality is contained in this
46 ** file. There is no separate implementation file.
47 **
48 ** API documentation is in the doc/ directory of the source code tarball
49 ** and at http://www.mega-nerd.com/libsndfile/api.html.
50 */
51
52 #ifndef SNDFILE_HH
53 #define SNDFILE_HH
54
55 #include <iostream>
56
57 #include <sndfile.h>
58
59 #include <string>
60 #include <new> // for std::nothrow
61
62 // Prevent conflicts
63 namespace AudioGrapher {
64
65 class SndfileHandle
66 {       private :
67                 struct SNDFILE_ref
68                 {       SNDFILE_ref (void) ;
69                         ~SNDFILE_ref (void) ;
70
71                         SNDFILE *sf ;
72                         SF_INFO sfinfo ;
73                         int ref ;
74                         } ;
75
76                 SNDFILE_ref *p ;
77
78         public :
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) ;
88
89                         SndfileHandle (const SndfileHandle &orig) ;
90                         SndfileHandle & operator = (const SndfileHandle &rhs) ;
91
92                 /* Mainly for debugging/testing. */
93                 int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
94
95                 operator bool () const { return (p != NULL) ; }
96
97                 bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
98
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 ; }
103
104                 int error (void) const ;
105                 const char * strError (void) const ;
106
107                 int command (int cmd, void *data, int datasize) ;
108
109                 sf_count_t      seek (sf_count_t frames, int whence) ;
110
111                 void writeSync (void) ;
112
113                 int setString (int str_type, const char* str) ;
114
115                 const char* getString (int str_type) const ;
116
117                 static int formatCheck (int format, int channels, int samplerate) ;
118
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) ;
123
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) ;
128
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) ;
133
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) ;
138
139                 sf_count_t      readRaw         (void *ptr, sf_count_t bytes) ;
140                 sf_count_t      writeRaw        (const void *ptr, sf_count_t bytes) ;
141
142 } ;
143
144 /*==============================================================================
145 **      Nothing but implementation below.
146 */
147
148 inline
149 SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
150 : ref (1)
151 {}
152
153 inline
154 SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
155 {       if (sf != NULL) {               std::cerr << "sf_closing " << sf << std::endl; sf_close (sf) ; } }
156
157 inline
158 SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
159 : p (NULL)
160 {
161         p = new (std::nothrow) SNDFILE_ref () ;
162
163         if (p != NULL)
164         {       p->ref = 1 ;
165
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 ;
172
173                 p->sf = sf_open (path, mode, &p->sfinfo) ;
174                 std::cerr << "3 attempted to open " << path << " got " << p->sf << std::endl;
175                 } ;
176
177         return ;
178 } /* SndfileHandle const char * constructor */
179
180 inline
181 SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
182 : p (NULL)
183 {
184         p = new (std::nothrow) SNDFILE_ref () ;
185
186         if (p != NULL)
187         {       p->ref = 1 ;
188
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 ;
195
196                 p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
197                 std::cerr << "attempted to open " << path << " got " << p->sf << std::endl;
198                 } ;
199
200         return ;
201 } /* SndfileHandle std::string constructor */
202
203 inline
204 SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
205 : p (NULL)
206 {
207         if (fd < 0)
208                 return;
209
210         p = new (std::nothrow) SNDFILE_ref () ;
211
212         if (p != NULL)
213         {       p->ref = 1 ;
214
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 ;
221
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;
224                 } ;
225
226         return ;
227 } /* SndfileHandle fd constructor */
228
229 inline
230 SndfileHandle::~SndfileHandle (void)
231 {       if (p != NULL && --p->ref == 0)
232                 delete p ;
233 } /* SndfileHandle destructor */
234
235
236 inline
237 SndfileHandle::SndfileHandle (const SndfileHandle &orig)
238 : p (orig.p)
239 {       if (p != NULL)
240                 ++p->ref ;
241 } /* SndfileHandle copy constructor */
242
243 inline SndfileHandle &
244 SndfileHandle::operator = (const SndfileHandle &rhs)
245 {
246         if (&rhs == this)
247                 return *this ;
248         if (p != NULL && --p->ref == 0)
249                 delete p ;
250
251         p = rhs.p ;
252         if (p != NULL)
253                 ++p->ref ;
254
255         return *this ;
256 } /* SndfileHandle assignment operator */
257
258 inline int
259 SndfileHandle::error (void) const
260 {       return sf_error (p->sf) ; }
261
262 inline const char *
263 SndfileHandle::strError (void) const
264 {       return sf_strerror (p->sf) ; }
265
266 inline int
267 SndfileHandle::command (int cmd, void *data, int datasize)
268 {       return sf_command (p->sf, cmd, data, datasize) ; }
269
270 inline sf_count_t
271 SndfileHandle::seek (sf_count_t frame_count, int whence)
272 {       return sf_seek (p->sf, frame_count, whence) ; }
273
274 inline void
275 SndfileHandle::writeSync (void)
276 {       sf_write_sync (p->sf) ; }
277
278 inline int
279 SndfileHandle::setString (int str_type, const char* str)
280 {       return sf_set_string (p->sf, str_type, str) ; }
281
282 inline const char*
283 SndfileHandle::getString (int str_type) const
284 {       return sf_get_string (p->sf, str_type) ; }
285
286 inline int
287 SndfileHandle::formatCheck(int fmt, int chans, int srate)
288 {
289         SF_INFO sfinfo ;
290
291         sfinfo.frames = 0 ;
292         sfinfo.channels = chans ;
293         sfinfo.format = fmt ;
294         sfinfo.samplerate = srate ;
295         sfinfo.sections = 0 ;
296         sfinfo.seekable = 0 ;
297
298         return sf_format_check (&sfinfo) ;
299 }
300
301 /*---------------------------------------------------------------------*/
302
303 inline sf_count_t
304 SndfileHandle::read (short *ptr, sf_count_t items)
305 {       return sf_read_short (p->sf, ptr, items) ; }
306
307 inline sf_count_t
308 SndfileHandle::read (int *ptr, sf_count_t items)
309 {       return sf_read_int (p->sf, ptr, items) ; }
310
311 inline sf_count_t
312 SndfileHandle::read (float *ptr, sf_count_t items)
313 {       return sf_read_float (p->sf, ptr, items) ; }
314
315 inline sf_count_t
316 SndfileHandle::read (double *ptr, sf_count_t items)
317 {       return sf_read_double (p->sf, ptr, items) ; }
318
319 inline sf_count_t
320 SndfileHandle::write (const short *ptr, sf_count_t items)
321 {       return sf_write_short (p->sf, ptr, items) ; }
322
323 inline sf_count_t
324 SndfileHandle::write (const int *ptr, sf_count_t items)
325 {       return sf_write_int (p->sf, ptr, items) ; }
326
327 inline sf_count_t
328 SndfileHandle::write (const float *ptr, sf_count_t items)
329 {       return sf_write_float (p->sf, ptr, items) ; }
330
331 inline sf_count_t
332 SndfileHandle::write (const double *ptr, sf_count_t items)
333 {       return sf_write_double (p->sf, ptr, items) ; }
334
335 inline sf_count_t
336 SndfileHandle::readf (short *ptr, sf_count_t frame_count)
337 {       return sf_readf_short (p->sf, ptr, frame_count) ; }
338
339 inline sf_count_t
340 SndfileHandle::readf (int *ptr, sf_count_t frame_count)
341 {       return sf_readf_int (p->sf, ptr, frame_count) ; }
342
343 inline sf_count_t
344 SndfileHandle::readf (float *ptr, sf_count_t frame_count)
345 {       return sf_readf_float (p->sf, ptr, frame_count) ; }
346
347 inline sf_count_t
348 SndfileHandle::readf (double *ptr, sf_count_t frame_count)
349 {       return sf_readf_double (p->sf, ptr, frame_count) ; }
350
351 inline sf_count_t
352 SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
353 {       return sf_writef_short (p->sf, ptr, frame_count) ; }
354
355 inline sf_count_t
356 SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
357 {       return sf_writef_int (p->sf, ptr, frame_count) ; }
358
359 inline sf_count_t
360 SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
361 {       return sf_writef_float (p->sf, ptr, frame_count) ; }
362
363 inline sf_count_t
364 SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
365 {       return sf_writef_double (p->sf, ptr, frame_count) ; }
366
367 inline sf_count_t
368 SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
369 {       return sf_read_raw (p->sf, ptr, bytes) ; }
370
371 inline sf_count_t
372 SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
373 {       return sf_write_raw (p->sf, ptr, bytes) ; }
374
375
376 #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
377
378 inline
379 SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate)
380 : p (NULL)
381 {
382         p = new (std::nothrow) SNDFILE_ref () ;
383
384         if (p != NULL)
385         {       p->ref = 1 ;
386
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 ;
393
394                 p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
395                 } ;
396
397         return ;
398 } /* SndfileHandle const wchar_t * constructor */
399
400 #endif
401
402 } // namespace AudioGrapher
403
404 #endif  /* SNDFILE_HH */
405