Integrating PyAudio with PsychoPy

Hey, so I’m trying to integrate PyAudio with my code to basically find the sound dB levels using the microphone and use that with my psychopy experiment. However, when I try and run the PsychoPy experiment (PsychoPyTask.zip [106.1 KB]), I get the following error:

import _portaudio as pa
ModuleNotFoundError: No module named ‘_portaudio’

I think it has to do with the fact that _portaudio is a C package that works with pyaudio, but I have found no solution to this issue online.

Is there a way to fix this issue, or accomplish the same idea using a psychopy-compatible module:

import random
import pandas as pd
import numpy as np
import time
from math import log10
import audioop
import pyaudio

#In begin routine
init_db = []

p = pyaudio.PyAudio()
WIDTH = 2
RATE = int(p.get_default_input_device_info()['defaultSampleRate'])
DEVICE = p.get_default_input_device_info()['index']
rms = 1
tm = time.time()

def callback(in_data, frame_count, time_info, status):
    global rms
    rms = audioop.rms(in_data, WIDTH) / 32767
    return in_data, pyaudio.paContinue
    
stream = p.open(format=p.get_format_from_width(WIDTH),
                input_device_index=DEVICE,
                channels=1,
                rate=RATE,
                input=True,
                output=False,
                stream_callback=callback)

stream.start_stream()
db = abs(20 * log10(rms))

#In the frames section
get_curr = time.time()
if get_curr - tm >= 0.3:
    db = abs(20 * log10(rms))
    init_db.append(db)
    tm = get_curr
1 Like

Hi there, I’m running into the exact same problem- were you ever able to find a solution?