How to use 2 datacursors to dynamically change audio files in GUI's?

In my GUI, I have loaded an audio file ( .wav ) into the variable storedStructure, and saved it in the handles structure - handles.str1
Now, i would like to use 2 data cursors to dynamically alter the audio playing length. For example, if the length of the audio is 5 seconds, if I were to choose 2 cursors at 2 seconds and 4 seconds, it would play only that part of the audio.
I need to put this code right away in my .m file for my GUI.

 Accepted Answer

Is there a reason why you need to use data cursors instead of (for example) ginput() ?
What is the data cursor to be applied against? Data cursors require a graph, but you have not indicated anything as being graphed. Are you graphing the signal level, or are you graphing the time?
Why not use a couple of sliders?

13 Comments

Well, the person who gave me this assignment asked me for the ability to pinpoint any 2 points on the graph, and use those points to play.
I understand I can use sliders, but that would not let me use 2 points, but only 1 starting point.
Correct me if I'm wrong?
You can use two sliders. Or you could use ginput(2) to collect the two points.
a = get(handles.slider1, 'Value');
x = 0:16000;
y=a*x;
sig = audioread(handles.str1);
plot(handles.Audio,y,sig);
This is the code I thought I should use. It is clearly wrong. Do you know how I can change this to fit my needs?
What do you intend your "a" value to represent?
Okay. So basically, I would like the slider to represent the audio file time. Like, as I move through the file, I want to play the file from that part, and simultaneously plot the graph from that point to the end.
For example. I use audio read (handles.str1) and I want to plot and play the file dynamically as I change the slider.
Hope that makes sense.
[sig, fs] - audiread(handles.str1);
nsamp = size(sig,1);
slider_value = get(handles.slider1, 'Value'); %assume slider is configured for percentage
first_samp = 1 + floor(slider_value / 100 * nsamp);
plot(handles.Audio, first_samp : nsamp, sig(nsamp:end, :));
audioplayer( sig(nsamp:end, :), fs)
Hey, the wave isn't plotting. I used the same code. However, there is no error that is associated with the code.
You might want to add
drawnow()
after the plot()
Also exactly how did you configure your slider? The above assumes it was configured for minimum 0 and maximum 100 (percent)
I did configure it to percentage.
If you are going to put it into your slider callback, replace handles.slider1 with hObject
However, I would not put it in the slider callback. The slider callback often gets called multiple times while you are moving it longer distances or fine-tuning it. You should adjust the position and then have something else to trigger starting the playing and plotting.
The error message is indicating that you have either deleted the slider that handles.slider1 referred to, or you never initialized it to a valid slider handle. You might have deleted the slider if your code creates the slider with an explicit uicontrol() call, rather than having the slider created and stored with the .fig .
Note for clarity: once you move the action outside the slider callback, you would not use hObject to refer to the slider; instead you would track down why handles.slider1 was not correct and fix that problem so that handles.slider1 can be used in the place you initiate the plotting and playing.
If you need to, you could use
handles.slider1 = findall(0, 'Tag', 'slider1');
Okay, I understood that. Here are 2 pictures of the 2 callbacks in question. What am I doing wrong here?
<<
>>
There are no errors but the Audio axes aren't plotting the graph....
Correction:
plot(handles.Audio, first_samp : nsamp, sig(first_samp:end, :));

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!