FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
tones.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef TONES_HH
22#define TONES_HH
23
24#include "../square_wave.h"
25
26namespace devices
27{
31 namespace audio
32 {
33 }
34}
35
36namespace devices::audio
37{
56 enum class Tone : uint16_t
57 {
58 USER0 = 0,
59 USER1,
60 USER2,
61 USER3,
62 USER4,
63 USER5,
64 USER6,
65 USER7,
66
67 // Use this tone for rest (no tone)
68 SILENCE = USER7 + 1,
69 REST = SILENCE,
70
71 C0 = 131,
72 Cs0 = 139,
73 Df0 = Cs0,
74 D0 = 147,
75 Ds0 = 156,
76 Ef0 = Ds0,
77 E0 = 165,
78 F0 = 175,
79 Fs0 = 185,
80 Gf0 = Fs0,
81 G0 = 196,
82 Gs0 = 208,
83 Af0 = Gs0,
84 A0 = 220,
85 As0 = 233,
86 Bf0 = As0,
87 B0 = 247,
88
89 C1 = 262,
90 Cs1 = 277,
91 Df1 = Cs1,
92 D1 = 294,
93 Ds1 = 311,
94 Ef1 = Ds1,
95 E1 = 330,
96 F1 = 349,
97 Fs1 = 370,
98 Gf1 = Fs1,
99 G1 = 392,
100 Gs1 = 415,
101 Af1 = Gs1,
102 A1 = 440,
103 As1 = 466,
104 Bf1 = As1,
105 B1 = 494,
106
107 C2 = 523,
108 Cs2 = 554,
109 Df2 = Cs2,
110 D2 = 587,
111 Ds2 = 622,
112 Ef2 = Ds2,
113 E2 = 659,
114 F2 = 698,
115 Fs2 = 740,
116 Gf2 = Fs2,
117 G2 = 784,
118 Gs2 = 831,
119 Af2 = Gs2,
120 A2 = 880,
121 As2 = 932,
122 Bf2 = As2,
123 B2 = 988,
124
125 C3 = 1046,
126 Cs3 = 1109,
127 Df3 = Cs3,
128 D3 = 1175,
129 Ds3 = 1245,
130 Ef3 = Ds3,
131 E3 = 1319,
132 F3 = 1397,
133 Fs3 = 1480,
134 Gf3 = Fs3,
135 G3 = 1568,
136 Gs3 = 1662,
137 Af3 = Gs3,
138 A3 = 1760,
139 As3 = 1865,
140 Bf3 = As3,
141 B3 = 1976,
142
143 C4 = 2093,
144 Cs4 = 2217,
145 Df4 = Cs4,
146 D4 = 2349,
147 Ds4 = 2489,
148 Ef4 = Ds4,
149 E4 = 2637,
150 F4 = 2794,
151 Fs4 = 2960,
152 Gf4 = Fs4,
153 G4 = 3136,
154 Gs4 = 3322,
155 Af4 = Gs4,
156 A4 = 3520,
157 As4 = 3729,
158 Bf4 = As4,
159 B4 = 3951,
160 };
161
182 template<board::Timer NTIMER, board::PWMPin OUTPUT> class ToneGenerator
183 {
184 private:
186
187 public:
189 ToneGenerator(const ToneGenerator&) = delete;
190 ToneGenerator& operator=(const ToneGenerator&) = delete;
192
194 using PRESCALER = typename SQWGEN::TIMER::PRESCALER;
196 using COUNTER = typename SQWGEN::TIMER::TYPE;
197
201 ToneGenerator() = default;
202
215 void start_tone(Tone tone)
216 {
217 if (tone > Tone::SILENCE) generator_.start_frequency(uint32_t(tone));
218 }
219
233 void start_tone(PRESCALER prescaler, COUNTER counter)
234 {
235 generator_.start_frequency(prescaler, counter);
236 }
237
243 {
244 generator_.stop();
245 }
246
247 private:
248 SQWGEN generator_;
249 };
250}
251
252#endif /* TONES_HH */
API class for tone generation to a buzzer (or better an amplifier) connected to pin OUTPUT.
Definition: tones.h:183
ToneGenerator()=default
Create a new generator of tones.
typename SQWGEN::TIMER::TYPE COUNTER
The counter type (uint8_t or uint16_t) for the selected NTIMER.
Definition: tones.h:196
void stop_tone()
Stop the tone being currently generated to the connected buzzer.
Definition: tones.h:242
void start_tone(Tone tone)
Start generating a tone on the connected buzzer until stop_tone() is called.
Definition: tones.h:215
void start_tone(PRESCALER prescaler, COUNTER counter)
Start generating a tone on the connected buzzer until stop_tone() is called.
Definition: tones.h:233
typename SQWGEN::TIMER::PRESCALER PRESCALER
The TimerPrescaler type matching the selected NTIMER.
Definition: tones.h:194
void start_frequency(uint32_t frequency)
Start producing, on the ouput pin, a square wave with the specified frequency.
Definition: square_wave.h:105
void stop()
Stop square wave generation.
Definition: square_wave.h:140
Defines API for audio tones (square waves) generation and simple melodies playing.
Definition: tone_player.h:29
Tone
This enum defines all possible audio tones that can be generated.
Definition: tones.h:57
Defines all API for all external devices supported by FastArduino.