0

CPX and multitasking with speaker
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CPX and multitasking with speaker

by lionfood on Wed Mar 10, 2021 1:40 pm

I'm trying to blink the cpx's onboard neopixels while simultaneously playing a sound effect with circuitpython. The sound effect takes roughly 4 secs to complete. However, based upon my current code (based upon both the basic animations project https://learn.adafruit.com/circuitpython-led-animations/basic-animations#comet-3063738-30 and the drum mixer https://learn.adafruit.com/adafruit-circuit-playground-express/playground-drum-machine I can't seem to get the sound playback to work consistently with the blink animation. If I run the following code the wav file plays too quickly but the blink rate is correct. Sleeping wont help any either.
Code: Select all | TOGGLE FULL SIZE
import board
import neopixel

import time
import digitalio

from adafruit_led_animation.animation import blink
from adafruit_led_animation.color import *

pixel_pin = board.D8
pixel_num = 9
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)
blink = blink.Blink(pixels, speed=0.5, color=YELLOW)


try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile
 
try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

# Enable the speaker
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.direction = digitalio.Direction.OUTPUT
speaker_enable.value = True
 
audio = AudioOut(board.SPEAKER)

def play_file(filename):
    print("playing file " + filename)
    file = open(filename, "rb")
    wave = WaveFile(file)
    audio.play(wave)


while True:
    blink.animate()
    play_file('star.wav')



Help, can you point me in the right direction code wise or is this behavior with the speaker a limitation of the CPX?

lionfood
 
Posts: 17
Joined: Sun Sep 20, 2020 11:56 am

Re: CPX and multitasking with speaker

by mikeysklar on Thu Mar 11, 2021 1:35 pm

@lionfood,

It looks like you are really close.

The scream project plays the wavfile in the background while swinging a servo around. Same idea as what you are doing. Check out how they do it:

Code: Select all | TOGGLE FULL SIZE
 # Start playing the file (in the background).
def play_file(wavfile):
    print("Playing scream!")
    with open(wavfile, "rb") as f:
        wav = audiocore.WaveFile(f)
        a.play(wav)
        while a.playing:
            head_servo.angle = 60
            time.sleep(.01)
            head_servo.angle = 120
            time.sleep(.01)


https://learn.adafruit.com/the-scream-m ... cuitpython

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: CPX and multitasking with speaker

by lionfood on Thu Mar 11, 2021 4:03 pm

mikeysklar wrote:@lionfood,

It looks like you are really close.

The scream project plays the wavfile in the background while swinging a servo around. Same idea as what you are doing. Check out how they do it:

Code: Select all | TOGGLE FULL SIZE
 # Start playing the file (in the background).
def play_file(wavfile):
    print("Playing scream!")
    with open(wavfile, "rb") as f:
        wav = audiocore.WaveFile(f)
        a.play(wav)
        while a.playing:
            head_servo.angle = 60
            time.sleep(.01)
            head_servo.angle = 120
            time.sleep(.01)


https://learn.adafruit.com/the-scream-m ... cuitpython



This is great I literally just found that before posting a reply just now. I wrote out all of the sounds and LED effect combinations as individual functions and tested successfully. The next step was going to be to have different effects triggered by different actions such as tapping. CPX has everything that I want but I can't import cpx without having a conflict with the neopixels (i.e. NEOPIXEL in use"). I can't run audio and the onboard neopixels simultaneously using the CPX library either. Am I missing something about how this all is supposed to be imported? I feel like I am missing something obvious here.

Again, really appreciate the help

lionfood
 
Posts: 17
Joined: Sun Sep 20, 2020 11:56 am

Re: CPX and multitasking with speaker

by mikeysklar on Fri Mar 12, 2021 12:58 pm

Did the above example not work? I mean you will probably not be able to use separate functions, but if you add your NeoPixel code inside the while a.playing: loop it should be workable.

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: CPX and multitasking with speaker

by lionfood on Sat Mar 13, 2021 11:12 am

It worked just fine. I didn't realize until after I had gotten over that hurdle that I I couldn't utilize my code in conjunction with the library written specifically for cpx (adafruit_circuitplayground.express) with function like cpx.tapped, cpx.shake, and cpx.were_pressed. If I try rewriting the code for the cpx express library there is no way for me to utilize while a.playing.

mikeysklar wrote:Did the above example not work? I mean you will probably not be able to use separate functions, but if you add your NeoPixel code inside the while a.playing: loop it should be workable.

lionfood
 
Posts: 17
Joined: Sun Sep 20, 2020 11:56 am

Please be positive and constructive with your questions and comments.