0

So, I am building a Dalek and instead of buying a hardware voice modulator, we have managed to use the Raspberry Pi to take sound from a USB Mic - apply a live sound effect and play the amended sound out of an attached speaker.

What we would like to do know is illuminate the dome lights on the Dalek whenever sound comes out of the speaker.

I thought I'd try to do this using the Pi to read the sound source and if it is above a certain level, then turn on the lights.

For testing I am simply using an LED.

I have tried to find out how to do this to no avail, so (don't shout at me) I tried Chat GPT. This is the program it gave me, needless to say it is not yet working.

import alsaaudio

def main ():

# now just write the code you would use on a real Raspberry Pi
# Sound to light program for Dalek dome lights
from gpiozero import LED
import time
import alsaaudio

# Initialize the GPIO pins
led =LED(2)
# Turn off LED
led.off()


# Create an ALSA audio input object
audio = alsaaudio.PCM(alsaaudio.PCM_CAPTURE)
audio.setformat(alsaaudio.PCM_FORMAT_S16_LE)
audio.setrate(44100)
audio.setperiodsize(160)

#Threshold for sound detection
SOUND_THRESHOLD = 5000

try:
    while True:
        # Read audio data
        _, data = audio.read()
        #sound_level = max(abs(int.from_bytes(data,byteorder='little')))
        sound_level = max(abs(int.from_bytes(data,byteorder='little')))
        if sound_level > SOUND_THRESHOLD:
            led.on()
        else:
            led.off()

except KeyboardInterrupt:
    pass
led.off()

main()

The error I am getting now is:

File "/home/dalek/sound_light.py", line 32, in main
sound_level = max(abs(int.from_bytes(data,byteorder='little')))
TypeError: 'int' object is not iterable
2
  • does your code look correctly formatted on this web page?
    – jsotola
    Dec 9, 2023 at 19:13
  • 1
    I am building a Dalek nothing good ever started like that :p Dec 10, 2023 at 1:36

1 Answer 1

0

So, I have now tried a different route. The code below now appears to work. It uses pyaudio and prints the result to the screen.

import pyaudio
import audioop

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 60

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)


for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK, 
                       exception_on_overflow=False)
    rms = audioop.rms(data, 2)   
    print(rms)

stream.stop_stream()
stream.close()
p.terminate()

I have started the program, then played some sound and I can see the rms output increase in value.

I then tried to print the value to a text file just for debugging, but I could only get the last value of the variable rms. How can I print all the values of the variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.