0

I am writing a program in Python to play audio on a Focusrite Scarlett 6i6 while simultaneous recording another waveform on a Picoscope 2205AMSO. To play audio on the Focusrite, I am using the sounddevice library:

sounddevice.play(noise, blocking=True)

To record the other waveform, I am using the picoscope library:

ps.runBlock()
ps.waitReady()
dataA = ps.getDataV('A', nSamples, returnOverflow=False)

However, these two statements won't run at the same time because they are blocking. If I remove the "blocking = True" argument from the sounddevice.play function call, then it never plays the audio. Is there a way that I can both record and play without blocking?

PetSven
  • 366
  • 3
  • 13
  • 1
    A bit of warning... I'm not sure what your use case is, but keep in mind that two devices won't stay in-sync over time, even if you did manage to start them simultaneously. – Brad Feb 06 '20 at 22:20

1 Answers1

0

I was able to get both functions running simultaneously by using the asyncio library:

import asyncio  

def record():
    ps.runBlock()
    ps.waitReady()

def play():
    sounddevice.play(noise, blocking=True)

async def non_blocking(loop, executor):
    await asyncio.wait(
        fs={
            loop.run_in_executor(executor, play),
            loop.run_in_executor(executor, record),
        },
        return_when=asyncio.ALL_COMPLETED
    )

loop = asyncio.get_event_loop()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
loop.run_until_complete(non_blocking(loop, executor))
PetSven
  • 366
  • 3
  • 13