forked from melodyians/melodyian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components_melody.cpp
93 lines (65 loc) · 2.49 KB
/
components_melody.cpp
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
#include "components.h"
#include "types.h"
#import "helper_midi.h"
byte MaxRandomNoteValue(float random_jitter, int total_num_notes) {
return (byte) (random_jitter * total_num_notes);
}
Melody::Melody(int length, int m[], int d[]) {
this->length = length;
this->melody = new int[length];
this->durations = new int[length];
// Copy note and duration arrays. Necessary so these
// don't get garbage collected.
for (int i = 0; i < length; i++) {
this->melody[i] = m[i];
this->durations[i] = d[i];
}
this->note_position = 0;
this->elapsed = 0;
}
void Melody::reset() {
this->note_position = 0;
this->elapsed = 0;
}
int Melody::current_note_duration(int rate) {
// To calculate the note duration, take a time value (2ms - ~10 seconds)
// divided by the note type. (e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.)
return rate / this->durations[this->note_position];
}
int Melody::current_note() {
//return 60;
return this->melody[this->note_position];
}
void Melody::play(unsigned short dt, State * robot_state) {
float jitter = 0;
if (robot_state->bypassRandomNote() == false) {
jitter = robot_state->randomness();
}
this->elapsed += dt;
int current_note_duration = this->current_note_duration(robot_state->rate());
int pause_duration = current_note_duration * 1.30;
int max_note_event_length = current_note_duration + pause_duration;
// If we are still playing the note
if (this->elapsed <= current_note_duration) {
if (this->current_note() == 0) {
robot_state->sound_state()->turnOffAllNotes();
} else {
robot_state->sound_state()->turnNoteOn(this->current_note());
}
// If we are during the pause after the note
} else if (this->elapsed <= max_note_event_length) {
robot_state->sound_state()->turnOffAllNotes();
// Progress to next note next time around
} else {
// Reset time
this->elapsed = 0;
// Progress note, with jitter if we have it. If we are bypassing,
// or jitter == 0, this reduces to "advance to the next note"
byte max_offset = MaxRandomNoteValue(jitter, this->length);
byte random_note = 1 + random(0, max_offset);
this->note_position = this->note_position + (1 * random_note);
if (this->note_position >= this->length) {
this->note_position = this->note_position - this->length;
}
}
}