-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_tracks.m
147 lines (118 loc) · 3.96 KB
/
add_tracks.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
function add_tracks( directory_name, varargin )
%ADD_TRACKS Generación base de datos.
% Debe leer todos los wav y mp3 de una carpeta
% 1 - Remuestrea a 8kHz
% 2 - Extrae hashes
% 3 - Guarda resultados
%
% Tres niveles de verbosidad estan definidos para debugging:
% - 'd': Muestra: 'song_id <-> nombre cancion' + Elapsed time
% - 'dd': Muestra 'Elapsed time' subprocesos
% - 'ddd': Muestra, cancion a cancion, STFT + Landmarks
% Ejemplo: add_tracks('Target', 'dd')
clc
%% Inicializacion de variables
[hashes, song_id] = create_database(directory_name);
dirlist = dir(directory_name);
debug = 0;
if length(varargin) == 1
mode = varargin{1};
if strcmp(mode,'d')
debug = 1;
end
if strcmp(mode,'dd')
debug = 2;
end
if strcmp(mode,'ddd')
debug = 3;
end
end
if debug >= 1
tic % Calculo del tiempo de procesado. Inicio del 'stopwatch'
end
%% Recorremos el directorio en busca de archivos de audio.
for i = 1:length(dirlist)
[pathstr, name, ext] = fileparts(dirlist(i).name);
if (strcmp(ext,'.mp3') || strcmp(ext,'.wav') || strcmp(ext,'.wave'))
if debug == 0
STR = sprintf('song_id: %d - Track: %s', song_id, dirlist(i).name);
disp(STR);
else
if debug >= 1
t_ini = toc;
STR = sprintf('Start track processing: \n\tAssigned song_id: %d \n\tTrack name: %s', song_id, dirlist(i).name);
disp(STR);
end
end
%% Remuestreo a 8kHz
% Debug: Subprocess time init
if debug >= 2
t_ini_d = toc;
end
file_name = strcat(directory_name,'/',dirlist(i).name);
[y,Fs] = audioread(file_name);
% info = audioinfo(file_name)
[P,Q] = rat(8000/Fs);
x = resample(y,P,Q);
% Debug: Subprocess time clac + display
if debug >= 2
t_end_d = toc;
STR = sprintf('\nResampling: \n\tElapsed time: %.5f \n', (t_end_d-t_ini_d));
disp(STR);
end
%% Extraccion de hashes
% Debug: Subprocess time init
if debug >= 2
t_ini_d = toc;
end
[L, S, maxes] = find_landmarks(x, 30);
% Debug: Subprocess time clac + display
if debug >= 2
t_end_d = toc;
STR = sprintf('\nFind_landmarks: \n\tElapsed time: %.5f \n', (t_end_d-t_ini_d));
disp(STR);
t_ini_d = toc;
end
H = landmark2hash(L, song_id);
% Debug: Subprocess time clac + display
if debug >= 2
t_end_d = toc;
STR = sprintf('\nLandmark2Hash: \n\tElapsed time: %.5f \n', (t_end_d-t_ini_d));
disp(STR);
end
%% Almacenamiento de resultados
% Debug: Subprocess time init
if debug >= 2
t_ini_d = toc;
end
hashes = record_hashes(H, hashes);
% Debug: Subprocess time clac + display
if debug >= 2
t_end_d = toc;
STR = sprintf('\nRecord_hashes: \n\tElapsed time: %.5f \n', (t_end_d-t_ini_d));
disp(STR);
end
% Debug: Show landmarks
if debug >= 3
show_landmarks(L,S,maxes);
pause
end
% Debug: Elapsed time info (Track total)
if debug >= 1
t_end = toc;
STR = sprintf('End: \n\tsong_id: %d \n\tElapsed time: %.5f',song_id, (t_end-t_ini));
disp(STR);
end
song_id = song_id+1;
end
end
%% Almacenamiento en disco
next_song_id = song_id;
save(strcat(directory_name,'/','hashes'), 'hashes', 'next_song_id');
% Debug: Elapsed time info (Total)
if debug >= 1
t_end = toc;
STR = sprintf('\n\nResult: \n\tTotal songs: %d \n\tTotal elapsed time: %.5f',song_id, t_end);
disp(STR);
end
end