def play(filename_or_url, volume=None, loop=False, sync_beat=None, start_at_next=None, on_finished=None): if volume is None: try: volume = NVS('system').get_i32('volume') except: volume = 255 _start_audio_if_needed() channel_id = _add_channel(filename_or_url, on_finished) if channel_id is None or channel_id < 0: print('Failed to start audio channel') return channel_id sndmixer.volume(channel_id, volume) if loop: sndmixer.loop(channel_id, True) if sync_beat is not None and start_at_next is not None: sndmixer.start_beat_sync(sync_beat) sndmixer.start_at_next(channel_id, start_at_next) else: sndmixer.play(channel_id) return channel_id
def _add_channel(filename_or_url, on_finished=None): global handles stream = True is_url = filename_or_url.startswith('http') if is_url: file = urequests.get(filename_or_url).raw else: if not os.path.isfile(filename_or_url): print('File %s does not exist' % filename_or_url) return -1 file = open(filename_or_url, 'rb') lower = filename_or_url.lower() if(lower.endswith('.mp3')): channel_id = sndmixer.mp3_stream(file) elif(lower.endswith('.wav')): channel_id = sndmixer.wav_stream(file) elif(lower.endswith('.ogg') or lower.endswith('.opus')): channel_id = sndmixer.opus_stream(file) # No support for looping yet elif(lower.endswith('.mod') or lower.endswith('.s3m') or lower.endswith('.xm')): channel_id = sndmixer.mod(file.read()) # No support for streaming mod files or looping yet stream = False else: print('Audio: unknown filetype') channel_id = -1 if channel_id < 0: return -1 if is_url: # Needed when streaming from HTTP sockets sndmixer.play(channel_id) def finish_callback(_): _clean_channel(channel_id) if on_finished is not None: try: on_finished() except: pass handles[channel_id] = file if stream: sndmixer.on_finished(channel_id, finish_callback) return channel_id
def play(index): global global_playing, global_file, global_channels, global_filenames, MAX_FILES if index < 0 or index > MAX_FILES: print("Play: invalid index") if global_playing[index]: print("Play: already playing") return channel = sndmixer.mp3_stream(global_file[index]) if not channel: print("Play: invalid channel id") return try: volume = machine.nvs_getint("system", "volume") or 255 print("Volume:",volume) sndmixer.volume(channel, volume) sndmixer.play(channel) except: pass global_channels[index] = channel global_playing[index] = True
def on_key(key_index, is_pressed): global pattern if is_pressed: x, y = keypad.index_to_coords(key_index) pattern[x + y * 4] = (pattern[x + y * 4] + 1) % 4 if pattern[x + y * 4] == 0: display.drawPixel(x, y, 0x400000) tone = 396 elif pattern[x + y * 4] == 1: display.drawPixel(x, y, 0x004000) tone = 440 elif pattern[x + y * 4] == 2: display.drawPixel(x, y, 0x004040) tone = 495 elif pattern[x + y * 4] == 3: display.drawPixel(x, y, 0x404000) tone = 528 display.flush() sndmixer.freq(synth, tone) sndmixer.play(synth) sndmixer.volume(synth, 64) virtualtimers.new(100, sound_off, False)