Debian Codegraph @

include/sndfile.hh

raw
1 /*
2 ** Copyright (C) 2005-2017 Erik de Castro Lopo <[email protected]>
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://libsndfile.github.io/libsndfile/api.html.
50 **
51 ** This file is intended to compile with C++98 and newer.
52 */
53
54 #ifndef SNDFILE_HH
55 #define SNDFILE_HH
56
57 #include <sndfile.h>
58
59 #include <string>
60 #include <new> // for std::nothrow
61
62 #if ((defined (_MSC_VER) && (_MSC_VER >= 1600)) || (__cplusplus >= 201100L))
63 #define SF_NULL nullptr
64 #else
65 #define SF_NULL NULL
66 #endif
67
68 class SndfileHandle
69 { private :
70 struct SNDFILE_ref
71 { SNDFILE_ref (void) ;
72 ~SNDFILE_ref (void) ;
73
74 SNDFILE *sf ;
75 SF_INFO sfinfo ;
76 int ref ;
77 } ;
78
79 SNDFILE_ref *p ;
80
81 public :
82 /* Default constructor */
83 SndfileHandle (void) : p (SF_NULL) {} ;
84 SndfileHandle (const char *path, int mode = SFM_READ,
85 int format = 0, int channels = 0, int samplerate = 0) ;
86 SndfileHandle (std::string const & path, int mode = SFM_READ,
87 int format = 0, int channels = 0, int samplerate = 0) ;
88 SndfileHandle (int fd, bool close_desc, int mode = SFM_READ,
89 int format = 0, int channels = 0, int samplerate = 0) ;
90 SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode = SFM_READ,
91 int format = 0, int channels = 0, int samplerate = 0) ;
92
93 #ifdef _WIN32
94 SndfileHandle (const wchar_t *wpath, int mode = SFM_READ,
95 int format = 0, int channels = 0, int samplerate = 0) ;
96 #endif
97
98 ~SndfileHandle (void) ;
99
100 SndfileHandle (const SndfileHandle &orig) ;
101 SndfileHandle & operator = (const SndfileHandle &rhs) ;
102
103 #if (__cplusplus >= 201100L)
104 SndfileHandle (SndfileHandle &&orig) noexcept ;
105 SndfileHandle & operator = (SndfileHandle &&rhs) noexcept ;
106 #endif
107
108 /* Mainly for debugging/testing. */
109 int refCount (void) const { return (p == SF_NULL) ? 0 : p->ref ; }
110
111 operator bool () const { return (p != SF_NULL) ; }
112
113 bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
114
115 sf_count_t frames (void) const { return p ? p->sfinfo.frames : 0 ; }
116 int format (void) const { return p ? p->sfinfo.format : 0 ; }
117 int channels (void) const { return p ? p->sfinfo.channels : 0 ; }
118 int samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; }
119
120 int error (void) const ;
121 const char * strError (void) const ;
122
123 int command (int cmd, void *data, int datasize) ;
124
125 sf_count_t seek (sf_count_t frames, int whence) ;
126
127 void writeSync (void) ;
128
129 int setString (int str_type, const char* str) ;
130
131 const char* getString (int str_type) const ;
132
133 static int formatCheck (int format, int channels, int samplerate) ;
134
135 sf_count_t read (short *ptr, sf_count_t items) ;
136 sf_count_t read (int *ptr, sf_count_t items) ;
137 sf_count_t read (float *ptr, sf_count_t items) ;
138 sf_count_t read (double *ptr, sf_count_t items) ;
139
140 sf_count_t write (const short *ptr, sf_count_t items) ;
141 sf_count_t write (const int *ptr, sf_count_t items) ;
142 sf_count_t write (const float *ptr, sf_count_t items) ;
143 sf_count_t write (const double *ptr, sf_count_t items) ;
144
145 sf_count_t readf (short *ptr, sf_count_t frames) ;
146 sf_count_t readf (int *ptr, sf_count_t frames) ;
147 sf_count_t readf (float *ptr, sf_count_t frames) ;
148 sf_count_t readf (double *ptr, sf_count_t frames) ;
149
150 sf_count_t writef (const short *ptr, sf_count_t frames) ;
151 sf_count_t writef (const int *ptr, sf_count_t frames) ;
152 sf_count_t writef (const float *ptr, sf_count_t frames) ;
153 sf_count_t writef (const double *ptr, sf_count_t frames) ;
154
155 sf_count_t readRaw (void *ptr, sf_count_t bytes) ;
156 sf_count_t writeRaw (const void *ptr, sf_count_t bytes) ;
157
158 /**< Raw access to the handle. SndfileHandle keeps ownership. */
159 SNDFILE * rawHandle (void) ;
160
161 /**< Take ownership of handle, if reference count is 1. */
162 SNDFILE * takeOwnership (void) ;
163 } ;
164
165 /*==============================================================================
166 ** Nothing but implementation below.
167 */
168
169 inline
170 SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
171 : sf (SF_NULL), sfinfo (), ref (1)
172 {}
173
174 inline
175 SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
176 { if (sf != SF_NULL) sf_close (sf) ; }
177
178 inline
179 SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
180 : p (SF_NULL)
181 {
182 p = new (std::nothrow) SNDFILE_ref () ;
183
184 if (p != SF_NULL)
185 { p->ref = 1 ;
186
187 p->sfinfo.frames = 0 ;
188 p->sfinfo.channels = chans ;
189 p->sfinfo.format = fmt ;
190 p->sfinfo.samplerate = srate ;
191 p->sfinfo.sections = 0 ;
192 p->sfinfo.seekable = 0 ;
193
194 p->sf = sf_open (path, mode, &p->sfinfo) ;
195 } ;
196
197 return ;
198 } /* SndfileHandle const char * constructor */
199
200 inline
201 SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int chans, int srate)
202 : p (SF_NULL)
203 {
204 p = new (std::nothrow) SNDFILE_ref () ;
205
206 if (p != SF_NULL)
207 { p->ref = 1 ;
208
209 p->sfinfo.frames = 0 ;
210 p->sfinfo.channels = chans ;
211 p->sfinfo.format = fmt ;
212 p->sfinfo.samplerate = srate ;
213 p->sfinfo.sections = 0 ;
214 p->sfinfo.seekable = 0 ;
215
216 p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
217 } ;
218
219 return ;
220 } /* SndfileHandle std::string constructor */
221
222 inline
223 SndfileHandle::SndfileHandle (int fd, bool close_desc, int mode, int fmt, int chans, int srate)
224 : p (SF_NULL)
225 {
226 if (fd < 0)
227 return ;
228
229 p = new (std::nothrow) SNDFILE_ref () ;
230
231 if (p != SF_NULL)
232 { p->ref = 1 ;
233
234 p->sfinfo.frames = 0 ;
235 p->sfinfo.channels = chans ;
236 p->sfinfo.format = fmt ;
237 p->sfinfo.samplerate = srate ;
238 p->sfinfo.sections = 0 ;
239 p->sfinfo.seekable = 0 ;
240
241 p->sf = sf_open_fd (fd, mode, &p->sfinfo, close_desc) ;
242 } ;
243
244 return ;
245 } /* SndfileHandle fd constructor */
246
247 inline
248 SndfileHandle::SndfileHandle (SF_VIRTUAL_IO &sfvirtual, void *user_data, int mode, int fmt, int chans, int srate)
249 : p (SF_NULL)
250 {
251 p = new (std::nothrow) SNDFILE_ref () ;
252
253 if (p != SF_NULL)
254 { p->ref = 1 ;
255
256 p->sfinfo.frames = 0 ;
257 p->sfinfo.channels = chans ;
258 p->sfinfo.format = fmt ;
259 p->sfinfo.samplerate = srate ;
260 p->sfinfo.sections = 0 ;
261 p->sfinfo.seekable = 0 ;
262
263 p->sf = sf_open_virtual (&sfvirtual, mode, &p->sfinfo, user_data) ;
264 } ;
265
266 return ;
267 } /* SndfileHandle std::string constructor */
268
269 inline
270 SndfileHandle::~SndfileHandle (void)
271 { if (p != SF_NULL && -- p->ref == 0)
272 delete p ;
273 } /* SndfileHandle destructor */
274
275
276 inline
277 SndfileHandle::SndfileHandle (const SndfileHandle &orig)
278 : p (orig.p)
279 { if (p != SF_NULL)
280 ++ p->ref ;
281 } /* SndfileHandle copy constructor */
282
283 inline SndfileHandle &
284 SndfileHandle::operator = (const SndfileHandle &rhs)
285 {
286 if (&rhs == this)
287 return *this ;
288 if (p != SF_NULL && -- p->ref == 0)
289 delete p ;
290
291 p = rhs.p ;
292 if (p != SF_NULL)
293 ++ p->ref ;
294
295 return *this ;
296 } /* SndfileHandle copy assignment */
297
298 #if (__cplusplus >= 201100L)
299
300 inline
301 SndfileHandle::SndfileHandle (SndfileHandle &&orig) noexcept
302 : p (orig.p)
303 {
304 orig.p = SF_NULL ;
305 } /* SndfileHandle move constructor */
306
307 inline SndfileHandle &
308 SndfileHandle::operator = (SndfileHandle &&rhs) noexcept
309 {
310 if (&rhs == this)
311 return *this ;
312 if (p != SF_NULL && -- p->ref == 0)
313 delete p ;
314
315 p = rhs.p ;
316 rhs.p = SF_NULL ;
317
318 return *this ;
319 } /* SndfileHandle move assignment */
320
321 #endif
322
323 inline int
324 SndfileHandle::error (void) const
325 { return sf_error (p->sf) ; }
326
327 inline const char *
328 SndfileHandle::strError (void) const
329 { return sf_strerror (p->sf) ; }
330
331 inline int
332 SndfileHandle::command (int cmd, void *data, int datasize)
333 { return sf_command (p->sf, cmd, data, datasize) ; }
334
335 inline sf_count_t
336 SndfileHandle::seek (sf_count_t frame_count, int whence)
337 { return sf_seek (p->sf, frame_count, whence) ; }
338
339 inline void
340 SndfileHandle::writeSync (void)
341 { sf_write_sync (p->sf) ; }
342
343 inline int
344 SndfileHandle::setString (int str_type, const char* str)
345 { return sf_set_string (p->sf, str_type, str) ; }
346
347 inline const char*
348 SndfileHandle::getString (int str_type) const
349 { return sf_get_string (p->sf, str_type) ; }
350
351 inline int
352 SndfileHandle::formatCheck (int fmt, int chans, int srate)
353 {
354 SF_INFO sfinfo ;
355
356 sfinfo.frames = 0 ;
357 sfinfo.channels = chans ;
358 sfinfo.format = fmt ;
359 sfinfo.samplerate = srate ;
360 sfinfo.sections = 0 ;
361 sfinfo.seekable = 0 ;
362
363 return sf_format_check (&sfinfo) ;
364 }
365
366 /*---------------------------------------------------------------------*/
367
368 inline sf_count_t
369 SndfileHandle::read (short *ptr, sf_count_t items)
370 { return sf_read_short (p->sf, ptr, items) ; }
371
372 inline sf_count_t
373 SndfileHandle::read (int *ptr, sf_count_t items)
374 { return sf_read_int (p->sf, ptr, items) ; }
375
376 inline sf_count_t
377 SndfileHandle::read (float *ptr, sf_count_t items)
378 { return sf_read_float (p->sf, ptr, items) ; }
379
380 inline sf_count_t
381 SndfileHandle::read (double *ptr, sf_count_t items)
382 { return sf_read_double (p->sf, ptr, items) ; }
383
384 inline sf_count_t
385 SndfileHandle::write (const short *ptr, sf_count_t items)
386 { return sf_write_short (p->sf, ptr, items) ; }
387
388 inline sf_count_t
389 SndfileHandle::write (const int *ptr, sf_count_t items)
390 { return sf_write_int (p->sf, ptr, items) ; }
391
392 inline sf_count_t
393 SndfileHandle::write (const float *ptr, sf_count_t items)
394 { return sf_write_float (p->sf, ptr, items) ; }
395
396 inline sf_count_t
397 SndfileHandle::write (const double *ptr, sf_count_t items)
398 { return sf_write_double (p->sf, ptr, items) ; }
399
400 inline sf_count_t
401 SndfileHandle::readf (short *ptr, sf_count_t frame_count)
402 { return sf_readf_short (p->sf, ptr, frame_count) ; }
403
404 inline sf_count_t
405 SndfileHandle::readf (int *ptr, sf_count_t frame_count)
406 { return sf_readf_int (p->sf, ptr, frame_count) ; }
407
408 inline sf_count_t
409 SndfileHandle::readf (float *ptr, sf_count_t frame_count)
410 { return sf_readf_float (p->sf, ptr, frame_count) ; }
411
412 inline sf_count_t
413 SndfileHandle::readf (double *ptr, sf_count_t frame_count)
414 { return sf_readf_double (p->sf, ptr, frame_count) ; }
415
416 inline sf_count_t
417 SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
418 { return sf_writef_short (p->sf, ptr, frame_count) ; }
419
420 inline sf_count_t
421 SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
422 { return sf_writef_int (p->sf, ptr, frame_count) ; }
423
424 inline sf_count_t
425 SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
426 { return sf_writef_float (p->sf, ptr, frame_count) ; }
427
428 inline sf_count_t
429 SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
430 { return sf_writef_double (p->sf, ptr, frame_count) ; }
431
432 inline sf_count_t
433 SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
434 { return sf_read_raw (p->sf, ptr, bytes) ; }
435
436 inline sf_count_t
437 SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
438 { return sf_write_raw (p->sf, ptr, bytes) ; }
439
440 inline SNDFILE *
441 SndfileHandle::rawHandle (void)
442 { return (p ? p->sf : SF_NULL) ; }
443
444 inline SNDFILE *
445 SndfileHandle::takeOwnership (void)
446 {
447 if (p == SF_NULL || (p->ref != 1))
448 return SF_NULL ;
449
450 SNDFILE * sf = p->sf ;
451 p->sf = SF_NULL ;
452 delete p ;
453 p = SF_NULL ;
454 return sf ;
455 }
456
457 #ifdef _WIN32
458
459 inline
460 SndfileHandle::SndfileHandle (const wchar_t *wpath, int mode, int fmt, int chans, int srate)
461 : p (SF_NULL)
462 {
463 p = new (std::nothrow) SNDFILE_ref () ;
464
465 if (p != SF_NULL)
466 { p->ref = 1 ;
467
468 p->sfinfo.frames = 0 ;
469 p->sfinfo.channels = chans ;
470 p->sfinfo.format = fmt ;
471 p->sfinfo.samplerate = srate ;
472 p->sfinfo.sections = 0 ;
473 p->sfinfo.seekable = 0 ;
474
475 p->sf = sf_wchar_open (wpath, mode, &p->sfinfo) ;
476 } ;
477
478 return ;
479 } /* SndfileHandle const wchar_t * constructor */
480
481 #endif
482
483 #endif /* SNDFILE_HH */
484