def play_song(song_number): # Preloaded with... # 1: Jingle bells # 2: Let It Snow # 3: Linus & Lucy # Choose to play a single song if song_number == 1: # Choose to play Jingle Bells song = jingle_bells # pylint: disable=consider-using-enumerate elif song_number == 2: # Choose to play Let it Snow song = let_it_snow elif song_number == 3: # Choose to play Linus & Lucy song = linus_and_lucy # Play each tone in the chosen song! for n in range(len(song)): # Break song playback if user interrupts with new shake if rolling == False: cpx.start_tone(song[n][0]) time.sleep(song[n][1]) cpx.stop_tone() else: break
def game_of_thrones(): # length of a beat beat = .1 # Frequency of Pitch / # of beats to play pitch phrase1 = [ (g3, 3), (c3, 3), (eb3, 0.5), (f3, 0.5), (g3, 2), (c3, 2), (eb3, .5), (f3, .5), (d3, 12) ] phrase2 = [ (f3, 3), (bb3, 3), (eb3, .5), (d3, .5), (f3, 2), (bb3, 3), (eb3, .5), (d3, .5), (c3, 11) ] notes = [] notes.extend(phrase1) notes.extend(phrase2) tmp = [(raise_octave(p), l) for p, l in notes] notes.extend(tmp) notes.append((25000, 12)) for n in notes: if not cpx.switch and not cpx.button_a: break cpx.start_tone(n[0]) time.sleep(beat * n[1]) cpx.stop_tone()
def change_tone_if_needed(self): """Find the frequency for the current arpeggio and tilt, and restart the tone if changed""" arpeggio_index = 0 if cpx.switch else 1 freq = self.freq_maker.freq(TiltingArpeggios.tilt(), arpeggio_index) if freq != self.last_freq: self.last_freq = freq cpx.stop_tone() cpx.start_tone(freq)
def play_error(): for led in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: cpx.pixels[led] = (255, 0, 0) cpx.start_tone(160) time.sleep(1) cpx.stop_tone() for led in [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9]: cpx.pixels[led] = (0, 0, 0)
def play_song(song_number): # 1: Jingle bells # 2: Let It Snow # set up time signature whole_note = 1.5 # adjust this to change tempo of everything # these notes are fractions of the whole note half_note = whole_note / 2 quarter_note = whole_note / 4 dotted_quarter_note = quarter_note * 1.5 eighth_note = whole_note / 8 # set up note values A3 = 220 Bb3 = 233 B3 = 247 C4 = 262 Db4 = 277 D4 = 294 Eb4 = 311 E4 = 330 F4 = 349 Gb4 = 370 G4 = 392 Ab4 = 415 A4 = 440 Bb4 = 466 B4 = 494 if song_number == 1: # jingle bells jingle_bells_song = [[E4, quarter_note], [E4, quarter_note], [E4, half_note], [E4, quarter_note], [E4, quarter_note], [E4, half_note], [E4, quarter_note], [G4, quarter_note], [C4, dotted_quarter_note], [D4, eighth_note], [E4, whole_note]] for n in range(len(jingle_bells_song)): cpx.start_tone(jingle_bells_song[n][0]) time.sleep(jingle_bells_song[n][1]) cpx.stop_tone() if song_number == 2: # Let It Snow let_it_snow_song = [[B4, dotted_quarter_note], [A4, eighth_note], [G4, quarter_note], [G4, dotted_quarter_note], [F4, eighth_note], [E4, quarter_note], [E4, dotted_quarter_note], [D4, eighth_note], [C4, whole_note]] for n in range(len(let_it_snow_song)): cpx.start_tone(let_it_snow_song[n][0]) time.sleep(let_it_snow_song[n][1]) cpx.stop_tone()
def light_region(region, duration=1): # turn the LEDs for the selected region on for led in REGION_LEDS[region]: cpx.pixels[led] = REGION_COLOR[region] # play a tone for the selected region cpx.start_tone(REGION_TONE[region]) # wait the requested amount of time time.sleep(duration) # stop the tone cpx.stop_tone() # turn the LEDs for the selected region off for led in REGION_LEDS[region]: cpx.pixels[led] = (0, 0, 0)
def tone_piano(): if cpx.touch_A1: cpx.start_tone(262) elif cpx.touch_A2: cpx.start_tone(294) elif cpx.touch_A3: cpx.start_tone(330) elif cpx.touch_A4: cpx.start_tone(349) elif cpx.touch_A5: cpx.start_tone(392) elif cpx.touch_A6: cpx.start_tone(440) elif cpx.touch_A7: cpx.start_tone(494) else: cpx.stop_tone()
def lose_round(): for i in range(10): cpx.stop_tone() if cpx.switch: cpx.start_tone( [220.0,190.0][i%2] ) cpx.pixels.fill((32,0,0)) for j in range(0,10,2): cpx.pixels[j] = (255,0,0) cpx.pixels.show() cpx.pixels.fill((32,0,0)) for j in range(1,10,2): cpx.pixels[j] = (255,0,0) cpx.pixels.show() cpx.pixels.fill(0) cpx.pixels.show() cpx.stop_tone() time.sleep(0.25)
def win_round(): for i in range(11): t = i/10.0 cpx.stop_tone() if cpx.switch: cpx.start_tone( 220.0 * (1.0-t) + 440.0 * t ) cpx.pixels.fill((0,32,0)) for j in range(0,10,2): cpx.pixels[j] = (0,255,0) cpx.pixels.show() cpx.pixels.fill((0,32,0)) for j in range(1,10,2): cpx.pixels[j] = (0,255,0) cpx.pixels.show() cpx.pixels.fill(0) cpx.pixels.show() cpx.stop_tone() time.sleep(0.25)
def piano(): # Set TONE_PIANO to True above to enable a tone piano on the touch pads! if TONE_PIANO: if cpx.touch_A1: cpx.start_tone(262) elif cpx.touch_A2: cpx.start_tone(294) elif cpx.touch_A3: cpx.start_tone(330) elif cpx.touch_A4: cpx.start_tone(349) elif cpx.touch_A5: cpx.start_tone(392) elif cpx.touch_A6: cpx.start_tone(440) elif cpx.touch_A7: cpx.start_tone(494) else: cpx.stop_tone()
def happy_birthday(): notes = [(c4, .75), (c4, .25), (d4, 1), (c4, 1), (f4, 1), (e4, 2), (c4, .75), (c4, .25), (d4, 1), (c4, 1), (g4, 1), (f4, 2), (c4, .75), (c4, .25), (c5, 1), (a4, 1), (f4, 1), (e4, 1), (d4, 1), (bb4, .75), (bb4, .25), (a4, 1), (f4, 1), (g4, 1), (f4, 2), (25000, 8)] beat = 0.4 for i, n in enumerate(notes): if not cpx.switch and not cpx.button_b: break begin = time.monotonic() cpx.start_tone(n[0]) birthday_lights(clear=True) if n[1] >= 1: while time.monotonic() < begin + beat * n[1]: # alt_birthday_lights(n[1]) wheel_lights() else: time.sleep(beat * n[1]) cpx.stop_tone()
def play_sounds(): # A Minor Pentatonic tones = [587, 523, 440, 392, 330, 294, 262] for i in range(1, 8): cpx._touch(i) prev = 0 touched = True while True: yield for i, touch in enumerate(cpx._touches[1:]): if touch.value: if not (i == prev and touched): prev = i cpx.stop_tone() cpx.start_tone(tones[i]) touched = True break else: touched = False
def touch_demo(demo_count): while True: if cpx.touch_A1: cpx.start_tone(330) cpx.pixels[6] = ((255, 0, 0)) print("Touched A1!") elif cpx.touch_A2: cpx.start_tone(394) cpx.pixels[8] = ((255, 0, 0)) print("Touched A2!") elif cpx.touch_A3: cpx.start_tone(294) cpx.pixels[9] = ((255, 0, 0)) print("Touched A3!") elif cpx.touch_A4: cpx.start_tone(262) cpx.pixels[0] = ((255, 0, 0)) print("Touched A4!") elif cpx.touch_A5: cpx.start_tone(440) cpx.pixels[1] = ((255, 0, 0)) print("Touched A5!") elif cpx.touch_A6: cpx.start_tone(494) cpx.pixels[8] = ((255, 0, 0)) print("Touched A6!") elif cpx.touch_A7: cpx.start_tone(523) cpx.pixels[3] = ((255, 0, 0)) print("Touched A7!") elif cpx.button_a or cpx.button_b: while cpx.button_a or cpx.button_b: #debounce time.sleep(0.1) demo_count = demo_count + 1 return demo_count else: cpx.stop_tone() cpx.pixels.fill((0, 0, 0))
for i in range(10): cpx.pixels[i] = ((0, 0, 0)) cpx.start_tone(baseFreq + i) # increasing pitch sweep time.sleep(wait) for i in range(10): cpx.pixels[i] = ((red, green, blue)) time.sleep(wait) # Main loop gets x, y and z axis acceleration, prints the values, and turns on # lights if the UFO is upside down, plays tones while True: R = 0 G = 0 B = 0 x, y, z = cpx.acceleration # read the accelerometer values R = 10 * (R + abs(int(x))) # scale up the accel values into color values G = 10 * (G + abs(int(y))) B = 10 * (B + abs(int(z))) # check for upside down state on z axis if z < 0: # any negative number on z axis means it's upside down enough speed = (0.01 * (B * 0.025)) simple_circle(speed, R, G, B) # speed based on tilt, .01 is good start else: # right side up means no colors or sound! cpx.pixels.fill((0, 0, 0)) cpx.stop_tone()
def stop_note(): cpx.stop_tone()
def play_song(song_number): # 1: Jingle bells # 2: Let It Snow whole_note = 1.5 half_note = whole_note / 2 quarter_note = whole_note / 4 dotted_quarter_note = quarter_note * 1.5 eighth_note = whole_note / 8 A3 = 220 Bb3 = 233 B3 = 247 C4 = 262 Db4 = 277 D4 = 294 Eb4 = 311 E4 = 330 F4 = 349 Gb4 = 370 G4 = 392 Ab4 = 415 A4 = 440 Bb4 = 466 B4 = 494 if song_number == 1: # jingle bells jingle_bells_song = [ [E4, quarter_note], [E4, quarter_note], [E4, half_note], [E4, quarter_note], [E4, quarter_note], [E4, half_note], [E4, quarter_note], [G4, quarter_note], [C4, dotted_quarter_note], [D4, eighth_note], [E4, whole_note], ] for n in range(len(jingle_bells_song)): cpx.start_tone(jingle_bells_song[n][0]) time.sleep(jingle_bells_song[n][1]) cpx.stop_tone() if song_number == 2: # Let It Snow let_it_snow_song = [ [B4, dotted_quarter_note], [A4, eighth_note], [G4, quarter_note], [G4, dotted_quarter_note], [F4, eighth_note], [E4, quarter_note], [E4, dotted_quarter_note], [D4, eighth_note], [C4, whole_note], ] for n in range(len(let_it_snow_song)): cpx.start_tone(let_it_snow_song[n][0]) time.sleep(let_it_snow_song[n][1]) cpx.stop_tone()
def play_error(): cpx.start_tone(160) time.sleep(1) cpx.stop_tone()
# cpx.pixels.brightness = 0.3 # Check the switch if cpx.switch: if last_switch != cpx.switch: # if it moved, print it out print("Switch moved left") else: if last_switch != cpx.switch: print("Switch moved right") last_switch = cpx.switch if TOUCH_PIANO: if cpx.touch_A4: # If we set to play tones cpx.start_tone(524) elif cpx.touch_A5: cpx.start_tone(588) elif cpx.touch_A6: cpx.start_tone(660) elif cpx.touch_A7: cpx.start_tone(698) elif cpx.touch_A1: cpx.start_tone(784) elif cpx.touch_A2: cpx.start_tone(880) elif cpx.touch_A3: cpx.start_tone(988) else: cpx.stop_tone() # nothing touched? turn off the audio # loop to the beginning!
if not cpx.button_a and not cpx.button_b: cpx.pixels.brightness = 0.3 # Check the switch if cpx.switch: if last_switch != cpx.switch: # if it moved, print it out print("Switch moved left") else: if last_switch != cpx.switch: print("Switch moved right") last_switch = cpx.switch if TOUCH_PIANO: if cpx.touch_A4: # If we set to play tones cpx.start_tone(524) elif cpx.touch_A5: cpx.start_tone(588) elif cpx.touch_A6: cpx.start_tone(660) elif cpx.touch_A7: cpx.start_tone(698) elif cpx.touch_A1: cpx.start_tone(784) elif cpx.touch_A2: cpx.start_tone(880) elif cpx.touch_A3: cpx.start_tone(988) else: cpx.stop_tone() # nothing touched? turn off the audio # loop to the beginning!
def stop_playing(): global last_tone cpx.stop_tone() last_tone = 0
def play(tone): global last_tone if last_tone != tone or last_tone == 0: cpx.stop_tone() cpx.start_tone(tone) last_tone = tone
def play_scale(scale): for n in range(8): cpx.start_tone(scale[n]) time.sleep(0.5) cpx.stop_tone()
from adafruit_circuitplayground.express import cpx while True: if cpx.button_a: cpx.start_tone(262) elif cpx.button_b: cpx.start_tone(294) else: cpx.stop_tone()