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