def from_medleydb(_session, limit=None): """Import data from medleydb Parameters ---------- session : SQLAlchemy session The session """ import itertools with open(medleydb.INST_TAXONOMY) as f_handle: data = yaml.load(f_handle) walk_taxonomy(data, _session) _session.commit() if limit is None: entries = medleydb.load_all_multitracks() else: entries = itertools.islice(medleydb.load_all_multitracks(), limit) for mt in entries: data = model.Track.from_medleydb(mt, _session) _session.add(data) _session.commit()
def get_tracks(self, min_sources=10, instruments=None, rm_silence=False, trim=None, count=None, **kwargs): if instruments: valid_instruments = instruments else: valid_instruments = self.get_valid_instruments(min_sources) logging.info('Valid instruments: ' + str(valid_instruments) + '\n') counts = defaultdict(lambda: 0) multitrack_list = mdb.load_all_multitracks() for track in multitrack_list: if not track.has_bleed: for stem in track.stems: if stem.instrument in valid_instruments: if count and counts[stem.instrument] >= count: continue counts[stem.instrument] += 1 if rm_silence or trim: ext = os.path.splitext(stem.file_path)[1] dest = tempfile.mktemp(suffix=ext, dir=self.tempdir) logging.info('Processing (sox): ' + str(stem.file_path)) sox_args = ['sox', stem.file_path, dest] if rm_silence: sox_args.extend(['silence', '1', '0.1', '0.1%', '-1', '0.1', '0.1%']) if trim: sox_args.extend(['trim', '0', str(trim)]) subprocess.Popen(sox_args, stderr=subprocess.PIPE) stem.file_path = dest stem.label = stem.instrument yield self.track_to_dict(stem) logging.info(counts)
def main(args): mtracks = mdb.load_all_multitracks() mix_coefs = dict() for mtrack in mtracks: mix_coefs[mtrack.track_id] = analyze_mix(mtrack) with open(args.output_path, 'w') as fdesc: yaml.dump(mix_coefs, fdesc)
def get_tracks(self, min_sources=10, instruments=None, rm_silence=False, trim=None, count=None, **kwargs): import medleydb as mdb if instruments: valid_instruments = instruments else: valid_instruments = self.get_valid_instruments(min_sources) logging.info('Valid instruments: ' + str(valid_instruments) + '\n') counts = defaultdict(lambda: 0) multitrack_list = mdb.load_all_multitracks() for track in multitrack_list: if not track.has_bleed: for stem in track.stems: if stem.instrument in valid_instruments: if count and counts[stem.instrument] >= count: continue counts[stem.instrument] += 1 if rm_silence or trim: ext = os.path.splitext(stem.file_path)[1] dest = tempfile.mktemp(suffix=ext, dir=self.tempdir) logging.info('Processing (sox): ' + str(stem.file_path)) sox_args = ['sox', stem.file_path, dest] if rm_silence: sox_args.extend(['silence', '1', '0.1', '0.1%', '-1', '0.1', '0.1%']) if trim: sox_args.extend(['trim', '0', str(trim)]) subprocess.Popen(sox_args, stderr=subprocess.PIPE) stem.file_path = dest stem.label = stem.instrument yield self.track_to_dict(stem) logging.info(counts)
def get_tracks(self): multitrack_list = mdb.load_all_multitracks() for track in multitrack_list: if track.genre: track.file_path = track.mix_path track.label = track.genre yield track
def get_valid_genres(self, min_sources): """Get set of genres with at least min_sources different sources""" logging.info('Determining valid genres...\n') multitrack_list = mdb.load_all_multitracks() genre_counts = defaultdict(lambda: 0) for track in multitrack_list: if track.genre: genre_counts[track.genre] += 1 logging.info(genre_counts) return {g for g in genre_counts if genre_counts[g] >= min_sources}
def get_valid_genres(self, min_sources): """Get set of genres with at least min_sources different sources""" import medleydb as mdb logging.info('Determining valid genres...\n') multitrack_list = mdb.load_all_multitracks() genre_counts = defaultdict(lambda: 0) for track in multitrack_list: if track.genre: genre_counts[track.genre] += 1 logging.info(genre_counts) return {g for g in genre_counts if genre_counts[g] >= min_sources}
def main(args): if args.use_mdb2: dataset_version = ['V1', 'V2', 'EXTRA'] else: dataset_version = ['V1'] mtracks = mdb.load_all_multitracks(dataset_version=dataset_version) Parallel(n_jobs=args.n_jobs, verbose=5)(delayed(compute_features_mtrack)( mtrack, args.save_dir, args.option, args.precomputed_hcqt_path) for mtrack in mtracks) print("Done!")
def get_valid_instruments(self, min_sources): """Get set of instruments with at least min_sources different sources""" logging.info('Determining valid instruments...\n') multitrack_list = mdb.load_all_multitracks() instrument_counts = defaultdict(lambda: 0) for track in multitrack_list: if not track.has_bleed: instruments = set() for stem in track.stems: instruments.add(stem.instrument) for instrument in instruments: instrument_counts[instrument] += 1 logging.info(instrument_counts) return {i for i in instrument_counts if instrument_counts[i] >= min_sources}
def get_valid_instruments(self, min_sources): """Get set of instruments with at least min_sources different sources""" import medleydb as mdb logging.info('Determining valid instruments...\n') multitrack_list = mdb.load_all_multitracks() instrument_counts = defaultdict(lambda: 0) for track in multitrack_list: if not track.has_bleed: instruments = set() for stem in track.stems: instruments.add(stem.instrument) for instrument in instruments: instrument_counts[instrument] += 1 logging.info(instrument_counts) return {i for i in instrument_counts if instrument_counts[i] >= min_sources}
def main(args): mtracks = mdb.load_all_multitracks(dataset_version=['V1', 'V2', 'EXTRA', 'BACH10']) mix_coefs = dict() for mtrack in mtracks: print(mtrack.track_id) # compute mixing weights on both stft and squared audio coeffs_stft = analyze_mix_stft(mtrack) coeffs_audio = analyze_mix_audio(mtrack) mix_coefs[mtrack.track_id] = {'stft': coeffs_stft, 'audio': coeffs_audio} print(mix_coefs[mtrack.track_id]) print("") with open(args.output_path, 'w') as fdesc: yaml.dump(mix_coefs, fdesc)
def main(args): mtracks = mdb.load_all_multitracks(dataset_version=['V1', 'V2', 'EXTRA']) for mtrack in mtracks: print("Processing {}...".format(mtrack.track_id)) if os.path.exists( os.path.join(args.save_dir, "{}_training_pairs.json".format( mtrack.track_id))): print(" already done!") continue json_path = get_all_audio_annot_pairs(mtrack, args.save_dir, args.resynth_path, args.replace_path) print("...saved to {}".format(json_path)) print("") print("done!")
def get_tracks(self, min_sources=10, genres=None, count=None, **kwargs): if genres: valid_genres = genres else: valid_genres = self.get_valid_genres(min_sources) logging.info('Valid genres: ' + str(valid_genres) + '\n') counts = defaultdict(lambda: 0) multitrack_list = mdb.load_all_multitracks() for track in multitrack_list: if track.genre and track.genre in valid_genres: if count and counts[track.genre] >= count: continue counts[track.genre] += 1 track.file_path = track.mix_path track.label = track.genre yield self.track_to_dict(track) logging.info(counts)
def get_tracks(self, min_sources=10, genres=None, count=None, **kwargs): import medleydb as mdb if genres: valid_genres = genres else: valid_genres = self.get_valid_genres(min_sources) logging.info('Valid genres: ' + str(valid_genres) + '\n') counts = defaultdict(lambda: 0) multitrack_list = mdb.load_all_multitracks() for track in multitrack_list: if track.genre and track.genre in valid_genres: if count and counts[track.genre] >= count: continue counts[track.genre] += 1 track.file_path = track.mix_path track.label = track.genre yield self.track_to_dict(track) logging.info(counts)
def main(args): mtracks = mdb.load_all_multitracks( dataset_version=['V1', 'V2', 'EXTRA', 'BACH10']) mix_coefs = dict() for mtrack in mtracks: print(mtrack.track_id) # compute mixing weights on both stft and squared audio coeffs_stft = analyze_mix_stft(mtrack) coeffs_audio = analyze_mix_audio(mtrack) mix_coefs[mtrack.track_id] = { 'stft': coeffs_stft, 'audio': coeffs_audio } print(mix_coefs[mtrack.track_id]) print("") with open(args.output_path, 'w') as fdesc: yaml.dump(mix_coefs, fdesc)
import utils import medleydb as mdb import json # Load all multitracks mtrack_generator = mdb.load_all_multitracks() all_tracks_id = [mtrack.track_id for mtrack in mtrack_generator] # Create a dictionary with all vocal pieces and its sources mtrack_generator = mdb.load_all_multitracks() soma = 0 lista = [] dicionario = {} for mtrack in mtrack_generator: if not mtrack.is_instrumental: soma += 1 lista.append(mtrack.track_id) dicionario[mtrack.track_id] = mtrack.stem_instruments # Save dictionary to a json file with open('vocal_pieces.json', 'w') as json_file: json.dump(dicionario, json_file) print(" >> Generated vocal_pieces.json!")
def test_all(): assert next(mdb.load_all_multitracks())
nargs='*', default=None, help='List of instruments to extract') parser.add_argument('-k', '--keep_silence', action='store_true', help="Don't remove silence from audio files") args = parser.parse_args() if args.instruments: valid_instruments = args.instruments else: valid_instruments = get_valid_instruments(args.min_sources) logging.info('Valid instruments: ' + str(valid_instruments) + '\n') multitrack_list = mdb.load_all_multitracks() for track in multitrack_list: if not track.has_bleed: for stem in track.stems: if stem.instrument in valid_instruments: filename = os.path.basename(stem.file_path) dest = os.path.join(args.destination, stem.instrument, filename) # Create directory if necessary try: os.makedirs(os.path.dirname(dest)) except OSError: pass if args.keep_silence:
def compute_feature(n_fft, hop_length, genres, chunk, rsr, unvoice, melody, bass, n_random, n_val, get_attr): # init genres = genres.split(",") t_slice = int(chunk * rsr) tracks = mdb.load_all_multitracks() instruments_dict = {} train_set = {} val_set = {} tracks_meta = [[] for i in range(len(genres))] idx_cnt = 0 print("Reading the metadata and build dictionary for instruments ...") for t in tracks: if t.genre in genres: # get the mix audio as well as build a table for instrumental attributes metadata = get_metadata(t, unvoice, melody, bass) if get_attr: for instrument in metadata["instruments"]: if instrument not in instruments_dict.keys(): instruments_dict[instrument] = idx_cnt idx_cnt += 1 tracks_meta[get_gid(t.genre, genres)].append(metadata) print("Done! Start generating features ...") target_path = "mix_path" if not unvoice else "unvoice_path" # generate data for each genre for gid, g in enumerate(genres): start = time.time() audio_chunks = [] instruments_attrs = [] print("Generating features for {} ...".format(g)) file_path = "{}.hdf5".format(g) for md in tracks_meta[gid]: print("Converting {} ...".format(md[target_path])) audio = (md[target_path],) audio += (md["melody_path"],) if melody else () # append the melody part if needed. audio += (md["bass_path"],) if bass else () # get the atual mix, and turn them into stft. chunks = get_mix_chunks(audio, t_slice, n_fft, hop_length, n_random[gid], rsr) audio_chunks.extend(chunks) print("{} clips for {}.".format(len(chunks), md[target_path])) if get_attr: attrs = get_instruments_attrs(md["instruments"], instruments_dict, len(chunks)) instruments_attrs.extend(attrs) # get random indices print("We have {} clips for {}.".format(len(audio_chunks), g)) idx = np.linspace(0, len(audio_chunks)-1, len(audio_chunks), dtype=int) np.random.shuffle(idx) audio_chunks = np.asarray(audio_chunks, dtype=np.float32) print("shape: {}, type: {}".format(audio_chunks.shape, audio_chunks.dtype)) if audio_chunks.shape[1] == 1: audio_chunks = np.squeeze(audio_chunks, axis=1) print(audio_chunks.shape) audio_chunks = (audio_chunks - audio_chunks.mean()) / audio_chunks.std() train_set["audio"] = audio_chunks[idx][n_val:, ...] val_set["audio"] = audio_chunks[idx][:n_val, ...] print(train_set["audio"].shape) print(val_set["audio"].shape) if get_attr: train_set["attrs"] = np.asarray(instruments_attrs)[idx][n_val:, ...] val_set["attrs"] = np.asarray(instruments_attrs)[idx][:n_val, ...] print("Saving ...") for k in train_set.keys(): np.save("./output/{}_{}_val.npy".format(g, k), val_set[k]) np.save("./output/{}_{}_train.npy".format(g, k), train_set[k]) print("Generation for {} is complete, {} sec elapsed.".format(g, time.time()-start))
parser.add_argument('-s', '--min_sources', type=int, default=10, help='Minimum number of seperate sources in medleyDB ' 'for an instrument to be valid') parser.add_argument('-i', '--instruments', nargs='*', default=None, help='List of instruments to extract') parser.add_argument('-k', '--keep_silence', action='store_true', help="Don't remove silence from audio files") args = parser.parse_args() if args.instruments: valid_instruments = args.instruments else: valid_instruments = get_valid_instruments(args.min_sources) logging.info('Valid instruments: ' + str(valid_instruments) + '\n') multitrack_list = mdb.load_all_multitracks() for track in multitrack_list: if not track.has_bleed: for stem in track.stems: if stem.instrument in valid_instruments: filename = os.path.basename(stem.file_path) dest = os.path.join(args.destination, stem.instrument, filename) # Create directory if necessary try: os.makedirs(os.path.dirname(dest)) except OSError: pass