1

I am trying to use sounddevice OutputStream within my PyQt application to play sound.

I want something like

import sounddevice as sd

def callback(...):
    #stuff that works with a "with" statement

class MyApp(QtWidgets.QMainWindow):
    def __init__(self):
        #stuff and buttons

    def startSound(self):
        #called by a button
        self.streamInstance = sd.OutputStream(settings)


    def stopSound(self):
        #called by another button
        self.streamInstance.close()

Now this does not work but if I set to have:

with sd.OutputStream(settings):
        #a loop

It works but I then cannot stop the stream from another function and the app stops running.

If anyone has an idea for a workaround or a fix, it would be greatly appreciated

VictorC
  • 71
  • 7
  • What is the error message? Is it similar to https://github.com/spatialaudio/python-sounddevice/issues/69? – Matthias Jun 14 '17 at 13:31
  • That's the thing. I do not get an error. It goes through but the callback stream doesn't get called – VictorC Jun 14 '17 at 16:17
  • There may be a ton of (possibly unrelated) things going wrong ... it might be a simple typo in your code ... it's impossible to say with the given information. You should try to come up with an [MCVE](https://stackoverflow.com/help/mcve). – Matthias Jun 14 '17 at 18:45

1 Answers1

0

The stream doesn't start in the initial code sample because you need to explicitly call streamInstance.start() on it to start the processing. This is done automatically when you enclose it in a with block (as defined within the _StreamBase superclass's __enter__ method).

As Matthias says, it's impossible to tell why you are unable to stop the stream from another function without including more code. If the loop inside your with is blocking and you're not using multi-threading, it could be that the other function never gets called.

Daniel Jones
  • 615
  • 5
  • 17