belangeo / pyo

Python DSP module
GNU Lesser General Public License v3.0
1.32k stars 132 forks source link

`pyo` doesn't produce sound in Qt App #260

Closed dschiller closed 1 year ago

dschiller commented 1 year ago

demo.py

from pyo import *
from PyQt5.QtWidgets import QApplication, QWidget

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.show()

        self.s = Server()
        self.s.boot()
        self.s.start()

        # Sets fundamental frequency.
        freq = 187.5

        # Roland JP-8000 Supersaw emulator.
        lfo4 = Sine(0.1).range(0.1, 0.75)
        osc4 = SuperSaw(freq=freq, detune=lfo4, mul=0.3).out()

    def closeEvent(self, event):
        self.s.stop()
        return super().closeEvent(event)

app = QApplication([])
window = Window()
sys.exit(app.exec_())

It seems when pyo is in the window class of a Qt app then there is no sound.

dschiller commented 1 year ago

Foudn a solution:

from pyo import *
from PyQt5.QtWidgets import QApplication, QWidget
import threading

class Sound:

    def __init__(self):
        self.server = Server()
        self.server.deactivateMidi()
        self.server.setBufferSize(512)

    def start(self):
        self.server.boot().start()

    def stop(self):
        self.server.stop()

    def _synth(self):
        # Sets fundamental frequency.
        freq = 187.5

        # Roland JP-8000 Supersaw emulator.
        lfo4 = Sine(0.1).range(0.1, 0.75)
        osc4 = SuperSaw(freq=freq, detune=lfo4, mul=0.3).out()

        while True:
            time.sleep(.0001)

    def synth(self):

        self.synth_thread = threading.Thread(target=self._synth, daemon=True)
        self.synth_thread.start()

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.show()

        self.sound = Sound()
        self.sound.start()
        self.sound.synth()

    def closeEvent(self, event):
        self.sound.stop()
        return super().closeEvent(event)

app = QApplication([])
window = Window()
sys.exit(app.exec_())