1

I am trying to see how long it takes pygame.mixer.music.play() to actually initialize a sound. I have an audio cord running out the 3.5 jack back into the Pi through a USB soundcard. I think the code I have should, in theory, work but I am still fairly new to Python and pyaudio. It ignores input entirely right now, I can have the audio disconnected and it still returns a time.

import pygame
import RPi.GPIO as GPIO
import time
import os
import pyaudio

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
THRESHOLD = 500

pygame.mixer.pre_init(44100, -16, 1, 370)
pygame.mixer.init()

while True:

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


    data = stream.read(CHUNK)

    print "recording..."

    Sound_Start = time.time()
    print 'Music Command Started:',Sound_Start

    pygame.mixer.music.play(1,0.0) 

    if data > THRESHOLD:
        Sound_Finish = time.time()
        print 'Music Command Finished:', Sound_Finish
        print '-------', '\n',print Sound_Finish - Sound_Start


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

I've tried messing with the CHUNK and THRESHOLD values but it doesn't seem to matter. What am I doing wrong here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.