Mercurial > hg > octave-nkf
changeset 19751:398e42431dcc
improve argument decoding in audioread.cc functions
* audioread.cc (Faudioread, Faudioinfo): Improve argument decoding and
error checking.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 09 Jan 2015 13:17:51 -0500 |
parents | 57efd0a020cf |
children | 9bd2f0a07c95 |
files | libinterp/dldfcn/audioread.cc |
diffstat | 1 files changed, 39 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/dldfcn/audioread.cc +++ b/libinterp/dldfcn/audioread.cc @@ -82,11 +82,14 @@ SF_INFO info; info.format = 0; SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); - int start = 0; - int end = info.frames; + OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); + sf_read_float (file, data, info.frames * info.channels); + sf_count_t start = 0; + sf_count_t end = info.frames; + if ((nargin == 2 && ! args(1).is_string ()) || nargin == 3) { RowVector range = args(1).row_vector_value (); @@ -94,16 +97,38 @@ if (error_state) return retval; - start = range(0); - end = range(1); + if (range.nelem () != 2) + { + error ("audioread: invalid specification for range of frames"); + return retval; + } + + double dstart = range(0); + double dend = range(1); + + if (dstart < 0 || dstart > dend || D_NINT (dstart) != dstart + || D_NINT (dend) != dend) + { + error ("audioread: invalid specification for range of frames"); + return retval; + } + + start = dstart; + end = dend; } - Matrix audio (end - start, info.channels); + sf_count_t items = end - start; + + Matrix audio (items, info.channels); - for (int i = start; i < end; i++) + double *paudio = audio.fortran_vec (); + + data += start * info.channels; + + for (int i = 0; i < items; i++) { for (int channel = 0; channel < info.channels; channel++) - audio(i - start, channel) = data[i * info.channels + channel]; + paudio[items*channel+i] = *data++; } octave_value ret_audio; @@ -406,13 +431,18 @@ return retval; } + std::string filename = args(0).string_value (); + + if (error_state) + return retval; + SF_INFO info; info.format = 0; - SNDFILE *file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info); + SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); octave_scalar_map result; - result.assign ("Filename", args(0).string_value ()); + result.assign ("Filename", filename); result.assign ("CompressionMethod", ""); result.assign ("NumChannels", info.channels); result.assign ("SampleRate", info.samplerate);