How to display audio file length in Matlab GUI

Hello everyone,
I have created an audio file player on Matlab, with Upload file and Play/Stop buttons.
Interested to code a display timer to show how long is the audio file while being played.
Can I use disp.AudioFileReader to count its length?
Need to count seconds in every loop.
Any suggestions appreciated.

2 Comments

What does "in every loop" mean? audioinfo tells you the length of a sound file.
Just trying to code a timer to show at which second the audio file that is being played is.

Sign in to comment.

 Accepted Answer

@Nikos Korobos - if you are using an audioplayer, you could use the CurrentSample to determine how many samples havve been played which you can translate to how much (in seconds) of the track has been played (you would need to use the sampling rate to perform the correct calculation).

10 Comments

Any chance you got some code for it?
Tried to implement audioplayer before but made my audio file restart every time i would switch volume level.
Thanks in advance!
@Nikos Korobos - the code (some of which will be applicable to your work) could look something ike
function audioplayerExample
s = load('handel.mat');
player = audioplayer(s.y, s.Fs);
player.TimerFcn = @timerCallback;
player.TimerPeriod = 1.0;
playblocking(player);
function timerCallback(player,~)
currentSample = player.CurrentSample;
totalSamples = player.TotalSamples;
sampleRate = player.SampleRate;
durationSec = totalSamples / sampleRate;
currentSecond = currentSample / sampleRate;
fprintf('%f of %f secs\n', currentSecond, durationSec);
end
end
Obviously you wouldn't use a playblocking in your GUI as I need that here to keep the player instance from being removed.
Tried to implement audioplayer before but made my audio file restart every time i would switch volume level.
audioplayer() is only for the case where you have the complete list of samples available at the time you create the audioplayer() object.
When you use audioplayer(), the only built-in support for volume control is to multiply the samples by a volume factor before creating the audioplayer() object. audioplayer() does not offer any interface for adjusting the volume while playing.
You might be able to use operating-system specific means for changing the playback volume, or you could use https://www.mathworks.com/matlabcentral/fileexchange/25584-soundvolume-set-or-get-the-system-speaker-sound-volume
If you want to be able to adjust volume during playback, then the supported way to do that is to use Audio System Toolbox to create an audio device system object, that you would periodically queue more samples for, and you would multiply the samples by the current volume at the time you queue them. The more samples at a time that you queue, the more accurate the timing of the samples would be, but the less often you would be able to adjust the samples being queued.
This is my code so far (including the Play button):
These are the volume slider values:
Do you notice anything wrong? Increasing the slider only increases the noise in my audio file.
Thanks in advance!
Your Value appears to be in percent, but you are treating it as an absolute scalar without dividing by 100.
What do you mean with divided by 100 exactly?
Thought slider values should be doubled.
How would you write it instead?
Sorry for spamming, total beginner here.
app.VolumeSlider.Value/100 .* audio
One more question, what if i wanted to code two buttons on my audio player, one to rewind and one to fast forward while a file is playing?
Can i use audioDeviceWriter method for those?
Thanks in advance!
audioDeviceWriter does not care where you get the audio samples that you send to the device. As far as it is concerned, you could flipud() the vector of samples or read two buffers from the file and throw away every second sample (fast forward) or whatever.
But for moving backwards the challenge is the audio file reader, which has no documented way of changing the position in the file - no way to seek back by 10 seconds for example. So in order to accomplish that you would need to keep a copy of the audio you read in, using a buffer the same size as the maximum total distance back you want the user to be able to move back. If you want the user to be able to move back all the way to the beginning (even if only 5 seconds at a time) then you would need to record the entire file in memory... unless there is an undocumented property that can help.

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!