-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsong.h
69 lines (62 loc) · 2.24 KB
/
song.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
#ifndef SONG_H
#define SONG_H
/**
* Class for simple songs
*
* Class Song is used to play simple tunes on a piezo element. The songs are
* described as simple ASCII text strings in a simplified abc notation (only
* note names, sharps and flats, and note lengths are recognised. No keys,
* repeats, or whitespace. Output on the piezo is done using the tone()
* function. To keep the song playing, its update() function should be called
* every millisecond (i.e. with a frequency of 1 kHz), most likely from a timer
* ISR.
*/
class Song
{
public:
/**
* Constructor
*
* Create a new Song object for the tune described in \a desc. The duration
* in milliseconds of a single note, without length denotation, is given by
* \a note_length. The \a octave argument specifies the base octave of the
* tune; when octave = 4 (the default), an A will be 440Hz.
* \param desc abc description of the tune
* \param note_length length of a single note in ms
* \param octave base octave of the tune
*/
Song(const char* desc, int note_length=250, int octave=4):
_desc(desc), _note_length(note_length), _octave(octave) {}
/// Start or restart playing the tune
void start()
{
_cur_char = _desc;
_cur_ms = 0;
}
/// Stop playing this tune
void stop() { _cur_ms = -1; }
/// Return \c true if this tune has finished playing, \c false otherwise
bool finished() const { return _cur_ms < 0; }
/**
* Update the current playing position
*
* Update the timestamp of the tune. If the current not has finished,
* parse a new note and start playing it. This function should be called
* every millisecond.
*/
void update();
private:
/// Description of the tune to play, in simplified abc notation
const char *_desc;
/// Length of a single note, in ms
int _note_length;
/// Octave offset of the tune
int _octave;
/// Pointer in the description string to the next note to play
const char* _cur_char;
/// Number of ms the current note still needs to play, or -1 when song has finished.
int _cur_ms;
/// Return the length in ms of the next note in the description
int _getLength();
};
#endif // SONG_H