-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound.cpp
More file actions
140 lines (114 loc) · 3.4 KB
/
Copy pathSound.cpp
File metadata and controls
140 lines (114 loc) · 3.4 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "Sound.hpp"
using namespace console;
Sound::Sound()
{
}
Sound::~Sound()
{
}
void Sound::setPin(const int& buzzer_pin)
{
buzzer_pin_ = buzzer_pin;
}
void Sound::init()
{
pinMode(buzzer_pin_, OUTPUT);
}
void Sound::setCallback(std::function<void()> callback)
{
done_callback_ = callback;
}
void Sound::check()
{
if (playing_)
{
unsigned long int now = millis();
if (source_ == SoundSource::SIMPLE_TONE)
{
// Check when to stop
if (now - none_duration_ > note_start_timestamp_)
{
noTone(buzzer_pin_);
playing_ = false;
if (done_callback_)
{
done_callback_();
}
}
}
else
{
// Source is melody
// Melody is coded as [NOTE1, duration1, NOTE2, duration2]
// Duration is a divider:
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// negative numbers are used to represent dotted notes
if (melody_play_next_)
{
// When note must be updated
const int tempo = 120; // Change this to make it faster or slower
const int wholenote = (60000 * 4) / tempo; // Duration of whole note in ms
// Calculate the duration of each note
int divider = melody_[melody_cursor_ + 1];
if (divider > 0)
{
none_duration_ = (wholenote) / divider; // regular note, just proceed
}
else if (divider < 0)
{
none_duration_ = (wholenote) / abs(divider);
none_duration_ *= 1.5; // increases the duration in half for dotted notes
}
note_start_timestamp_ = millis();
tone(buzzer_pin_, melody_[melody_cursor_]);
melody_play_next_ = false;
}
else
{
if (now - none_duration_ > note_start_timestamp_)
{
// Note done
if (melody_cursor_ + 2 == melody_size_)
{
// Last note done
noTone(buzzer_pin_);
playing_ = false;
if (done_callback_)
{
done_callback_();
}
}
else
{
// Increment cursor
melody_cursor_ += 2;
melody_play_next_ = true;
}
}
}
}
}
}
void Sound::play(unsigned int frequency, unsigned long int duration)
{
source_ = SoundSource::SIMPLE_TONE;
none_duration_ = duration;
note_start_timestamp_ = millis();
playing_ = true;
tone(buzzer_pin_, frequency);
}
void Sound::play(const int melody[], const int melody_array_size_bytes, bool repeat)
{
source_ = SoundSource::MELODY;
melody_repeat_ = repeat;
playing_ = true;
melody_ = melody;
melody_size_ = melody_array_size_bytes / sizeof(int);
melody_cursor_ = 0; // Start
melody_play_next_ = true;
}
void Sound::stop()
{
noTone(buzzer_pin_);
playing_ = false;
}