-
Notifications
You must be signed in to change notification settings - Fork 0
/
getlocalvelocities.m
58 lines (43 loc) · 2.13 KB
/
getlocalvelocities.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
%GETLOCALVELOCITIES Calculate the average velocities over segments of a
% MIDI matrix.
% Calculates the mean velocities from the MIDI matrix over sections of the
% audio determined by the BPM and number of beats in each bar.
% Input arguments:
% midiMatrix - the MIDI toolbox MIDI matrix of the MIDI clip
% onsets - list of MIDI note onset points in seconds
% bpm - the BPM of the MIDI clip
% timeSigNumber - the number of beats in each bar
function[velocities] = getlocalvelocities(midiMatrix, onsets, bpm, timeSigNumerator)
% Determine the length of a crotchet based on the BPM in seconds
crotchetLength = 60 / bpm;
% Determine the length of a bar in seconds
barLength = crotchetLength * timeSigNumerator;
% Determine the length of a segment
segmentLength = barLength * 2;
% Extract the onsets and velocities from the MIDI matrix
velocityOnsets = zeros(length(midiMatrix), 2);
velocityOnsets(:, 1) = midiMatrix(:, 1);
velocityOnsets(:, 2) = midiMatrix(:, 5);
velocityOnsets(:, 1) = (velocityOnsets(:, 1) - 1) .* crotchetLength;
% Calculate the last onset position
lastOnset = onsets(end);
% Caluclate the total number of segments
nSegments = ceil(lastOnset / segmentLength);
% Initialise a variable for storing the velocities
velocities = zeros(nSegments, 1);
% Loop over each segment of the audio
for n=1:nSegments
% Determine the start and end points for the segment
segmentStart = (n - 1) * segmentLength;
segmentEnd = segmentStart + segmentLength;
% Extract the velocities and onsets that fall within the audio
% segment time range
segmentVelocityOnsets = velocityOnsets(velocityOnsets(:, 1) >= segmentStart & velocityOnsets(:, 1) < segmentEnd, :);
% Calculate the mean velocity in the segment
meanVelocity = mean(segmentVelocityOnsets(:, 2));
% Store the mean velocity
velocities(n) = meanVelocity;
end
% Crop any NaN values from the array
velocities = velocities(all(~isnan(velocities), 2), :);
end