Fixes re: zognia's testing #2
2 changed files with 17 additions and 21 deletions
|
@ -26,7 +26,7 @@ os.close(old_stderr)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Clip:
|
class Clip:
|
||||||
def __init__(self, path, samplerate=None, speed=1, force_stereo=True):
|
def __init__(self, path, samplerate=None, speed=1, keep_pitch=True, force_stereo=True):
|
||||||
self.path = path
|
self.path = path
|
||||||
raw, native_rate = librosa.load(self.path, sr=None, dtype='float32', mono=False)
|
raw, native_rate = librosa.load(self.path, sr=None, dtype='float32', mono=False)
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class Clip:
|
||||||
self.raw = np.ascontiguousarray(self._stereo_transpose(raw), dtype='float32')
|
self.raw = np.ascontiguousarray(self._stereo_transpose(raw), dtype='float32')
|
||||||
|
|
||||||
if speed != 1:
|
if speed != 1:
|
||||||
self.stretch(speed)
|
self.stretch(speed, keep_pitch=keep_pitch)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def length(self):
|
def length(self):
|
||||||
|
@ -51,8 +51,11 @@ class Clip:
|
||||||
def _stereo_transpose(self, ndata):
|
def _stereo_transpose(self, ndata):
|
||||||
return ndata if self.channels == 1 else ndata.T
|
return ndata if self.channels == 1 else ndata.T
|
||||||
|
|
||||||
def stretch(self, speed):
|
def stretch(self, speed, keep_pitch=True):
|
||||||
|
if keep_pitch:
|
||||||
stretched = tsm.wsola(self._stereo_transpose(self.raw), speed)
|
stretched = tsm.wsola(self._stereo_transpose(self.raw), speed)
|
||||||
|
else:
|
||||||
|
stretched = librosa.resample(self._stereo_transpose(self.raw), self.samplerate * (1 / speed), self.samplerate, fix=False, scale=True)
|
||||||
self.raw = np.ascontiguousarray(self._stereo_transpose(stretched), dtype='float32')
|
self.raw = np.ascontiguousarray(self._stereo_transpose(stretched), dtype='float32')
|
||||||
|
|
||||||
def save(self, filename):
|
def save(self, filename):
|
||||||
|
|
|
@ -22,22 +22,21 @@ class AudioAlert(PluginBase):
|
||||||
|
|
||||||
self._cleanup_task = asyncio.create_task(self._cleanup())
|
self._cleanup_task = asyncio.create_task(self._cleanup())
|
||||||
|
|
||||||
def run(self, path, speed=1, immediate=True, poly=1, **kwargs):
|
def run(self, path, speed=1, keep_pitch=False, immediate=True, poly=1, **kwargs):
|
||||||
poly = int(poly)
|
poly = int(poly)
|
||||||
key = f'{path}@{speed}x'
|
key = f'{path}@{speed}{"X" if keep_pitch else "x"}'
|
||||||
clip = self.clips.get(key, [None, None])[0]
|
clip = self.clips.get(key, [None, None])[0]
|
||||||
|
|
||||||
if clip is None:
|
if clip is None:
|
||||||
clip = Clip(path, speed=speed, force_stereo=self.force_stereo)
|
clip = Clip(path, speed=speed, keep_pitch=keep_pitch, force_stereo=self.force_stereo)
|
||||||
self.clips[key] = [clip, maya.now()]
|
self.clips[key] = [clip, maya.now()]
|
||||||
else:
|
else:
|
||||||
self.clips[key][1] = maya.now()
|
self.clips[key][1] = maya.now()
|
||||||
|
|
||||||
stream_dq, refs = self.streams.get(path, (None, set()))
|
stream_dq = self.streams.get(path, None)
|
||||||
if stream_dq is None:
|
if stream_dq is None:
|
||||||
stream_dq = deque(maxlen=poly)
|
stream_dq = deque(maxlen=poly)
|
||||||
self.streams[path] = (stream_dq, refs)
|
self.streams[key] = stream_dq
|
||||||
refs.add(key)
|
|
||||||
|
|
||||||
if stream_dq.maxlen != poly:
|
if stream_dq.maxlen != poly:
|
||||||
self.logger.warn('Cannot change poly while streams are active!')
|
self.logger.warn('Cannot change poly while streams are active!')
|
||||||
|
@ -61,15 +60,9 @@ class AudioAlert(PluginBase):
|
||||||
now = maya.now()
|
now = maya.now()
|
||||||
for key, [clip, last_used] in list(self.clips.items()):
|
for key, [clip, last_used] in list(self.clips.items()):
|
||||||
if now >= last_used.add(minutes=self.timeout_min, seconds=clip.length):
|
if now >= last_used.add(minutes=self.timeout_min, seconds=clip.length):
|
||||||
del self.clips[key]
|
|
||||||
self.logger.debug(f'Dropping {key}')
|
self.logger.debug(f'Dropping {key}')
|
||||||
|
streams = self.streams.get(key, [])
|
||||||
streams, refs = self.streams.get(clip.path, (None, None))
|
|
||||||
if refs:
|
|
||||||
refs.remove(key)
|
|
||||||
self.logger.debug(f'Stream {clip.path} now refs {refs}')
|
|
||||||
if len(refs) == 0:
|
|
||||||
self.logger.debug('Closing streams...')
|
|
||||||
for stream in streams:
|
for stream in streams:
|
||||||
stream.close()
|
stream.close()
|
||||||
del self.streams[clip.path]
|
del self.streams[key]
|
||||||
|
del self.clips[key]
|
||||||
|
|
Loading…
Add table
Reference in a new issue