MATLAB: Error Using audioread : Expected input number 2, range, to be positive.

audioreadMATLAB and Simulink Student Suite

I'm trying to read a portion of an audio file, and matlab's returning the error consistently on the same line.
It used to work, but I'm not entirely sure what I changed to make this occur.
The code for reproduction is below. I'm doing this in a app where the user inputs a start and end time (i.e. 0:00 and 1:00) the program then converts that into seconds for the samples and calls audioread. app.path1 is the path to the audio file.
sng1 = app.StartofComparisonEditField.Value;
startSongOne = times(str2num(sng1(1)), 60);
startSongOne = startSongOne + times(str2num(sng1(3)), 10);
startSongOne = startSongOne + str2num(sng1(4));
sng1 = app.EndofComparisonEditField.Value;
endSongOne = times(str2num(sng1(1)), 60);
endSongOne = endSongOne + times(str2num(sng1(3)), 10);
endSongOne = endSongOne + str2num(sng1(4));
Fs = 44100;
samplesSongOne = [startSongOne,endSongOne * Fs];
clear Fs;
[y, Fs] = audioread(app.path1, samplesSongOne);
Many Thanks.

Best Answer

  • audioRead() requires that the second input contains two positive integers. I would guess, based on the code and the error, that you are passing 0 in as startSongOne.
    If you've got a song with 10 samples, recorded at 10 samples per second, and you want to play the song from time 0 to time 1 (in units of seconds), that means you want to play samples 1 to 10, not samples 0 to 10. If the user enters a start time of 0:00, the value of startSongOne should be 1, not 0. Also, it seems to me that the startSongOne value should be multiplied by Fs as well.
    Try this:
    samplesSongOne = [startSongOne * Fs + 1, endSongOne * Fs];