Mercurial > octave-nkf
comparison libinterp/dldfcn/audioread.cc @ 19553:e616c2830413
various style fixes for audio code
* audiodevinfo.cc, audioread.cc: Style fixes.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 02 Jan 2015 13:09:27 -0500 |
parents | fc85d9026bb6 |
children | 7f271cef5850 |
comparison
equal
deleted
inserted
replaced
19552:572b9bd43e92 | 19553:e616c2830413 |
---|---|
61 Read a file and return a specified range of frames in an array of specified type.\n\ | 61 Read a file and return a specified range of frames in an array of specified type.\n\ |
62 \n\ | 62 \n\ |
63 @end deftypefn") | 63 @end deftypefn") |
64 { | 64 { |
65 octave_value_list retval; | 65 octave_value_list retval; |
66 #ifdef HAVE_SNDFILE | 66 |
67 Matrix audio; | 67 #ifdef HAVE_SNDFILE |
68 octave_value ret_audio; | 68 |
69 SNDFILE *file; | 69 int nargin = args.length (); |
70 | |
71 if (nargin < 1 || nargin > 3) | |
72 { | |
73 print_usage (); | |
74 return retval; | |
75 } | |
76 | |
77 std::string filename = args(0).string_value (); | |
78 | |
79 if (error_state) | |
80 return retval; | |
81 | |
70 SF_INFO info; | 82 SF_INFO info; |
71 info.format = 0; | 83 info.format = 0; |
72 int start, end; | 84 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); |
73 file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info); | 85 int start = 0; |
74 start = 0; | 86 int end = info.frames; |
75 end = info.frames; | |
76 OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); | 87 OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); |
77 sf_read_float (file, data, info.frames * info.channels); | 88 sf_read_float (file, data, info.frames * info.channels); |
78 if (args.length () == 2 && !args(1).is_string () || args.length () == 3) | 89 |
90 if ((nargin == 2 && ! args(1).is_string ()) || nargin == 3) | |
79 { | 91 { |
80 RowVector range = args(1).row_vector_value (); | 92 RowVector range = args(1).row_vector_value (); |
93 | |
94 if (error_state) | |
95 return retval; | |
96 | |
81 start = range(0); | 97 start = range(0); |
82 end = range(1); | 98 end = range(1); |
83 } | 99 } |
84 audio.resize (end - start, info.channels); | 100 |
101 Matrix audio (end - start, info.channels); | |
102 | |
85 for (int i = start; i < end; i++) | 103 for (int i = start; i < end; i++) |
86 { | 104 { |
87 for (int channel = 0; channel < info.channels; channel++) | 105 for (int channel = 0; channel < info.channels; channel++) |
88 { | 106 audio(i - start, channel) = data[i * info.channels + channel]; |
89 audio(i - start, channel) = data[i * info.channels + channel]; | 107 } |
90 } | 108 |
91 } | 109 octave_value ret_audio; |
92 | 110 |
93 if (args.length () == 2 && args(1).is_string () || args.length () == 3) | 111 if ((nargin == 2 && args(1).is_string ()) || nargin == 3) |
94 { | 112 { |
95 std::string type; | 113 std::string type; |
96 if (args.length () == 3) | 114 if (nargin == 3) |
97 type = args(2).string_value (); | 115 type = args(2).string_value (); |
98 else | 116 else |
99 type = args(1).string_value (); | 117 type = args(1).string_value (); |
100 | 118 |
119 if (error_state) | |
120 return retval; | |
121 | |
101 if (type == "native") | 122 if (type == "native") |
102 { | 123 { |
103 if (info.format & SF_FORMAT_PCM_S8) | 124 if (info.format & SF_FORMAT_PCM_S8) |
104 ret_audio = octave_value ((audio * 127)).int8_array_value (); | 125 ret_audio = int8NDArray (audio * 127); |
105 else if (info.format & SF_FORMAT_PCM_U8) | 126 else if (info.format & SF_FORMAT_PCM_U8) |
106 ret_audio = octave_value ((audio * 127 + 127)).uint8_array_value (); | 127 ret_audio = uint8NDArray (audio * 127 + 127); |
107 else if (info.format & SF_FORMAT_PCM_16) | 128 else if (info.format & SF_FORMAT_PCM_16) |
108 ret_audio = octave_value ((audio * 32767)).int16_array_value (); | 129 ret_audio = int16NDArray (audio * 32767); |
109 else if (info.format & SF_FORMAT_PCM_24) | 130 else if (info.format & SF_FORMAT_PCM_24) |
110 ret_audio = octave_value ((audio * 8388608)).int32_array_value (); | 131 ret_audio = int32NDArray (audio * 8388608); |
111 else if (info.format & SF_FORMAT_PCM_32) | 132 else if (info.format & SF_FORMAT_PCM_32) |
112 ret_audio = octave_value ((audio * 2147483648)).int32_array_value (); | 133 ret_audio = int32NDArray (audio * 2147483648); |
113 else | 134 else |
114 ret_audio = octave_value (audio); | 135 ret_audio = audio; |
115 } | 136 } |
116 else | 137 else |
117 ret_audio = octave_value (audio); | 138 ret_audio = audio; |
118 } | 139 } |
119 else | 140 else |
120 ret_audio = octave_value (audio); | 141 ret_audio = audio; |
121 | 142 |
143 retval(1) = info.samplerate; | |
122 retval(0) = ret_audio; | 144 retval(0) = ret_audio; |
123 retval(1) = info.samplerate; | 145 |
124 #else | 146 #else |
147 | |
125 error ("sndfile not found on your system and thus audioread is not functional"); | 148 error ("sndfile not found on your system and thus audioread is not functional"); |
126 #endif | 149 |
127 return octave_value (retval); | 150 #endif |
151 | |
152 return retval; | |
128 } | 153 } |
129 | 154 |
130 #ifdef HAVE_SNDFILE | 155 #ifdef HAVE_SNDFILE |
131 static void | 156 static void |
132 fill_extension_table (std::map<std::string, int> &table) | 157 fill_extension_table (std::map<std::string, int> &table) |
184 @item Comment\n\ | 209 @item Comment\n\ |
185 Comment.\n\ | 210 Comment.\n\ |
186 @end table\n\ | 211 @end table\n\ |
187 @end deftypefn") | 212 @end deftypefn") |
188 { | 213 { |
189 octave_scalar_map retval; | 214 // FIXME: shouldn't we return something to indicate whether the file |
190 #ifdef HAVE_SNDFILE | 215 // was written successfully? |
216 | |
217 octave_value retval; | |
218 | |
219 #ifdef HAVE_SNDFILE | |
220 | |
221 int nargin = args.length (); | |
222 | |
223 if (nargin < 3) | |
224 { | |
225 print_usage (); | |
226 return retval; | |
227 } | |
228 | |
229 std::string filename = args(0).string_value (); | |
230 | |
231 if (error_state) | |
232 return retval; | |
233 | |
191 std::map<std::string, int> extension_to_format; | 234 std::map<std::string, int> extension_to_format; |
192 fill_extension_table (extension_to_format); | 235 fill_extension_table (extension_to_format); |
193 std::string filename = args(0).string_value (); | 236 |
194 std::string extension = filename.substr (filename.find_last_of (".") + 1); | 237 std::string extension = filename.substr (filename.find_last_of (".") + 1); |
195 std::transform (extension.begin (), extension.end (), extension.begin (), ::tolower); | 238 std::transform (extension.begin (), extension.end (), extension.begin (), ::tolower); |
239 | |
196 Matrix audio = args(1).matrix_value (); | 240 Matrix audio = args(1).matrix_value (); |
241 | |
242 if (error_state) | |
243 return retval; | |
244 | |
197 SNDFILE *file; | 245 SNDFILE *file; |
198 SF_INFO info; | 246 SF_INFO info; |
247 | |
199 OCTAVE_LOCAL_BUFFER (float, data, audio.rows () * audio.cols ()); | 248 OCTAVE_LOCAL_BUFFER (float, data, audio.rows () * audio.cols ()); |
200 | 249 |
201 for (int i = 0; i < audio.cols (); i++) | 250 for (int i = 0; i < audio.cols (); i++) |
202 { | 251 { |
203 for (int j = 0; j < audio.rows (); j++) | 252 for (int j = 0; j < audio.rows (); j++) |
204 { | 253 data[j * audio.cols () + i] = audio(j, i); |
205 data[j * audio.cols () + i] = audio(j, i); | |
206 } | |
207 } | 254 } |
208 | 255 |
209 if (extension == "ogg") | 256 if (extension == "ogg") |
210 info.format = SF_FORMAT_VORBIS; | 257 info.format = SF_FORMAT_VORBIS; |
211 else | 258 else |
215 std::string artist = ""; | 262 std::string artist = ""; |
216 std::string comment = ""; | 263 std::string comment = ""; |
217 // Quality is currently unused? | 264 // Quality is currently unused? |
218 // | 265 // |
219 // float quality = 0.75; | 266 // float quality = 0.75; |
220 for (int i = 3; i < args.length (); i += 2) | 267 for (int i = 3; i < nargin; i += 2) |
221 { | 268 { |
222 if (args(i).string_value () == "BitsPerSample") | 269 if (args(i).string_value () == "BitsPerSample") |
223 { | 270 { |
224 int bits = args(i + 1).int_value (); | 271 int bits = args(i + 1).int_value (); |
225 if (bits == 8) | 272 if (bits == 8) |
229 else if (bits == 24) | 276 else if (bits == 24) |
230 info.format |= SF_FORMAT_PCM_24; | 277 info.format |= SF_FORMAT_PCM_24; |
231 else if (bits == 32) | 278 else if (bits == 32) |
232 info.format |= SF_FORMAT_PCM_32; | 279 info.format |= SF_FORMAT_PCM_32; |
233 else | 280 else |
234 error ("audiowrite: wrong number of bits specified"); | 281 { |
282 error ("audiowrite: wrong number of bits specified"); | |
283 return retval; | |
284 } | |
235 } | 285 } |
236 else if (args(i).string_value () == "BitRate") | 286 else if (args(i).string_value () == "BitRate") |
237 ; | 287 ; |
238 // Quality is currently unused? | 288 // Quality is currently unused? |
239 // | 289 // |
244 else if (args(i).string_value () == "Artist") | 294 else if (args(i).string_value () == "Artist") |
245 artist = args(i + 1).string_value (); | 295 artist = args(i + 1).string_value (); |
246 else if (args(i).string_value () == "Comment") | 296 else if (args(i).string_value () == "Comment") |
247 comment = args(i + 1).string_value (); | 297 comment = args(i + 1).string_value (); |
248 else | 298 else |
249 error ("audiowrite: wrong argument name"); | 299 { |
250 } | 300 error ("audiowrite: wrong argument name"); |
301 return retval; | |
302 } | |
303 } | |
304 | |
251 info.samplerate = args(2).int_value (); | 305 info.samplerate = args(2).int_value (); |
252 info.channels = audio.cols (); | 306 info.channels = audio.cols (); |
253 info.format |= extension_to_format[extension]; | 307 info.format |= extension_to_format[extension]; |
308 | |
254 file = sf_open (filename.c_str (), SFM_WRITE, &info); | 309 file = sf_open (filename.c_str (), SFM_WRITE, &info); |
310 | |
255 if (title != "") | 311 if (title != "") |
256 sf_set_string (file, SF_STR_TITLE, title.c_str ()); | 312 sf_set_string (file, SF_STR_TITLE, title.c_str ()); |
313 | |
257 if (artist != "") | 314 if (artist != "") |
258 sf_set_string (file, SF_STR_ARTIST, artist.c_str ()); | 315 sf_set_string (file, SF_STR_ARTIST, artist.c_str ()); |
316 | |
259 if (comment != "") | 317 if (comment != "") |
260 sf_set_string (file, SF_STR_COMMENT, comment.c_str ()); | 318 sf_set_string (file, SF_STR_COMMENT, comment.c_str ()); |
319 | |
261 sf_write_float (file, data, audio.rows () * audio.cols ()); | 320 sf_write_float (file, data, audio.rows () * audio.cols ()); |
262 sf_close (file); | 321 sf_close (file); |
263 free (data); | 322 |
264 #else | 323 #else |
324 | |
265 error ("sndfile not found on your system and thus audiowrite is not functional"); | 325 error ("sndfile not found on your system and thus audiowrite is not functional"); |
266 #endif | 326 |
267 return octave_value (retval); | 327 #endif |
328 | |
329 return retval; | |
268 } | 330 } |
269 | 331 |
270 DEFUN_DLD (audioinfo, args, , | 332 DEFUN_DLD (audioinfo, args, , |
271 "-*- texinfo -*-\n\ | 333 "-*- texinfo -*-\n\ |
272 @deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\ | 334 @deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\ |
273 Return information about an audio file specified by @var{filename}.\n\ | 335 Return information about an audio file specified by @var{filename}.\n\ |
274 @end deftypefn") | 336 @end deftypefn") |
275 { | 337 { |
276 octave_scalar_map retval; | 338 octave_value retval; |
277 if (args.length () != 1 || not args(0).is_string ()) | 339 |
340 if (args.length () != 1) | |
278 { | 341 { |
279 print_usage (); | 342 print_usage (); |
280 return octave_value (retval); | 343 return retval; |
281 } | 344 } |
282 #ifdef HAVE_SNDFILE | 345 |
283 Matrix audio; | 346 #ifdef HAVE_SNDFILE |
284 SNDFILE *file; | 347 |
285 SF_INFO info; | 348 SF_INFO info; |
286 info.format = 0; | 349 info.format = 0; |
287 file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info); | 350 SNDFILE *file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info); |
288 retval.assign ("Filename", args(0).string_value ()); | 351 |
289 retval.assign ("CompressionMethod", ""); | 352 octave_scalar_map result; |
290 retval.assign ("NumChannels", info.channels); | 353 |
291 retval.assign ("SampleRate", info.samplerate); | 354 result.assign ("Filename", args(0).string_value ()); |
292 retval.assign ("TotalSamples", info.frames); | 355 result.assign ("CompressionMethod", ""); |
356 result.assign ("NumChannels", info.channels); | |
357 result.assign ("SampleRate", info.samplerate); | |
358 result.assign ("TotalSamples", info.frames); | |
359 | |
293 double dframes = info.frames; | 360 double dframes = info.frames; |
294 double drate = info.samplerate; | 361 double drate = info.samplerate; |
295 retval.assign ("Duration", dframes / drate); | 362 result.assign ("Duration", dframes / drate); |
296 | 363 |
297 int bits; | 364 int bits; |
298 if (info.format & SF_FORMAT_PCM_S8) | 365 if (info.format & SF_FORMAT_PCM_S8) |
299 bits = 8; | 366 bits = 8; |
300 else if (info.format & SF_FORMAT_PCM_U8) | 367 else if (info.format & SF_FORMAT_PCM_U8) |
306 else if (info.format & SF_FORMAT_PCM_32) | 373 else if (info.format & SF_FORMAT_PCM_32) |
307 bits = 32; | 374 bits = 32; |
308 else | 375 else |
309 bits = -1; | 376 bits = -1; |
310 | 377 |
311 retval.assign ("BitsPerSample", bits); | 378 result.assign ("BitsPerSample", bits); |
312 retval.assign ("BitRate", -1); | 379 result.assign ("BitRate", -1); |
313 retval.assign ("Title", sf_get_string (file, SF_STR_TITLE)); | 380 result.assign ("Title", sf_get_string (file, SF_STR_TITLE)); |
314 retval.assign ("Artist", sf_get_string (file, SF_STR_ARTIST)); | 381 result.assign ("Artist", sf_get_string (file, SF_STR_ARTIST)); |
315 retval.assign ("Comment", sf_get_string (file, SF_STR_COMMENT)); | 382 result.assign ("Comment", sf_get_string (file, SF_STR_COMMENT)); |
383 | |
384 retval = result; | |
385 | |
316 #else | 386 #else |
387 | |
317 error ("sndfile not found on your system and thus audioinfo is not functional"); | 388 error ("sndfile not found on your system and thus audioinfo is not functional"); |
318 #endif | 389 |
319 return octave_value (retval); | 390 #endif |
391 | |
392 return retval; | |
320 } | 393 } |