2 ** Copyright (C) 2007-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
19 #include <octave/oct.h>
23 #define FOUR_GIG (0x100000000LL)
24 #define BUFFER_FRAMES 8192
27 static int format_of_str (const std::string
& fmt
) ;
28 static void string_of_format (std::string
& fmt
, int format
) ;
31 DEFUN_DLD (sfversion
, args
, nargout
,
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\
39 octave_value_list retval
;
41 /* Bail out if the input parameters are bad. */
42 if (args
.length () != 0 || nargout
> 1)
47 sf_command (NULL
, SFC_GET_LIB_VERSION
, buffer
, sizeof (buffer
)) ;
49 std::string
version (buffer
) ;
51 retval
.append (version
) ;
56 DEFUN_DLD (sfread
, args
, nargout
,
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\
66 octave_value_list retval
;
68 int nargin
= args
.length () ;
70 /* Bail out if the input parameters are bad. */
71 if ((nargin
!= 1) || !args (0) .is_string () || nargout
< 1 || nargout
> 3)
76 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
78 std::string filename
= args (0).string_value () ;
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
)) ;
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
) ;
88 dim_vector dim
= dim_vector () ;
90 dim (0) = sfinfo
.frames
;
91 dim (1) = sfinfo
.channels
;
93 /* Should I be using Matrix instead? */
94 NDArray
out (dim
, 0.0) ;
96 float buffer
[BUFFER_FRAMES
* sfinfo
.channels
] ;
98 sf_count_t total
= 0 ;
101 { readcount
= sf_readf_float (file
, buffer
, BUFFER_FRAMES
) ;
103 /* Make sure we don't read more frames than we allocated. */
104 if (total
+ readcount
> sfinfo
.frames
)
105 readcount
= sfinfo
.frames
- total
;
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
] ;
113 } while (readcount
> 0 && total
< sfinfo
.frames
) ;
115 retval
.append (out
.squeeze ()) ;
118 retval
.append ((octave_uint32
) sfinfo
.samplerate
) ;
121 { std::string
fmt ("") ;
122 string_of_format (fmt
, sfinfo
.format
) ;
123 retval
.append (fmt
) ;
132 DEFUN_DLD (sfwrite
, args
, nargout
,
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\
142 octave_value_list retval
;
144 int nargin
= args
.length () ;
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 ()
154 std::string filename
= args (0).string_value () ;
155 std::string format
= args (3).string_value () ;
157 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
159 sfinfo
.format
= format_of_str (format
) ;
160 if (sfinfo
.format
== 0)
161 { error ("Bad format '%s'", format
.c_str ()) ;
165 sfinfo
.samplerate
= lrint (args (2).scalar_value ()) ;
166 if (sfinfo
.samplerate
< 1)
167 { error ("Bad sample rate : %d.\n", sfinfo
.samplerate
) ;
171 Matrix data
= args (1).matrix_value () ;
172 long rows
= args (1).rows () ;
173 long cols
= args (1).columns () ;
176 { error ("Audio data should have one column per channel, but supplied data "
177 "has %ld rows and %ld columns.\n", rows
, cols
) ;
181 sfinfo
.channels
= cols
;
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
)) ;
188 float buffer
[BUFFER_FRAMES
* sfinfo
.channels
] ;
194 writecount
= BUFFER_FRAMES
;
196 /* Make sure we don't read more frames than we allocated. */
197 if (total
+ writecount
> rows
)
198 writecount
= rows
- total
;
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
) ;
206 sf_writef_float (file
, buffer
, writecount
) ;
208 total
+= writecount
;
209 } while (writecount
> 0 && total
< rows
) ;
219 str_split (const std::string
& str
, const std::string
& delim
, std::vector
<std::string
> & output
)
221 unsigned int offset
= 0 ;
222 size_t delim_index
= 0 ;
224 delim_index
= str
.find (delim
, offset
) ;
226 while (delim_index
!= std::string::npos
)
228 output
.push_back (str
.substr(offset
, delim_index
- offset
)) ;
229 offset
+= delim_index
- offset
+ delim
.length () ;
230 delim_index
= str
.find (delim
, offset
) ;
233 output
.push_back (str
.substr (offset
)) ;
237 hash_of_str (const std::string
& str
)
241 for (unsigned k
= 0 ; k
< str
.length () ; k
++)
242 hash
= (hash
* 3) + tolower (str
[k
]) ;
248 major_format_of_hash (const std::string
& str
)
251 hash
= hash_of_str (str
) ;
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
;
279 printf ("%s : hash '%s' -> 0x%x\n", __func__
, str
.c_str (), hash
) ;
282 } /* major_format_of_hash */
285 minor_format_of_hash (const std::string
& str
)
288 hash
= hash_of_str (str
) ;
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
;
310 printf ("%s : hash '%s' -> 0x%x\n", __func__
, str
.c_str (), hash
) ;
313 } /* minor_format_of_hash */
317 string_of_major_format (int format
)
319 switch (format
& SF_FORMAT_TYPEMASK
)
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" ;
346 } /* string_of_major_format */
349 string_of_minor_format (int format
)
351 switch (format
& SF_FORMAT_SUBMASK
)
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" ;
372 } /* string_of_minor_format */
375 format_of_str (const std::string
& fmt
)
377 std::vector
<std::string
> split
;
379 str_split (fmt
, "-", split
) ;
381 if (split
.size () != 2)
384 int major_fmt
= major_format_of_hash (split
.at (0)) ;
388 int minor_fmt
= minor_format_of_hash (split
.at (1)) ;
392 return major_fmt
| minor_fmt
;
393 } /* format_of_str */
396 string_of_format (std::string
& fmt
, int format
)
400 snprintf (buffer
, sizeof (buffer
), "%s-%s", string_of_major_format (format
), string_of_minor_format (format
)) ;
405 } /* string_of_format */