user4514279
user4514279

Reputation: 35

Volume Slider in Matlab GUI

Link of Previous Post:
Matlab Questions about Audioplayer GUI

I am trying to make a volume slider in Matlab...
So currently what I have in mind from what I have searched is using handles variable to get the slider's value. After that, I am planning to have the variable multiply the (x,fs) in the audioplayer.
My current code for the beginning of the handle is like this...

handles.a = []  ; %I don't know what to put for this >.<
handles.output = hObject;
handles.myPlayer=[];
guidata(hObject, handles);

My code for the volume slider is like this:

function slider1_Callback(hObject, eventdata, handles) 
handles.a = get(handles.slider1,'Value');
guidata(hObject,handles);

And my code for the audioplayer will be something like this(not sure if it will work if I do it this way):

FullPath='C:\Users\Y400\Documents\MATLAB\test1'; 
[x,Fs]=wavread(FullPath);
handles.myPlayer = audioplayer(handles.a*x,Fs);
play(handles.myPlayer);`
guidata(hObject, handles)`

So when I tried "Playing" the audio after this, I get the following error:
Undefined function or variable 'a'.

Will appreciate it if anyone can guide me on this? >.<

EDIT

I manage to solve(?) the error: Undefined Function or Variable 'a'.

My current error now is I am getting this error:
Error using * Inner matrix dimensions must agree.

Above code is edited to have the following error as well...

The " * " comes from when I try to multiply handles.a with x

Upvotes: 0

Views: 2026

Answers (2)

BillBokeey
BillBokeey

Reputation: 3511

Let's now see how we can do to increase/decrease the volume without restarting the sample play :

In the slider1_Callback :

function slider1_Callback(hObject,handles,eventdata)

%Pause audioplayer

pause(handles.myPlayer);

%Know how far the user has got in the sample

NewStart=get(handles.myPlayer,'CurrentSample')+1;

%stop current player

stop(handles.myPlayer);

%Reload your sample

[x,Fs]=wavread(FullPath);

%Create a new sample by cutting x and keeping only the lines from NewStart 
%to the end

x=x(NewStart:end,:); 

%Get the value of the slider

Volume=get(handles.slider1,'value');

%Set new audioplayer

handles.myPlayer=audioplayer(x*Volume,Fs);

%Play

play(handles.myPlayer);

% save handles structure

guidata(hObject,handles);

How long is your audio sample? If it's too long, it might take some time to load and interrupt play. To reduce loading times, you might consider saving the data (for example in one of your button's 'userdata' property).

Upvotes: 1

BillBokeey
BillBokeey

Reputation: 3511

In your last post, in order to make the audioplayer object we created visible by the whole program, we had to store it manually in the habdles structure.

Uicontrols like pushbuttons, sliders, edit texts, etc.. though, are stored automatically in the GUI handles structure.

For example, your slider whose tag is 'slider1' can be accessed via 'handles.slider1'.

To get its value, you can just use :

get(handles.slider1, 'value');

Note that, in the property inspector, you can change its Min and Max attribute to fit your needs (here Min=0 and Max=5 seems quite right).

Now, we'll have to actually change the volume of your audio sample.

An easy way to do it (but which will replay the sample since the beginning), would be to implement the volume modification in the button you use to play the sound (Button3 if i remember well) :

After the line :

[x,Fs]=wavread('FullPath');

Add :

Volume=get(handles.slider1,'value');

And then :

handles.myPlayer=audioplayer(Volume*x,Fs);

Last step is to call the callback of your Button3 in your slider1_Callback :

Function     Slider1_Callback(hObject,handles,eventdata)

Button3_Callback(hObject,handles,eventdata);

Tell me if this is what you need, or if you want the volume to be increased without restarting the sample from the beginning, which would be a bit harder to do, but would still be doable.

Upvotes: 1

Related Questions