comparison libinterp/dldfcn/audioread.cc @ 19593:701b43ce4467

properly close audio files * audioread.cc (Faudioread, Faudiowrite, Faudioinfo): Check that file was opened. Use unwind_protect to close file.
author John W. Eaton <jwe@octave.org>
date Fri, 09 Jan 2015 14:11:19 -0500
parents 398e42431dcc
children 72304a4e010a
comparison
equal deleted inserted replaced
19592:0b068273340f 19593:701b43ce4467
26 26
27 #include <string> 27 #include <string>
28 #include <map> 28 #include <map>
29 29
30 #include "oct-locbuf.h" 30 #include "oct-locbuf.h"
31 #include "unwind-prot.h"
31 32
32 #include "defun-dld.h" 33 #include "defun-dld.h"
33 #include "error.h" 34 #include "error.h"
34 #include "gripes.h" 35 #include "gripes.h"
35 #include "oct-obj.h" 36 #include "oct-obj.h"
38 39
39 #ifdef HAVE_SNDFILE 40 #ifdef HAVE_SNDFILE
40 #include <sndfile.h> 41 #include <sndfile.h>
41 #endif 42 #endif
42 43
44 #ifdef HAVE_SNDFILE
45 static void
46 safe_close (SNDFILE *file)
47 {
48 sf_close (file);
49 }
50 #endif
51
43 DEFUN_DLD (audioread, args, , 52 DEFUN_DLD (audioread, args, ,
44 "-*- texinfo -*-\n\ 53 "-*- texinfo -*-\n\
45 @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename})\n\ 54 @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename})\n\
46 @deftypefnx {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples})\n\ 55 @deftypefnx {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples})\n\
47 \n\ 56 \n\
80 return retval; 89 return retval;
81 90
82 SF_INFO info; 91 SF_INFO info;
83 info.format = 0; 92 info.format = 0;
84 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); 93 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info);
94
95 if (! file)
96 {
97 error ("audioread: failed to open input file %s", filename.c_str ());
98 return retval;
99 }
100
101 unwind_protect frame;
102
103 frame.add_fcn (safe_close, file);
85 104
86 OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); 105 OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels);
87 106
88 sf_read_float (file, data, info.frames * info.channels); 107 sf_read_float (file, data, info.frames * info.channels);
89 108
361 error ("audiowrite: wrong argument name"); 380 error ("audiowrite: wrong argument name");
362 return retval; 381 return retval;
363 } 382 }
364 } 383 }
365 384
366 const char *out = filename.c_str (); 385 SNDFILE *file = sf_open (filename.c_str (), SFM_WRITE, &info);
367
368 SNDFILE *file = sf_open (out, SFM_WRITE, &info);
369 386
370 if (! file) 387 if (! file)
371 { 388 {
372 error ("audiowrite: failed to open output file %s", out); 389 error ("audiowrite: failed to open output file %s", filename.c_str ());
373 return retval; 390 return retval;
374 } 391 }
392
393 unwind_protect frame;
394
395 frame.add_fcn (safe_close, file);
375 396
376 if (title != "") 397 if (title != "")
377 sf_set_string (file, SF_STR_TITLE, title.c_str ()); 398 sf_set_string (file, SF_STR_TITLE, title.c_str ());
378 399
379 if (artist != "") 400 if (artist != "")
437 return retval; 458 return retval;
438 459
439 SF_INFO info; 460 SF_INFO info;
440 info.format = 0; 461 info.format = 0;
441 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); 462 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info);
463
464 if (! file)
465 {
466 error ("audioinfo: failed to open file %s", filename.c_str ());
467 return retval;
468 }
469
470 unwind_protect frame;
471
472 frame.add_fcn (safe_close, file);
442 473
443 octave_scalar_map result; 474 octave_scalar_map result;
444 475
445 result.assign ("Filename", filename); 476 result.assign ("Filename", filename);
446 result.assign ("CompressionMethod", ""); 477 result.assign ("CompressionMethod", "");