-
Notifications
You must be signed in to change notification settings - Fork 1
/
chorusClassifierFinal.m
170 lines (147 loc) · 5.31 KB
/
chorusClassifierFinal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
% *************************************************************************
% Kunal Jathal
% MusixMatch
%
% CHORUS CLASSIFIER
%
% Name: chorusClassifierFinal
%
% Description:
%
% This function implements a basic chorus detection classifier. It uses a
% training set of audio snippets that contain either just a verse, or a
% verse that transitions to a chorus. It analyzes and uses the frequency of
% energy fluctuations in the audio snippets to try and feed a kNN
% classifier, which eventually tries to detect if a test sample audio
% snippet contains a chorus transition or not. In addition to implementing
% the classifier, a rough chorus location system has also been implemented.
% The chorus location system tries to identify the position of the
% chorus/transition and plays it back.
%
% Usage
%
% Call this function as you would any other MATLAB function. The list of
% training & testing audio snippets & groups can be specified in the
% songVector and groupVector variables below.
% *************************************************************************
function chorusClassifierFinal
% List out the file names of the audio snippets to be used for TRAINING here
songVectorTrain = char(...
'ET_Chorus.wav',...
'ET_NoChorus.wav',...
'Firework_Chorus.wav',...
'Firework_NoChorus.wav',...
'GotAway_Chorus.wav',...
'GotAway_NoChorus.wav',...
'Habits_Chorus.wav',...
'Habits_NoChorus.wav',...
'TGIF_Chorus.wav',...
'TGIF_NoChorus.wav',...
'Virtual_Chorus.wav',...
'Virtual_NoChorus.wav'...
);
% List out the file names of the audio snippets to be used for TESTING here
songVectorTest = char(...
'BabyOneMoreTime_Chorus.wav',...
'BabyOneMoreTime_NoChorus.wav',...
'CaliforniaGirls_Chorus.wav',...
'CaliforniaGirls_NoChorus.wav',....
'Circle_Chorus.wav',...
'Circle_NoChorus.wav',...
'ET_Chorus.wav',....
'ET_NoChorus.wav',....
'ET_Chorus2.wav',....
'ET_NoChorus2.wav',...
'Firework_Chorus.wav',...
'Firework_NoChorus.wav',...
'GotAway_Chorus.wav',...
'GotAway_NoChorus.wav',...
'Habits_Chorus.wav',...
'Habits_NoChorus.wav',...
'Happy_Chorus.wav',...
'Happy_NoChorus.wav',...
'Living_Chorus.wav',...
'Living_NoChorus.wav',...
'Peacock_Chorus.wav',...
'Peacock_NoChorus.wav',...
'TeenageDream_Chorus.wav',...
'TeenageDream_NoChorus.wav',...
'TGIF_Chorus.wav',...
'TGIF_NoChorus.wav',...
'Tubthumping_Chorus.wav',...
'Tubthumping_NoChorus.wav',...
'Umbrella_Chorus.wav',...
'Umbrella_NoChorus.wav',...
'Virtual_Chorus.wav',...
'Virtual_NoChorus.wav',...
'Warning_Chorus.wav',...
'Warning_NoChorus.wav'...
);
% MATLAB is a bit cumbersome with string arrays, so use numerical values
% for groups. These can be easily translated later. 1 = Chorus, 0 = No
% Chorus. List the *corresponding* groups for the TRAIN & TEST sets here.
groupVectorTrain = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
groupVectorTest = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,...
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
% Gather song list sizes
songTrainList = size(songVectorTrain);
numberOfTrainSongs = songTrainList(1);
songTestList = size(songVectorTest);
numberOfTestSongs = songTestList(1);
% Initialize empty training & testing feature vectors
featureVectorTrain = zeros(numberOfTrainSongs, 1);
featureVectorTest = zeros(numberOfTestSongs, 1);
% Set the number of nearest neighbors searched for here
k = 1;
% Initialize success metrics
numSongsCorrectlyClassified = 0;
successRate = 0;
%Train the system.
disp('Training the system. Please wait...');
for song=1:numberOfTrainSongs
fileNameTrain = strcat(songVectorTrain(song, :));
fileNameTrainPath = sprintf('Songs/%s', fileNameTrain);
featureVectorTrain(song, 1) = FeatureExtractor(fileNameTrainPath, false);
end
% Build test feature vector now.
disp('Gathering test feature vector data...');
for song=1:numberOfTestSongs
fileNameTest = strcat(songVectorTest(song, :));
fileNameTestPath = sprintf('Songs/%s', fileNameTest);
featureVectorTest(song, 1) = FeatureExtractor(fileNameTestPath, false);
end
% Test the system.
disp('Testing the System now...');
class = knnclassify(featureVectorTest, featureVectorTrain, groupVectorTrain, k);
% Display results
for song=1:numberOfTestSongs
fileNameTest = strcat(songVectorTest(song, :));
fileNameTestPath = sprintf('Songs/%s', fileNameTest);
testResult = class(song);
actualResult = groupVectorTest(song);
match= [];
% Evaluate result
if (testResult == actualResult)
match = '';
numSongsCorrectlyClassified = numSongsCorrectlyClassified + 1; %Track Success
else
match = '**MISMATCH**';
end
if (testResult == 0)
disp(sprintf('\nAudio snippet: %s does NOT contain a chorus transition. %s\n', fileNameTest, match));
else
disp(sprintf('\nAudio snippet: %s contains a chorus transition! %s\n', fileNameTest, match));
if (actualResult)
% Uncomment this to try and find the chorus location and play a
% small preview of it. Optional!
% FeatureExtractor(fileNameTestPath, true);
end
end
end
% Calculate success rate
successRate = (numSongsCorrectlyClassified/numberOfTestSongs) * 100;
% Print result summary
disp(sprintf('Results Summary:\n%d out of %d instances correctly classified.',...
numSongsCorrectlyClassified, numberOfTestSongs));
disp(sprintf('Success Rate: %f %%\n', successRate));
end