-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadMidiFromFolder.m
60 lines (59 loc) · 2.2 KB
/
readMidiFromFolder.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
function [X,noteScale,aveDt]=readMidiFromFolder(folderPath,QN,maxLen,varargin)
fileCellArray=dir(folderPath);
lowestNote=130; highestNote = 0;
% Do a first pass on all files to find lowest and highest notes
for i = 1:length(fileCellArray)
[~,~,EXT] = fileparts(fileCellArray(i).name);
if (strcmp(EXT,'.mid'))
filePath = strcat(folderPath,fileCellArray(i).name);
midi = readmidi(filePath);
Notes = midiInfo(midi,0);
lowNote = min(Notes(:,3)); highNote=max(Notes(:,3));
if lowNote<lowestNote
lowestNote=lowNote;
end
if highNote>highestNote
highestNote = highNote;
end
end
end
if nargin>0
noteRange = varargin{1};
lowestNote=min(noteRange);
highestNote=max(noteRange);
end
X=[];
noteScale = lowestNote:highestNote;
aveDt=[]; %holds average time step values
%second pass to pad matrices and scale tempos
for i = 1:length(fileCellArray)
[~,~,EXT] = fileparts(fileCellArray(i).name);
if (strcmp(EXT,'.mid'))
filePath = strcat(folderPath,fileCellArray(i).name);
midi = readmidi(filePath);
[Notes,~,aveMicroSPerQN] = midiInfo(midi,0);
%Use the average microseconds per quarter note to compute the
%time step between notes to get approx QN # of quarter notes in
%maxLen number of notes
ts=(QN*(aveMicroSPerQN/1e6))/maxLen;
aveDt=[aveDt,ts];
% compute piano-roll:
[PR,~,nn] = piano_roll(Notes,0,ts);
% take only maxLen columns from PR
featureMat = PR(:,1:maxLen);
% pad top if necessary
lowNote=min(nn);
if lowNote>lowestNote
featureMat=[zeros(lowNote-lowestNote,maxLen);featureMat];
end
% pad bottom if necessary
highNote=max(nn);
if highNote<highestNote
featureMat=[featureMat;zeros(highestNote-highNote,maxLen)];
end
% convert to vector and add to X
X=[X;featureMat(:)'];
end
end
aveDt=mean(aveDt);
end