andrew@0
|
1 /*
|
andrew@0
|
2 ** Copyright (C) 2005,2006 Erik de Castro Lopo <erikd@mega-nerd.com>
|
andrew@0
|
3 **
|
andrew@0
|
4 ** All rights reserved.
|
andrew@0
|
5 **
|
andrew@0
|
6 ** Redistribution and use in source and binary forms, with or without
|
andrew@0
|
7 ** modification, are permitted provided that the following conditions are
|
andrew@0
|
8 ** met:
|
andrew@0
|
9 **
|
andrew@0
|
10 ** * Redistributions of source code must retain the above copyright
|
andrew@0
|
11 ** notice, this list of conditions and the following disclaimer.
|
andrew@0
|
12 ** * Redistributions in binary form must reproduce the above copyright
|
andrew@0
|
13 ** notice, this list of conditions and the following disclaimer in
|
andrew@0
|
14 ** the documentation and/or other materials provided with the
|
andrew@0
|
15 ** distribution.
|
andrew@0
|
16 ** * Neither the author nor the names of any contributors may be used
|
andrew@0
|
17 ** to endorse or promote products derived from this software without
|
andrew@0
|
18 ** specific prior written permission.
|
andrew@0
|
19 **
|
andrew@0
|
20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
andrew@0
|
21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
andrew@0
|
22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
andrew@0
|
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
andrew@0
|
24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
andrew@0
|
25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
andrew@0
|
26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
andrew@0
|
27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
andrew@0
|
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
andrew@0
|
29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
andrew@0
|
30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
andrew@0
|
31 */
|
andrew@0
|
32
|
andrew@0
|
33 /*
|
andrew@0
|
34 ** The above modified BSD style license (GPL and LGPL compatible) applies to
|
andrew@0
|
35 ** this file. It does not apply to libsndfile itself which is released under
|
andrew@0
|
36 ** the GNU LGPL or the libsndfile test suite which is released under the GNU
|
andrew@0
|
37 ** GPL.
|
andrew@0
|
38 ** This means that this header file can be used under this modified BSD style
|
andrew@0
|
39 ** license, but the LGPL still holds for the libsndfile library itself.
|
andrew@0
|
40 */
|
andrew@0
|
41
|
andrew@0
|
42 /*
|
andrew@0
|
43 ** sndfile.hh -- A lightweight C++ wrapper for the libsndfile API.
|
andrew@0
|
44 **
|
andrew@0
|
45 ** All the methods are inlines and all functionality is contained in this
|
andrew@0
|
46 ** file. There is no separate implementation file.
|
andrew@0
|
47 **
|
andrew@0
|
48 ** API documentation is in the doc/ directory of the source code tarball
|
andrew@0
|
49 ** and at http://www.mega-nerd.com/libsndfile/api.html.
|
andrew@0
|
50 */
|
andrew@0
|
51
|
andrew@0
|
52 #ifndef SNDFILE_HH
|
andrew@0
|
53 #define SNDFILE_HH
|
andrew@0
|
54
|
andrew@0
|
55 #include <sndfile.h>
|
andrew@0
|
56
|
andrew@0
|
57 #include <new> // for std::nothrow
|
andrew@0
|
58
|
andrew@0
|
59 class SndfileHandle
|
andrew@0
|
60 { private :
|
andrew@0
|
61 struct SNDFILE_ref
|
andrew@0
|
62 { SNDFILE_ref (void) ;
|
andrew@0
|
63 ~SNDFILE_ref (void) ;
|
andrew@0
|
64
|
andrew@0
|
65 SNDFILE *sf ;
|
andrew@0
|
66 SF_INFO sfinfo ;
|
andrew@0
|
67 int ref ;
|
andrew@0
|
68 } ;
|
andrew@0
|
69
|
andrew@0
|
70 SNDFILE_ref *p ;
|
andrew@0
|
71
|
andrew@0
|
72 public :
|
andrew@0
|
73 /* Default constructor */
|
andrew@0
|
74 SndfileHandle (void) : p (NULL) {} ;
|
andrew@0
|
75 SndfileHandle (const char *path, int mode = SFM_READ,
|
andrew@0
|
76 int format = 0, int channels = 0, int samplerate = 0) ;
|
andrew@0
|
77 ~SndfileHandle (void) ;
|
andrew@0
|
78
|
andrew@0
|
79 SndfileHandle (const SndfileHandle &orig) ;
|
andrew@0
|
80 SndfileHandle & operator = (const SndfileHandle &rhs) ;
|
andrew@0
|
81
|
andrew@0
|
82 /* Mainly for debugging/testing. */
|
andrew@0
|
83 int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
|
andrew@0
|
84
|
andrew@0
|
85 operator bool () const { return (p != NULL) ; }
|
andrew@0
|
86
|
andrew@0
|
87 bool operator == (const SndfileHandle &rhs) const { return (p == rhs.p) ; }
|
andrew@0
|
88
|
andrew@0
|
89 sf_count_t frames (void) const { return p ? p->sfinfo.frames : 0 ; }
|
andrew@0
|
90 int format (void) const { return p ? p->sfinfo.format : 0 ; }
|
andrew@0
|
91 int channels (void) const { return p ? p->sfinfo.channels : 0 ; }
|
andrew@0
|
92 int samplerate (void) const { return p ? p->sfinfo.samplerate : 0 ; }
|
andrew@0
|
93
|
andrew@0
|
94 int error (void) const ;
|
andrew@0
|
95 const char * strError (void) const ;
|
andrew@0
|
96
|
andrew@0
|
97 int command (int cmd, void *data, int datasize) ;
|
andrew@0
|
98
|
andrew@0
|
99 sf_count_t seek (sf_count_t frames, int whence) ;
|
andrew@0
|
100
|
andrew@0
|
101 void writeSync (void) ;
|
andrew@0
|
102
|
andrew@0
|
103 int setString (int str_type, const char* str) ;
|
andrew@0
|
104
|
andrew@0
|
105 const char* getString (int str_type) const ;
|
andrew@0
|
106
|
andrew@0
|
107 sf_count_t read (short *ptr, sf_count_t items) ;
|
andrew@0
|
108 sf_count_t read (int *ptr, sf_count_t items) ;
|
andrew@0
|
109 sf_count_t read (float *ptr, sf_count_t items) ;
|
andrew@0
|
110 sf_count_t read (double *ptr, sf_count_t items) ;
|
andrew@0
|
111
|
andrew@0
|
112 sf_count_t write (const short *ptr, sf_count_t items) ;
|
andrew@0
|
113 sf_count_t write (const int *ptr, sf_count_t items) ;
|
andrew@0
|
114 sf_count_t write (const float *ptr, sf_count_t items) ;
|
andrew@0
|
115 sf_count_t write (const double *ptr, sf_count_t items) ;
|
andrew@0
|
116
|
andrew@0
|
117 sf_count_t readf (short *ptr, sf_count_t frames) ;
|
andrew@0
|
118 sf_count_t readf (int *ptr, sf_count_t frames) ;
|
andrew@0
|
119 sf_count_t readf (float *ptr, sf_count_t frames) ;
|
andrew@0
|
120 sf_count_t readf (double *ptr, sf_count_t frames) ;
|
andrew@0
|
121
|
andrew@0
|
122 sf_count_t writef (const short *ptr, sf_count_t frames) ;
|
andrew@0
|
123 sf_count_t writef (const int *ptr, sf_count_t frames) ;
|
andrew@0
|
124 sf_count_t writef (const float *ptr, sf_count_t frames) ;
|
andrew@0
|
125 sf_count_t writef (const double *ptr, sf_count_t frames) ;
|
andrew@0
|
126
|
andrew@0
|
127 sf_count_t readRaw (void *ptr, sf_count_t bytes) ;
|
andrew@0
|
128 sf_count_t writeRaw (const void *ptr, sf_count_t bytes) ;
|
andrew@0
|
129
|
andrew@0
|
130 } ;
|
andrew@0
|
131
|
andrew@0
|
132 /*==============================================================================
|
andrew@0
|
133 ** Nothing but implementation below.
|
andrew@0
|
134 */
|
andrew@0
|
135
|
andrew@0
|
136 inline
|
andrew@0
|
137 SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
|
andrew@0
|
138 : ref (1)
|
andrew@0
|
139 {}
|
andrew@0
|
140
|
andrew@0
|
141 inline
|
andrew@0
|
142 SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
|
andrew@0
|
143 { if (sf != NULL) sf_close (sf) ; }
|
andrew@0
|
144
|
andrew@0
|
145 inline
|
andrew@0
|
146 SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
|
andrew@0
|
147 : p (NULL)
|
andrew@0
|
148 {
|
andrew@0
|
149 p = new (std::nothrow) SNDFILE_ref () ;
|
andrew@0
|
150
|
andrew@0
|
151 if (p != NULL)
|
andrew@0
|
152 { p->ref = 1 ;
|
andrew@0
|
153
|
andrew@0
|
154 p->sfinfo.frames = 0 ;
|
andrew@0
|
155 p->sfinfo.channels = chans ;
|
andrew@0
|
156 p->sfinfo.format = fmt ;
|
andrew@0
|
157 p->sfinfo.samplerate = srate ;
|
andrew@0
|
158 p->sfinfo.sections = 0 ;
|
andrew@0
|
159 p->sfinfo.seekable = 0 ;
|
andrew@0
|
160
|
andrew@0
|
161 if ((p->sf = sf_open (path, mode, &p->sfinfo)) == NULL)
|
andrew@0
|
162 { delete p ;
|
andrew@0
|
163 p = NULL ;
|
andrew@0
|
164 } ;
|
andrew@0
|
165 } ;
|
andrew@0
|
166 } /* SndfileHandle constructor */
|
andrew@0
|
167
|
andrew@0
|
168 inline
|
andrew@0
|
169 SndfileHandle::~SndfileHandle (void)
|
andrew@0
|
170 { if (p != NULL && --p->ref == 0)
|
andrew@0
|
171 delete p ;
|
andrew@0
|
172 } /* SndfileHandle destructor */
|
andrew@0
|
173
|
andrew@0
|
174
|
andrew@0
|
175 inline
|
andrew@0
|
176 SndfileHandle::SndfileHandle (const SndfileHandle &orig)
|
andrew@0
|
177 : p (orig.p)
|
andrew@0
|
178 { if (p != NULL)
|
andrew@0
|
179 ++p->ref ;
|
andrew@0
|
180 } /* SndfileHandle copy constructor */
|
andrew@0
|
181
|
andrew@0
|
182 inline SndfileHandle &
|
andrew@0
|
183 SndfileHandle::operator = (const SndfileHandle &rhs)
|
andrew@0
|
184 {
|
andrew@0
|
185 if (&rhs == this)
|
andrew@0
|
186 return *this ;
|
andrew@0
|
187 if (p != NULL && --p->ref == 0)
|
andrew@0
|
188 delete p ;
|
andrew@0
|
189
|
andrew@0
|
190 p = rhs.p ;
|
andrew@0
|
191 if (p != NULL)
|
andrew@0
|
192 ++p->ref ;
|
andrew@0
|
193
|
andrew@0
|
194 return *this ;
|
andrew@0
|
195 } /* SndfileHandle assignment operator */
|
andrew@0
|
196
|
andrew@0
|
197 inline int
|
andrew@0
|
198 SndfileHandle::error (void) const
|
andrew@0
|
199 { return sf_error (p->sf) ; }
|
andrew@0
|
200
|
andrew@0
|
201 inline const char *
|
andrew@0
|
202 SndfileHandle::strError (void) const
|
andrew@0
|
203 { return sf_strerror (p->sf) ; }
|
andrew@0
|
204
|
andrew@0
|
205 inline int
|
andrew@0
|
206 SndfileHandle::command (int cmd, void *data, int datasize)
|
andrew@0
|
207 { return sf_command (p->sf, cmd, data, datasize) ; }
|
andrew@0
|
208
|
andrew@0
|
209 inline sf_count_t
|
andrew@0
|
210 SndfileHandle::seek (sf_count_t frame_count, int whence)
|
andrew@0
|
211 { return sf_seek (p->sf, frame_count, whence) ; }
|
andrew@0
|
212
|
andrew@0
|
213 inline void
|
andrew@0
|
214 SndfileHandle::writeSync (void)
|
andrew@0
|
215 { sf_write_sync (p->sf) ; }
|
andrew@0
|
216
|
andrew@0
|
217 inline int
|
andrew@0
|
218 SndfileHandle::setString (int str_type, const char* str)
|
andrew@0
|
219 { return sf_set_string (p->sf, str_type, str) ; }
|
andrew@0
|
220
|
andrew@0
|
221 inline const char*
|
andrew@0
|
222 SndfileHandle::getString (int str_type) const
|
andrew@0
|
223 { return sf_get_string (p->sf, str_type) ; }
|
andrew@0
|
224
|
andrew@0
|
225
|
andrew@0
|
226 /*---------------------------------------------------------------------*/
|
andrew@0
|
227
|
andrew@0
|
228 inline sf_count_t
|
andrew@0
|
229 SndfileHandle::read (short *ptr, sf_count_t items)
|
andrew@0
|
230 { return sf_read_short (p->sf, ptr, items) ; }
|
andrew@0
|
231
|
andrew@0
|
232 inline sf_count_t
|
andrew@0
|
233 SndfileHandle::read (int *ptr, sf_count_t items)
|
andrew@0
|
234 { return sf_read_int (p->sf, ptr, items) ; }
|
andrew@0
|
235
|
andrew@0
|
236 inline sf_count_t
|
andrew@0
|
237 SndfileHandle::read (float *ptr, sf_count_t items)
|
andrew@0
|
238 { return sf_read_float (p->sf, ptr, items) ; }
|
andrew@0
|
239
|
andrew@0
|
240 inline sf_count_t
|
andrew@0
|
241 SndfileHandle::read (double *ptr, sf_count_t items)
|
andrew@0
|
242 { return sf_read_double (p->sf, ptr, items) ; }
|
andrew@0
|
243
|
andrew@0
|
244 inline sf_count_t
|
andrew@0
|
245 SndfileHandle::write (const short *ptr, sf_count_t items)
|
andrew@0
|
246 { return sf_write_short (p->sf, ptr, items) ; }
|
andrew@0
|
247
|
andrew@0
|
248 inline sf_count_t
|
andrew@0
|
249 SndfileHandle::write (const int *ptr, sf_count_t items)
|
andrew@0
|
250 { return sf_write_int (p->sf, ptr, items) ; }
|
andrew@0
|
251
|
andrew@0
|
252 inline sf_count_t
|
andrew@0
|
253 SndfileHandle::write (const float *ptr, sf_count_t items)
|
andrew@0
|
254 { return sf_write_float (p->sf, ptr, items) ; }
|
andrew@0
|
255
|
andrew@0
|
256 inline sf_count_t
|
andrew@0
|
257 SndfileHandle::write (const double *ptr, sf_count_t items)
|
andrew@0
|
258 { return sf_write_double (p->sf, ptr, items) ; }
|
andrew@0
|
259
|
andrew@0
|
260 inline sf_count_t
|
andrew@0
|
261 SndfileHandle::readf (short *ptr, sf_count_t frame_count)
|
andrew@0
|
262 { return sf_readf_short (p->sf, ptr, frame_count) ; }
|
andrew@0
|
263
|
andrew@0
|
264 inline sf_count_t
|
andrew@0
|
265 SndfileHandle::readf (int *ptr, sf_count_t frame_count)
|
andrew@0
|
266 { return sf_readf_int (p->sf, ptr, frame_count) ; }
|
andrew@0
|
267
|
andrew@0
|
268 inline sf_count_t
|
andrew@0
|
269 SndfileHandle::readf (float *ptr, sf_count_t frame_count)
|
andrew@0
|
270 { return sf_readf_float (p->sf, ptr, frame_count) ; }
|
andrew@0
|
271
|
andrew@0
|
272 inline sf_count_t
|
andrew@0
|
273 SndfileHandle::readf (double *ptr, sf_count_t frame_count)
|
andrew@0
|
274 { return sf_readf_double (p->sf, ptr, frame_count) ; }
|
andrew@0
|
275
|
andrew@0
|
276 inline sf_count_t
|
andrew@0
|
277 SndfileHandle::writef (const short *ptr, sf_count_t frame_count)
|
andrew@0
|
278 { return sf_writef_short (p->sf, ptr, frame_count) ; }
|
andrew@0
|
279
|
andrew@0
|
280 inline sf_count_t
|
andrew@0
|
281 SndfileHandle::writef (const int *ptr, sf_count_t frame_count)
|
andrew@0
|
282 { return sf_writef_int (p->sf, ptr, frame_count) ; }
|
andrew@0
|
283
|
andrew@0
|
284 inline sf_count_t
|
andrew@0
|
285 SndfileHandle::writef (const float *ptr, sf_count_t frame_count)
|
andrew@0
|
286 { return sf_writef_float (p->sf, ptr, frame_count) ; }
|
andrew@0
|
287
|
andrew@0
|
288 inline sf_count_t
|
andrew@0
|
289 SndfileHandle::writef (const double *ptr, sf_count_t frame_count)
|
andrew@0
|
290 { return sf_writef_double (p->sf, ptr, frame_count) ; }
|
andrew@0
|
291
|
andrew@0
|
292 inline sf_count_t
|
andrew@0
|
293 SndfileHandle::readRaw (void *ptr, sf_count_t bytes)
|
andrew@0
|
294 { return sf_read_raw (p->sf, ptr, bytes) ; }
|
andrew@0
|
295
|
andrew@0
|
296 inline sf_count_t
|
andrew@0
|
297 SndfileHandle::writeRaw (const void *ptr, sf_count_t bytes)
|
andrew@0
|
298 { return sf_write_raw (p->sf, ptr, bytes) ; }
|
andrew@0
|
299
|
andrew@0
|
300
|
andrew@0
|
301 #endif /* SNDFILE_HH */
|
andrew@0
|
302
|
andrew@0
|
303 /*
|
andrew@0
|
304 ** Do not edit or modify anything in this comment block.
|
andrew@0
|
305 ** The following line is a file identity tag for the GNU Arch
|
andrew@0
|
306 ** revision control system.
|
andrew@0
|
307 **
|
andrew@0
|
308 ** arch-tag: a0e9d996-73d7-47c4-a78d-79a3232a9eef
|
andrew@0
|
309 */
|