Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wait(self, action, sleeptime=10):
"""Wait for *action* to be finished.
Between repeatedly checking if the action is finished, this
waits for *sleeptime* milliseconds.
"""
while action in self.actions:
_sd.sleep(sleeptime)
with sf.SoundFile(filename) as f:
with rtmixer.Mixer(channels=f.channels,
blocksize=playback_blocksize,
samplerate=f.samplerate, latency=latency) as m:
elementsize = f.channels * m.samplesize
rb = rtmixer.RingBuffer(elementsize, reading_blocksize * rb_size)
# Pre-fill ringbuffer:
_, buf, _ = rb.get_write_buffers(reading_blocksize * rb_size)
written = f.buffer_read_into(buf, dtype='float32')
rb.advance_write_index(written)
action = m.play_ringbuffer(rb)
while True:
while rb.write_available < reading_blocksize:
if action not in m.actions:
break
sd.sleep(int(1000 * reading_blocksize / f.samplerate))
if action not in m.actions:
break
size, buf1, buf2 = rb.get_write_buffers(reading_blocksize)
assert not buf2
written = f.buffer_read_into(buf1, dtype='float32')
rb.advance_write_index(written)
if written < size:
break
m.wait(action)
if action.done_frames != f.frames:
RuntimeError('Something went wrong, not all frames were played')
if action.stats.output_underflows:
print('output underflows:', action.stats.output_underflows)
def flush(self):
print("flushing")
while not self.queue.empty():
sounddevice.sleep(1000)
print("flushed")
def play_input_to_output(duration, device, sample_rate=44100, chunksize=1024, nb_channel=2):
#~ duration = 5 # seconds
dev = sd.query_devices(device=device)
sample_rate = dev['default_samplerate']
print(dev)
def callback(indata, outdata, frames, time, status):
if status:
print(status, flush=True)
outdata[:] = indata
with sd.Stream(device=device, channels=nb_channel, callback=callback, samplerate=sample_rate):
sd.sleep(int(duration * 1000) )
def flush(self):
with self.stream:
while not self.queue.empty():
sounddevice.sleep(1000)
assert bleep.flags.c_contiguous
bleeplist.append(bleep)
actionlist = []
with rtmixer.Mixer(device=device, channels=channels, blocksize=blocksize,
samplerate=samplerate, latency=latency, qsize=qsize) as m:
start_time = m.time
actionlist = [
m.play_buffer(bleep,
channels=[r.randint(channels) + 1],
start=start_time + r.uniform(start_min, start_max),
allow_belated=False)
for bleep in bleeplist
]
while m.actions:
sd.sleep(100)
print('{0} buffer underflows in {1} processed audio blocks'.format(
m.stats.output_underflows, m.stats.blocks))
belated = 0
min_delay = np.inf
max_delay = -np.inf
for action in actionlist:
assert action.type == rtmixer.PLAY_BUFFER
# NB: action.allow_belated might have been invalidated
assert action.requested_time != 0
if not action.actual_time:
belated += 1
assert action.done_frames == 0
continue
assert action.done_frames == action.total_frames
delay = action.actual_time - action.requested_time