New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: '_AudioCallbackUserdata' object is not callable #128
Comments
I've fixed the import math
import time
from typing import Any
import numpy as np
from numpy.typing import NDArray
from scipy import signal # type: ignore[import]
import tcod.sdl.audio
VOLUME = 0.04
class PullWave:
def __init__(self) -> None:
self.time = 0.0
def __call__(self, device: tcod.sdl.audio.AudioDevice, stream: NDArray[Any]) -> None:
sample_rate = device.frequency
n_samples = device.buffer_samples
duration = n_samples / sample_rate
print(f"{duration=} {self.time=}")
t = np.linspace(self.time, self.time + duration, n_samples, endpoint=False)
self.time += duration
wave = signal.square(t * (math.tau * 440)).astype(np.float32)
wave *= VOLUME
stream[:] = device.convert(wave)
with tcod.sdl.audio.open(callback=PullWave()) as device:
print(device)
time.sleep(1) I smoothed the waveform and reduced the volume. Sorry for the lack of documentation around this, you seemed to have done pretty well in spite of that. I'm also interested in what you were having trouble with before you switched to callbacks. |
Finally, are you meaning to say that the error is already fixed, and will not be present in the next version of tcod ? In this case, can you tell me when it will release and be available on pypi? Alternatively, how could I be able to install the current codebase ? (I suppose the easiest way would be to clone, build, install local package with pip) |
You're correct, you do want to be careful what you do in the audio callback. I did it this way to keep artifacts out of the waveform. Better math could be used to make a looping pattern instead. I still need to look into push vs pull. I leaned towards push because Python doesn't like it as much when it gets called into from C, but that seems to work a little better since I starting passing exceptions to the unraisable hook. The mixer I experimented with can probably be modified to work with the pull method. Keep in mind that with either method you'll have to worry about the Python GIL and that most large Numpy operations will release the GIL for their duration. Any pure-Python implementation of audio handling is going to fall behind in performance, so try to do as much with Numpy as possible. I already made new a release on PyPI, then I made another release with more bugfixes. If you got it from pip before, then you can upgrade your installation with: |
``I am trying to repeatedly play a very small sound sample (1/60th of a second) using tcod's sdl.audio module.
Since I am having trouble synchronizing the audio buffer, I decided to try the pull approach with a callback.
Here is a minimal example :
And here is the error I am getting (same as with the more complicated usage inside my application) :
Platform : Windows 10
Python version : 3.9.13
tcod version from pip : 15.0.1
Notes :
The text was updated successfully, but these errors were encountered: