ArduinoLibs
|
00001 /* 00002 * Copyright (C) 2012 Southern Storm Software, Pty Ltd. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00020 * DEALINGS IN THE SOFTWARE. 00021 */ 00022 00023 #ifndef Melody_h 00024 #define Melody_h 00025 00026 #include <inttypes.h> 00027 00028 // Note frequencies from http://arduino.cc/en/Tutorial/Tone 00029 #define NOTE_B0 31 00030 #define NOTE_C1 33 00031 #define NOTE_CS1 35 00032 #define NOTE_D1 37 00033 #define NOTE_DS1 39 00034 #define NOTE_E1 41 00035 #define NOTE_F1 44 00036 #define NOTE_FS1 46 00037 #define NOTE_G1 49 00038 #define NOTE_GS1 52 00039 #define NOTE_A1 55 00040 #define NOTE_AS1 58 00041 #define NOTE_B1 62 00042 #define NOTE_C2 65 00043 #define NOTE_CS2 69 00044 #define NOTE_D2 73 00045 #define NOTE_DS2 78 00046 #define NOTE_E2 82 00047 #define NOTE_F2 87 00048 #define NOTE_FS2 93 00049 #define NOTE_G2 98 00050 #define NOTE_GS2 104 00051 #define NOTE_A2 110 00052 #define NOTE_AS2 117 00053 #define NOTE_B2 123 00054 #define NOTE_C3 131 00055 #define NOTE_CS3 139 00056 #define NOTE_D3 147 00057 #define NOTE_DS3 156 00058 #define NOTE_E3 165 00059 #define NOTE_F3 175 00060 #define NOTE_FS3 185 00061 #define NOTE_G3 196 00062 #define NOTE_GS3 208 00063 #define NOTE_A3 220 00064 #define NOTE_AS3 233 00065 #define NOTE_B3 247 00066 #define NOTE_C4 262 00067 #define NOTE_CS4 277 00068 #define NOTE_D4 294 00069 #define NOTE_DS4 311 00070 #define NOTE_E4 330 00071 #define NOTE_F4 349 00072 #define NOTE_FS4 370 00073 #define NOTE_G4 392 00074 #define NOTE_GS4 415 00075 #define NOTE_A4 440 00076 #define NOTE_AS4 466 00077 #define NOTE_B4 494 00078 #define NOTE_C5 523 00079 #define NOTE_CS5 554 00080 #define NOTE_D5 587 00081 #define NOTE_DS5 622 00082 #define NOTE_E5 659 00083 #define NOTE_F5 698 00084 #define NOTE_FS5 740 00085 #define NOTE_G5 784 00086 #define NOTE_GS5 831 00087 #define NOTE_A5 880 00088 #define NOTE_AS5 932 00089 #define NOTE_B5 988 00090 #define NOTE_C6 1047 00091 #define NOTE_CS6 1109 00092 #define NOTE_D6 1175 00093 #define NOTE_DS6 1245 00094 #define NOTE_E6 1319 00095 #define NOTE_F6 1397 00096 #define NOTE_FS6 1480 00097 #define NOTE_G6 1568 00098 #define NOTE_GS6 1661 00099 #define NOTE_A6 1760 00100 #define NOTE_AS6 1865 00101 #define NOTE_B6 1976 00102 #define NOTE_C7 2093 00103 #define NOTE_CS7 2217 00104 #define NOTE_D7 2349 00105 #define NOTE_DS7 2489 00106 #define NOTE_E7 2637 00107 #define NOTE_F7 2794 00108 #define NOTE_FS7 2960 00109 #define NOTE_G7 3136 00110 #define NOTE_GS7 3322 00111 #define NOTE_A7 3520 00112 #define NOTE_AS7 3729 00113 #define NOTE_B7 3951 00114 #define NOTE_C8 4186 00115 #define NOTE_CS8 4435 00116 #define NOTE_D8 4699 00117 #define NOTE_DS8 4978 00118 00119 // Special note value that indicates a rest. 00120 #define NOTE_REST 0 00121 00122 class Melody { 00123 public: 00124 Melody(uint8_t pin); 00125 00126 bool isPlaying() const { return playing; } 00127 00128 int loopCount() const { return _loopCount; } 00129 void setLoopCount(int count) { _loopCount = count; } 00130 00131 void setLoopDuration(unsigned long ms); 00132 00133 void play(); 00134 void playOnce(); 00135 void stop(); 00136 00137 void setMelody(const int *notes, const uint8_t *lengths, unsigned int size); 00138 00139 void run(); 00140 00141 private: 00142 uint8_t _pin; 00143 bool playing; 00144 int _loopCount; 00145 int loopsLeft; 00146 const int *notes; 00147 const uint8_t *lengths; 00148 unsigned int size; 00149 unsigned int posn; 00150 unsigned long duration; 00151 unsigned long startNote; 00152 00153 void nextNote(); 00154 }; 00155 00156 #endif