-
Notifications
You must be signed in to change notification settings - Fork 27
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
FR: jack.Ringbuffer example #113
Comments
Something like this? class AudioBuffer(object):
def __init__(self, blocksize):
self.ringbuffer = jack.RingBuffer(blocksize*2)
self.blocksize = blocksize
def insert(self, data):
if self.ringbuffer.write_space < (len(data)):
# no space in the ringbuffer, discard it
return
self.ringbuffer.write(data)
def __iter__(self):
while True:
while self.ringbuffer.read_space < self.blocksize:
time.sleep(0.05) #wait a little bit to not use all the cpu
data = self.ringbuffer.read(self.blocksize)
yield data
clientname = 'jackclient'
client = jack.Client(clientname)
blocksize = client.blocksize
samplerate = client.samplerate
if client.status.server_started:
print('JACK server started')
if client.status.name_not_unique:
print('unique name {0!r} assigned'.format(client.name))
event = threading.Event()
audio_buffer = AudioBuffer(blocksize)
@client.set_process_callback
def process(frames):
assert frames == client.blocksize
for i in client.inports:
data = i.get_buffer()[:]
audio_buffer.insert(data)
@client.set_shutdown_callback
def shutdown(status, reason):
print('JACK shutdown!')
print('status:', status)
print('reason:', reason)
event.set()
capture = []
# create two port pairs
for number in 1, 2:
client.inports.register('input_{0}'.format(number))
capture.append(client.get_port_by_name(f'mycaptureport{number}'))
with client:
for src, dest in zip(capture, client.inports):
client.connect(src, dest)
print('Press Ctrl+C to stop')
try:
for data in audio_buffer:
vals = np.frombuffer(data, dtype=numpy.float32)
print(vals)
event.wait()
except KeyboardInterrupt:
print("Exiting..")
pass |
Thanks for bringing this up! I think an example might be nice, but I consider the
TBH, I don't see a lot of code, so I don't actually know if and how this module is used. If you have links to public projects using it, please share them here!
I don't know, but maybe that's a good thing? It is quite error-prone and I think there aren't many cases where it has a clear advantage over alternatives. I think
If you don't care about an occasional dropout, you probably don't need the JACK ring buffer in the first place. The whole reason to use it would be to avoid locking, exactly to avoid dropouts. I think the only situation where the JACK ring buffer is really useful is when the process callback is implemented in a compiled language (like e.g. C), but this is not yet supported (see also #59 (comment)).
Sure, but |
Hey! Thanks for the quick reply
ah interesting - well, I guess i've been having success using
yeah I was looking at this! and this is what I meant when I was talking about extra dependencies. RTMixer looks fantastic - but in my case, I'm actually using other JACK functions like the transport controls and the CPU monitoring, which It seems the CFFI is at work within this lib - so could this be implemented here too? |
Yeah, as the SO answers say,
The PortAudio ring buffer is not necessary here, it does about the same thing as the JACK ring buffer. You can already use |
On my Raspberry Pi4, using the I wonder, would C based process callback function optimise this also? |
I don't think the choice of queue should have a significant influence on the average CPU usage. It's more about the worst-case behavior. If you implement the process callback in C (or some other compiled language like C++ or Rust, and probably it also works with Cython + Using Python + If you want to reduce the average CPU usage, you can try to optimize your Python code, maybe using some NumPy functions? Either way, you should try to do some profiling to find out where the most CPU load is generated, this can often be counter-intuitive. |
Hi there,
Cheers for a great, easy to use library :) I think it would be nice to show an example using
jack.Ringbuffer()
since it would be helpful for some folks to see the intended implementation.Seems there was some discussion of this here: #59 but never materialised into an example for this lib.
I often see Queue being used with
jackclient-python
and never the ringbuffer.Any ideas why this feature isn't really used?
edit: I see/get #18 - but for folks who don't care too much about the occasional dropout (many use cases where dropouts are fine) be nice to see how to keep everything in JACK without going to extra dependencies :)
The text was updated successfully, but these errors were encountered: