def __init__(self, features): assert 1 <= len( features ) <= 4, 'Current implementation supports up to 4 Csound features' self.features = features self.features_order = list(features) self.feature_indexes = { self.features_order[i]: i for i in range(len(self.features_order)) } template_file_path = os.path.join(self.DIR, 'csound_basic.csd.jinja2') with open(template_file_path, 'r') as template_file: template_string = template_file.read() self.template = template_handler.TemplateHandler( template_dir=self.DIR, template_string=template_string) self.template.compile(ksmps=settings.HOP_SIZE, frame_size=settings.FRAME_SIZE, features=self.features, features_order=self.features_order, num_features=len(self.features)) self.csd_path = os.path.join(settings.CSD_DIRECTORY, 'csound_analyzer.csd') self.template.write_result(self.csd_path) self.csound_handler = csound_handler.CsoundHandler(self.csd_path)
def test_serial_execution(self): self.start_time = time.time() template = template_handler.TemplateHandler(settings.EFFECT_DIRECTORY, self.template_string) template.compile( ksmps=settings.HOP_SIZE, duration=self.drums.get_duration() ) for i in range(self.num_sounds): csd_path = os.path.join( settings.CSD_DIRECTORY, experiment.Experiment.folder_name, 'test_effect_{}.csd'.format(i) ) template.write_result(csd_path) csound = csound_handler.CsoundHandler(csd_path) output_filename = self.drums.filename + '.test_processed_serial_{}.wav'.format(i) output_file_path = os.path.join( settings.OUTPUT_DIRECTORY, experiment.Experiment.folder_name, output_filename ) csound.run( input_file_path=self.drums.file_path, output_file_path=output_file_path, async=False ) self.files_to_delete.append(csd_path) self.files_to_delete.append(output_file_path) print("Serial execution time for {0} sounds: {1} seconds".format( self.num_sounds, time.time() - self.start_time) )
def augment(self, sound, factor, pause_between, seed, keep_csd, playback_speed_std_dev, gain_std_dev): assert factor >= 2 if seed >= 0: np.random.seed(seed) start_time = 0.001 playback_speed_values = np.clip( np.exp(np.random.normal(0, playback_speed_std_dev, factor)), 0.66, 1.5 ) gain_values = np.clip( np.exp(np.random.normal(0, gain_std_dev, factor)), 0.05, 3 ) playback_speed_values[0] = 1.0 gain_values[0] = 1.0 events = [] for i in range(factor): playback_speed = playback_speed_values[i] gain = gain_values[i] duration = sound.get_duration() / playback_speed events.append({ 'start': start_time, 'duration': duration, 'playback_speed': playback_speed, 'gain': gain }) start_time += duration + pause_between self.template_handler.compile( input_sound=sound, ksmps=settings.HOP_SIZE, factor=factor, events=events ) csd_path = os.path.join( settings.TEMP_DIRECTORY, 'data_augmentation.csd' ) self.template_handler.write_result(csd_path) csound = csound_handler.CsoundHandler(csd_path) output_file_path = os.path.join( settings.INPUT_DIRECTORY, sound.filename + '.augmented.wav' ) csound.run( input_file_path=None, output_file_path=output_file_path, async=False ) if not keep_csd: os.remove(csd_path) return output_file_path
def cross_adapt(self, parameter_vectors, effect, output_filename): vectors = copy.deepcopy(parameter_vectors) # map normalized values to the appropriate ranges of the effect parameters for i in range(effect.num_parameters): mapping = effect.parameters[i]['mapping'] min_value = mapping['min_value'] max_value = mapping['max_value'] skew_factor = mapping['skew_factor'] for parameter_vector in vectors: parameter_vector[ i] = standardizer.Standardizer.get_mapped_value( normalized_value=parameter_vector[i], min_value=min_value, max_value=max_value, skew_factor=skew_factor) channels = zip(*vectors) channels_csv = [] for channel in channels: channel_csv = ','.join(map(str, channel)) channels_csv.append(channel_csv) self.template.compile(parameter_names=effect.parameter_names, parameter_channels=channels_csv, ksmps=settings.HOP_SIZE, duration=self.input_sound.get_duration(), parameter_lpf_cutoff=self.parameter_lpf_cutoff) csd_path = os.path.join(settings.CSD_DIRECTORY, experiment.Experiment.folder_name, output_filename + '.csd') self.template.write_result(csd_path) csound = csound_handler.CsoundHandler(csd_path) output_file_path = os.path.join(settings.OUTPUT_DIRECTORY, experiment.Experiment.folder_name, output_filename) process = csound.run(input_file_path=self.input_sound.file_path, output_file_path=output_file_path, async=True) output_sound_file = sound_file.SoundFile(output_filename, is_input=False) return process, output_sound_file, csd_path