def loris_synth(filename, freq_res=150, analysis_window=100,amp_floor=-30, max_len_s = 10, noise_ratio=1, hop_time=0.04): # Pure loris synth for A/B testing audio = pydub.AudioSegment.from_file(filename) audio = audio[:int(max_len_s*1000.0)] y = np.array(audio.get_array_of_samples()) if audio.channels == 2: y =y.reshape((-1, 2)) y = y[:,1] y = np.float64(y) / 2**15 analyzer = loris.Analyzer(freq_res, analysis_window) analyzer.setAmpFloor(amp_floor) analyzer.setHopTime(hop_time) partials = analyzer.analyze(y,44100) bps = 0 for i in list_from_py2_iterator(partials, len(partials)): bps = bps + len(list_from_py2_iterator(i, i.numBreakpoints())) print("%d partials %d bps" % (len(partials), bps)) loris.scaleNoiseRatio(partials, noise_ratio) return loris.synthesize(partials,44100)
def resynthesize(partials,path, samplerate = 44100, nchannels = 1, bitsPerSamp = 16): resynthesis_vector = loris.synthesize(partials,samplerate) loris.exportAiff(path,resynthesis_vector,samplerate,nchannels,bitsPerSamp)
length_of_first = get_length_of_aiff(first_aiff)[0] length_of_second = get_length_of_aiff(second_aiff)[0] longest = max(length_of_first,length_of_second) if longest == length_of_first: loris.dilate(second_analysis,[0.,length_of_second],[0,length_of_first]) elif longest == length_of_second: loris.dilate(first_analysis,[0.,length_of_first],[0,length_of_second]) #loris.distill(first_analysis) #loris.distill(second_analysis) envelope_create = loris.BreakpointEnvelope() envelope_create.insertBreakpoint(naked_start,0) envelope_create.insertBreakpoint(longest-naked_end,1) morph = loris.morph(first_analysis,second_analysis,envelope_create,envelope_create, envelope_create) loris.crop(morph,0,longest) synth = loris.synthesize(morph, 44100) loris.exportAiff( first_aiff+'morph.aiff',synth,44100, 1, 16 ) def thin_by_duration(partialList,minimum_duration):#stolen--- iter = partialList.begin() end = partialList.end() tempList = loris.PartialList() tempIter = tempList.begin() while not iter.equals(end): part = iter.partial() dur = part.duration() if dur >= minimum_duration: tempIter = tempList.insert(tempIter, part) tempIter = tempIter.next() #print tempIter iter = iter.next()
f = f + pos.frequency() n = n + 1 else: p = clar.iterator().next() it = p.iterator() while not it.atEnd(): f = f + it.next().frequency() n = n + 1 print "avg frequency of first distilled clarinet partial is", f/n print 'shifting pitch of clarinet' loris.shiftPitch( clar, loris.BreakpointEnvelope( -600 ) ) # check clarinet synthesis: loris.exportAiff( 'clarOK.pytest.aiff', loris.synthesize( clar, samplerate ), samplerate, 16 ) # # analyze flute tone (reuse Analyzer) # print 'analyzing flute 4D (%s)' % time.ctime(time.time()) a = loris.Analyzer( 270 ) # reconfigure Analyzer a.setFreqDrift( 30 ) v = loris.AiffFile( os.path.join(path, 'flute.aiff') ).samples() flut = a.analyze( v, samplerate ) loris.channelize( flut, loris.createFreqReference( flut, 291*.8, 291*1.2, 50 ), 1 ) loris.distill( flut ) # check flute synthesis: loris.exportAiff( 'flutOK.pytest.aiff', loris.synthesize( flut, samplerate ), samplerate, 16 )
print 'flute times:', flute_times loris.dilate( flut, flute_times, tgt_times ) print 'clarinet times:', clar_times loris.dilate( clar, clar_times, tgt_times ) print 'cello times:', cel_times loris.dilate( cel, cel_times, tgt_times ) # # synthesize and save dilated sources # # Save the synthesized samples files, and SDIF files # for each dilated source. # fname = 'flute.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff( fname, loris.synthesize( flut, samplerate ), samplerate, 16 ) fname = 'flute.dilated.sdif' print 'exporting sdif file:', fname, '(%s)' % time.ctime(time.time()) loris.exportSdif( fname, flut ) fname = 'clar.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff( fname, loris.synthesize( clar, samplerate ), samplerate, 16 ) fname = 'clarinet.dilated.sdif' print 'exporting sdif file:', fname, '(%s)' % time.ctime(time.time()) loris.exportSdif( fname, clar ) fname = 'cello.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff( fname, loris.synthesize( cel, samplerate ), samplerate, 16 ) fname = 'cello.dilated.sdif'
f = f + pos.frequency() n = n + 1 else: p = clar.iterator().next() it = p.iterator() while not it.atEnd(): f = f + it.next().frequency() n = n + 1 print "avg frequency of first distilled clarinet partial is", f / n print 'shifting pitch of clarinet' loris.shiftPitch(clar, loris.LinearEnvelope(-600)) # check clarinet synthesis: loris.exportAiff('clarOK.pytest.aiff', loris.synthesize(clar, samplerate), samplerate, 16) # # analyze flute tone # print 'analyzing flute 4D (%s)' % time.ctime(time.time()) a = loris.Analyzer(270) # reconfigure Analyzer a.setFreqDrift(30) v = loris.AiffFile(os.path.join(path, 'flute.aiff')).samples() flut = a.analyze(v, samplerate) # loris.channelize( flut, loris.createFreqReference( flut, 291*.8, 291*1.2, 50 ), 1 ) refenv = a.fundamentalEnv() loris.channelize(flut, refenv, 1) loris.distill(flut)
print 'flute times:', flute_times loris.dilate(flut, flute_times, tgt_times) print 'clarinet times:', clar_times loris.dilate(clar, clar_times, tgt_times) print 'cello times:', cel_times loris.dilate(cel, cel_times, tgt_times) # # synthesize and save dilated sources # # Save the synthesized samples files, and SDIF files # for each dilated source. # fname = 'flute.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff(fname, loris.synthesize(flut, samplerate), samplerate, 16) fname = 'flute.dilated.sdif' print 'exporting sdif file:', fname, '(%s)' % time.ctime(time.time()) loris.exportSdif(fname, flut) fname = 'clar.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff(fname, loris.synthesize(clar, samplerate), samplerate, 16) fname = 'clarinet.dilated.sdif' print 'exporting sdif file:', fname, '(%s)' % time.ctime(time.time()) loris.exportSdif(fname, clar) fname = 'cello.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff(fname, loris.synthesize(cel, samplerate), samplerate, 16) fname = 'cello.dilated.sdif'
def read(midi, crossover=0, fade_start=0): frequency = get_frequency(midi) loop_start = Durations[midi] - 0.3 def get_partials(frames): analyzer = loris.Analyzer(0.8 * frequency, frequency) analyzer.setFreqDrift(0.48 * frequency) partials = analyzer.analyze(frames, SAMPLE_RATE) stretch = compute_stretch_factor(partials, frequency) if (stretch < 0): stretch = 0 # loris.resample(partials, 0.01) loris.Channelizer(frequency, stretch).channelize(partials) loris.distill(partials) return partials def crop_and_extend(partials): loris.crop(partials, 0, loop_start) for p in partials: last = p.last() p.insert(loop_start + 6, loris.Breakpoint(last.frequency(), last.amplitude(), 0)) # Read file into frames noise_frames = read_file("./Input/noise-%s" % midi, rate=SAMPLE_RATE) tonal_frames = read_file("./Input/tonal-%s" % midi, rate=SAMPLE_RATE) tonal_frames = tonal_frames[:SAMPLE_RATE * math.ceil(loop_start + 1)] left_frames = tonal_frames[:, 0] right_frames = tonal_frames[:, 1] # Get partials for left and right frames left_all_partials = get_partials(left_frames) right_all_partials = get_partials(right_frames) # Retune all partials for p in left_all_partials: freq = p.label() * frequency for bp in p: bp.setBandwidth(0) bp.setFrequency(freq) for p in right_all_partials: freq = p.label() * frequency for bp in p: bp.setBandwidth(0) bp.setFrequency(freq) # Extract lower and higher partials into separate partial lists left_lower_partials = get_lower_partials(left_all_partials, crossover) right_lower_partials = get_lower_partials(right_all_partials, crossover) left_higher_partials = get_higher_partials(left_all_partials, crossover) right_higher_partials = get_higher_partials(right_all_partials, crossover) # Create mono signal for lower partials. lower_partials = merge_partials(left_lower_partials, right_lower_partials) crop_and_extend(lower_partials) # Make loop partials. These are the lower partials, but with amplitudes frozen at the start loop point loop_partials = make_loop_partials(lower_partials, loop_start) loop_frames = np.array(loris.synthesize(loop_partials, SAMPLE_RATE)) loop_length = LoopCounts[midi] * 2 loop_frames = get_looped_frames(loop_frames, midi, loop_start, loop_length, output_duration=6) # Synthesize lower partials, combine with upper frames nonloop_frames = np.array(loris.synthesize(lower_partials, SAMPLE_RATE)) loop_start_index = int(loop_start * SAMPLE_RATE) lower_frames = np.zeros(len(loop_frames)) lower_frames[:loop_start_index] = nonloop_frames[:loop_start_index] lower_frames[loop_start_index:] = loop_frames[loop_start_index:] tonal_left_frames = np.array( loris.synthesize(left_higher_partials, SAMPLE_RATE)) tonal_right_frames = np.array( loris.synthesize(right_higher_partials, SAMPLE_RATE)) fade_start = 0 fade_start_index = int(fade_start * SAMPLE_RATE) loop_start_index = int(loop_start * SAMPLE_RATE) combined_left = get_combined_frames(tonal_left_frames, noise_frames[:, 0]) combined_right = get_combined_frames(tonal_right_frames, noise_frames[:, 1]) fade_out(combined_left[fade_start_index:loop_start_index], 2) fade_out(combined_right[fade_start_index:loop_start_index], 2) combined_left[loop_start_index:] = 0 combined_right[loop_start_index:] = 0 combined_left = get_combined_frames(lower_frames, combined_left) combined_right = get_combined_frames(lower_frames, combined_right) # Find highest point of combined max_value = 0 max_index = 0 for i in range(0, len(combined_left)): if combined_left[i] > max_value: max_value = combined_left[i] max_index = i fade_len = 441 combined_left = combined_left[max_index - fade_len:] combined_right = combined_right[max_index - fade_len:] fade_in(combined_left[:fade_len]) fade_in(combined_right[:fade_len]) combined = make_stereo(combined_left, combined_right) write_file("./Intermediate/result-%s.wav" % midi, combined, SAMPLE_RATE)
def get_frames_without_partials(frames, partials): to_remove = np.array(loris.synthesize(partials, SAMPLE_RATE)) to_remove *= -1 return get_combined_frames(frames, to_remove)
print 'flute times:', flute_times loris.dilate( flut, str(flute_times), str(tgt_times) ) print 'clarinet times:', clar_times loris.dilate( clar, str(clar_times), str(tgt_times) ) print 'cello times:', cel_times loris.dilate( cel, str(cel_times), str(tgt_times) ) # # synthesize and save dilated sources # # Save the synthesized samples files, and SDIF files # for each dilated source. # fname = 'flute.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff( fname, loris.synthesize( flut, samplerate ), samplerate, 1, 16 ) fname = 'flute.dilated.sdif' print 'exporting sdif file:', fname, '(%s)' % time.ctime(time.time()) loris.exportSdif( fname, flut ) fname = 'clar.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff( fname, loris.synthesize( clar, samplerate ), samplerate, 1, 16 ) fname = 'clarinet.dilated.sdif' print 'exporting sdif file:', fname, '(%s)' % time.ctime(time.time()) loris.exportSdif( fname, clar ) fname = 'cello.dilated.aiff' print 'synthesizing', fname, '(%s)' % time.ctime(time.time()) loris.exportAiff( fname, loris.synthesize( cel, samplerate ), samplerate, 1, 16 ) fname = 'cello.dilated.sdif'