Reputation: 13
im trying to manipulate audio via volume slider, however I've been searching online and have not found any example.
This is my code which display the slider number:
handles.volume=get(hObject,'Value');
set(handles.vol_box,'String',num2str(handles.volume,'%2.f'));
guidata(hObject, handles);
Could anyone one provide an example?
Upvotes: 0
Views: 1249
Reputation: 13945
There is no documented function in MATLAB allowing you to do so. However, there are many undocumented features that rely on java which you can use to your advantage, such as controlling your computer's volume.
Fortunately, there is a wonderful website about undocumented Matlab stuff right here in which Yair showed some code to access the system's volume controller. To be honest I can't explain this part to you, however with a bit of playing around it's possible to create a simple GUI with a slider to control interactively the sound volume.
1) Here is Yair's function called SoundVolume
, available on the File Exchange here.
For the sake of completeness here is the whole code. I wrote a simple function to generate a GUI and call SoundVolume to interactively change the speaker volume, which is right after.
function volume = SoundVolume(volume)
% Loop over the system's mixers to find the speaker port
import javax.sound.sampled.*
mixerInfos = AudioSystem.getMixerInfo;
foundFlag = 0;
for mixerIdx = 1 : length(mixerInfos)
mixer = AudioSystem.getMixer(mixerInfos(mixerIdx));
ports = getTargetLineInfo(mixer);
for portIdx = 1 : length(ports)
port = ports(portIdx);
try
portName = port.getName; % better
catch %#ok
portName = port.toString; % sub-optimal
end
if ~isempty(strfind(lower(char(portName)),'speaker'))
foundFlag = 1; break;
end
end
end
if ~foundFlag
error('Speaker port not found');
end
% Get and open the speaker port's Line object
line = AudioSystem.getLine(port);
line.open();
% Loop over the Line's controls to find the Volume control
ctrls = line.getControls;
foundFlag = 0;
for ctrlIdx = 1 : length(ctrls)
ctrl = ctrls(ctrlIdx);
ctrlName = char(ctrls(ctrlIdx).getType);
if ~isempty(strfind(lower(ctrlName),'volume'))
foundFlag = 1; break;
end
end
if ~foundFlag
error('Volume control not found');
end
% Get or set the volume value according to the user request
oldValue = ctrl.getValue;
if nargin
ctrl.setValue(volume);
end
if nargout
volume = oldValue;
end
2) In order to interactively set the volume, we only need to call SoundVolume
with a number from 0 to 1 and we're good to go. That's exactly what we are going to do with the following GUI and a slider:
function SetSound()
clear
clc
close all
%// Create GUI components, i.e. a figure and a slider
handles.fig = figure('Position',[500 500 600 200],'Units','pixels');
handles.slider = uicontrol('Style','slider','Position',[50 50 400 20],'Min',0,'Max',1,'Value',.5);
%// Add a listener to interactively update the volume as you move it.
handles.Listener = addlistener(handles.slider,'Value','PostSet',@(s,e) GetValue(handles));
guidata(handles.fig);
%// Now here call Yair's function SoundVolume, with the value of the slider which is between 0 and 1.
function GetValue(handles)
%// Get the value of the slider here and call SoundVolume with it.
Value = (get(handles.slider,'Value'));
SoundVolume(Value)
end
end
And that's it! To see the result call SetSound
from the command window and you're good to go.
Hope that helps!
Upvotes: 1