This repository has been archived by the owner on Oct 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
sndfile.cc
405 lines (320 loc) · 11.2 KB
/
sndfile.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
** Copyright (C) 2007-2011 Erik de Castro Lopo <[email protected]>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <octave/oct.h>
#include "sndfile.h"
#define FOUR_GIG (0x100000000LL)
#define BUFFER_FRAMES 8192
static int format_of_str (const std::string & fmt) ;
static void string_of_format (std::string & fmt, int format) ;
DEFUN_DLD (sfversion, args, nargout ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{version} =} sfversion ()\n\
@cindex Reading sound files\n\
Return a string containing the libsndfile version.\n\
@seealso{sfread, sfwrite}\n\
@end deftypefn")
{ char buffer [256] ;
octave_value_list retval ;
/* Bail out if the input parameters are bad. */
if (args.length () != 0 || nargout > 1)
{ print_usage () ;
return retval ;
} ;
sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
std::string version (buffer) ;
retval.append (version) ;
return retval ;
} /* sfversion */
DEFUN_DLD (sfread, args, nargout ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{data},@var{srate},@var{format} =} sfread (@var{filename})\n\
@cindex Reading sound files\n\
Read a sound file from disk using libsndfile.\n\
@seealso{sfversion, sfwrite}\n\
@end deftypefn")
{ SNDFILE * file ;
SF_INFO sfinfo ;
octave_value_list retval ;
int nargin = args.length () ;
/* Bail out if the input parameters are bad. */
if ((nargin != 1) || !args (0) .is_string () || nargout < 1 || nargout > 3)
{ print_usage () ;
return retval ;
} ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
std::string filename = args (0).string_value () ;
if ((file = sf_open (filename.c_str (), SFM_READ, &sfinfo)) == NULL)
{ error ("sfread: couldn't open file %s : %s", filename.c_str (), sf_strerror (NULL)) ;
return retval ;
} ;
if (sfinfo.frames > FOUR_GIG)
printf ("This is a really huge file (%lld frames).\nYou may run out of memory trying to load it.\n", (long long) sfinfo.frames) ;
dim_vector dim = dim_vector () ;
dim.resize (2) ;
dim (0) = sfinfo.frames ;
dim (1) = sfinfo.channels ;
/* Should I be using Matrix instead? */
NDArray out (dim, 0.0) ;
float buffer [BUFFER_FRAMES * sfinfo.channels] ;
int readcount ;
sf_count_t total = 0 ;
do
{ readcount = sf_readf_float (file, buffer, BUFFER_FRAMES) ;
/* Make sure we don't read more frames than we allocated. */
if (total readcount > sfinfo.frames)
readcount = sfinfo.frames - total ;
for (int ch = 0 ; ch < sfinfo.channels ; ch )
{ for (int k = 0 ; k < readcount ; k )
out (total k, ch) = buffer [k * sfinfo.channels ch] ;
} ;
total = readcount ;
} while (readcount > 0 && total < sfinfo.frames) ;
retval.append (out.squeeze ()) ;
if (nargout >= 2)
retval.append ((octave_uint32) sfinfo.samplerate) ;
if (nargout >= 3)
{ std::string fmt ("") ;
string_of_format (fmt, sfinfo.format) ;
retval.append (fmt) ;
} ;
/* Clean up. */
sf_close (file) ;
return retval ;
} /* sfread */
DEFUN_DLD (sfwrite, args, nargout ,
"-*- texinfo -*-\n\
@deftypefn {Function File} sfwrite (@var{filename},@var{data},@var{srate},@var{format})\n\
Write a sound file to disk using libsndfile.\n\
@seealso{sfread, sfversion}\n\
@end deftypefn\n\
")
{ SNDFILE * file ;
SF_INFO sfinfo ;
octave_value_list retval ;
int nargin = args.length () ;
/* Bail out if the input parameters are bad. */
if (nargin != 4 || !args (0).is_string () || !args (1).is_real_matrix ()
|| !args (2).is_real_scalar () || !args (3).is_string ()
|| nargout != 0)
{ print_usage () ;
return retval ;
} ;
std::string filename = args (0).string_value () ;
std::string format = args (3).string_value () ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
sfinfo.format = format_of_str (format) ;
if (sfinfo.format == 0)
{ error ("Bad format '%s'", format.c_str ()) ;
return retval ;
} ;
sfinfo.samplerate = lrint (args (2).scalar_value ()) ;
if (sfinfo.samplerate < 1)
{ error ("Bad sample rate : %d.\n", sfinfo.samplerate) ;
return retval ;
} ;
Matrix data = args (1).matrix_value () ;
long rows = args (1).rows () ;
long cols = args (1).columns () ;
if (cols > rows)
{ error ("Audio data should have one column per channel, but supplied data "
"has %ld rows and %ld columns.\n", rows, cols) ;
return retval ;
} ;
sfinfo.channels = cols ;
if ((file = sf_open (filename.c_str (), SFM_WRITE, &sfinfo)) == NULL)
{ error ("Couldn't open file %s : %s", filename.c_str (), sf_strerror (NULL)) ;
return retval ;
} ;
float buffer [BUFFER_FRAMES * sfinfo.channels] ;
int writecount ;
long total = 0 ;
do
{
writecount = BUFFER_FRAMES ;
/* Make sure we don't read more frames than we allocated. */
if (total writecount > rows)
writecount = rows - total ;
for (int ch = 0 ; ch < sfinfo.channels ; ch )
{ for (int k = 0 ; k < writecount ; k )
buffer [k * sfinfo.channels ch] = data (total k, ch) ;
} ;
if (writecount > 0)
sf_writef_float (file, buffer, writecount) ;
total = writecount ;
} while (writecount > 0 && total < rows) ;
/* Clean up. */
sf_close (file) ;
return retval ;
} /* sfwrite */
static void
str_split (const std::string & str, const std::string & delim, std::vector <std::string> & output)
{
unsigned int offset = 0 ;
size_t delim_index = 0 ;
delim_index = str.find (delim, offset) ;
while (delim_index != std::string::npos)
{
output.push_back (str.substr(offset, delim_index - offset)) ;
offset = delim_index - offset delim.length () ;
delim_index = str.find (delim, offset) ;
}
output.push_back (str.substr (offset)) ;
} /* str_split */
static int
hash_of_str (const std::string & str)
{
int hash = 0 ;
for (unsigned k = 0 ; k < str.length () ; k )
hash = (hash * 3) tolower (str [k]) ;
return hash ;
} /* hash_of_str */
static int
major_format_of_hash (const std::string & str)
{ int hash ;
hash = hash_of_str (str) ;
switch (hash)
{
case 0x5c8 : /* 'wav' */ return SF_FORMAT_WAV ;
case 0xf84 : /* 'aiff' */ return SF_FORMAT_AIFF ;
case 0x198 : /* 'au' */ return SF_FORMAT_AU ;
case 0x579 : /* 'paf' */ return SF_FORMAT_PAF ;
case 0x5e5 : /* 'svx' */ return SF_FORMAT_SVX ;
case 0x1118 : /* 'nist' */ return SF_FORMAT_NIST ;
case 0x5d6 : /* 'voc' */ return SF_FORMAT_VOC ;
case 0x324a : /* 'ircam' */ return SF_FORMAT_IRCAM ;
case 0x505 : /* 'w64' */ return SF_FORMAT_W64 ;
case 0x1078 : /* 'mat4' */ return SF_FORMAT_MAT4 ;
case 0x1079 : /* 'mat5' */ return SF_FORMAT_MAT5 ;
case 0x5b8 : /* 'pvf' */ return SF_FORMAT_PVF ;
case 0x1d1 : /* 'xi' */ return SF_FORMAT_XI ;
case 0x56f : /* 'htk' */ return SF_FORMAT_HTK ;
case 0x5aa : /* 'sds' */ return SF_FORMAT_SDS ;
case 0x53d : /* 'avr' */ return SF_FORMAT_AVR ;
case 0x11d0 : /* 'wavx' */ return SF_FORMAT_WAVEX ;
case 0x569 : /* 'sd2' */ return SF_FORMAT_SD2 ;
case 0x1014 : /* 'flac' */ return SF_FORMAT_FLAC ;
case 0x504 : /* 'caf' */ return SF_FORMAT_CAF ;
case 0x5f6 : /* 'wve' */ return SF_FORMAT_WVE ;
default : break ;
} ;
printf ("%s : hash '%s' -> 0x%x\n", __func__, str.c_str (), hash) ;
return 0 ;
} /* major_format_of_hash */
static int
minor_format_of_hash (const std::string & str)
{ int hash ;
hash = hash_of_str (str) ;
switch (hash)
{
case 0x1085 : /* 'int8' */ return SF_FORMAT_PCM_S8 ;
case 0x358a : /* 'uint8' */ return SF_FORMAT_PCM_U8 ;
case 0x31b0 : /* 'int16' */ return SF_FORMAT_PCM_16 ;
case 0x31b1 : /* 'int24' */ return SF_FORMAT_PCM_24 ;
case 0x31b2 : /* 'int32' */ return SF_FORMAT_PCM_32 ;
case 0x3128 : /* 'float' */ return SF_FORMAT_FLOAT ;
case 0x937d : /* 'double' */ return SF_FORMAT_DOUBLE ;
case 0x11bd : /* 'ulaw' */ return SF_FORMAT_ULAW ;
case 0xfa1 : /* 'alaw' */ return SF_FORMAT_ALAW ;
case 0xfc361 : /* 'ima_adpcm' */ return SF_FORMAT_IMA_ADPCM ;
case 0x5739a : /* 'ms_adpcm' */ return SF_FORMAT_MS_ADPCM ;
case 0x9450 : /* 'gsm610' */ return SF_FORMAT_GSM610 ;
case 0x172a3 : /* 'g721_32' */ return SF_FORMAT_G721_32 ;
case 0x172d8 : /* 'g723_24' */ return SF_FORMAT_G723_24 ;
case 0x172da : /* 'g723_40' */ return SF_FORMAT_G723_40 ;
default : break ;
} ;
printf ("%s : hash '%s' -> 0x%x\n", __func__, str.c_str (), hash) ;
return 0 ;
} /* minor_format_of_hash */
static const char *
string_of_major_format (int format)
{
switch (format & SF_FORMAT_TYPEMASK)
{
case SF_FORMAT_WAV : return "wav" ;
case SF_FORMAT_AIFF : return "aiff" ;
case SF_FORMAT_AU : return "au" ;
case SF_FORMAT_PAF : return "paf" ;
case SF_FORMAT_SVX : return "svx" ;
case SF_FORMAT_NIST : return "nist" ;
case SF_FORMAT_VOC : return "voc" ;
case SF_FORMAT_IRCAM : return "ircam" ;
case SF_FORMAT_W64 : return "w64" ;
case SF_FORMAT_MAT4 : return "mat4" ;
case SF_FORMAT_MAT5 : return "mat5" ;
case SF_FORMAT_PVF : return "pvf" ;
case SF_FORMAT_XI : return "xi" ;
case SF_FORMAT_HTK : return "htk" ;
case SF_FORMAT_SDS : return "sds" ;
case SF_FORMAT_AVR : return "avr" ;
case SF_FORMAT_WAVEX : return "wavx" ;
case SF_FORMAT_SD2 : return "sd2" ;
case SF_FORMAT_FLAC : return "flac" ;
case SF_FORMAT_CAF : return "caf" ;
case SF_FORMAT_WVE : return "wfe" ;
default : break ;
} ;
return "unknown" ;
} /* string_of_major_format */
static const char *
string_of_minor_format (int format)
{
switch (format & SF_FORMAT_SUBMASK)
{
case SF_FORMAT_PCM_S8 : return "int8" ;
case SF_FORMAT_PCM_U8 : return "uint8" ;
case SF_FORMAT_PCM_16 : return "int16" ;
case SF_FORMAT_PCM_24 : return "int24" ;
case SF_FORMAT_PCM_32 : return "int32" ;
case SF_FORMAT_FLOAT : return "float" ;
case SF_FORMAT_DOUBLE : return "double" ;
case SF_FORMAT_ULAW : return "ulaw" ;
case SF_FORMAT_ALAW : return "alaw" ;
case SF_FORMAT_IMA_ADPCM : return "ima_adpcm" ;
case SF_FORMAT_MS_ADPCM : return "ms_adpcm" ;
case SF_FORMAT_GSM610 : return "gsm610" ;
case SF_FORMAT_G721_32 : return "g721_32" ;
case SF_FORMAT_G723_24 : return "g723_24" ;
case SF_FORMAT_G723_40 : return "g723_40" ;
default : break ;
} ;
return "unknown" ;
} /* string_of_minor_format */
static int
format_of_str (const std::string & fmt)
{
std::vector <std::string> split ;
str_split (fmt, "-", split) ;
if (split.size () != 2)
return 0 ;
int major_fmt = major_format_of_hash (split.at (0)) ;
if (major_fmt == 0)
return 0 ;
int minor_fmt = minor_format_of_hash (split.at (1)) ;
if (minor_fmt == 0)
return 0 ;
return major_fmt | minor_fmt ;
} /* format_of_str */
static void
string_of_format (std::string & fmt, int format)
{
char buffer [64] ;
snprintf (buffer, sizeof (buffer), "%s-%s", string_of_major_format (format), string_of_minor_format (format)) ;
fmt = buffer ;
return ;
} /* string_of_format */