How can I play audio in MATLAB 8.1 (R2013a) while simultaneously moving a marker on a plot of the audio data, synchronized to the sound?

7 views (last 30 days)
I would like to play audio sounds in MATLAB while simultaneously displaying the audio waveform and a marker on the waveform showing the sample that is currently being played.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Dec 2021
Edited: MathWorks Support Team on 13 Dec 2021
In order to play audio data in MATLAB we will first instantiate an AUDIOPLAYER object with the data. Thus, if "y" is the audio data and "fs" is the sampling frequency then the following line of code will create the audioplayer object.
player = audioplayer(y, fs);
In order to play the audio samples from this object one can use the PLAY function as follows:
play(player);
This object "player" has several properties that can be read while the audio is playing. More information on these properties can be found in the documentation at the following location. We will use several of these properties to accomplish our task.
player.Running = 'on' | 'off'
This property is either 'on' or 'off' and indicates whether samples are currently being played. This is the first thing that we will read. If samples are not being played then we do not wish to update our diagram (plot).
player.CurrentSample = 5
This property gives the sample number that is currently being played. If the player is running, then we use this property to find out where we need to plot a marker on our audio plot. Thus, if the 5th sample is currently playing then we will plot a marker on x = 5 on the plot.
The plotting of these markers needs to be done periodically as long as the sound is playing. To achieve the periodicity we use the following two properties of the "player" object.
player.TimerFcn = @function
This is a callback function associated with the audioplayer and will be called at a particular interval as long as the sound is playing. To this property we must assign a function that will perform the actual plotting of the marker.
player.TimerPeriod = 0.01
This property contains a number which is the time period with which the above TimerFcn is called as long as sound is playing. Thus while the audioplayer is running, the timer function will be called at intervals of 0.01 seconds according to the above property.
Thus we will use these properties to accomplish what we want. Once the sound starts playing, at intervals of 0.01 seconds we will call a function that will plot a new marker at the sample that is currently being played.
The attached MATLAB program presents an example of how these properties can be used to accomplish this task. Download and execute the file to see the results.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!