-
Notifications
You must be signed in to change notification settings - Fork 0
/
inviareaTasiera.m
51 lines (43 loc) · 1.64 KB
/
inviareaTasiera.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
clear all
device=mididevice("CASIO USB-MIDI");
readme = fopen('starwars.mid');
[readOut, byteCount] = fread(readme);
fclose(readme);
channel = 1;
note = 23;
velocity = 127;
msg = midimsg('NoteOn',channel,note,velocity);
midisend(device,msg);
%receivedMessages = midireceive(device)
chunkIndex = 14; % Header chunk is always 14 bytes
ts = 0; % Timestamp - Starts at zero
BPM = 120;
ticksPerQNote=4;
% Parse track chunks in outer loop
while chunkIndex < byteCount
% Read header of track chunk, find chunk length
% Add 8 to chunk length to account for track chunk header length
chunkLength = polyval(readOut(chunkIndex+(5:8)),256)+8;
ptr = 8+chunkIndex; % Determine start for MIDI event parsing
statusByte = -1; % Initialize statusByte. Used for running status support
% Parse MIDI track events in inner loop
while ptr < chunkIndex+chunkLength
% Read delta-time
[deltaTime,deltaLen] = findVariableLength(ptr,readOut);
% Push pointer to beginning of MIDI message
ptr = ptr+deltaLen;
% Read MIDI message
[statusByte,messageLen,message] = interpretMessage(statusByte,ptr,readOut);
% Extract relevant data - Create midimsg object
[ts,msg] = createMessage(message,ts,deltaTime,ticksPerQNote,BPM);
% Add midimsg to msgArray
if(~isempty(msg))
midisend(device,msg);
end
pause(0.1)
% Push pointer to next MIDI message
ptr = ptr+messageLen;
end
% Push chunkIndex to next track chunk
chunkIndex = chunkIndex+chunkLength;
end