def playFile(filename): wave_file = open(filename, "rb") with WaveFile(wave_file) as wave: with AudioOut(board.SPEAKER) as audio: audio.play(wave) while audio.playing: pass
def _init_wave_files(self, files, directory): """Open files from AUDIO_DIR and return a dict with FileIO objects or None if file not present.""" # 2048 triggers bug in https://github.com/adafruit/circuitpython/issues/3030 self._file_buf = bytearray( 512) # DO NOT CHANGE size til #3030 is fixed missing = [] fhs = {} for file in files: wav_file = None filename = directory + "/" + file + ".wav" try: wav_file = open(filename, "rb") fhs[file] = WaveFile(wav_file, self._file_buf) except OSError: # OSError: [Errno 2] No such file/directory: 'filename.ext' missing.append(filename) # Raises an exception at the end to allow it to report ALL # of the missing files in one go to help out the user if missing: raise SampleJukeboxError(missing) self._wave_files = fhs
def playAudioFile(filename): global audio wave_file = open(filename, "rb") wave = WaveFile(wave_file) audio.play(wave) while audio.playing: pass audio.stop()
def playfile(filename): wave_file = open("/sounds/" + filename, "rb") with WaveFile(wave_file) as wave: with AudioOut(board.SPEAKER) as audio: audio.play(wave) while audio.playing: brightPulse() cp.pixels.fill(BLACK)
def playfile(filename): wave_file = open(filename, "rb") with WaveFile(wave_file) as wave: with AudioOut(board.SPEAKER) as audio: audio.play(wave) moveUp() while audio.playing: move() moveBack()
def play_file(filename): print("Playing file: " + filename) wave_file = open(filename, "rb") with WaveFile(wave_file) as wave: with AudioOut(board.SPEAKER) as audio: audio.play(wave) while audio.playing: pass print("Finished")
def play_file(filename): print("Playing file: " + filename) wave_file = open(filename, "rb") with WaveFile(wave_file) as wave: audio.play(wave) while audio.playing: pass wave_file.close() print("Finished")
def play_message(): """Play recorded WAV message and transmit to I2S amp.""" with open("d1.wav", "rb") as file: wave = WaveFile(file) i2s = I2SOut(TX, RX, D9) i2s.play(wave) while i2s.playing: pass wave.deinit() i2s.deinit()
def lightSaberOn(): i = 0 enable.value = True wave_file = open(audiofiles[0], "rb") redled.value = True with WaveFile(wave_file) as wave: audio.play(wave) while audio.playing: neopixels[i] = (0,0,255) i = (i + 1) % NUM_PIXELS wave_file.close() print("Finished")
def flashOnClash(): global clashNum wave_file = open(audiofiles[clashNum], "rb") with WaveFile(wave_file) as wave: audio.play(wave) neopixels.fill((255,255,255)) led.fill((255,255,255)) while audio.playing: neopixels.fill((0,0,255)) led.fill((0,0,255)) wave_file.close() clashNum = random.randint(2,5)
def play_file(number): # The two files assigned to buttons A & B audiofiles = ["eins.wav", "zwei.wav", "drei.wav", "vier.wav", "funf.wav", "sechs.wav"] filename = audiofiles[number] print("Playing file: " + filename) wave_file = open(filename, "rb") with WaveFile(wave_file) as wave: with AudioOut(board.SPEAKER) as audio: audio.play(wave) while audio.playing: pass print("Finished")
def lightSaberOff(): wave_file = open(audiofiles[1], "rb") redled.value = False i = NUM_PIXELS - 1 with WaveFile(wave_file) as wave: audio.play(wave) while audio.playing: neopixels[i] = (0,0,0) i = (i - 1) if i < 0: i = 0 wave_file.close() enable.value = False print("Finished")
def play_wav(name, loop=False): """ Play a WAV file in the 'sounds' directory. :param name: partial file name string, complete name will be built around this, e.g. passing 'foo' will play file 'sounds/foo.wav'. :param loop: if True, sound will repeat indefinitely (until interrupted by another sound). """ global wave_file # pylint: disable=global-statement print("playing", name) if wave_file: wave_file.close() try: wave_file = open('sounds/' + name + '.wav', 'rb') wave = WaveFile(wave_file) audio.play(wave, loop=loop) except OSError: pass # we'll just skip playing then
def _init_wave_files(self, files, directory): """Open files from AUDIO_DIR and return a dict with FileIO objects or None if file not present.""" ### 2048 triggers bug in https://github.com/adafruit/circuitpython/issues/3030 self._file_buf = bytearray( 512) ### DO NOT CHANGE size til #3030 is fixed error_output = None fhs = {} for file in files: wav_file = None filename = directory + "/" + file + ".wav" try: wav_file = open(filename, "rb") except OSError: ### OSError: [Errno 2] No such file/directory: 'filename.ext' if error_output: print("ERROR: missing audio file:", filename) fhs[file] = WaveFile(wav_file, self._file_buf) self._wave_files = fhs
adb_uart = busio.UART(board.GP16, board.GP17, baudrate=115200) scanner_left = 1 scanner_right = 2 PWMAUDIO_CLASH_PINS = () else: ### TODO - PIO PWM would be nice to avoid losing GP2 GP3 for audio AUDIO_PIN_L = board.GP18 AUDIO_PIN_R = board.GP19 audio_out = AudioOut(AUDIO_PIN_L, right_channel=AUDIO_PIN_R) left_file = open("scanner-left-16k.wav", "rb") right_file = open("scanner-right-16k.wav", "rb") ### This non pops a bit on 7.0.0-alpha.6 and after 10-100 minutes ### the audio stops and both audio pwm get stuck perhaps at ### a lowish duty cycle scanner_left = WaveFile(left_file) scanner_right = WaveFile(right_file) ### The use of GP18 also effectively reserves GP19 ### and the RP2040 hardware cannot then offer PWM on GP2 and GP3 ### PIO PWM could be a solution here when it works in CircuitPython PWMAUDIO_CLASH_PINS = (board.GP2, board.GP3, board.GP18, board.GP19) ### Pins and vertical displacement left_pins = ((board.GP0, 0), (board.GP1, 1), # gap (board.GP2, 3), ### clash with GP18 audio due to PWM architecture (board.GP3, 4), ### clash with GP18 audio due to PWM architecture (board.GP4, 5),
import time import random from board import * from digitalio import DigitalInOut, Direction from audiopwmio import PWMAudioOut as AudioOut from audiocore import WaveFile ### Maker Pi Pico has small speaker (left channel) on GP18 audio_out = AudioOut(GP18) ### Audio is part of tack00's https://freesound.org/people/tack00/sounds/399257/ DRIP_FILENAME = "one-drip-16k.wav" try: drip = WaveFile(open(DRIP_FILENAME, "rb")) except OSError: print("Missing audio file:", DRIP_FILENAME) drip = None animation_step = 0.1 ### tenth of a second ### Each "frame" in animation is either a list of pins ### to illuminate or a sound sample to start playing animation = [[GP0], [GP0], [GP0], [GP0], [GP0], [GP1], [], # gap [GP2],
# Debouncer ------------------------------------------------------ buttons = [Debouncer(mpr121[i]) for i in range(12)] # Audio Setup ------------------------------------------------------ spkr_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) spkr_enable.direction = digitalio.Direction.OUTPUT spkr_enable.value = True audio = AudioOut(board.SPEAKER) tracks = ( WaveFile(open("sounds/F2.wav", "rb")), # 0 WaveFile(open("sounds/G2.wav", "rb")), # 1 WaveFile(open("sounds/A2.wav", "rb")), # 2 WaveFile(open("sounds/Bb2.wav", "rb")), # 3 WaveFile(open("sounds/C2.wav", "rb")), # 4 WaveFile(open("sounds/D3.wav", "rb")), # 5 WaveFile(open("sounds/E3.wav", "rb")), # 6 WaveFile(open("sounds/F3.wav", "rb")), # 7 WaveFile(open("sounds/F1.wav", "rb")), # 7 WaveFile(open("sounds/G1.wav", "rb")), # 8 WaveFile(open("sounds/A1.wav", "rb")), # 9 WaveFile(open("sounds/Bb1.wav", "rb")), # 10 WaveFile(open("sounds/C1.wav", "rb")), # 11 WaveFile(open("sounds/D2.wav", "rb")), # 12 WaveFile(open("sounds/E2.wav", "rb")), # 13 WaveFile(open("sounds/F2.wav", "rb")), # 13
audio = AudioOut(board.TX) # PWM out pin mixer = audiomixer.Mixer( voice_count=4, sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True, ) audio.play(mixer) mixer.voice[0].level = 1.0 # dial tone voice mixer.voice[1].level = 1.0 # touch tone voice mixer.voice[2].level = 0.0 # song/message voice mixer.voice[3].level = 0.0 # busy signal wave_file0 = open(dial_tone, "rb") wave0 = WaveFile(wave_file0) mixer.voice[0].play(wave0, loop=True) # play dial tone wave_file2 = open(wrong_number, "rb") wave2 = WaveFile(wave_file2) wave_file3 = open(busy_signal, "rb") wave3 = WaveFile(wave_file3) mixer.voice[3].play(wave3, loop=True) # play dial tone def reset_number(): # pylint: disable=global-statement global digits_entered, dialed, dialed_str digits_entered = 0 dialed = []
scan_response = Advertisement() ble = adafruit_ble.BLERadio() if ble.connected: for c in ble.connections: c.disconnect() if ble_enabled.value: print("advertising") ble.start_advertising(advertisement, scan_response) k = Keyboard(hid.devices) kl = KeyboardLayoutUS(k) wave_file = open("jeopardy.wav", "rb") wave = WaveFile(wave_file) audio = AudioOut(board.SPEAKER) while True: if ble_enabled.value: while not ble.connected: pass if ble.connected: print("Connected") led.value = True time.sleep(0.1) led.value = False time.sleep(0.1) led.value = True time.sleep(0.1) led.value = False
### has completed playing mixer = Mixer(voice_count=2, sample_rate=16000, channel_count=2, bits_per_sample=16, samples_signed=True) wav_files = ("scanner-left-16k.wav", "scanner-right-16k.wav") ### Use same pins which would be used on a Feather M4 with real DACs AUDIO_PIN_L = board.A0 AUDIO_PIN_R = board.A1 audio_out = AudioOut(AUDIO_PIN_L, right_channel=AUDIO_PIN_R) wav_fh = [open(fn, "rb") for fn in wav_files] wavs = [WaveFile(fh) for fh in wav_fh] ### Voice 0 behaves strangely ### https://github.com/adafruit/circuitpython/issues/3210 mixer.voice[0].level = 0.0 mixer.voice[1].level = 1.0 audio_out.play(mixer) audio = mixer.voice[1] uart = busio.UART(board.TX, board.RX, baudrate=115200) rx_bytes = bytearray(1) while True: if uart.readinto(rx_bytes) and rx_bytes[0]: try:
# list of all waveform types WAVE_TYPES = ["sine", "square", "sawtooth", "triangle"] # current waveform type. Will get changed from the last column current_wave_type = "sine" # load the notes dictionary for wave_type in WAVE_TYPES: for octave in range(3, 6): # [3,4,5] for note_letter in note_letters: # note with octave e.g. a4 cur_note = "{}{}".format(note_letter, octave) # add wave file to dictionary key = "{}{}".format(wave_type, cur_note) notes[key] = WaveFile( open("notes/{}/{}.wav".format(wave_type, cur_note), "rb")) # main audio object audio = AudioOut(left_channel=board.A0, right_channel=board.A1) # mixer to allow pylyphonic playback mixer = Mixer(voice_count=8, sample_rate=8000, channel_count=2, bits_per_sample=16, samples_signed=True) audio.play(mixer) # turn on the rainbow lights for i, color in enumerate(colors): trellis.pixels[i, 0] = color
"com.apple.mobilephone": "/ancs_phone.bmp" } BLOCKLIST = [] DELAY_AFTER_PRESS = 15 DEBOUNCE = 0.1 DIM_TIMEOUT = 20 # Amount of timeout to turn off backlight DIM_LEVEL = 0.05 a = digitalio.DigitalInOut(board.BUTTON_A) a.switch_to_input(pull=digitalio.Pull.DOWN) b = digitalio.DigitalInOut(board.BUTTON_B) b.switch_to_input(pull=digitalio.Pull.DOWN) file = open("/triode_rise.wav", "rb") wave = WaveFile(file) def play_sound(): audio.play(wave) time.sleep(1) def find_connection(): for connection in radio.connections: if AppleNotificationCenterService not in connection: continue if not connection.paired: connection.pair() return connection, connection[AppleNotificationCenterService] return None, None
def play_file(filename): print("playing file " + filename) file = open(filename, "rb") wave = WaveFile(file) audio.play(wave) time.sleep(bpm / 960) # Sixteenth note delay
# To simplify the build, each key is wired to a separate input pin rather # than making an X/Y matrix. CircuitPython's keypad module is still used # (treating the buttons as a 1x10 matrix) as this gives us niceties such # as background processing, debouncing and an event queue! keys = keypad.Keys([x[0] for x in pin_to_wave], value_when_pressed=False, pull=True) event = keypad.Event() # Single key event for re-use keys.events.clear() # Load all the WAV files from the pin_to_wave list, and one more for the # mode selector, sharing a common buffer since only one is used at a time. # Also, play a startup sound. audio_buf = bytearray(1024) waves = [ WaveFile(open(sound_folder + "/" + x[1], "rb"), audio_buf) for x in pin_to_wave ] active_sound = 0 # Index of waves[] to play when trigger is pressed selector_wave = WaveFile(open(sound_folder + "/" + "click.wav", "rb"), audio_buf) audio.play(WaveFile(open(sound_folder + "/" + "startup.wav", "rb"), audio_buf)) # MAIN LOOP --------- repeat forever ---- while True: # Process the mode selector slider, check if moved into a new position. # This is currently just used to make click noises, it doesn't actually # change any "mode" in the operation of the prop, but it could if we # really wanted, with additional code (e.g. different sound sets).