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