Save individual Audio and Video sample using timer callback function

1 Ansicht (letzte 30 Tage)
william wades
william wades am 20 Okt. 2019
Bearbeitet: william wades am 20 Okt. 2019
I want to play an audio file, plot its current position and save the resulting animation in an avi file along with the corresponding audio sample.
This is the plot marking function where I want to save plot data and the corresponding audio sample
%% ------------------------------------------------------------------------
%% the timer callback function
function plotter(obj, eventdata, player, figHandle, plotdata)
% check if sound is playing, then only plot new marker
p = 1;
filename ='eg.avi';
videoFWriter = vision.VideoFileWriter(filename);
open(videoFWriter);
if strcmp(player.Running, 'on')
% get the handle of current marker and delete the marker
hMarker = findobj(figHandle, 'Color', 'r');
delete(hMarker);
% get the currently playing sample
x = player.CurrentSample;
z(p) = x
% plot the new marker
plot(repmat(x, size(plotdata)), plotdata, 'r');
F(p) = getframe(gcf)
step(videoFWriter,F(p), z(p));
p = p+1;
%
close(videoFWriter);
end
end
This is the main function loads the audio file and the timer callback function
%% load some audio data (available with MATLAB)
load handel;
%% create the plot of audio samples
figure(1); hold on;
plot(y, 'b'); % plot audio data
title('Audio');
xlabel(strcat('Sample no (fs = ', num2str(Fs), ')'));
ylabel('Mag');
ylimits = get(gca, 'YLim'); % get the y-axis limits
plotdata = [ylimits(1):0.1:ylimits(2)];
hline = plot(repmat(0, size(plotdata)), plotdata, 'r'); % plot the marker
%% instantiate the audioplayer object
player = audioplayer(y, Fs);
%% setup the timer for the audioplayer object
player.TimerFcn = {@plotter, player, gcf, plotdata}; % timer callback function (defined below)
player.TimerPeriod = 0.005; % period of the timer in seconds
%% start playing the audio
play(player);

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by