How to change color of a push button for the duration of its callback (a sound)?

1 view (last 30 days)
Essentially I've built a synthesizer using GUIDE with various keys which are all connected to their respective callback functions, as well as keypressfunctions where every synth key is connected to a different keyboard key. I want the keys to change color for the duration of the sound that is played by pressing the button (either with the mouse or the keyboard). Here is an example of all the code that is used to play my 'LowC' which is played with the 'z' key:
% --- Executes just before project3 is made visible.
function project3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to project3 (see VARARGIN)
% Choose default command line output for project3
handles.output = hObject;
% Update handles structure
octave = 0;
guidata(hObject, handles);
handles.amp = exp(-2.5);
handles.fs = 44100;
handles.octave = octave;
handles.freq = 2^3*[32.7032 34.6478 36.7081 38.8909 41.2034 43.6535 46.2493 48.9994 51.9131 55.0000 58.2705 61.7354 65.4064 69.2957 73.4162 77.7817 82.4069 87.3071 92.4986 97.9989 103.826 110 116.541 123.471 130.813] ;
handles.sig = handles.amp*cos(2*pi*2^handles.octave*handles.freq'*[0:40000]/44100);
guidata(hObject, handles);
% --- Executes on button press in LowC.
function LowC_Callback(hObject, eventdata, handles)
% hObject handle to LowC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sound(handles.sig(1,:), 44100);
% --- Executes on key press with focus on figure1 or any of its controls.
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata structure with the following fields (see FIGURE)jhgf
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
switch eventdata.Key
case 'z'
LowC_Callback(hObject, eventdata, handles);
The initialization code is provided for context into how the callback of each key works.

Answers (1)

Geoff Hayes
Geoff Hayes on 14 Nov 2014
Max - rather than using the sound function, consider using the audioplayer which has the ability to block any other (GUI) processing until the audio (data) associated with the player has finished. If we assume that your key z button on your GUI is tagged as pushbuttonZ, then in your LowC_Callback, do the following
function LowC_Callback(hObject, eventdata, handles)
% get the current back ground colour of the pushbutton
bgClr = get(handles.pushbuttonZ,'BackgroundColor');
% change the background colour to blue
set(handles.pushbuttonZ,'BackgroundColor',[0 0 1]);
% create the audio player with the appropriate sound for low C
playerObj = audioplayer(handles.sig(1,:),handles.fs);
% block processing until the audio has played out
playblocking(playerObj);
% restore the background colour
set(handles.pushbuttonZ,'BackgroundColor',bgClr);
Try the above and see what happens!

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!