2
\$\begingroup\$

I'm from a background of using OpenAL with other languages; and I'm on a project that benefits very much from Python. I remember PyAL, but it hasn't been updated since 2013 and my computer can't even find a copy of the package to download. I'm also aware of such relative-fringe packages as alpy and python-openal; but I'm hesitant to pick them up if they are also unmaintained. (Can't be sure yet.)

Is there a library that provides a Python binding to OpenAL? Or do I simply need to either write my own or make due with one of the other, non-3D-sound, packages? If getting it is more complicated than pip, how would I go about doing so?

Thanks.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

OpenAL being cross-plattform, C and open source can most probably be used with Python. However, indeed there seems to be curently no actively supported Python binding available.

Some current attempts I could find:

They all do not look very good (you can try them out though) and none is easily installable with pip.

I'm afraid but if you want a working Python binding for OpenAL you probably have to wait or start writing your own, maybe based on one of the approaches above.

As for the alternatives, they all have advantages and disadvantages too (PyGlet, PyAudio, PyDub, PyMedia, PyGame, PyQt). Support might vary.

\$\endgroup\$
3
\$\begingroup\$

PyOpenAL (released Dec 17, 2019): https://pypi.org/project/PyOpenAL/

pip install PyOpenAL

You can use original OpenAL API but PyOpenAL has helpful wrapper functions. If your audio file plays with problems try to open it in the Audacity and export it with Encoding: Signed 16-bit PCM:

enter image description here

My example in PyQt6 shows how to load music, set looping, set volume, play music with QCheckBox and play sound with QPushButton. QSlider is here to set a volume:

enter image description here

pip install PyOpenAL PyQt6

import sys

from openal import oalOpen, oalQuit
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import (QApplication, QCheckBox, QHBoxLayout, QPushButton,
                             QSlider, QVBoxLayout, QWidget)


class Widget(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyOpenAL, PyQt6")
        self.resize(300, 150)

        # Music check box
        playMusic = QCheckBox("Play music")
        # playMusic.toggle()
        # print(playMusic.isChecked())
        playMusic.stateChanged.connect(self.onPlayMusic)

        # Sound button
        playSoundButton = QPushButton("Play sound")
        playSoundButton.clicked.connect(self.onPlaySound)

        mousicVolume = 0.1

        # Volume slider
        volumeSlider = QSlider(Qt.Orientation.Horizontal)
        volumeSlider.setValue(int(mousicVolume * 100))
        volumeSlider.valueChanged[int].connect(self.onChangeVolume)

        self.mousicSource = oalOpen("assets/infant_tour.wav")
        self.soundSource = oalOpen("assets/bounce.wav")
        self.mousicSource.set_gain(mousicVolume)

        self.mousicSource.set_looping(True)

        vbox = QVBoxLayout()
        vbox.addWidget(volumeSlider)
        vbox.addWidget(playMusic)
        vbox.addWidget(playSoundButton)
        vbox.addStretch(1)

        hbox = QHBoxLayout()
        hbox.addLayout(vbox)
        hbox.addStretch(1)
        self.setLayout(hbox)

    def onPlayMusic(self, state):
        
        if state == Qt.CheckState.Checked.value:
            self.mousicSource.play()
        else:
            self.mousicSource.pause()

    def onPlaySound(self):
        self.soundSource.play()

    def onChangeVolume(self, value):
        self.mousicSource.set_gain(value / 100)

    def closeEvent(self, event):
        oalQuit()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec())
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.