Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio: handle looping and large file playback better. #3

Merged
merged 8 commits into from
Aug 1, 2024

Conversation

Gadgetoid
Copy link
Member

@Gadgetoid Gadgetoid commented Jul 30, 2024

  • Stops large files from crashing my Pico, since anything >10k would fail to write audio data on callback
  • Special case looped playback to fill a whole buffer with copies of the wav file, ensures a gapless loop (in theory)
  • Implement a WavReader class to abstract away the nuances of offset and data length (avoid playing metadata as audio)
  • Add a deinit() method to WavPlayer which MUST be called to avoid hard crashes on soft resets

Examples:

import os
import time
from tiny_fx import TinyFX
from audio_new import WavPlayer
import random

wav = WavPlayer(0, TinyFX.I2S_BCLK, TinyFX.I2S_LRCLK, TinyFX.I2S_DATA, amp_enable=TinyFX.AMP_EN_PIN, root="/")

wav.play_wav("440Hz_44100Hz_16bit_05sec.wav", loop=True)

try:
    while True:
        time.sleep(1.0)
        print(wav._loop_count)
finally:
    wav.deinit()

Single shot playback:

import os
import time
from tiny_fx import TinyFX
from audio_new import WavPlayer
import random

wav = WavPlayer(0, TinyFX.I2S_BCLK, TinyFX.I2S_LRCLK, TinyFX.I2S_DATA, amp_enable=TinyFX.AMP_EN_PIN, root="/sounds")

files = os.listdir("/sounds")

try:
    while True:
        file = random.choice(files)
        wav.play_wav(file)
        print(f"Playing: {file}")
        while wav.is_playing():
            time.sleep(0.1)
        print("done!")
        time.sleep(0.5)
except KeyboardInterrupt:
    pass
finally:
    wav.deinit()

This *must* be called in a `finally:` clause around any audio playback,
to avoid hard crashes on soft reset.
@Gadgetoid
Copy link
Member Author

Demo for wave shapes:

import os
import time
from tiny_fx import TinyFX
from audio import WavPlayer
import random

wav = WavPlayer(0, TinyFX.I2S_BCLK, TinyFX.I2S_LRCLK, TinyFX.I2S_DATA, amp_enable=TinyFX.AMP_EN_PIN)
tinyfx = TinyFX()

tones = [440, 440, 440, 0]
durations = [1.0, 1.0, 1.0, 2.0]
shapes = [wav.TONE_SINE, wav.TONE_SQUARE, wav.TONE_TRIANGLE, 0]
leds = [1, 2, 3, 0]

n = 0

try:
    while True:
        tone = tones[n]
        duration = durations[n]
        shape = shapes[n]
        led = leds[n]
        if tone:
            wav.play_tone(tone, 1.0, shape)
        tinyfx.clear()
        if led:
            tinyfx.outputs[led - 1].on()
        time.sleep(duration)
        wav.stop()
        time.sleep(0.1)
        n += 1
        n %= len(tones)
except KeyboardInterrupt:
    pass
finally:
    wav.deinit()

@ZodiusInfuser ZodiusInfuser merged commit 982ddfa into main Aug 1, 2024
8 checks passed
@ZodiusInfuser ZodiusInfuser deleted the patch-audio branch October 8, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants