Class 'AudioGrapher::Analyser' needs to be exportable for building with MSVC
[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 <sys/types.h>
56 #include <sys/stat.h>
57 #include <fcntl.h>
58
59 #include <glib.h>
60 #include "pbd/gstdio_compat.h"
61
62 #include <sndfile.h>
63
64 #include <string>
65 #include <new> // for std::nothrow
66
67 // Prevent conflicts
68 namespace AudioGrapher {
69
70 class SndfileHandle
71 {       private :
72                 struct SNDFILE_ref
73                 {       SNDFILE_ref (void) ;
74                         ~SNDFILE_ref (void) ;
75
76                         SNDFILE *sf ;
77                         SF_INFO sfinfo ;
78                         int ref ;
79                         } ;
80
81                 SNDFILE_ref *p ;
82
83         public :
84                         /* Default constructor */
85                         SndfileHandle (void) : p (NULL) {} ;
86                         SndfileHandle (const char *path, int mode = SFM_READ,
87                                                         int format = 0, int channels = 0, int samplerate = 0) ;
88                         SndfileHandle (std::string const & path, int mode = SFM_READ,
89                                                         int format = 0, int channels = 0, int samplerate = 0) ;
90                         SndfileHandle (int fd, bool close_desc, int mode = SFM_READ,
91                                                         int format = 0, int channels = 0, int samplerate = 0) ;
92                         ~SndfileHandle (void) ;
93
94                         SndfileHandle (const SndfileHandle &orig) ;
95                         SndfileHandle & operator = (const SndfileHandle &rhs) ;
96
97                         void close (void) ;
98
99                 /* Mainly for debugging/testing. */
100                 int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
101
102                 operator bool () const { return (p != NULL) ; }
103
104                 bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
105
106                 sf_count_t      frames (void) const             { return p ? p->sfinfo.frames : 0 ; }
107                 int                     format (void) const             { return p ? p->sfinfo.format : 0 ; }
108                 int                     channels (void) const   { return p ? p->sfinfo.channels : 0 ; }
109                 int                     samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; }
110
111                 int error (void) const ;
112                 const char * strError (void) const ;
113
114                 int command (int cmd, void *data, int datasize) ;
115
116                 sf_count_t      seek (sf_count_t frames, int whence) ;
117
118                 void writeSync (void) ;
119
120                 int setString (int str_type, const char* str) ;
121
122                 const char* getString (int str_type) const ;
123
124                 static int formatCheck (int format, int channels, int samplerate) ;
125
126                 sf_count_t read (short *ptr, sf_count_t items) ;
127                 sf_count_t read (int *ptr, sf_count_t items) ;
128                 sf_count_t read (float *ptr, sf_count_t items) ;
129                 sf_count_t read (double *ptr, sf_count_t items) ;
130
131                 sf_count_t write (const short *ptr, sf_count_t items) ;
132                 sf_count_t write (const int *ptr, sf_count_t items) ;
133                 sf_count_t write (const float *ptr, sf_count_t items) ;
134                 sf_count_t write (const double *ptr, sf_count_t items) ;
135
136                 sf_count_t readf (short *ptr, sf_count_t frames) ;
137                 sf_count_t readf (int *ptr, sf_count_t frames) ;
138                 sf_count_t readf (float *ptr, sf_count_t frames) ;
139                 sf_count_t readf (double *ptr, sf_count_t frames) ;
140
141                 sf_count_t writef (const short *ptr, sf_count_t frames) ;
142                 sf_count_t writef (const int *ptr, sf_count_t frames) ;
143                 sf_count_t writef (const float *ptr, sf_count_t frames) ;
144                 sf_count_t writef (const double *ptr, sf_count_t frames) ;
145
146                 sf_count_t      readRaw         (void *ptr, sf_count_t bytes) ;
147                 sf_count_t      writeRaw        (const void *ptr, sf_count_t bytes) ;
148
149 } ;
150
151 /*==============================================================================
152 **      Nothing but implementation below.
153 */
154
155 inline
156 SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
157 : ref (1)
158 {}
159
160 inline
161 SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
162 {       if (sf != NULL) { sf_close (sf) ; } }
163
164
165 void
166 SndfileHandle::close (void)
167 {
168         if (p != NULL && --p->ref == 0)
169         {
170                 delete p ;
171                 p = NULL;
172         }
173 }
174
175
176 inline
177 SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
178 : p (NULL)
179 {
180         p = new (std::nothrow) SNDFILE_ref () ;
181
182         if (p != NULL)
183         {       p->ref = 1 ;
184
185                 p->sfinfo.frames = 0 ;
186                 p->sfinfo.channels = chans ;
187                 p->sfinfo.format = fmt ;
188                 p->sfinfo.samplerate = srate ;
189                 p->sfinfo.sections = 0 ;
190                 p->sfinfo.seekable = 0 ;
191
192                 bool writable = false;
193                 if (mode & SFM_WRITE) {
194                         writable = true;
195                 }
196 #ifdef PLATFORM_WINDOWS
197                 int fd = g_open (path, writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
198 #else
199                 int fd = ::open (path, writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
200 #endif
201
202                 p->sf = sf_open_fd (fd, mode, &p->sfinfo, true) ;
203                 } ;
204
205         return ;
206 } /* SndfileHandle const char * constructor */
207
208 inline
209 SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
210 : p (NULL)
211 {
212         p = new (std::nothrow) SNDFILE_ref () ;
213
214         if (p != NULL)
215         {       p->ref = 1 ;
216
217                 p->sfinfo.frames = 0 ;
218                 p->sfinfo.channels = chans ;
219                 p->sfinfo.format = fmt ;
220                 p->sfinfo.samplerate = srate ;
221                 p->sfinfo.sections = 0 ;
222                 p->sfinfo.seekable = 0 ;
223
224                 bool writable = false;
225                 if (mode & SFM_WRITE) {
226                         writable = true;
227                 }
228 #ifdef PLATFORM_WINDOWS
229                 int fd = g_open (path.c_str(), writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
230 #else
231                 int fd = ::open (path.c_str(), writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
232 #endif
233
234                 p->sf = sf_open_fd (fd, mode, &p->sfinfo, true) ;
235                 } ;
236
237         return ;
238 } /* SndfileHandle std::string constructor */
239
240 inline
241 SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
242 : p (NULL)
243 {
244         if (fd < 0)
245                 return;
246
247         p = new (std::nothrow) SNDFILE_ref () ;
248
249         if (p != NULL)
250         {       p->ref = 1 ;
251
252                 p->sfinfo.frames = 0 ;
253                 p->sfinfo.channels = chans ;
254                 p->sfinfo.format = fmt ;
255                 p->sfinfo.samplerate = srate ;
256                 p->sfinfo.sections = 0 ;
257                 p->sfinfo.seekable = 0 ;
258
259                 p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ;
260                 } ;
261
262         return ;
263 } /* SndfileHandle fd constructor */
264
265 inline
266 SndfileHandle::~SndfileHandle (void)
267 {       if (p != NULL && --p->ref == 0)
268                 delete p ;
269 } /* SndfileHandle destructor */
270
271
272 inline
273 SndfileHandle::SndfileHandle (const SndfileHandle &orig)
274 : p (orig.p)
275 {       if (p != NULL)
276                 ++p->ref ;
277 } /* SndfileHandle copy constructor */
278
279 inline SndfileHandle &
280 SndfileHandle::operator = (const SndfileHandle &rhs)
281 {
282         if (&rhs == this)
283                 return *this ;
284         if (p != NULL && --p->ref == 0)
285                 delete p ;
286
287         p = rhs.p ;
288         if (p != NULL)
289                 ++p->ref ;
290
291         return *this ;
292 } /* SndfileHandle assignment operator */
293
294 inline int
295 SndfileHandle::error (void) const
296 {       return sf_error (p->sf) ; }
297
298 inline const char *
299 SndfileHandle::strError (void) const
300 {       return sf_strerror (p->sf) ; }
301
302 inline int
303 SndfileHandle::command (int cmd, void *data, int datasize)
304 {       return sf_command (p->sf, cmd, data, datasize) ; }
305
306 inline sf_count_t
307 SndfileHandle::seek (sf_count_t frame_count, int whence)
308 {       return sf_seek (p->sf, frame_count, whence) ; }
309
310 inline void
311 SndfileHandle::writeSync (void)
312 {       sf_write_sync (p->sf) ; }
313
314 inline int
315 SndfileHandle::setString (int str_type, const char* str)
316 {       return sf_set_string (p->sf, str_type, str) ; }
317
318 inline const char*
319 SndfileHandle::getString (int str_type) const
320 {       return sf_get_string (p->sf, str_type) ; }
321
322 inline int
323 SndfileHandle::formatCheck(int fmt, int chans, int srate)
324 {
325         SF_INFO sfinfo ;
326
327         sfinfo.frames = 0 ;
328         sfinfo.channels = chans ;
329         sfinfo.format = fmt ;
330         sfinfo.samplerate = srate ;
331         sfinfo.sections = 0 ;
332         sfinfo.seekable = 0 ;
333
334         return sf_format_check (&sfinfo) ;
335 }
336
337 /*---------------------------------------------------------------------*/
338
339 inline sf_count_t
340 SndfileHandle::read (short *ptr, sf_count_t items)
341 {       return sf_read_short (p->sf, ptr, items) ; }
342
343 inline sf_count_t
344 SndfileHandle::read (int *ptr, sf_count_t items)
345 {       return sf_read_int (p->sf, ptr, items) ; }
346
347 inline sf_count_t
348 SndfileHandle::read (float *ptr, sf_count_t items)
349 {       return sf_read_float (p->sf, ptr, items) ; }
350
351 inline sf_count_t
352 SndfileHandle::read (double *ptr, sf_count_t items)
353 {       return sf_read_double (p->sf, ptr, items) ; }
354
355 inline sf_count_t
356 SndfileHandle::write (const short *ptr, sf_count_t items)
357 {       return sf_write_short (p->sf, ptr, items) ; }
358
359 inline sf_count_t
360 SndfileHandle::write (const int *ptr, sf_count_t items)
361 {       return sf_write_int (p->sf, ptr, items) ; }
362
363 inline sf_count_t
364 SndfileHandle::write (const float *ptr, sf_count_t items)
365 {       return sf_write_float (p->sf, ptr, items) ; }
366
367 inline sf_count_t
368 SndfileHandle::write (const double *ptr, sf_count_t items)
369 {       return sf_write_double (p->sf, ptr, items) ; }
370
371 inline sf_count_t
372 SndfileHandle::readf (short *ptr, sf_count_t frame_count)
373 {       return sf_readf_short (p->sf, ptr, frame_count) ; }
374
375 inline sf_count_t
376 SndfileHandle::readf (int *ptr, sf_count_t frame_count)
377 {       return sf_readf_int (p->sf, ptr, frame_count) ; }
378
379 inline sf_count_t
380 SndfileHandle::readf (float *ptr, sf_count_t frame_count)
381 {       return sf_readf_float (p->sf, ptr, frame_count) ; }
382
383 inline sf_count_t
384 SndfileHandle::readf (double *ptr, sf_count_t frame_count)
385 {       return sf_readf_double (p->sf, ptr, frame_count) ; }
386
387 inline sf_count_t
388 SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
389 {       return sf_writef_short (p->sf, ptr, frame_count) ; }
390
391 inline sf_count_t
392 SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
393 {       return sf_writef_int (p->sf, ptr, frame_count) ; }
394
395 inline sf_count_t
396 SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
397 {       return sf_writef_float (p->sf, ptr, frame_count) ; }
398
399 inline sf_count_t
400 SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
401 {       return sf_writef_double (p->sf, ptr, frame_count) ; }
402
403 inline sf_count_t
404 SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
405 {       return sf_read_raw (p->sf, ptr, bytes) ; }
406
407 inline sf_count_t
408 SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
409 {       return sf_write_raw (p->sf, ptr, bytes) ; }
410
411
412 #ifdef ENABLE_SNDFILE_WINDOWS_PROTOTYPES
413
414 inline
415 SndfileHandle::SndfileHandle (LPCWSTR wpath, int mode, int fmt, int chans, int srate)
416 : p (NULL)
417 {
418         p = new (std::nothrow) SNDFILE_ref () ;
419
420         if (p != NULL)
421         {       p->ref = 1 ;
422
423                 p->sfinfo.frames = 0 ;
424                 p->sfinfo.channels = chans ;
425                 p->sfinfo.format = fmt ;
426                 p->sfinfo.samplerate = srate ;
427                 p->sfinfo.sections = 0 ;
428                 p->sfinfo.seekable = 0 ;
429
430                 p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
431                 } ;
432
433         return ;
434 } /* SndfileHandle const wchar_t * constructor */
435
436 #endif
437
438 } // namespace AudioGrapher
439
440 #endif  /* SNDFILE_HH */
441