code.py
1 # SPDX-FileCopyrightText: 2018 John Edgar Park for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Plays the 007 theme song 7 Gemma M0 with Piezo on D0 and GND 8 """ 9 10 import time 11 12 import board 13 import pwmio 14 15 piezo = pwmio.PWMOut(board.D0, duty_cycle=0, frequency=440, 16 variable_frequency=True) 17 18 tempo = 2 19 20 # tempo is length of whole note in seconds, e.g. 1.5 21 # set up time signature 22 whole_note = tempo # adjust this to change tempo of everything 23 dotted_whole_note = whole_note * 1.5 24 # these notes are fractions of the whole note 25 half_note = whole_note / 2 26 dotted_half_note = half_note * 1.5 27 quarter_note = whole_note / 4 28 dotted_quarter_note = quarter_note * 1.5 29 eighth_note = whole_note / 8 30 dotted_eighth_note = eighth_note * 1.5 31 sixteenth_note = whole_note / 16 32 33 # set up note values 34 A2 = 110 35 As2 = 117 # 's' stands for sharp: A#2 36 Bb2 = 117 37 B2 = 123 38 39 C3 = 131 40 Cs3 = 139 41 Db3 = 139 42 D3 = 147 43 Ds3 = 156 44 Eb3 = 156 45 E3 = 165 46 F3 = 175 47 Fs3 = 185 48 Gb3 = 185 49 G3 = 196 50 Gs3 = 208 51 Ab3 = 208 52 A3 = 220 53 As3 = 233 54 Bb3 = 233 55 B3 = 247 56 57 C4 = 262 58 Cs4 = 277 59 Db4 = 277 60 D4 = 294 61 Ds4 = 311 62 Eb4 = 311 63 E4 = 330 64 F4 = 349 65 Fs4 = 370 66 Gb4 = 370 67 G4 = 392 68 Gs4 = 415 69 Ab4 = 415 70 A4 = 440 71 As4 = 466 72 Bb4 = 466 73 B4 = 494 74 75 C5 = 523 76 Cs5 = 554 77 Db5 = 554 78 D5 = 587 79 Ds5 = 622 80 Eb5 = 622 81 E5 = 659 82 F5 = 698 83 Fs5 = 740 84 Gb5 = 740 85 G5 = 784 86 Gs5 = 831 87 Ab5 = 831 88 A5 = 880 89 As5 = 932 90 Bb5 = 932 91 B5 = 987 92 93 # here's another way to express the note pitch, double the previous octave 94 C6 = C5 * 2 95 Cs6 = Cs5 * 2 96 Db6 = Db5 * 2 97 D6 = D5 * 2 98 Ds6 = Ds5 * 2 99 Eb6 = Eb5 * 2 100 E6 = E5 * 2 101 F6 = F5 * 2 102 Fs6 = Fs5 * 2 103 Gb6 = Gb5 * 2 104 G6 = G5 * 2 105 Gs6 = Gs5 * 2 106 Ab6 = Ab5 * 2 107 A6 = A5 * 2 108 As6 = As5 * 2 109 Bb6 = Bb5 * 2 110 B6 = B5 * 2 111 112 rst = 24000 # rest is just a tone out of normal hearing range 113 114 Bond01 = [[B3, half_note], 115 [C4, half_note], 116 [Cs4, half_note], 117 [C4, half_note]] 118 119 Bond02 = [[E3, eighth_note], 120 [Fs3, sixteenth_note], 121 [Fs3, sixteenth_note], 122 [Fs3, eighth_note], 123 [Fs3, eighth_note], 124 [Fs3, eighth_note], 125 [E3, eighth_note], 126 [E3, eighth_note], 127 [E3, eighth_note]] 128 129 Bond03 = [[E3, eighth_note], 130 [G3, sixteenth_note], 131 [G3, sixteenth_note], 132 [G3, eighth_note], 133 [G3, eighth_note], 134 [G3, eighth_note], 135 [Fs3, eighth_note], 136 [Fs3, eighth_note], 137 [Fs3, eighth_note]] 138 139 Bond04 = [[E3, eighth_note], 140 [G3, sixteenth_note], 141 [G3, sixteenth_note], 142 [G3, eighth_note], 143 [G3, eighth_note], 144 [G3, eighth_note], 145 [Fs3, eighth_note], 146 [Fs3, eighth_note], 147 [E3, eighth_note]] 148 149 Bond05 = [[Ds4, eighth_note], 150 [D4, eighth_note], 151 [D4, half_note], 152 [B3, eighth_note], 153 [A3, eighth_note], 154 [B3, whole_note]] 155 156 Bond06 = [[E4, eighth_note], 157 [G4, quarter_note], 158 [Ds5, eighth_note], 159 [D5, quarter_note], 160 [D5, eighth_note], 161 [G4, eighth_note], 162 [As4, eighth_note], 163 [B4, eighth_note], 164 [B4, half_note], 165 [B4, quarter_note]] 166 167 Bond07 = [[G4, quarter_note], 168 [A4, sixteenth_note], 169 [G4, sixteenth_note], 170 [Fs4, quarter_note], 171 [Fs4, eighth_note], 172 [B3, eighth_note], 173 [E4, eighth_note], 174 [Cs4, eighth_note], 175 [Cs4, whole_note]] 176 177 Bond08 = [[G4, quarter_note], 178 [A4, sixteenth_note], 179 [G4, sixteenth_note], 180 [Fs4, quarter_note], 181 [Fs4, eighth_note], 182 [B3, eighth_note], 183 [Ds4, eighth_note], 184 [E4, eighth_note], 185 [E4, whole_note]] 186 187 Bond09 = [[E4, eighth_note], 188 [E4, quarter_note], 189 [E4, eighth_note], 190 [Fs4, eighth_note], 191 [Fs4, sixteenth_note], 192 [E4, eighth_note], 193 [Fs4, quarter_note]] 194 195 Bond10 = [ 196 [G4, eighth_note], 197 [G4, quarter_note], 198 [G4, eighth_note], 199 [Fs4, eighth_note], 200 [Fs4, sixteenth_note], 201 [G4, eighth_note], 202 [Fs4, quarter_note]] 203 204 Bond11 = [[B4, eighth_note], 205 [B4, eighth_note], 206 [rst, eighth_note], 207 [B3, eighth_note], 208 [B3, quarter_note], 209 [B4, eighth_note], 210 [B4, eighth_note], 211 [rst, eighth_note], 212 [B3, eighth_note], 213 [B3, quarter_note], 214 [B4, sixteenth_note], 215 [B4, eighth_note], 216 [B4, sixteenth_note], 217 [B4, eighth_note], 218 [B4, eighth_note]] 219 220 Bond12 = [[E3, eighth_note], 221 [G3, quarter_note], 222 [Ds4, eighth_note], 223 [D4, quarter_note], 224 [G3, eighth_note], 225 [B3, quarter_note], 226 [Fs4, eighth_note], 227 [F4, quarter_note], 228 [B3, eighth_note], 229 [D4, quarter_note], 230 [As4, eighth_note], 231 [A4, quarter_note], 232 [F4, eighth_note], 233 [A4, quarter_note], 234 [Ds5, eighth_note], 235 [D5, quarter_note], 236 [rst, eighth_note], 237 [rst, quarter_note], 238 [Fs4, whole_note]] 239 240 241 def song_playback(song): 242 for note in song: 243 piezo.frequency = (note[0]) 244 piezo.duty_cycle = 65536 // 2 # on 50% 245 time.sleep(note[1]) # note duration 246 piezo.duty_cycle = 0 # off 247 time.sleep(0.01) 248 249 250 # this plays the full song roadmap 251 song_playback(Bond01) 252 song_playback(Bond01) 253 song_playback(Bond02) 254 song_playback(Bond03) 255 song_playback(Bond02) 256 song_playback(Bond03) 257 song_playback(Bond02) 258 song_playback(Bond04) 259 song_playback(Bond05) 260 song_playback(Bond06) 261 song_playback(Bond07) 262 song_playback(Bond06) 263 song_playback(Bond08) 264 song_playback(Bond09) 265 song_playback(Bond10) 266 song_playback(Bond09) 267 song_playback(Bond10) 268 song_playback(Bond11) 269 song_playback(Bond01) 270 song_playback(Bond01) 271 song_playback(Bond01) 272 song_playback(Bond01) 273 song_playback(Bond05) 274 song_playback(Bond12)