1

I'm trying to play a sound file play to two different devices and so I've tried the multiprocessing and threading modules but it doesn't yield the wanted result

At first, I wanted to execute the function that plays the file twice at the same time but then I realized that it wasn't possible so I want to execute both in a very small time window. I tried the threading and multiprocessing module to make two processes. after I tried to simply execute it with the play function returning immediately so it can execute the next play but it just plays them one after the other

import sounddevice as sd
import soundfile as sf
from multiprocessing import Process
import argparse

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("filename", help="audio file to be played back")
args = parser.parse_args()

def _play_(d):
    try:
       data, fs = sf.read(args.filename, dtype='float32')
       sd.play(data, fs, device=d, blocking = False)
       status = sd.wait()
    if status:
        parser.exit('Error during playback: ' + str(status))
    except KeyboardInterrupt:
        parser.exit('\nInterrupted by user')
    except Exception as e:
        parser.exit(type(e).__name__ + ': ' + str(e))


if __name__ == '__main__':
        Process(target =_play_(8),name = 'playing1').start()
        Process(target =_play_(9),name = 'playing2').start()

I expect it to play twice the file I specified to two different devices at almost the same time but instead, it just plays one at a time

Sai
  • 225
  • 3
  • 15
  • You are using `Process` wrong. The argument `target` is supposed to be a callable object, but in your case it isn't! Also, try not using `Process` at all. You should try using two instances of `sd.OutputStream`, each with its callback function. – Matthias Jul 30 '19 at 06:31
  • Thank you for your answer I'm working on the solution I think what i need to do is somehow make sounddevice believe that the devices are one and the same and have their own callback functions as you said – Joseph Dittrick Aug 01 '19 at 08:46

0 Answers0