def sound_play_play(): '''Tests race condition case where a very short sound playing could cause a followup wait() to never complete. The curious call to Note() is there because it caused the race codition to occur more reliably frequently. ''' import signal class TimeoutException(Exception): pass def handler(signum, frame): raise TimeoutException() signal.signal(signal.SIGALRM, handler) signal.alarm(5) timeout = False try: Note('A').play().play() Sound(TEST_SOUND).play().wait().play(duration=0.001).wait() except TimeoutException: timeout = True signal.alarm(0) return not timeout
def note_volume_check(note, expected): s = Note(note) actual = s.volume print("Note: ", note) print("Expected: ", expected) print("Actual: ", actual) return actual == expected
def sound_flushing_sound_wont_stop_bugfix(): note = Note('A') start = time.time() note.play(2) time.sleep(1) note.stop() return verify_duration(start, 1)
def note_playing_before_end_of_duration_play(): s = Note('A') s.play(duration=1.0) time.sleep(0.5) playing = s.is_playing() s.stop() return playing
def _note_freq(note, expected_freq): # Frequencies from: http://www.phy.mtu.edu/~suits/notefreqs.html n = Note(note) actual_freq = n.frequency n = None print('Note: {}, expected frequency: {}'.format(note, expected_freq)) print('Acutal frequency: {}'.format(actual_freq)) return abs(expected_freq - actual_freq) < 0.1
def sound_play_test_sound_and_note_mixed(): n = Note('A') s = Sound(TEST_SOUND) n.play(duration=None) s.play() s.wait() n.stop() return True
def sound_manual_play_test_sound_and_note_mixed(): '''The sound match1.wav will play TWO TIMES on the speaker, mixed with an 'A' note. ''' n = Note('A') s = Sound(TEST_SOUND) n.play(duration=None) s.play() s.wait() s.play() s.wait() n.stop()
#!/usr/bin/env python3 from rstem.button import Button from rstem.sound import Note buttons = [Button(22), Button(23), Button(24), Button(27)] notes = [Note('A'), Note('B'), Note('C'), Note('D')] while True: for button, note in zip(buttons, notes): if button.is_pressed(): if not note.is_playing(): note.play(duration=None) else: note.stop()
def note_bad_note(): try: Note('cnasdj') except ValueError: return True return False
''') TILT_FORCE = 0.1 SPACESHIP_STEP = 0.1 # Initialize aliens ALIENS_STEP_TIME = .8 # Initialize missiles fire_button = Button(7) MISSILE_COLOR = 10 MISSILE_STEP_TIME = 0.1 # Initialize sounds fire_sound = Sound("fire.wav") hit_sound = Sound("hit.wav") notes = [Note('B5'), Note('G5'), Note('E5'), Note('C5')] notes_cycle = cycle(notes) while True: fire_button.presses() while True: if scroll(start_text, cancel=fire_button.presses): break spaceship_middle = 1 spaceship_position = fb.width / 2 alien_columns = [0, 1, 2, 3] alien_row = fb.height - 1 alien_start_time = time.time() alien_direction = 1 alien_speed = 2 missile_x, missile_y = -1, -1
from rstem.sound import Note import time accel = Accel() # Calibrate z_rest = 0 SAMPLES = 100 for i in range(SAMPLES): x, y, z = accel.forces() z_rest += z time.sleep(0.01) z_rest /= SAMPLES # Beep to tell user we're starting recording beep = Note('A6') beep.play(0.2).wait() # Record taps TOTAL_TICKS = 2000 raw_tap_ticks = [] for i in range(TOTAL_TICKS): x, y, z = accel.forces() if abs(z_rest - z) > 0.1: raw_tap_ticks.append(i) time.sleep(0.001) # Beep to tell user we've stopped recording beep.play(0.2).wait() # Convert raw ticks to absolute ticks
#!/usr/bin/env python3 import rstem from rstem.button import Button from rstem.gpio import Output from rstem.sound import Note from random import randrange import time buttons = [Button(27), Button(23), Button(24), Button(22)] lights = [Output(4), Output(18), Output(14), Output(15)] notes = [Note('A'), Note('B'), Note('C'), Note('D')] you_failed_note = Note('E2') you_failed_note.volume = 1000 for note in notes: note.volume = 400 for light in lights: light.off() play_order = [] failed = False while not failed: play_order += [randrange(4)] # Play sequence for i in play_order: lights[i].on() notes[i].play(0.4).wait() lights[i].off() time.sleep(0.2)
def note_wait_chainable(): Note('A').wait().wait() return True
def note_stop_chainable(): Note('A').stop().stop() return True
def note_play_chainable(): Note('A').play().play() return True
# ################################## # Import Modules and Initialize Game # ################################## from rstem.button import Button from rstem.gpio import Output from rstem.sound import Note from random import randrange import time buttons = [Button(14), Button(15), Button(23), Button(17)] lights = [Output(4, active_low=False), Output(18, active_low=False), Output(24, active_low=False), Output(27, active_low=False)] notes = [Note('C'), Note('D'), Note('E'), Note('F')] you_failed_note = Note('E3') while True: button_pressed = 0 count = 0 while not button_pressed: for light in lights: if count % 15: light.off() else: light.on() for button in buttons: button_pressed += button.presses() count += 1 time.sleep(0.2) for light in lights:
# ################################## # Import Modules and Initialize Game # ################################## from rstem.button import Button from rstem.gpio import Output from rstem.sound import Note from random import randrange import time from itertools import cycle buttons = [Button(14), Button(15), Button(23), Button(17)] lights = [Output(4), Output(18), Output(24), Output(27)] notes = [Note('C5'), Note('D5'), Note('E5'), Note('F5')] you_failed_note = Note('E4') light_cycle = cycle(lights) while True: for b in buttons: b.presses() pressed = False prev_light = next(light_cycle) cur_light = next(light_cycle) while not pressed: prev_light.off() cur_light.on() prev_light = cur_light cur_light = next(light_cycle) for b in buttons:
def note_play(): n = Note('A') n.play() n.wait() return True
from rstem.mcpi import minecraft, control, block from rstem.sound import Note import time from random import randint from math import log control.show(hide_at_exit=True) mc = minecraft.Minecraft.create() beep = Note('A5') BEEP_DURATION = 0.05 ARENA_WIDTH = 10 GOLD_DEPTH = 2 gold_pos = mc.player.getTilePos() gold_pos.x += randint(-ARENA_WIDTH, ARENA_WIDTH) gold_pos.z += randint(-ARENA_WIDTH, ARENA_WIDTH) gold_pos.y = mc.getHeight(gold_pos.x, gold_pos.z) - GOLD_DEPTH mc.setBlock(gold_pos, block.GOLD_BLOCK) next_beep_time = time.time() while block.Block(mc.getBlock(gold_pos)) == block.GOLD_BLOCK: player_pos = mc.player.getTilePos() vector_to_gold = gold_pos - player_pos vector_to_gold.y = 0 distance_to_gold = vector_to_gold.length() if time.time() > next_beep_time: if distance_to_gold <= 1: if not beep.is_playing(): beep.play(duration=None)
def note_not_playing_after_end_of_duration_play(): s = Note('A') s.play(duration=1.0) time.sleep(1.5) return not s.is_playing()
from rstem.button import Button from rstem.sound import Note import time buttons_and_notes = [ [Button(10), Note('G3')], [Button(3), Note('A3')], [Button(2), Note('B3')], [Button(15), Note('C')], [Button(17), Note('D')], [Button(23), Note('E')], [Button(11), Note('F')], [Button(7), Note('G')], [Button(19), Note('A')], [Button(20), Note('B')], [Button(26), Note('C5')], [Button(21), Note('D5')], ] while True: for button, note in buttons_and_notes: if button.is_pressed(): if not note.is_playing(): note.play(duration=None) else: note.stop() time.sleep(0.01)
from rstem.accel import Accel from rstem.button import Button from rstem.led_matrix import FrameBuffer, Sprite from rstem.sound import Sound, Note import time from itertools import cycle fire_button = Button(25) fire_sound = Sound("fire.wav") hit_sound = Sound("hit.wav") notes = cycle([Note('B3'), Note('G3'), Note('E3'), Note('C3')]) fb = FrameBuffer([(0,0,90)]) accel = Accel() spaceship = Sprite(''' -F- FAF ''') spaceship_middle = 1 spaceship_position = fb.width / 2 alien_columns = [0, 1, 2, 3] alien_row = fb.height - 1 alien_start_time = time.time() alien_direction = 1 alien_speed = 1 ALIENS_STEP_TIME = 0.8 missile_x, missile_y = -1, -1