user3168654
user3168654

Reputation: 95

Play the sound when click the pushbutton in matlab GUI

I am trying to play the audio in synchronization with moving line on graph of audio after click on pushbutton in GUI matlab. the code for the above task is

  fs = 44100;
durT = 3; %seconds
durS = fs*durT; %samples
x = randn(durS, 1);

dt = 1/fs;
tAxis = dt:dt:durT;

frameRate = 25; %fps
frameT = 1/frameRate;

mag = 5;

figure;
plot(tAxis, x);
ylim([-mag mag])
xlim([0 durT])
xlabel('Time [s]')

playHeadLoc = 0;
hold on; ax = plot([playHeadLoc playHeadLoc], [-mag mag], 'r', 'LineWidth', 2);

player = audioplayer(x, fs);
myStruct.playHeadLoc = playHeadLoc;
myStruct.frameT = frameT;
myStruct.ax = ax;

set(player, 'UserData', myStruct);
set(player, 'TimerFcn', @apCallback);
set(player, 'TimerPeriod', frameT);
play(player);

the callback function is

function src = apCallback(src, eventdata)
    myStruct = get(src, 'UserData'); %//Unwrap

    newPlayHeadLoc = ...
        myStruct.playHeadLoc + ...
        myStruct.frameT;
    set(myStruct.ax, 'Xdata', [newPlayHeadLoc newPlayHeadLoc])

    myStruct.playHeadLoc = newPlayHeadLoc;
    set(src, 'UserData', myStruct); %//Rewrap
end

it is working well on matlab command window...but when i put this code in the callback function of pushbutton in GUI,it is just displaying the graph of signal with red line at the start.

i am not able to figure out why it is not working here.. please help. your contribution will appreciated greatly.

Upvotes: 0

Views: 1111

Answers (1)

M.Khouli
M.Khouli

Reputation: 4122

Well just specify an axes to be used for Plotting and give it the initial default attributes, instead of using Figures.

 btn1_callbck (VAR)
 set(handles.axes1);
 plot(t,signal);
 end

OR

 btn1_callbck (VAR)
 figure(1000);
 plot(t,signal);
 end

hope this will help.

Upvotes: 2

Related Questions