Skip to content

Commit d86c7bc

Browse files
Change the method to calculate a note duration.
1 parent 08b6107 commit d86c7bc

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

BGMusic.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static CBTimer cbtimer;
2222

2323
// Initialize static data menbers
2424
MusicScore_t BGMusic::score = { 0, 0, 0, 0, false };
25-
int (*BGMusic::duration_function)(int wholenote, int duration) = nullptr;
25+
int (*BGMusic::duration_function)(int duration) = nullptr;
2626

2727
// Constructor / Destructor
2828
BGMusic::BGMusic() {}
@@ -92,7 +92,7 @@ void BGMusic::music_callback(void) {
9292

9393
else {
9494
// you can select the original method
95-
duration = duration_function(score.wholenote, duration);
95+
duration = duration_function(duration);
9696
}
9797

9898
debug_print("duration = "); debug_println(duration);

BGMusic.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BGMusic {
3737
private:
3838
static MusicScore_t score;
3939
static void music_callback(void);
40-
static int (*duration_function)(int wholenote, int duration);
40+
static int (*duration_function)(int duration);
4141

4242
public:
4343
BGMusic();
@@ -47,7 +47,7 @@ class BGMusic {
4747
static bool stop(void);
4848
void end(void);
4949

50-
void set_duration_function(int (*calc_duration_function)(int wholenote, int duration)) {
50+
void set_duration_function(int (*calc_duration_function)(int duration)) {
5151
duration_function = calc_duration_function;
5252
}
5353
};

README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,19 @@ void loop() {
9090

9191
## API
9292

93-
### bool begin(int pin, int notes[], int n_notes, int tempo, bool loop)
93+
### bool begin(int pin, const int notes[], int n_notes, int tempo, bool loop = false, bool start = false)
9494

95-
- int pin ... the pin number connected to piezo buzzer or a speaker.
95+
- `int pin` ... the pin number connected to piezo buzzer or a speaker.
9696

97-
- int notes[] ... An array of note that is a pair of pitch frequency and duration.
97+
- `const int notes[]` ... An array of musical notes made up of flat pairs of frequency and duration. (same format as in [arduino-songs](https://github.com/robsoncouto/arduino-songs "robsoncouto/arduino-songs") by [Robson Couto](https://github.com/robsoncouto "robsoncouto (Robson Couto)"))
9898

99-
- int n_notes ... Number of notes in the array. This is half the number of elements in the array.
99+
- `int n_notes` ... Number of notes in the array. This is half the number of elements in the array.
100100

101-
- int tempo ... The metronome number. For example, `60` means **60 quarter-note beats per minute**.
101+
- `int tempo` ... The metronome number. For example, `60` means **60 quarter-note beats per minute**.
102102

103-
- bool loop ... If `true`, play repeatedly.
103+
- `bool loop` ... When `true`, play repeatedly.
104+
105+
- `bool start` ... When `true`, play immediately.
104106

105107
### bool start(void)
106108
Start playing after calling the "begin" method.
@@ -111,7 +113,7 @@ Stop playing.
111113
### void end(void)
112114
Calling this method followed by `stop()` will release the timer resource.
113115

114-
### void set_duration_function(int (*calc_duration_function)(int wholenote, int duration))
116+
### void set_duration_function(int (*calc_duration_function)(int duration))
115117

116118
Register a function to calculate the duration of `tone()`. The default calculating method is as follows:
117119

@@ -143,9 +145,12 @@ As an alternative, you can define some symbols as follows:
143145
And register your function of calculating duration like this:
144146

145147
```C++
148+
#define TEMPO 155 // change this to make the song slower or faster
149+
#define QUARTER_NOTE ((60000 / N4) / TEMPO) // quarter note duration in milliseconds based on the number of beats in 60 second
150+
146151
// Calculate note length from note length symbol
147152
int calc_duration(int wholenote, int duration) {
148-
return (wholenote / 16) * duration;
153+
return QUARTER_NOTE * duration;
149154
}
150155
...
151156
void setup() {

0 commit comments

Comments
 (0)