def chroma(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.npy" if not cache.exists(): y, sr = librosa.load(workable) chroma = librosa.feature.chroma_cqt(y=y, sr=sr) np.save(cache, chroma) else: chroma = np.load(cache) self.buffer[str(workable)] = chroma.tolist()
def filter_duration(x, low: float, high: float) -> bool: hsh = create_hash(x, low, high) tmp = Path("/tmp") / "ftis_cache" tmp.mkdir(exist_ok=True) cache = tmp / f"{hsh}.npy" if not cache.exists(): dur = get_duration(x) np.save(cache, dur) else: dur = np.load(cache) return dur < high and dur > low
def flux(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.npy" if not cache.exists(): sr = get_sr(workable) y, _ = librosa.load(workable, sr=sr) fft = librosa.stft(y, win_length=self.windowsize, hop_length=self.hopsize) flux = np.sum(np.abs(np.diff(np.abs(fft))), axis=0) np.save(cache, flux) else: flux = np.load(cache) self.buffer[workable] = flux.tolist()
def analyse(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.npy" if cache.exists(): mfcc = np.load(cache, allow_pickle=True) else: mfcc = get_buffer( fluid.mfcc( str(workable), fftsettings=self.fftsettings, numbands=self.numbands, numcoeffs=self.numcoeffs, minfreq=self.minfreq, maxfreq=self.maxfreq, ), "numpy", ) np.save(cache, mfcc) self.buffer[str(workable)] = mfcc.tolist()
def analyse(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.npy" if cache.exists(): feature = np.load(cache, allow_pickle=True) print("loaded cache") else: y, sr = librosa.load(workable, sr=None, mono=True) feature = librosa.feature.mfcc( y=y, sr=sr, n_mfcc=self.numcoeffs, dct_type=self.dct, n_mels=self.numbands, fmax=self.maxfreq, fmin=self.minfreq, hop_length=self.hop, n_fft=self.window, ) np.save(cache, feature)
def analyse(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.npy" if cache.exists(): loudness = np.load(cache, allow_pickle=True) else: loudness = get_buffer( fluid.loudness(workable["file"], windowsize=self.windowsize, hopsize=self.hopsize, kweighting=self.kweighting, truepeak=self.truepeak, numframes=workable["numframes"], startframe=workable["startframe"]), "numpy", ) np.save(cache, loudness) workable["feature"] = loudness.tolist() self.buffer[workable["id"]] = workable
def analyse(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.wav" if not cache.exists(): slice_output = get_buffer( fluid.onsetslice( workable, indices=cache, fftsettings=self.fftsettings, filtersize=self.filtersize, framedelta=self.framedelta, metric=self.metric, minslicelength=self.minslicelength, threshold=self.threshold, ), "numpy", ) else: slice_output = get_buffer(cache) self.buffer[str(workable)] = slice_output.tolist()
def analyse(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.npy" if cache.exists(): pitch = np.load(cache, allow_pickle=True) else: pitch = get_buffer( fluid.pitch(workable["file"], algorithm=self.algorithm, minfreq=self.minfreq, maxfreq=self.maxfreq, unit=self.unit, fftsettings=self.fftsettings, numframes=workable["numframes"], startframe=workable["startframe"]), "numpy", ) np.save(cache, pitch) workable["features"] = pitch.tolist() self.buffer[workable["id"]] = workable
def analyse(self, workable): hsh = create_hash(workable, self.identity) cache = self.process.cache / f"{hsh}.npy" if cache.exists(): cqt = np.load(cache, allow_pickle=True) else: y, sr = librosa.load(workable, sr=None, mono=True) cqt = librosa.cqt(y, sr, fmin=self.minfreq, n_bins=self.n_bins, bins_per_octave=self.bins_per_octave, tuning=self.tuning, filter_scale=self.filter_scale, norm=self.norm, sparsity=self.sparsity, window=self.window, scale=self.scale, pad_mode=self.pad_mode, ) np.save(cache, cqt) self.buffer[str(workable)] = np.abs(cqt).tolist()
def analyse_items(self): # TODO import this function from a global place that can be used in CORPUS too median_loudness = {} for x in self.input: hsh = create_hash(x, self.min_loudness, self.max_loudness) cache = self.process.cache / f"{hsh}.npy" if not cache.exists(): med_loudness = get_buffer( stats(loudness(x, hopsize=4410, windowsize=17640)), "numpy") np.save(cache, med_loudness) else: med_loudness = np.load(cache, allow_pickle=True) median_loudness[str(x)] = med_loudness[0][5] vals = np.array([x for x in median_loudness.values()]) min_perc = np.percentile(vals, self.min_loudness) max_perc = np.percentile(vals, self.max_loudness) self.output = [ k for k, v in median_loudness.items() if v <= max_perc and v >= min_perc ]
def loudness(self, min_loudness: int = 0, max_loudness: int = 100): hopsize = 4410 windowsize = 17640 with Progress() as progress: task = progress.add_task("[cyan]Corpus Filtering: Loudness", total=len(self.items)) median_loudness = {} for x in self.items: hsh = create_hash(x, hopsize, windowsize) # Make sure a sane temporary path exists tmp = Path("/tmp") / "ftis_cache" tmp.mkdir(exist_ok=True) cache = tmp / f"{hsh}.npy" if not cache.exists(): med_loudness = get_buffer( stats( loudness(x, hopsize=hopsize, windowsize=windowsize)), "numpy") np.save(cache, med_loudness) else: med_loudness = np.load(cache, allow_pickle=True) median_loudness[str(x)] = med_loudness[0][5] progress.update(task, advance=1) # Get percentiles and filter vals = np.array([x for x in median_loudness.values()]) min_perc = np.percentile(vals, min_loudness) max_perc = np.percentile(vals, max_loudness) self.items = [ k for k, v in median_loudness.items() if v <= max_perc and v >= min_perc ] return self
def create_identity(self): self.identity["hash"] = create_hash(self.items, self.is_filtering, self.path, self.file_type)
def create_identity(self) -> None: p = {k: str(v) for k, v in vars(self).items() if k not in ignored_keys} self.parent_parameters = {} self.traverse_parent_parameters() self.identity["hash"] = create_hash(self.parent_parameters, p)