Monday, October 26, 2009

Write audio data to a GIF file to display animation

Sometimes it can be applied in the PPT presentation.
In the following MATLAB code, the framerate is 20frame/second, the window size for audio data is 2 seconds, and no overlap. It basically saves the ‘plot’ figures to a GIF frame by frame:

[wav, fs] = wavread('DEMO.wav');
window_length = 2; %2 seconds
window_sample = window_length * fs;

for i = 1 : window_sample : length(wav)-window_sample
block = wav(i:i+window_sample);
time = (0:1/fs:(length(block)-1)/fs) + i/fs;
h = plot(time, block);
axis([time(1) time(end) -1 1]);

saveas(gcf, 'block.jpg');
img_block = imread('block.jpg');
[ind, map] = rgb2ind(img_block,256);
if i == 1
writemode = 'overwrite';
else
writemode = 'append';
end
imwrite(ind,map,'WaveGIF.gif','gif', ...
'DelayTime', 1/20, 'WriteMode',writemode);
end

No comments:

Post a Comment