Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sounddevice.PortAudioError : Error opening Stream : Device unavailable [PaErrorCode -9985] #570

Open
TylerBerrymanBrash opened this issue Nov 19, 2024 · 2 comments

Comments

@TylerBerrymanBrash
Copy link

TylerBerrymanBrash commented Nov 19, 2024

I am trying to run a python script on boot using a systemd service on a Raspberry Pi4. The python script runs fine via the terminal, but flags a few errors (see attached image) when run via systemd. The python script opens an audio stream using the sounddevice library.
I noticed that the device indexes are different when the python script is run via the terminal and via systemd. So I tried hardcoding the device index for the input stream, but that has not worked.

import sounddevice as sd
import numpy as np
import time
from pvrecorder import PvRecorder
import wave
import struct
import pydub
import requests
import uuid_utils as uuid

speaking_status = False
end = time.time() +10
start = time.time() 
length = 0
timer = 0
Audio_file = "YOW.wav"
sound_threshold = 10
audio_timeout = 3
IATA_Code = "YOW"

devices = PvRecorder.get_available_devices()
print

for i in range(len(devices)):
    print("index : %d, device name: %s" % (i, devices[i]))
    
sd.default.device = 2,1    
print("SoundDevice Indexes:" + str(sd.query_devices()))
#print("SoundDevice Default Device:" + str(sd.default()))

recorder = PvRecorder(device_index = 12, frame_length=512)
print("Selected device:" + recorder.selected_device)
audio = []

def print_sound(indata, outdata, frames, t, status):
    global start
    global end
    global length
    global timer
    global speaking_status
    global Audio_file
    global sound_threshold
    global audio_timeout
    i = 0
    
    volume_norm = np.linalg.norm(indata)*10
    #print(volume_norm)
    
    if (volume_norm > sound_threshold and speaking_status==False):
        print("Recording")
        start = time.time()
        speaking_status=True
        recorder.start()
    elif (volume_norm > sound_threshold and speaking_status==True):
        start = time.time()
        frame = recorder.read()
        audio.extend(frame)
        print("Volume:" + str(volume_norm))
    else:
        end = time.time()
    timer = end-start
    
    if (timer > audio_timeout and speaking_status==True):
        recorder.stop()
        speaking_status=False
        print("Stop Recording Audio")
        volume_norm = 0
        
        headers = {
        'Content-Type': 'application/json; charset=utf-8',
        }
        
        content_type = {
        'Content-Type': 'audio/mpeg',
        }

        json_data = {
            'query': {
                'filename': Audio_mp3,
            },
        }
        
        try:
            with open(Audio_mp3, "rb") as file_data:
                 print("Uploading Audio File to AWS S3 Bucket")
                 put_response = requests.put(received_upload_link, data=file_data, headers=content_type)
                 print("HTTP Response:" + str(put_response.status_code))
        except Exception as e:
                print("An exception occured:", e)

with sd.Stream(callback=print_sound):
    while True:
        sd.sleep(1000)

PXL_20241118_181206850 (1)

@mgeier
Copy link
Member

mgeier commented Nov 21, 2024

Is it possible that PvRecorder takes over the input device and therefore makes it unavailable?

Could you try it with an OutputStream instead of a Stream?

@TylerBerrymanBrash
Copy link
Author

Issue was the input devices do not have the same device index when the python script is running in the terminal or when it is running with systemd. A few print statements showed the differences in device indexes. Another approach that worked for me was to get the specific device ID for the device name and plug that into the PvRecorder initiation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants