ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Melody.h
1 /*
2  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef Melody_h
24 #define Melody_h
25 
26 #include <inttypes.h>
27 
28 // Note frequencies from http://arduino.cc/en/Tutorial/Tone
29 #define NOTE_B0 31
30 #define NOTE_C1 33
31 #define NOTE_CS1 35
32 #define NOTE_D1 37
33 #define NOTE_DS1 39
34 #define NOTE_E1 41
35 #define NOTE_F1 44
36 #define NOTE_FS1 46
37 #define NOTE_G1 49
38 #define NOTE_GS1 52
39 #define NOTE_A1 55
40 #define NOTE_AS1 58
41 #define NOTE_B1 62
42 #define NOTE_C2 65
43 #define NOTE_CS2 69
44 #define NOTE_D2 73
45 #define NOTE_DS2 78
46 #define NOTE_E2 82
47 #define NOTE_F2 87
48 #define NOTE_FS2 93
49 #define NOTE_G2 98
50 #define NOTE_GS2 104
51 #define NOTE_A2 110
52 #define NOTE_AS2 117
53 #define NOTE_B2 123
54 #define NOTE_C3 131
55 #define NOTE_CS3 139
56 #define NOTE_D3 147
57 #define NOTE_DS3 156
58 #define NOTE_E3 165
59 #define NOTE_F3 175
60 #define NOTE_FS3 185
61 #define NOTE_G3 196
62 #define NOTE_GS3 208
63 #define NOTE_A3 220
64 #define NOTE_AS3 233
65 #define NOTE_B3 247
66 #define NOTE_C4 262
67 #define NOTE_CS4 277
68 #define NOTE_D4 294
69 #define NOTE_DS4 311
70 #define NOTE_E4 330
71 #define NOTE_F4 349
72 #define NOTE_FS4 370
73 #define NOTE_G4 392
74 #define NOTE_GS4 415
75 #define NOTE_A4 440
76 #define NOTE_AS4 466
77 #define NOTE_B4 494
78 #define NOTE_C5 523
79 #define NOTE_CS5 554
80 #define NOTE_D5 587
81 #define NOTE_DS5 622
82 #define NOTE_E5 659
83 #define NOTE_F5 698
84 #define NOTE_FS5 740
85 #define NOTE_G5 784
86 #define NOTE_GS5 831
87 #define NOTE_A5 880
88 #define NOTE_AS5 932
89 #define NOTE_B5 988
90 #define NOTE_C6 1047
91 #define NOTE_CS6 1109
92 #define NOTE_D6 1175
93 #define NOTE_DS6 1245
94 #define NOTE_E6 1319
95 #define NOTE_F6 1397
96 #define NOTE_FS6 1480
97 #define NOTE_G6 1568
98 #define NOTE_GS6 1661
99 #define NOTE_A6 1760
100 #define NOTE_AS6 1865
101 #define NOTE_B6 1976
102 #define NOTE_C7 2093
103 #define NOTE_CS7 2217
104 #define NOTE_D7 2349
105 #define NOTE_DS7 2489
106 #define NOTE_E7 2637
107 #define NOTE_F7 2794
108 #define NOTE_FS7 2960
109 #define NOTE_G7 3136
110 #define NOTE_GS7 3322
111 #define NOTE_A7 3520
112 #define NOTE_AS7 3729
113 #define NOTE_B7 3951
114 #define NOTE_C8 4186
115 #define NOTE_CS8 4435
116 #define NOTE_D8 4699
117 #define NOTE_DS8 4978
118 
119 // Special note value that indicates a rest.
120 #define NOTE_REST 0
121 
122 class Melody {
123 public:
124  Melody(uint8_t pin);
125 
126  bool isPlaying() const { return playing; }
127 
128  int loopCount() const { return _loopCount; }
129  void setLoopCount(int count) { _loopCount = count; }
130 
131  void setLoopDuration(unsigned long ms);
132 
133  void play();
134  void playOnce();
135  void stop();
136 
137  void setMelody(const int *notes, const uint8_t *lengths, unsigned int size);
138 
139  void run();
140 
141 private:
142  uint8_t _pin;
143  bool playing;
144  int _loopCount;
145  int loopsLeft;
146  const int *notes;
147  const uint8_t *lengths;
148  unsigned int size;
149  unsigned int posn;
150  unsigned long duration;
151  unsigned long startNote;
152 
153  void nextNote();
154 };
155 
156 #endif
void setMelody(const int *notes, const uint8_t *lengths, unsigned int size)
Sets the melody to the size elements of notes and lengths.
Definition: Melody.cpp:199
void stop()
Stops playing the melody.
Definition: Melody.cpp:178
void run()
Runs the melody control loop.
Definition: Melody.cpp:214
bool isPlaying() const
Returns true if the melody is currently playing; false if not.
Definition: Melody.h:126
void playOnce()
Plays the melody once and then stops.
Definition: Melody.cpp:162
Melody(uint8_t pin)
Constructs a new melody playing object for pin.
Definition: Melody.cpp:85
int loopCount() const
Returns the number of times the melody should loop before stopping.
Definition: Melody.h:128
Plays a melody on a digital output pin using tone().
Definition: Melody.h:122
void setLoopDuration(unsigned long ms)
Sets the maximum number of loops to last no longer than ms milliseconds.
Definition: Melody.cpp:131
void play()
Starts playing the melody, or restarts it if already playing.
Definition: Melody.cpp:146
void setLoopCount(int count)
Sets the number of times the melody should loop to count.
Definition: Melody.h:129