% KAIST CS891 Special Lecture on Music Information Retrieval (MIR)
% Lab3.1: Tempo Extraction/Beat Tracking
% Kyogu Lee (August 2008)

clear all;
close all;

% read audio file
%[x,fs] = wavread('../wav/09_words_of_love.wav');
[x,fs] = wavread('../wav/prelude_cmaj_2.wav');

if size(x,2) > size(x,1)
    x = x'; % make a column vector
end
x = mean(x,2); % make mono

% first extract tempo
[t,xcr,D,onset,sr_onset] = tempo(x,fs);

% then track beat
b = beat(x,fs);

% display
subplot(211); plot([0:length(x)-1]/fs,x);
vline(b,'r');
subplot(212); plot([0:length(onset)-1]/sr_onset,onset);

% generate beat audio pulse train
b_audio = 0.5*x;
b_audio(round(b*fs)) = 1;
wavwrite(b_audio,fs,'../wav/prelude_cmaj_2_beat.wav');
