def __coord_cqt_hz(n, fmin=None, bins_per_octave=12, **_kwargs): '''Get CQT bin frequencies''' if fmin is None: fmin = core.note_to_hz('C1') return core.cqt_frequencies(n + 1, fmin=fmin, bins_per_octave=bins_per_octave)
def __scale_axes(axes, ax_type, which): """Set the axis scaling""" kwargs = dict() if which == "x": if version_parse(matplotlib.__version__) < version_parse("3.3.0"): thresh = "linthreshx" base = "basex" scale = "linscalex" else: thresh = "linthresh" base = "base" scale = "linscale" scaler = axes.set_xscale limit = axes.set_xlim else: if version_parse(matplotlib.__version__) < version_parse("3.3.0"): thresh = "linthreshy" base = "basey" scale = "linscaley" else: thresh = "linthresh" base = "base" scale = "linscale" scaler = axes.set_yscale limit = axes.set_ylim # Map ticker scales if ax_type == "mel": mode = "symlog" kwargs[thresh] = 1000.0 kwargs[base] = 2 elif ax_type in ["cqt", "cqt_hz", "cqt_note", "cqt_svara"]: mode = "log" kwargs[base] = 2 elif ax_type in ["log", "fft_note", "fft_svara"]: mode = "symlog" kwargs[base] = 2 kwargs[thresh] = core.note_to_hz("C2") kwargs[scale] = 0.5 elif ax_type in ["tempo", "fourier_tempo"]: mode = "log" kwargs[base] = 2 limit(16, 480) else: return scaler(mode, **kwargs)
def __scale_axes(axes, ax_type, which): '''Set the axis scaling''' kwargs = dict() if which == 'x': thresh = 'linthreshx' base = 'basex' scale = 'linscalex' scaler = axes.set_xscale limit = axes.set_xlim else: thresh = 'linthreshy' base = 'basey' scale = 'linscaley' scaler = axes.set_yscale limit = axes.set_ylim # Map ticker scales if ax_type == 'mel': mode = 'symlog' kwargs[thresh] = 1000.0 kwargs[base] = 2 elif ax_type == 'log': mode = 'symlog' kwargs[base] = 2 kwargs[thresh] = core.note_to_hz('C2') kwargs[scale] = 0.5 elif ax_type in ['cqt', 'cqt_hz', 'cqt_note']: mode = 'log' kwargs[base] = 2 elif ax_type == 'tempo': mode = 'log' kwargs[base] = 2 limit(16, 480) else: return scaler(mode, **kwargs)
def __coord_cqt_hz(n, fmin=None, bins_per_octave=12, sr=22050, **_kwargs): """Get CQT bin frequencies""" if fmin is None: fmin = core.note_to_hz("C1") # Apply tuning correction fmin = fmin * 2.0**(_kwargs.get("tuning", 0.0) / bins_per_octave) # we drop by half a bin so that CQT bins are centered vertically freqs = core.cqt_frequencies( n + 1, fmin=fmin / 2.0**(0.5 / bins_per_octave), bins_per_octave=bins_per_octave, ) if np.any(freqs > 0.5 * sr): warnings.warn( "Frequency axis exceeds Nyquist. " "Did you remember to set all spectrogram parameters in specshow?") return freqs
def __decorate_axis(axis, ax_type, key="C:maj", Sa=None, mela=None, thaat=None): """Configure axis tickers, locators, and labels""" if ax_type == "tonnetz": axis.set_major_formatter(TonnetzFormatter()) axis.set_major_locator(FixedLocator(0.5 + np.arange(6))) axis.set_label_text("Tonnetz") elif ax_type == "chroma": axis.set_major_formatter(ChromaFormatter(key=key)) degrees = core.key_to_degrees(key) axis.set_major_locator( FixedLocator(0.5 + np.add.outer(12 * np.arange(10), degrees).ravel())) axis.set_label_text("Pitch class") elif ax_type == "chroma_h": if Sa is None: Sa = 0 axis.set_major_formatter(ChromaSvaraFormatter(Sa=Sa)) if thaat is None: # If no thaat is given, show all svara degrees = np.arange(12) else: degrees = core.thaat_to_degrees(thaat) # Rotate degrees relative to Sa degrees = np.mod(degrees + Sa, 12) axis.set_major_locator( FixedLocator(0.5 + np.add.outer(12 * np.arange(10), degrees).ravel())) axis.set_label_text("Svara") elif ax_type == "chroma_c": if Sa is None: Sa = 0 axis.set_major_formatter(ChromaSvaraFormatter(Sa=Sa, mela=mela)) degrees = core.mela_to_degrees(mela) # Rotate degrees relative to Sa degrees = np.mod(degrees + Sa, 12) axis.set_major_locator( FixedLocator(0.5 + np.add.outer(12 * np.arange(10), degrees).ravel())) axis.set_label_text("Svara") elif ax_type in ["tempo", "fourier_tempo"]: axis.set_major_formatter(ScalarFormatter()) axis.set_major_locator(LogLocator(base=2.0)) axis.set_label_text("BPM") elif ax_type == "time": axis.set_major_formatter(TimeFormatter(unit=None, lag=False)) axis.set_major_locator( MaxNLocator(prune=None, steps=[1, 1.5, 5, 6, 10])) axis.set_label_text("Time") elif ax_type == "s": axis.set_major_formatter(TimeFormatter(unit="s", lag=False)) axis.set_major_locator( MaxNLocator(prune=None, steps=[1, 1.5, 5, 6, 10])) axis.set_label_text("Time (s)") elif ax_type == "ms": axis.set_major_formatter(TimeFormatter(unit="ms", lag=False)) axis.set_major_locator( MaxNLocator(prune=None, steps=[1, 1.5, 5, 6, 10])) axis.set_label_text("Time (ms)") elif ax_type == "lag": axis.set_major_formatter(TimeFormatter(unit=None, lag=True)) axis.set_major_locator( MaxNLocator(prune=None, steps=[1, 1.5, 5, 6, 10])) axis.set_label_text("Lag") elif ax_type == "lag_s": axis.set_major_formatter(TimeFormatter(unit="s", lag=True)) axis.set_major_locator( MaxNLocator(prune=None, steps=[1, 1.5, 5, 6, 10])) axis.set_label_text("Lag (s)") elif ax_type == "lag_ms": axis.set_major_formatter(TimeFormatter(unit="ms", lag=True)) axis.set_major_locator( MaxNLocator(prune=None, steps=[1, 1.5, 5, 6, 10])) axis.set_label_text("Lag (ms)") elif ax_type == "cqt_note": axis.set_major_formatter(NoteFormatter(key=key)) # Where is C1 relative to 2**k hz? log_C1 = np.log2(core.note_to_hz("C1")) C_offset = 2.0**(log_C1 - np.floor(log_C1)) axis.set_major_locator(LogLocator(base=2.0, subs=(C_offset, ))) axis.set_minor_formatter(NoteFormatter(key=key, major=False)) axis.set_minor_locator( LogLocator(base=2.0, subs=C_offset * 2.0**(np.arange(1, 12) / 12.0))) axis.set_label_text("Note") elif ax_type == "cqt_svara": axis.set_major_formatter(SvaraFormatter(Sa=Sa, mela=mela)) # Find the offset of Sa relative to 2**k Hz sa_offset = 2.0**(np.log2(Sa) - np.floor(np.log2(Sa))) axis.set_major_locator(LogLocator(base=2.0, subs=(sa_offset, ))) axis.set_minor_formatter(SvaraFormatter(Sa=Sa, mela=mela, major=False)) axis.set_minor_locator( LogLocator(base=2.0, subs=sa_offset * 2.0**(np.arange(1, 12) / 12.0))) axis.set_label_text("Svara") elif ax_type in ["cqt_hz"]: axis.set_major_formatter(LogHzFormatter()) log_C1 = np.log2(core.note_to_hz("C1")) C_offset = 2.0**(log_C1 - np.floor(log_C1)) axis.set_major_locator(LogLocator(base=2.0, subs=(C_offset, ))) axis.set_major_locator(LogLocator(base=2.0)) axis.set_minor_formatter(LogHzFormatter(major=False)) axis.set_minor_locator( LogLocator(base=2.0, subs=C_offset * 2.0**(np.arange(1, 12) / 12.0))) axis.set_label_text("Hz") elif ax_type == "fft_note": axis.set_major_formatter(NoteFormatter(key=key)) # Where is C1 relative to 2**k hz? log_C1 = np.log2(core.note_to_hz("C1")) C_offset = 2.0**(log_C1 - np.floor(log_C1)) axis.set_major_locator(SymmetricalLogLocator(axis.get_transform())) axis.set_minor_formatter(NoteFormatter(key=key, major=False)) axis.set_minor_locator( LogLocator(base=2.0, subs=2.0**(np.arange(1, 12) / 12.0))) axis.set_label_text("Note") elif ax_type == "fft_svara": axis.set_major_formatter(SvaraFormatter(Sa=Sa, mela=mela)) # Find the offset of Sa relative to 2**k Hz log_Sa = np.log2(Sa) sa_offset = 2.0**(log_Sa - np.floor(log_Sa)) axis.set_major_locator( SymmetricalLogLocator(axis.get_transform(), base=2.0, subs=[sa_offset])) axis.set_minor_formatter(SvaraFormatter(Sa=Sa, mela=mela, major=False)) axis.set_minor_locator( LogLocator(base=2.0, subs=sa_offset * 2.0**(np.arange(1, 12) / 12.0))) axis.set_label_text("Svara") elif ax_type in ["mel", "log"]: axis.set_major_formatter(ScalarFormatter()) axis.set_major_locator(SymmetricalLogLocator(axis.get_transform())) axis.set_label_text("Hz") elif ax_type in ["linear", "hz", "fft"]: axis.set_major_formatter(ScalarFormatter()) axis.set_label_text("Hz") elif ax_type in ["frames"]: axis.set_label_text("Frames") elif ax_type in ["off", "none", None]: axis.set_label_text("") axis.set_ticks([]) else: raise ParameterError("Unsupported axis type: {}".format(ax_type))
import chromatemplate midilist = find_files(C.PATH_MIDI, ext="mid") itemcnt = len(midilist) i = 5000 for midifile in midilist: i += 1 print("Processing %d" % (i)) wav, sr = load(midifile, sr=C.SR) try: pm = pretty_midi.PrettyMIDI(midifile) wav = pm.fluidsynth(fs=C.SR) target = chromatemplate.GetConvnetTargetFromPianoroll( utils.GetPianoroll(midifile)) fmin = note_to_hz("C1") spec = np.vstack([ np.abs( cqt(wav, sr=C.SR, hop_length=C.H, n_bins=C.BIN_CNT, bins_per_octave=C.OCT_BIN, fmin=fmin * (h + 1))).T.astype(np.float32) for h in range(C.CQT_H) ]) except (Exception): print("Got error.Skip...") continue minsz = min([spec.shape[1], target.shape[1]])
def PlotPianoroll(P, min_note="C1"): specshow(P, y_axis="cqt_note", fmin=note_to_hz(min_note))