-
Notifications
You must be signed in to change notification settings - Fork 9
/
XibStave.h
179 lines (146 loc) · 5.3 KB
/
XibStave.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @brief Library to play multiple music staves
* @author Xibhu - Carlos MR - https://github.com/xibhu (create library)
* @author Yago-Nguyen Rodriguez Gonzalez (Initial code)
* @author Rubén (musical help)
* @note Telegram spanish group - 8-bit Melody Maker 2B
* @note Video demonstration https://youtu.be/ecUfr1Za3Rk
* @version 0.3
* @date 4-12-2020
* @copyright GNU General Public License v3.0
* @note This library allows you to use many staves independently, one stave per
* speaker. To create a stave, use the variables on "Durations.h" and
* "Pitches.h" and follows the example code.
* @note IMPORTANT. Use a resistor between the Arduino's pin and the speaker.
* @note IMPORTANT. Really, don't forget to change TOTAL_SPEAKERS or you will
* get strange durations and noises.
* @warning I don't know the speakers number limit.
*/
#ifndef XibStave_h
#define XibStave_h
#include <Arduino.h>
#include <Durations.h>
#include <Pitches.h>
#include <Tone.h>
/** IMPORTANT. Change the number according to the number of stave/speaker
you use. */
#define TOTAL_SPEAKERS 1
/**
* Do NOT change this number, unless you have changed the duration arrays for personal purposes.
*/
#define TOTAL_DURATIONS 14
class XibStave {
public:
XibStave();
~XibStave();
/**
* @brief Continue the music.
*/
void play();
/**
* @brief Pause the music.
*/
void pause();
/**
* @brief Pause the music and set it to the second zero.
*/
void stop();
/**
* @brief Allow you to set loop mode
* @param activateLoop Set to true to get the loop. False to reproduce the
* song only one time. Default:false.
*/
void loop(bool activateLoop);
/**
* @brief Add a new stave to the list
* @param stave Insert the array that act as a stave. Pointer.
* @param staveTotalPitches Stave (array) size. Number of pitches.
* @param pinSpeaker Pin connected to the speaker. Auto set to OUTPUT.
*/
void addStave(uint8_t *stave, uint16_t staveTotalPitches, uint8_t pinSpeaker);
/**
* @brief Initial configure.
*/
void begin();
/**
* @brief Reproduce the music, used inside Arduino's loop().
*/
void run();
/**
* @brief Optional to set other BPM. Default:60.
* @param newBPM Is the new BPM you want.
*/
void setBPM(uint8_t newBPM);
/**
* @brief Optional to set other duration between pitches. Default:40.
* @param newSilenceDuration Is the new duration you want
*/
void setSilenceDuration(uint16_t newSilenceDuration);
private:
/**
* @brief Indicates if the indicated stave has finished.
* @param indexStave Number of the finished stave you want to know.
* @return If true, it has finished. If false, it has NOT finished.
*/
bool staveFinished(uint8_t indexStave);
/**
* @brief Give you the current pitch duration.
* @param stave Stave you want to know the current pitch durantion.
* @return Milliseconds of the duration.
*/
uint16_t getCurrentDurationFromStave(uint8_t stave);
/**
* @brief Give you the current pitch frec.
* @param stave Stave you want to know the current pitch frec.
*/
uint16_t getCurrentPitchFromStave(uint8_t stave);
/**
* @brief Allows to repeat the song if the "inLoop" variable is set to TRUE.
*/
void repeatSong();
bool playing = false;
bool inLoop = false;
bool firstPitch = true;
uint8_t indexTotalSpeakers = 0;
uint8_t *staves[TOTAL_SPEAKERS];
Tone tones[TOTAL_SPEAKERS];
uint16_t currentPitch[TOTAL_SPEAKERS];
uint16_t totalPitches[TOTAL_SPEAKERS];
// uint8_t indexLargestStave = 0;
uint64_t currentTime[TOTAL_SPEAKERS];
uint64_t prevTime[TOTAL_SPEAKERS];
uint16_t pitchesFrec[90] = {
31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58,
62, 65, 69, 73, 78, 82, 87, 93, 98, 104, 110, 117,
123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233,
247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466,
494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932,
988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865,
1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729,
3951, 4186, 4435, 4699, 4978, 1};
const uint16_t minute = 60000;
uint8_t musicBPM = 60;
uint16_t silenceDuration = 40;
uint16_t crotchetBaseTime = minute / musicBPM;
/**
* @see "Durations.h" to see US and UK names, also for add new durations
*/
uint16_t durationsForCalc[TOTAL_DURATIONS] = {
crotchetBaseTime, // NEGRA
crotchetBaseTime * 15 / 10, // NEGRA CON PUNTILLO
crotchetBaseTime * 4, // REDONDA
crotchetBaseTime * 4 * 15 / 10, // REDONDA CON PUNTILLO
crotchetBaseTime * 2, // BLANCA
crotchetBaseTime * 2 * 15 / 10, // BLANCA CON PUNTILLO
crotchetBaseTime / 2, // CORCHEA
crotchetBaseTime / 2 * 15 / 10, // CORCHEA CON PUNTILLO
crotchetBaseTime / 3, // TRESILLO
crotchetBaseTime / 3 * 15 / 10, // TRESILLO CON PUNTILLO
crotchetBaseTime / 4, // SEMICORCHEA
crotchetBaseTime / 4 * 15 / 10, // SEMICORCHEA CON PUNTILLO
crotchetBaseTime / 8, // FUSA
crotchetBaseTime / 8 * 15 / 10 // FUSA CON PUNTILLO
};
uint16_t durations[TOTAL_DURATIONS];
};
#endif