-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthesize.m
71 lines (64 loc) · 2.17 KB
/
synthesize.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
function [notes] = synthesize(ordered_notes)
%{
SYNTHESIZE - takes in an ordered_notes cell and outputs a cell of audio
vectors, (each 4-seconds long). This function should be called prior
to producing sound.
Preconditions: The "driver" function has been called. Do not call this
function before independently of driver, as the data structures for sound
files need to be initialized.
C-style format of note data structure (for reference):
struct note{
used; (boolean to tell whether the note has been previously requested)
audio; (audio vector to actually play the note)
};
%}
notes =cell(1,1);
notesIdx = 1;
for(cellidx=1:1:length(ordered_notes))
g_ord_notes = ordered_notes{cellidx};
group = [];
i = 1;
while(i<=size(g_ord_notes,1))
curr_loc = g_ord_notes{i,1}; % give 5-pixel tolerance
group = [group ; g_ord_notes{i,3}, g_ord_notes{i,4}];
i = i+1;
if(i <= size(g_ord_notes,1) && abs(g_ord_notes{i,1}-curr_loc < 5))
%if i is in range AND next location is within tolerance,
continue;
else % get notes
notes{notesIdx} = getSoundVec(group);
notesIdx = notesIdx+1;
group = [];
end
end
end
end
% Gets the audio from string/fret value.
function soundOut = getSoundVec(note_vals)
%{
format of note vals should be [string, fret ; string, fret ; etc.]
This function will use dynamic programming to generate and return audio
vectors to be played.
%}
soundOut = [];
soundCnt = 0;
global MNR
for(j=1:1:size(note_vals,1))
strIdx = note_vals(j,1);
fretIdx = note_vals(j,2)+1;
if(~MNR(strIdx,fretIdx).used) % If note has NOT been used before, generate and memoize.
fprintf('Note %d, %d encountered the first time. Generating.\n',strIdx,fretIdx-1);
MNR(strIdx,fretIdx).used = true;
MNR(strIdx,fretIdx).audio = pitch_shift(MNR(strIdx,1).audio,fretIdx-1);
else
fprintf('Getting cached note %d, %d\n',strIdx,fretIdx-1);
end
if(length(soundOut)<1)
soundOut = MNR(strIdx,fretIdx).audio;
else
soundOut = soundOut + MNR(strIdx,fretIdx).audio;
end
soundCnt = soundCnt + 1;
end
soundOut = soundOut./soundCnt;
end