Esempio n. 1
0
    def play_mp3(self, file_name):
        """ Play a .mp3 file using the onboard speaker.

        :param file_name: The name of your .mp3 file in quotation marks including .mp3

        .. image :: ../docs/_static/speaker.jpg
          :alt: Onboard speaker

        To use with the Circuit Playground Bluefruit:

        .. code-block:: python

             from adafruit_circuitplayground import cp

             while True:
                 if cp.button_a:
                     cp.play_mp3("laugh.mp3")
                 elif cp.button_b:
                     cp.play_mp3("rimshot.mp3")
        """
        if file_name.lower().endswith(".mp3"):
            # Play a specified file.
            self.stop_tone()
            self._speaker_enable.value = True
            with self._audio_out(
                board.SPEAKER
            ) as audio:  # pylint: disable=not-callable
                mp3file = audiomp3.MP3Decoder(open(file_name, "rb"))
                audio.play(mp3file)
                while audio.playing:
                    pass
            self._speaker_enable.value = False
        else:
            raise ValueError("Filetype must be mp3")
Esempio n. 2
0
        if idx >= 1:
            result = all_folders[idx - 1]
            self.next_choice = idx + 1
            if self.next_choice == len(choices):
                self.next_choice = 1  # Go to first folder, not "surprise me"
        else:
            result = random.choice(all_folders)
        return join(base, result)


# pylint: disable=invalid-name
enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True
speaker = audioio.AudioOut(board.SPEAKER, right_channel=board.A1)
mp3stream = audiomp3.MP3Decoder(open("/rsrc/splash.mp3", "rb"))
speaker.play(mp3stream)

font = adafruit_bitmap_font.bitmap_font.load_font("rsrc/5x8.bdf")
playback_display = PlaybackDisplay()
board.DISPLAY.show(playback_display.group)
font.load_glyphs(range(32, 128))

BUTTON_SEL = const(8)
BUTTON_START = const(4)
BUTTON_A = const(2)
BUTTON_B = const(1)

joystick = analogjoy.AnalogJoystick()

up_key = repeat.KeyRepeat(lambda: joystick.up, rate=0.2)
        prettyprintname += file
        if isdir:
            prettyprintname += "/"
        print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))

        # recursively print directory contents
        if isdir:
            print_directory(path + "/" + file, tabs + 1)


print("Files on filesystem:")
print("====================")
print_directory("/sd")

data = open("/sd/cheer.mp3", "rb")
mp3 = audiomp3.MP3Decoder(data)
#a = audioio.AudioOut(board.A0) # mono
a = audioio.AudioOut(board.A0,
                     right_channel=board.A1)  # stereo sound through A0 & A1

i2c = io.I2C(board.SCL,
             board.SDA)  # Change to the appropriate I2C clock & data
# pins here!

# Create the RTC instance:
#rtc = adafruit_ds3231.DS3231(i2c)
rtc = adafruit_pcf8523.PCF8523(i2c)

# Lookup table for names of days (nicer printing).
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
        "Saturday")
Esempio n. 4
0
# SPDX-License-Identifier: Unlicense
"""
CircuitPython I2S MP3 playback example.
Plays an MP3 once.

Remove this line and all of the following docstring content before submitting to the Learn repo.

Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of
a proper I2S pin combination, run the pin combination script found here:
https://adafru.it/i2s-pin-combo-finder

Update the following pin names to a viable pin combination:
* BIT_CLOCK_PIN
* WORD_SELECT_PIN
* DATA_PIN
"""
import board
import audiomp3
import audiobusio

audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN,
                          board.DATA_PIN)

mp3 = audiomp3.MP3Decoder(open("slow.mp3", "rb"))

audio.play(mp3)
while audio.playing:
    pass

print("Done playing!")
Esempio n. 5
0
def eye_look():
    motor_eye.throttle = random.uniform(0.6, 1.0)
    time.sleep(random.random())  # 0 to 1.0 seconds
    motor_eye.throttle = 0
    time.sleep(random.random())
    motor_eye.throttle = random.uniform(-1.0, -0.6)
    time.sleep(random.random())
    motor_eye.throttle = 0
    time.sleep(random.random())


while True:
    if ss.digital_read(attract_switch
                       ):  #  regular mode, attrack switch not closed/shorted
        if not ss.digital_read(button):  # button has been pressed
            decoder = audiomp3.MP3Decoder(open("ring.mp3", "rb"))
            audio.play(decoder)
            while audio.playing:
                pass
            open_lid()
            blink(3)
            ss.digital_write(LED, True)  # light the eye
            decoder = audiomp3.MP3Decoder(
                open(audio_files[current_audio_file], "rb"))
            audio.play(decoder)
            while audio.playing:
                eye_look()
            motor_eye.throttle = 0  # audio is finished, pause the eye
            blink(5)
            close_lid()
            current_audio_file = ((current_audio_file + 1) % (len(audio_files))
Esempio n. 6
0
    wheel.x = idx * 22
    wheel.y = -20
    g.append(wheel)
    wheels.append(wheel)
display.show(g)

# We want a digital input to trigger the fruit machine
button = digitalio.DigitalInOut(board.A1)
button.switch_to_input(pull=digitalio.Pull.UP)

# Enable the speaker
enable = digitalio.DigitalInOut(board.D4)
enable.switch_to_output(True)

mp3file = open("/triangles-loop.mp3", "rb")
sample = audiomp3.MP3Decoder(mp3file)

# Play the sample (just loop it for now)
speaker = audioio.AudioOut(board.A0)
speaker.play(sample, loop=True)

# Here's the main loop
while True:
    # Refresh the dislpay (doing this manually ensures the wheels move
    # together, not at different times)
    display.refresh(minimum_frames_per_second=0, target_frames_per_second=60)

    all_stopped = all(si.state == STOPPED for si in wheels)
    if all_stopped:
        # Once everything comes to a stop, wait until the lever is pulled and
        # start everything over again.  Maybe you want to check if the
Esempio n. 7
0
comet_stripB_m1 = Comet(
    pixel_stripB, speed=b64TH, color=RED, tail_length=9, bounce=False
)

solid_white = Solid(pixel_ringsAll, color=WHITE)

# ---Anim Modes---
vu_strip_animations_mode0 = AnimationGroup(comet_stripA_m0, comet_stripB_m0, sync=True)
vu_strip_animations_mode1 = AnimationGroup(comet_stripA_m1, comet_stripB_m1, sync=True)

#  ---Audio Setup---
if mode_state:
    BGM = "/lucio/bgmheal.mp3"
else:
    BGM = "/lucio/bgmspeed.mp3"
sample0 = audiomp3.MP3Decoder(open(BGM, "rb"))
FX = "/lucio/shoot.mp3"
sample1 = audiomp3.MP3Decoder(open(FX, "rb"))
speaker.play(mixer)
mixer.voice[0].play(sample0, loop=True)
mixer.voice[0].level = 0.3 * VOLUME_MULT
mixer.voice[1].level = 0.7 * VOLUME_MULT


while True:
    if mode_state:  # heal mode on startup
        vu_strip_animations_mode0.animate()
        pulse_rings_m0.animate()
        pulse_jewel_m0.animate()
    else:  # speed mode on startup
        vu_strip_animations_mode1.animate()
Esempio n. 8
0
# Set up speaker enable pin
enable = digitalio.DigitalInOut(board.D10)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True

speaker = audioio.AudioOut(board.A0)

sample_number = 0

print("Lars says, 'Hello, CVT Joseph. Tap to play.'")

if startup_play:  # Play all on startup
    for i in range(10):
        sample = "/lars/lars_0{}.mp3".format(i)
        print("Now playing: '{}'".format(sample))
        mp3stream = audiomp3.MP3Decoder(open(sample, "rb"))
        speaker.play(mp3stream)

        while speaker.playing:
            time.sleep(0.1)
    enable.value = speaker.playing

while True:
    if accel.tapped and speaker.playing is False:
        sample = "/lars/lars_0{}.mp3".format(sample_number)
        print("Now playing: '{}'".format(sample))
        mp3stream = audiomp3.MP3Decoder(open(sample, "rb"))
        speaker.play(mp3stream)
        sample_number = (sample_number + 1) % 10
    enable.value = speaker.playing
"""
CircuitPython multiple MP3 playback example for Raspberry Pi Pico.
Plays two MP3 files consecutively, once time each.
"""
import board
import audiomp3
import audiopwmio

audio = audiopwmio.PWMAudioOut(board.GP0)

mp3files = ["slow.mp3", "happy.mp3"]

mp3 = open(mp3files[0], "rb")
decoder = audiomp3.MP3Decoder(mp3)

for filename in mp3files:
    decoder.file = open(filename, "rb")
    audio.play(decoder)
    print("Playing:", filename)
    while audio.playing:
        pass

print("Done playing!")
Esempio n. 10
0
#  setup for break beam LED pin
break_beam = digitalio.DigitalInOut(board.A1)
break_beam.direction = digitalio.Direction.INPUT
break_beam.pull = digitalio.Pull.UP

#  setup for button pin
button = digitalio.DigitalInOut(board.D4)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

#  setup for speaker pin
speaker = audioio.AudioOut(board.A0)

#  mp3 decoder setup
file = "/hoopBloop0.mp3"
mp3stream = audiomp3.MP3Decoder(open(file, "rb"))

#  state machines used in the loop
score = 0
hoops = False
button_state = False
beam_state = False
sample = 0

while True:
    #  button debouncing
    if not button.value and not button_state:
        button_state = True
    #  debouncing for break beam LED
    if not break_beam.value and not beam_state:
        beam_state = True