0

I am trying to record voice and then play it from the time index provided by the user. The problem is this code is not playing the recorded voice and gives "Invalid Playback Selection" error. What am I doing wrong? Here is the code:

function musiceditor(UserPassedTimeIndex)

Fs=44110;

y = wavrecord(5*Fs,Fs,'int16');

wavwrite(y,'Alfred.wav');

[Magnitude,SampleRate,x]=wavread('Alfred.wav');

AudioPlayer=audioplayer(Magnitude,SampleRate,x);

TotalPlayTime= length(Magnitude)/SampleRate;

Index= round((UserPassedTimeIndex/TotalPlayTime)*length(Magnitude));

play(AudioPlayer,Index);

Regards

Alfred
  • 1,543
  • 7
  • 33
  • 45

1 Answers1

0

From the equation

(UserPassedTimeIndex/TotalPlayTime)*length(Magnitude), 

UserPassedTimeIndex must be in seconds, and less than the total recording time of your audio (otherwise it will error).

By the definition of TotalPlayTime this equation equivalent to simply

UserPassedTimeIndex * SampleRate

Note that this is converting time in seconds into time in samples. Because MATLAB's indexing is 1-based, you also need to add a 1 to get the index of the sample to start at for the given start time.

wakjah
  • 4,541
  • 1
  • 18
  • 23