Skip to content

Commit af472bf

Browse files
committed
delay example: check if start was successful
1 parent 539c31a commit af472bf

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

examples/bleeperoo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@
7878
start=start_time + r.uniform(start_min, start_max))
7979
while m.actions:
8080
sd.sleep(100)
81+
# TODO: get list of actions and check if all were started on time?
8182
print('{0} buffer underflows in {1} processed audio blocks'.format(
8283
m.stats.output_underflows, m.stats.blocks))

examples/delay.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,42 @@
88
blocksize = 0
99
latency = 'low'
1010
samplerate = 48000
11-
safety = 0.1 # Shouldn't be less than the duration of an audio block
11+
safety = 0.001 # Increase if Python interpreter is slow
1212

13-
rb_size = 2**math.ceil(math.log2((delay + safety) * samplerate))
13+
rb_size = 2**math.ceil(math.log2(delay * samplerate))
1414

1515
stream = rtmixer.MixerAndRecorder(
1616
channels=channels, blocksize=blocksize, samplerate=samplerate,
1717
latency=latency)
1818
with stream:
1919
samplesize = 4
2020
assert {samplesize} == set(stream.samplesize)
21+
print(' input latency:', stream.latency[0])
22+
print(' output latency:', stream.latency[1])
23+
print(' sum:', sum(stream.latency))
24+
print('requested delay:', delay)
2125
rb = rtmixer.RingBuffer(samplesize * channels, rb_size)
2226
start = stream.time + safety
23-
stream.record_ringbuffer(rb, start=start, allow_belated=False)
24-
stream.play_ringbuffer(rb, start=start + delay, allow_belated=False)
25-
# TODO: check if start was successful
27+
record_action = stream.record_ringbuffer(rb, start=start,
28+
allow_belated=False)
29+
play_action = stream.play_ringbuffer(rb, start=start + delay,
30+
allow_belated=False)
31+
# Dummy recording to wait until "start" has passed:
32+
stream.wait(stream.record_buffer(b'', channels=1, start=start))
33+
if record_action not in stream.actions:
34+
if record_action.actual_time == 0:
35+
raise RuntimeError('Increase "safety"')
36+
else:
37+
# TODO: could there be another error?
38+
raise RuntimeError('Ring buffer overflow (increase "delay"?)')
39+
# Dummy playback to wait until "start + delay" has passed:
40+
stream.wait(stream.play_buffer(b'', channels=1, start=start + delay))
41+
if play_action not in stream.actions:
42+
if play_action.actual_time == 0:
43+
raise RuntimeError('Increase "safety" or "delay"')
44+
else:
45+
# TODO: could there be another error?
46+
raise RuntimeError('Ring buffer underflow (increase "delay"?)')
2647
print('#' * 80)
2748
print('press Return to quit')
2849
print('#' * 80)

0 commit comments

Comments
 (0)