def test_nmf_mfcc_initialization(self): # Test initializing the MFCC range # Set up the max of the MFCC range by only using an int nmf_mfcc = nussl.NMF_MFCC(self.signal_mono, num_sources=self.n_src, num_templates=6, num_iterations=5, mfcc_range=5) assert nmf_mfcc.mfcc_start == 1 assert nmf_mfcc.mfcc_end == 5 nmf_mfcc.run() nmf_mfcc.make_audio_signals() # Set up the MFCC range by using a list [min, max] nmf_mfcc = nussl.NMF_MFCC(self.signal_mono, num_sources=self.n_src, num_templates=6, num_iterations=5, mfcc_range=[3, 15]) assert nmf_mfcc.mfcc_start == 3 assert nmf_mfcc.mfcc_end == 15 # Set up the MFCC range by using a tuple (min, max) nmf_mfcc = nussl.NMF_MFCC(self.signal_mono, num_sources=self.n_src, num_templates=6, num_iterations=5, mfcc_range=(2, 14)) assert nmf_mfcc.mfcc_start == 2 assert nmf_mfcc.mfcc_end == 14
def test_random_seed_initialization(self): # Different random_seed and random_state, check if set separately kmean_kwargs = {'random_state': 0} nmf_mfcc = nussl.NMF_MFCC(self.signal_mono, num_sources=self.n_src, num_templates=6, num_iterations=5, random_seed=1, kmeans_kwargs=kmean_kwargs) assert nmf_mfcc.clusterer.random_state == 0 assert nmf_mfcc.random_seed == 1 # No random_seed, but random_state initialized kmean_kwargs = {'random_state': 0} nmf_mfcc = nussl.NMF_MFCC(self.signal_mono, num_sources=2, num_templates=6, num_iterations=5, kmeans_kwargs=kmean_kwargs) assert nmf_mfcc.clusterer.random_state == 0 assert nmf_mfcc.random_seed is None # No random_state, but random_seed initialized nmf_mfcc = nussl.NMF_MFCC(self.signal_mono, num_sources=self.n_src, num_templates=6, num_iterations=5, random_seed=0) assert nmf_mfcc.clusterer.random_state == nmf_mfcc.random_seed == 0
def main(): """ A simple example of Non-Negative Matrix Factorization (NMF) with k-Means MFCC clustering in nussl. See nussl documentation for more information about using NMF_MFCC. Returns: """ # Load input file input_file_name = os.path.join('..', 'input', 'piano_and_synth_arp_chord_mono.wav') signal = nussl.AudioSignal(path_to_input_file=input_file_name) # make a directory to store output if needed if not os.path.exists(os.path.join('..', 'Output/')): os.mkdir(os.path.join('..', 'Output/')) # Set up NMMF MFCC nmf_mfcc = nussl.NMF_MFCC(signal, num_sources=2, num_templates=6, num_iterations=10, random_seed=0, distance_measure=nussl.TransformerNMF.EUCLIDEAN) # and run nmf_mfcc.run() sources = nmf_mfcc.make_audio_signals() for i, source in enumerate(sources): output_file_name = '{}_{}.wav'.format( os.path.splitext(signal.file_name)[0], i) source.write_audio_to_file(output_file_name)
def main(): """ Freezes essential values from NMF MFCC in its current implementation for benchmarking See test_benchmark_nmf_mfcc() in test_nmf_mfcc.py for usage. Run with top level ('nussl/') as working directory """ metadata = {} path_to_input_file = os.path.join('input', 'piano_and_synth_arp_chord_mono.wav') metadata['input_file'] = path_to_input_file signal = nussl.AudioSignal(path_to_input_file) # Set random seed in NMF and KMeans to 0 params = { 'num_sources': 2, 'num_templates': 6, 'distance_measure': nussl.transformers.TransformerNMF.EUCLIDEAN, 'num_iterations': 10, 'random_seed': 0 } metadata['params'] = params nmf_mfcc = nussl.NMF_MFCC(signal, **params) if DEBUG: output_folder = os.path.join('tests', 'nmf_mfcc_reference', 'scratch') if not os.path.exists(output_folder): os.mkdir(output_folder) else: output_folder = os.path.join('tests', 'nmf_mfcc_reference', 'nmf_mfcc_benchmark_files') nmf_mfcc.run() np.save(os.path.join(output_folder, 'benchmark_labeled_templates'), nmf_mfcc.labeled_templates) np.save(os.path.join(output_folder, 'benchmark_masks'), nmf_mfcc.result_masks) nmf_mfcc.make_audio_signals() # Make sure the paths are empty for source in nmf_mfcc.sources: source.path_to_input_file = '' np.save(os.path.join(output_folder, 'benchmark_sources'), nmf_mfcc.sources) metadata['save_time'] = time.asctime() metadata['nussl_version'] = nussl.version metadata['made_by'] = platform.uname()[1] json.dump( metadata, open(os.path.join(output_folder, 'nmf_mfcc_benchmark_metadata.json'), 'w'))
def test_nmf_mfcc(self): path = os.path.join('..', 'Input', 'piano_and_synth_arp_chord_mono.wav') a = nussl.AudioSignal(path) n = nussl.NMF_MFCC(a, num_sources=2) n() j = n.to_json() f = nussl.NMF_MFCC.from_json(j) worked = n == f return worked
def test_piano_and_synth_stereo(self): # Set up NMMF MFCC nmf_mfcc = nussl.NMF_MFCC(self.signal_stereo, num_sources=self.n_src, num_templates=6, num_iterations=15, random_seed=0) # and run nmf_mfcc.run() estimated_sources = nmf_mfcc.make_audio_signals() assert len(estimated_sources) == self.n_src for source in estimated_sources: assert source.is_stereo
def test_piano_and_synth(self): # Set up NMMF MFCC nmf_mfcc = nussl.NMF_MFCC(self.signal_mono, num_sources=self.n_src * 2, num_templates=6, num_iterations=10, random_seed=0) # and run computed_masks = nmf_mfcc.run() estimated_sources = nmf_mfcc.make_audio_signals() assert len(estimated_sources) == self.n_src for source in estimated_sources: assert source.is_mono assert self.signal_mono == estimated_sources[0] + estimated_sources[1]
def test_piano_and_synth_stereo_to_mono(self): # Set up NMMF MFCC and convert input audio signal to mono nmf_mfcc = nussl.NMF_MFCC(self.signal_stereo, num_sources=self.n_src, num_templates=6, num_iterations=15, random_seed=0, distance_measure='euclidean', to_mono=True) assert nmf_mfcc.audio_signal.is_mono # and run nmf_mfcc.run() estimated_sources = nmf_mfcc.make_audio_signals() assert len(estimated_sources) == self.n_src for source in estimated_sources: assert source.is_mono
def test_2_sin_waves(self): sr = nussl.DEFAULT_SAMPLE_RATE dur = 3 # seconds length = dur * sr sine_wave_1 = np.sin(np.linspace(0, 440 * 2 * np.pi, length)) sine_wave_2 = np.sin(np.linspace(0, 440 * 5 * 2 * np.pi, length)) signal = sine_wave_1 + sine_wave_2 test_audio = nussl.AudioSignal() test_audio.load_audio_from_array(signal) # Set up NMMF MFCC nmf_mfcc = nussl.NMF_MFCC(test_audio, num_sources=self.n_src, num_templates=2, num_iterations=15) # and run nmf_mfcc.run() nmf_mfcc.make_audio_signals()
def audio_decomp(video=None, audio=None, OutputPath=None, file=None): signal = nussl.AudioSignal(audio_data_array=audio[0, :], sample_rate=44100) nmf_mfcc = nussl.NMF_MFCC(signal, num_sources=2, num_templates=25, distance_measure="euclidean", random_seed=0) nmf_mfcc.run() sources = nmf_mfcc.make_audio_signals() source_list = [] for i, source in enumerate(sources): outputfile = os.path.join(OutputPath, file + '_seg' + str(i + 1) + '.wav') source.write_audio_to_file(outputfile) source_list.append(source.audio_data) return [ os.path.join(OutputPath, file + "_seg1.wav"), os.path.join(OutputPath, file + "_seg2.wav") ], source_list
def test_benchmark_nmf_mfcc(self): metadata_file = os.path.join('tests', 'nmf_mfcc_reference', 'nmf_mfcc_benchmark_files', 'nmf_mfcc_benchmark_metadata.json') metadata = json.load(open(metadata_file, 'r')) print('Reading benchmark files from {}, version {}, made by {}'.format( metadata['save_time'], metadata['nussl_version'], metadata['made_by'])) # Load the audio signal = nussl.AudioSignal(metadata['input_file']) # Set up NMMF MFCC params = metadata['params'] # Load params from metadata nmf_mfcc = nussl.NMF_MFCC(signal, **params) # run nmf_mfcc.run() benchmark_labeled_templates = np.load( os.path.join('tests', 'nmf_mfcc_reference', 'nmf_mfcc_benchmark_files', 'benchmark_labeled_templates.npy')) benchmark_masks = np.load( os.path.join('tests', 'nmf_mfcc_reference', 'nmf_mfcc_benchmark_files', 'benchmark_masks.npy')) nmf_mfcc.make_audio_signals() # Set the paths to empty so we can compare for source in nmf_mfcc.sources: source.path_to_input_file = '' benchmark_sources = np.load( os.path.join('tests', 'nmf_mfcc_reference', 'nmf_mfcc_benchmark_files', 'benchmark_sources.npy')) assert np.all(benchmark_sources == nmf_mfcc.sources) assert np.all( benchmark_labeled_templates == nmf_mfcc.labeled_templates) assert np.all(benchmark_masks == nmf_mfcc.result_masks)