yjl
Posts: 25
Joined: Sun Aug 06, 2017 7:53 pm

sounddevice stream re-initialize?

Sun Aug 11, 2019 8:32 am

I am working on three pairs of audio playback streams using Sounddevice module. For real-streaming playback each object use Callback function now. The problem is sometimes device seems to be blocked while I control them by stop/close/start.

My USB audio card has indicator LED, so I can see the device is getting signal(data?) from raspberry pi but I can't listen anything, or even can't create the stream object again. Then I have to exit Python and run the script again.

Would be there way to re-initialize the Stream or diagnosis? ( I guess Callback 'status' could be possible but have no idea how to use it for release or close Stream object/open again.)

Below is prototype I am testing now

Code: Select all

def callback_p0(indata,outdata,status,*stuff):
    global rec_p0
    global stream_flag_p0
    global i
    
    if stream_flag_p0==1:
        outdata[:]=indata
        q.put(indata.copy())
    elif stream_flag_p0==2:
        if i<len(rec_p0):
            outdata[:]=rec_p0[i],
            i=1+i
    else:
        outdata[:]=indata

def callback_p1(indata,outdata,status,*stuff):
    global stop_flag_p1
    outdata[:]=indata

def callback_p2(indata,outdata,status,*stuff):
    global stop_flag_p2
    outdata[:]=indata


callback_name=[callback_p0,callback_p1,callback_p2]
streams=[]

def create_running_output_stream(index):
    global callback_name
    output = sd.Stream(
        device=index,channels=1,blocksize=900, latency='high', #blocksize decrease - fail 512 work 1024
        dtype='float32',callback=callback_name[index]
        )
    output.start()
    return output

streams = [create_running_output_stream(index) for index in range(3)] ##AUDIO IN/OUT WIRING

import time
stream_flag_p0=1	## 5second recording while stream is ON
time.sleep(5)
stream_flag_p0=0

Return to “Python”