add 176.4kHz to export sample possibilities
[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 <sndfile.h>
56
57 #include <string>
58 #include <new> // for std::nothrow
59
60 // Prevent conflicts
61 namespace AudioGrapher {
62
63 class SndfileHandle
64 {       private :
65                 struct SNDFILE_ref
66                 {       SNDFILE_ref (void) ;
67                         ~SNDFILE_ref (void) ;
68
69                         SNDFILE *sf ;
70                         SF_INFO sfinfo ;
71                         int ref ;
72                         } ;
73
74                 SNDFILE_ref *p ;
75
76         public :
77                         /* Default constructor */
78                         SndfileHandle (void) : p (NULL) {} ;
79                         SndfileHandle (const char *path, int mode = SFM_READ,
80                                                         int format = 0, int channels = 0, int samplerate = 0) ;
81                         SndfileHandle (std::string const & path, int mode = SFM_READ,
82                                                         int format = 0, int channels = 0, int samplerate = 0) ;
83                         SndfileHandle (int fd, bool close_desc, int mode = SFM_READ,
84                                                         int format = 0, int channels = 0, int samplerate = 0) ;
85                         ~SndfileHandle (void) ;
86
87                         SndfileHandle (const SndfileHandle &orig) ;
88                         SndfileHandle & operator = (const SndfileHandle &rhs) ;
89
90                         void close (void) ;
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) { sf_close (sf) ; } }
156
157
158 void
159 SndfileHandle::close (void)
160 {
161         if (p != NULL && --p->ref == 0)
162         {
163                 delete p ;
164                 p = NULL;
165         }
166 }
167
168
169 inline
170 SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
171 : p (NULL)
172 {
173         p = new (std::nothrow) SNDFILE_ref () ;
174
175         if (p != NULL)
176         {       p->ref = 1 ;
177
178                 p->sfinfo.frames = 0 ;
179                 p->sfinfo.channels = chans ;
180                 p->sfinfo.format = fmt ;
181                 p->sfinfo.samplerate = srate ;
182                 p->sfinfo.sections = 0 ;
183                 p->sfinfo.seekable = 0 ;
184
185                 p->sf = sf_open (path, mode, &p->sfinfo) ;
186                 } ;
187
188         return ;
189 } /* SndfileHandle const char * constructor */
190
191 inline
192 SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
193 : p (NULL)
194 {
195         p = new (std::nothrow) SNDFILE_ref () ;
196
197         if (p != NULL)
198         {       p->ref = 1 ;
199
200                 p->sfinfo.frames = 0 ;
201                 p->sfinfo.channels = chans ;
202                 p->sfinfo.format = fmt ;
203                 p->sfinfo.samplerate = srate ;
204                 p->sfinfo.sections = 0 ;
205                 p->sfinfo.seekable = 0 ;
206
207                 p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
208                 } ;
209
210         return ;
211 } /* SndfileHandle std::string constructor */
212
213 inline
214 SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
215 : p (NULL)
216 {
217         if (fd < 0)
218                 return;
219
220         p = new (std::nothrow) SNDFILE_ref () ;
221
222         if (p != NULL)
223         {       p->ref = 1 ;
224
225                 p->sfinfo.frames = 0 ;
226                 p->sfinfo.channels = chans ;
227                 p->sfinfo.format = fmt ;
228                 p->sfinfo.samplerate = srate ;
229                 p->sfinfo.sections = 0 ;
230                 p->sfinfo.seekable = 0 ;
231
232                 p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ;
233                 } ;
234
235         return ;
236 } /* SndfileHandle fd constructor */
237
238 inline
239 SndfileHandle::~SndfileHandle (void)
240 {       if (p != NULL && --p->ref == 0)
241                 delete p ;
242 } /* SndfileHandle destructor */
243
244
245 inline
246 SndfileHandle::SndfileHandle (const SndfileHandle &orig)
247 : p (orig.p)
248 {       if (p != NULL)
249                 ++p->ref ;
250 } /* SndfileHandle copy constructor */
251
252 inline SndfileHandle &
253 SndfileHandle::operator = (const SndfileHandle &rhs)
254 {
255         if (&rhs == this)
256                 return *this ;
257         if (p != NULL && --p->ref == 0)
258                 delete p ;
259
260         p = rhs.p ;
261         if (p != NULL)
262                 ++p->ref ;
263
264         return *this ;
265 } /* SndfileHandle assignment operator */
266
267 inline int
268 SndfileHandle::error (void) const
269 {       return sf_error (p->sf) ; }
270
271 inline const char *
272 SndfileHandle::strError (void) const
273 {       return sf_strerror (p->sf) ; }
274
275 inline int
276 SndfileHandle::command (int cmd, void *data, int datasize)
277 {       return sf_command (p->sf, cmd, data, datasize) ; }
278
279 inline sf_count_t
280 SndfileHandle::seek (sf_count_t frame_count, int whence)
281 {       return sf_seek (p->sf, frame_count, whence) ; }
282
283 inline void
284 SndfileHandle::writeSync (void)
285 {       sf_write_sync (p->sf) ; }
286
287 inline int
288 SndfileHandle::setString (int str_type, const char* str)
289 {       return sf_set_string (p->sf, str_type, str) ; }
290
291 inline const char*
292 SndfileHandle::getString (int str_type) const
293 {       return sf_get_string (p->sf, str_type) ; }
294
295 inline int
296 SndfileHandle::formatCheck(int fmt, int chans, int srate)
297 {
298         SF_INFO sfinfo ;
299
300         sfinfo.frames = 0 ;
301         sfinfo.channels = chans ;
302         sfinfo.format = fmt ;
303         sfinfo.samplerate = srate ;
304         sfinfo.sections = 0 ;
305         sfinfo.seekable = 0 ;
306
307         return sf_format_check (&sfinfo) ;
308 }
309
310 /*---------------------------------------------------------------------*/
311
312 inline sf_count_t
313 SndfileHandle::read (short *ptr, sf_count_t items)
314 {       return sf_read_short (p->sf, ptr, items) ; }
315
316 inline sf_count_t
317 SndfileHandle::read (int *ptr, sf_count_t items)
318 {       return sf_read_int (p->sf, ptr, items) ; }
319
320 inline sf_count_t
321 SndfileHandle::read (float *ptr, sf_count_t items)
322 {       return sf_read_float (p->sf, ptr, items) ; }
323
324 inline sf_count_t
325 SndfileHandle::read (double *ptr, sf_count_t items)
326 {       return sf_read_double (p->sf, ptr, items) ; }
327
328 inline sf_count_t
329 SndfileHandle::write (const short *ptr, sf_count_t items)
330 {       return sf_write_short (p->sf, ptr, items) ; }
331
332 inline sf_count_t
333 SndfileHandle::write (const int *ptr, sf_count_t items)
334 {       return sf_write_int (p->sf, ptr, items) ; }
335
336 inline sf_count_t
337 SndfileHandle::write (const float *ptr, sf_count_t items)
338 {       return sf_write_float (p->sf, ptr, items) ; }
339
340 inline sf_count_t
341 SndfileHandle::write (const double *ptr, sf_count_t items)
342 {       return sf_write_double (p->sf, ptr, items) ; }
343
344 inline sf_count_t
345 SndfileHandle::readf (short *ptr, sf_count_t frame_count)
346 {       return sf_readf_short (p->sf, ptr, frame_count) ; }
347
348 inline sf_count_t
349 SndfileHandle::readf (int *ptr, sf_count_t frame_count)
350 {       return sf_readf_int (p->sf, ptr, frame_count) ; }
351
352 inline sf_count_t
353 SndfileHandle::readf (float *ptr, sf_count_t frame_count)
354 {       return sf_readf_float (p->sf, ptr, frame_count) ; }
355
356 inline sf_count_t
357 SndfileHandle::readf (double *ptr, sf_count_t frame_count)
358 {       return sf_readf_double (p->sf, ptr, frame_count) ; }
359
360 inline sf_count_t
361 SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
362 {       return sf_writef_short (p->sf, ptr, frame_count) ; }
363
364 inline sf_count_t
365 SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
366 {       return sf_writef_int (p->sf, ptr, frame_count) ; }
367
368 inline sf_count_t
369 SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
370 {       return sf_writef_float (p->sf, ptr, frame_count) ; }
371
372 inline sf_count_t
373 SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
374 {       return sf_writef_double (p->sf, ptr, frame_count) ; }
375
376 inline sf_count_t
377 SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
378 {       return sf_read_raw (p->sf, ptr, bytes) ; }
379
380 inline sf_count_t
381 SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
382 {       return sf_write_raw (p->sf, ptr, bytes) ; }
383
384
385 #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
386
387 inline
388 SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate)
389 : p (NULL)
390 {
391         p = new (std::nothrow) SNDFILE_ref () ;
392
393         if (p != NULL)
394         {       p->ref = 1 ;
395
396                 p->sfinfo.frames = 0 ;
397                 p->sfinfo.channels = chans ;
398                 p->sfinfo.format = fmt ;
399                 p->sfinfo.samplerate = srate ;
400                 p->sfinfo.sections = 0 ;
401                 p->sfinfo.seekable = 0 ;
402
403                 p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
404                 } ;
405
406         return ;
407 } /* SndfileHandle const wchar_t * constructor */
408
409 #endif
410
411 } // namespace AudioGrapher
412
413 #endif  /* SNDFILE_HH */
414