def main(input_filename, output_filename, units, equal_silence): audio_file = audio.LocalAudioFile(input_filename) chunks = audio_file.analysis.__getattribute__(units) num_channels = audio_file.numChannels sample_rate = audio_file.sampleRate if equal_silence: new_shape = ((audio_file.data.shape[0] * 2) + 100000, audio_file.data.shape[1]) else: new_shape = (audio_file.data.shape[0] + (len(chunks) * 44100) + 10000, audio_file.data.shape[1]) out = audio.AudioData(shape=new_shape, sampleRate=sample_rate, numChannels=num_channels) for chunk in chunks: chunk_data = audio_file[chunk] if equal_silence: silence_shape = chunk_data.data.shape else: silence_shape = (44100, audio_file.data.shape[1]) silence = audio.AudioData(shape=silence_shape, sampleRate=sample_rate, numChannels=num_channels) silence.endindex = silence.data.shape[0] out.append(chunk_data) out.append(silence) out.encode(output_filename)
def main(input_filename, output_filename, tatums, beats, bars): audiofile = audio.LocalAudioFile(input_filename) num_channels = audiofile.numChannels sample_rate = audiofile.sampleRate # mono files have a shape of (len,) out_shape = list(audiofile.data.shape) out_shape[0] = len(audiofile) out = audio.AudioData(shape=out_shape, sampleRate=sample_rate, numChannels=num_channels) # same hack to change shape: we want blip_files[0] as a short, silent blip null_shape = list(audiofile.data.shape) null_shape[0] = 2 null_audio = audio.AudioData(shape=null_shape) null_audio.endindex = len(null_audio) low_blip = audio.AudioData(blip_filenames[0]) med_blip = audio.AudioData(blip_filenames[1]) high_blip = audio.AudioData(blip_filenames[2]) all_tatums = audiofile.analysis.tatums all_beats = audiofile.analysis.beats all_bars = audiofile.analysis.bars if not all_tatums: print "Didn't find any tatums in this analysis!" print "No output." sys.exit(-1) print "going to add blips..." for tatum in all_tatums: mix_list = [audiofile[tatum], null_audio, null_audio, null_audio] if tatums: print "match! tatum start time:" + str(tatum.start) mix_list[1] = low_blip if beats: for beat in all_beats: if beat.start == tatum.start: print "match! beat start time: " + str(beat.start) mix_list[2] = med_blip break if bars: for bar in all_bars: if bar.start == tatum.start: print "match! bar start time: " + str(bar.start) mix_list[3] = high_blip break out_data = audio.megamix(mix_list) out.append(out_data) del (out_data) print "blips added, going to encode", output_filename, "..." out.encode(output_filename) print "Finito, Benito!"
def makeTransition(inData, outData, inSong, outSong): """ Takes two arrays of AudioQuantum objects and returns a single AudioData object with a linear crossfade and a beatmatch """ # The number of beats has to be the same assert (len(inData) == len(outData)) # If the tempos are the same then it is easy if inSong.bpm == outSong.bpm: mixInSeg = audio.getpieces(inSong.AudioFile, inData) mixOutSeg = audio.getpieces(outSong.AudioFile, outData) # mixInSeg.encode("outfiles/Mixinseg.mp3") # mixOutSeg.encode("outfiles/MixOutseg.mp3") transition_length = 60.0 / 128.0 * MIX_LENGTH transitionSeg = action.Crossfade([mixOutSeg, mixInSeg], [0.0, 0.0], transition_length, mode="linear").render() return transitionSeg # Else we iterate over each one else: transitionTime = 0 tempoDiff = inSong.bpm - outSong.bpm marginalInc = float(tempoDiff) / float(MIX_LENGTH) inCollect = [] outCollect = [] # Rather than a linear slowdown, it is a step function where each beat slows down by marginalInc for i in range(MIX_LENGTH): inAudio = inData[i].render() outAudio = outData[i].render() # We scale the in and out beats so that they are at the same temp inScaled = dirac.timeScale( inAudio.data, inSong.bpm / (outSong.bpm + i * marginalInc)) outScaled = dirac.timeScale( outAudio.data, outSong.bpm / (outSong.bpm + i * marginalInc)) transitionTime += 60 / (outSong.bpm + i * marginalInc) ts = audio.AudioData(ndarray=inScaled, shape=inScaled.shape, sampleRate=inSong.AudioFile.sampleRate, numChannels=inScaled.shape[1]) ts2 = audio.AudioData(ndarray=outScaled, shape=outScaled.shape, sampleRate=outSong.AudioFile.sampleRate, numChannels=outScaled.shape[1]) inCollect.append(ts) outCollect.append(ts2) # Collect the audio and crossfade it mixInSeg = audio.assemble(inCollect, numChannels=2) mixOutSeg = audio.assemble(outCollect, numChannels=2) # mixInSeg.encode("outfiles/Mixinseg.mp3") # mixOutSeg.encode("outfiles/MixOutseg.mp3") transitionSeg = action.Crossfade([mixOutSeg, mixInSeg], [0.0, 0.0], transitionTime, mode="exponential").render() return transitionSeg
def insert_sfx(song_path): paths = ['bin' + os.sep + f for f in os.listdir('bin')] cowbell_paths = filter(lambda a: 'cowbell' in a, paths) cowbells = [ audio.AudioData(path, sampleRate=44100, numChannels=2) for path in cowbell_paths ] # other_paths = filter(lambda a: 'cowbell' not in a, paths) # others = [audio.AudioData(path, sampleRate = 44100, numChannels = 2) for path in other_paths] song = audio.LocalAudioFile(song_path) beats = song.analysis.beats tatums = song.analysis.tatums bars = song.analysis.bars beats_raw = [b.start for b in beats] tatums_raw = [t.start for t in beats] bars_raw = [b.start for b in bars] for tatum in tatums: if random.random() > 0.75: continue if is_in(tatum.start, bars_raw): song = mix(tatum.start, random.choice(cowbells), song) elif is_in(tatum.start, beats_raw) and random.random() < 0.75: song = mix(tatum.start, random.choice(cowbells), song) elif random.random() < 0.3: song = mix(tatum.start, random.choice(cowbells), song) return song
def main(input_filename, output_filename): # Just a local alias to the soundtouch library, which handles the pitch shifting. soundtouch = modify.Modify() # This takes your input track, sends it to the analyzer, and returns the results. audiofile = audio.LocalAudioFile(input_filename) # This gets a list of every beat in the track. # You can manipulate this just like any other Python list! beats = audiofile.analysis.beats # This creates a new chunk of audio that is the same size and shape as the input file # We can do this because we know that our output will be the same size as our input out_shape = (len(audiofile.data), ) out_data = audio.AudioData(shape=out_shape, numChannels=1, sampleRate=44100) # This loop pitch-shifts each beat and adds it to the new file! for i, beat in enumerate(beats): # Pitch shifting only works on the data from each beat, not the beat objet itself data = audiofile[beat].data # The amount to pitch shift each beat. # local_context just returns a tuple the position of a beat within its parent bar. # (0, 4) for the first beat of a bar, for example number = beat.local_context()[0] % 12 # Do the shift! new_beat = soundtouch.shiftPitchSemiTones(audiofile[beat], number * -1) out_data.append(new_beat) # Write the new file out_data.encode(output_filename)
def changeOneNoteOctave(note, noteList, octaves): new_note = shiftPitchOctaves(note.render().data, octaves) dat = audio.AudioData(ndarray=new_note, shape=new_note.shape, numChannels=new_note.shape[1], sampleRate=44100) noteList.append(dat.render())
def newTempo(song, oldTempo, newTempo): # ratio = newTempo / oldTempo ratio = oldTempo / newTempo scaled = dirac.timeScale(song.data, ratio) out = audio.AudioData(ndarray = scaled, shape = scaled.shape, sampleRate = song.sampleRate, numChannels = scaled.shape[1]) return out
def main(input_one, input_two): track_one = audio.LocalAudioFile(input_one) track_two = audio.LocalAudioFile(input_two) section_one = track_one.analysis.sections[0] section_two = track_two.analysis.sections[-1] tempo_one = section_one.tempo tempo_two = section_two.tempo tempo_diff = tempo_two - tempo_one bars_one = section_one.children() collect = [] for bar in bars_one: if bar == bars_one[-1]: numbeats = len(bar.children()) step = tempo_diff/numbeats for i, beat in enumerate(bar.children()): beat_audio = beat.render() ratio = (tempo_one + step*(i+1))/(tempo_one + step*i) scaled_beat = dirac.timeScale(beat_audio.data, ratio) new_beat = audio.AudioData(ndarray=scaled_beat, shape=scaled_beat.shape, sampleRate=track_one.sampleRate, numChannels=scaled_beat.shape[1]) collect.append(new_beat) break for beat in bar.children(): collect.append(beat.render()) out_data_one = audio.assemble(collect, numChannels=2) out_name_one = input_one.split('.')[0]+'-stretch.mp3' out_data_one.encode(out_name_one) play_one = audio.LocalAudioFile(out_name_one) aqp_one = Player(play_one) aqp_two = Player(track_two) beats_one = play_one.analysis.beats for beat in beats_one: aqp_one.play(beat) aqp_one.closeStream() aqp_two.play(section_two) aqp_two.closeStream()
def changeToCorrectTempo(audioFile, targetTempo): #takes in an audioFile, targetTempo, returns audioFile @ correct tempo currentTempo = audioFile.analysis.tempo['value'] bars = audioFile.analysis.bars collect = [] # This loop streches each beat by a varying ratio, and then re-assmbles them. for bar in bars: # Get the beats in the bar beats = bar.children() for beat in beats: # Note that dirac can't compress by less than 0.5! ratio = currentTempo / targetTempo #formula for changing currentTempo to targetTempo # Get the raw audio data from the beat and scale it by the ratio # dirac only works on raw data, and only takes floating-point ratios beat_audio = beat.render() scaled_beat = dirac.timeScale(beat_audio.data, ratio) # Create a new AudioData object from the scaled data ts = audio.AudioData(ndarray=scaled_beat, shape=scaled_beat.shape, sampleRate=audioFile.sampleRate, numChannels=scaled_beat.shape[1]) # Append the new data to the output list! collect.append(ts) # Assemble and write the output data output = audio.assemble(collect, numChannels=2) return output
def matchDuration(note, songseg, end): ratio = 3 * songseg.duration / note.duration print ratio for seg in note: seg_audio = seg.render() scaled_seg = dirac.timeScale(seg_audio.data, ratio) ts = audio.AudioData(ndarray=scaled_seg, shape=scaled_seg.shape, sampleRate=44100, numChannels=scaled_seg.shape[1]) end.append(ts)
def split_break(breakfile,n): drum_data = [] start = 0 for i in range(n): start = int((len(breakfile) * (i))/n) end = int((len(breakfile) * (i+1))/n) ndarray = breakfile.data[start:end] new_data = audio.AudioData(ndarray=ndarray, sampleRate=breakfile.sampleRate, numChannels=breakfile.numChannels) drum_data.append(new_data) return drum_data
def main(input, semitones): track = audio.LocalAudioFile(input) collect = [] for section in track.analysis.sections: section_data = section.render().data new_data = PyPitch.shiftPitchSemiTones(section_data, semitones) ts = audio.AudioData(ndarray=new_data, shape=new_data.shape, sampleRate=track.sampleRate, numChannels=new_data.shape[1]) collect.append(ts) out = audio.assemble(collect, numChannels=2) out.encode( input.split('.')[0] + '_' + ('d' if semitones < 0 else 'u') + str(abs(semitones)) + '.mp3')
def main(input_filename, output_filename, ratio): audiofile = audio.LocalAudioFile(input_filename) beats = audiofile.analysis.beats collect = [] for beat in beats: beat_audio = beat.render() scaled_beat = dirac.timeScale(beat_audio.data, ratio) ts = audio.AudioData(ndarray=scaled_beat, shape=scaled_beat.shape, sampleRate=audiofile.sampleRate, numChannels=scaled_beat.shape[1]) collect.append(ts) out = audio.assemble(collect, numChannels=2) out.encode(output_filename)
def main(input_filename, output_filename): soundtouch = modify.Modify() audiofile = audio.LocalAudioFile(input_filename) beats = audiofile.analysis.beats out_shape = (len(audiofile.data), ) out_data = audio.AudioData(shape=out_shape, numChannels=1, sampleRate=44100) for i, beat in enumerate(beats): data = audiofile[beat].data number = beat.local_context()[0] % 12 new_beat = soundtouch.shiftPitchSemiTones(audiofile[beat], number * -1) out_data.append(new_beat) out_data.encode(output_filename)
def main(input_filename, output_filename): audiofile = audio.LocalAudioFile(input_filename) bars = audiofile.analysis.bars collect = [] for bar in bars: bar_ratio = (bars.index(bar) % 4) / 2.0 beats = bar.children() for beat in beats: beat_index = beat.local_context()[0] ratio = beat_index / 2.0 + 0.5 ratio = ratio + bar_ratio # dirac can't compress by less than 0.5! beat_audio = beat.render() scaled_beat = dirac.timeScale(beat_audio.data, ratio) ts = audio.AudioData(ndarray=scaled_beat, shape=scaled_beat.shape, sampleRate=audiofile.sampleRate, numChannels=scaled_beat.shape[1]) collect.append(ts) out = audio.assemble(collect, numChannels=2) out.encode(output_filename)
def correct_segment_length(self, segment_data, reference_data): # if new segment is too short, pad w silence if segment_data.endindex < reference_data.endindex: if self.out.numChannels > 1: silence_shape = (reference_data.endindex, self.out.numChannels) else: silence_shape = (reference_data.endindex, ) new_segment = audio.AudioData(shape=silence_shape, sampleRate=self.out.sampleRate, numChannels=segment_data.numChannels) new_segment.append(segment_data) new_segment.endindex = len(new_segment) # or if new segment is too long, truncate it elif segment_data.endindex > reference_data.endindex: new_segment = segment_data # index = slice(0, int(reference_data.endindex), 1) # new_segment = audio.AudioData(None,segment_data.data[index], # sampleRate=segment_data.sampleRate) return new_segment
def initialize_output(self): # This chunk creates a new array of AudioData to put the resulting resynthesis in: # Add two seconds to the length, just in case dur = len(self.input_a.data) + 100000 # This determines the 'shape' of new array. # (Shape is a tuple (x, y) that indicates the length per channel of the audio file) # If we have a stereo shape, copy that shape if len(self.input_a.data.shape) > 1: new_shape = (dur, self.input_a.data.shape[1]) new_channels = self.input_a.data.shape[1] # If not, make a mono shape else: new_shape = (dur, ) new_channels = 1 # This creates the new AudioData array, based on the new shape out = audio.AudioData(shape=new_shape, sampleRate=self.input_b.sampleRate, numChannels=new_channels) return out
def main(input_filename, output_filename): audiofile = audio.LocalAudioFile(input_filename) bars = audiofile.analysis.bars collect = audio.AudioQuantumList() count = 0 for bar in bars: try: beat = bar.children()[1] beat_audio = beat.render() scaled_beat = dirac.timeScale( beat_audio.data, 1.2) if count == 1 else dirac.timeScale( beat_audio.data, 1.0) ts = audio.AudioData(ndarray=scaled_beat, shape=scaled_beat.shape, sampleRate=audiofile.sampleRate, numChannels=scaled_beat.shape[1]) collect.append(ts) count = (count + 1) % 3 except IndexError: pass out = audio.assemble(collect, numChannels=2) out.encode(output_filename)
def main(input_filename, output_filename): # This takes your input track, sends it to the analyzer, and returns the results. audiofile = audio.LocalAudioFile(input_filename) # This gets a list of every bar in the track. bars = audiofile.analysis.bars # The output array collect = [] # This loop streches each beat by a varying ratio, and then re-assmbles them. for bar in bars: # Caculate a stretch ratio that repeats every four bars. bar_ratio = (bars.index(bar) % 4) / 2.0 # Get the beats in the bar beats = bar.children() for beat in beats: # Find out where in the bar the beat is. beat_index = beat.local_context()[0] # Calculate a stretch ratio based on where in the bar the beat is ratio = beat_index / 2.0 + 0.5 # Note that dirac can't compress by less than 0.5! ratio = ratio + bar_ratio # Get the raw audio data from the beat and scale it by the ratio # dirac only works on raw data, and only takes floating-point ratios beat_audio = beat.render() scaled_beat = dirac.timeScale(beat_audio.data, ratio) # Create a new AudioData object from the scaled data ts = audio.AudioData(ndarray=scaled_beat, shape=scaled_beat.shape, sampleRate=audiofile.sampleRate, numChannels=scaled_beat.shape[1]) # Append the new data to the output list! collect.append(ts) # Assemble and write the output data out = audio.assemble(collect, numChannels=2) out.encode(output_filename)
def remove_vocals(song): '''give it a AudioData file, it'll return one w/ vocals removed''' if not issubclass(song.__class__, audio.AudioData): print 'Got %s, expected AudioData' % type(song) raise TypeError # new array of same size out_shape = (len(song.data), ) bg_only = audio.AudioData(shape=out_shape, numChannels=1, sampleRate=44100) # iterate though old song idx_0 = None count = 0 assign = 0 print len(song.data) for x in np.nditer(song.data): if count % 2 == 1: new = np.subtract(np.divide(idx_0, 2), np.divide(x, 2)) bg_only.data[assign] = new assign += 1 else: idx_0 = x count += 1 if count % 100000 == 0: print count return bg_only
""" # constants SLEIGH_OFFSET = [-0.2, -0.075] # seconds SIGNAL_OFFSET = -1.0 # seconds SANTA_OFFSET = -6.25 # seconds SLEIGH_THRESHOLD = 0.420 # seconds MIN_BEAT_DURATION = 0.280 # seconds MAX_BEAT_DURATION = 0.560 # seconds LIMITER_THRESHOLD = 29491 # 90% of the dynamic range LIMITER_COEFF = 0.2 # compresion curve # samples soundsPath = "sounds/" snowSantaSounds = map(lambda x: audio.AudioData(os.path.join(soundsPath, "snowSanta%s.wav" % x), sampleRate=44100, numChannels=2), range(1)) sleighBellSounds = map(lambda x: audio.AudioData(os.path.join(soundsPath, "sleighBell%s.wav" % x), sampleRate=44100, numChannels=2), range(2)) signalBellSounds = map(lambda x: audio.AudioData(os.path.join(soundsPath, "signalBell%s.wav" % x), sampleRate=44100, numChannels=2), range(3)) # some math useful for normalization def linear(input, in1, in2, out1, out2): return ((input-in1) / (in2-in1)) * (out2-out1) + out1 class Jingler: def __init__(self, input_file): self.audiofile = audio.LocalAudioFile(input_file) self.audiofile.data *= linear(self.audiofile.analysis.loudness, -2, -12, 0.5, 1.5) * 0.75 # Check that there are beats in the song if len(self.audiofile.analysis.beats) < 5: print 'not enough beats in this song...'
Example: python cowbell.py YouCanCallMeAl.mp3 YouCanCallMeCow.mp3 0.2 0.5 Reference: http://www.youtube.com/watch?v=ZhSkRHXTKlw """ # constants COWBELL_THRESHOLD = 0.85 COWBELL_OFFSET = -0.005 # samples soundsPath = "sounds/" cowbellSounds = map( lambda x: audio.AudioData(os.path.join(soundsPath, "cowbell%s.wav" % x), sampleRate=44100, numChannels=2), range(5)) walkenSounds = map( lambda x: audio.AudioData(os.path.join(soundsPath, "walken%s.wav" % x), sampleRate=44100, numChannels=2), range(16)) trill = audio.AudioData(os.path.join(soundsPath, "trill.wav"), sampleRate=44100, numChannels=2) def linear(input, in1, in2, out1, out2): return ((input - in1) / (in2 - in1)) * (out2 - out1) + out1 def exp(input, in1, in2, out1, out2, coeff):
def main(input_filename, output_filename, break_filename, break_parts, measures, mix): audiofile = audio.LocalAudioFile(input_filename) sample_rate = audiofile.sampleRate breakfile = audio.LocalAudioFile(break_filename) if breakfile.numChannels == 1: breakfile = mono_to_stereo(breakfile) num_channels = audiofile.numChannels drum_data = split_break(breakfile, break_parts) hits_per_beat = int(break_parts / (4 * measures)) bars = audiofile.analysis.bars out_shape = (len(audiofile) + 100000, num_channels) out = audio.AudioData(shape=out_shape, sampleRate=sample_rate, numChannels=num_channels) if not bars: print "Didn't find any bars in this analysis!" print "No output." sys.exit(-1) for bar in bars[:-1]: beats = bar.children() for i in range(len(beats)): try: break_index = ((bar.local_context()[0] %\ measures) * 4) + (i % 4) except ValueError: break_index = i % 4 tats = range((break_index) * hits_per_beat, (break_index + 1) * hits_per_beat) drum_samps = sum([len(drum_data[x]) for x in tats]) beat_samps = len(audiofile[beats[i]]) beat_shape = (beat_samps, num_channels) tat_shape = (float(beat_samps / hits_per_beat), num_channels) beat_data = audio.AudioData(shape=beat_shape, sampleRate=sample_rate, numChannels=num_channels) for j in tats: tat_data = audio.AudioData(shape=tat_shape, sampleRate=sample_rate, numChannels=num_channels) if drum_samps > beat_samps / hits_per_beat: # truncate drum hits to fit beat length tat_data.data = drum_data[j].data[:len(tat_data)] elif drum_samps < beat_samps / hits_per_beat: # space out drum hits to fit beat length #temp_data = add_fade_out(drum_data[j]) tat_data.append(drum_data[j]) tat_data.endindex = len(tat_data) beat_data.append(tat_data) del (tat_data) # account for rounding errors beat_data.endindex = len(beat_data) mixed_beat = audio.mix(beat_data, audiofile[beats[i]], mix=mix) del (beat_data) out.append(mixed_beat) finale = bars[-1].start + bars[-1].duration last = audio.AudioQuantum( audiofile.analysis.bars[-1].start, audiofile.analysis.duration - audiofile.analysis.bars[-1].start) last_data = audio.getpieces(audiofile, [last]) out.append(last_data) out.encode(output_filename)
def main(input_filename, output_filename, break_filename, break_parts, measures, mix, samples_dir): print break_filename audiofile = audio.LocalAudioFile(input_filename) sample_rate = audiofile.sampleRate breakfile = audio.LocalAudioFile(break_filename) if breakfile.numChannels == 1: breakfile = mono_to_stereo(breakfile) num_channels = audiofile.numChannels drum_data = split_break(breakfile, break_parts) hits_per_beat = int(break_parts / (4 * measures)) bars = audiofile.analysis.bars out_shape = (len(audiofile) + 100000, num_channels) out = audio.AudioData(shape=out_shape, sampleRate=sample_rate, numChannels=num_channels) if not bars: print "Didn't find any bars in this analysis!" print "No output." sys.exit(-1) for bar in bars[:-1]: beats = bar.children() for i in range(len(beats)): try: break_index = ((bar.local_context()[0] %\ measures) * 4) + (i % 4) except ValueError: break_index = i % 4 tats = range((break_index) * hits_per_beat, (break_index + 1) * hits_per_beat) drum_samps = sum([len(drum_data[x]) for x in tats]) beat_samps = len(audiofile[beats[i]]) beat_shape = (beat_samps, num_channels) tat_shape = (float(beat_samps / hits_per_beat), num_channels) beat_data = audio.AudioData(shape=beat_shape, sampleRate=sample_rate, numChannels=num_channels) for j in tats: tat_data = audio.AudioData(shape=tat_shape, sampleRate=sample_rate, numChannels=num_channels) if drum_samps > beat_samps / hits_per_beat: # truncate drum hits to fit beat length tat_data.data = drum_data[j].data[:len(tat_data)] elif drum_samps < beat_samps / hits_per_beat: # space out drum hits to fit beat length #temp_data = add_fade_out(drum_data[j]) tat_data.append(drum_data[j]) tat_data.endindex = len(tat_data) beat_data.append(tat_data) del (tat_data) # account for rounding errors beat_data.endindex = len(beat_data) mixed_beat = audio.mix(beat_data, audiofile[beats[i]], mix=mix) del (beat_data) out.append(mixed_beat) finale = bars[-1].start + bars[-1].duration last = audio.AudioQuantum( audiofile.analysis.bars[-1].start, audiofile.analysis.duration - audiofile.analysis.bars[-1].start) last_data = audio.getpieces(audiofile, [last]) out.append(last_data) samples_avail = os.listdir(samples_dir) #throw down some classic trap samples #the outfile should be the same length as the output file, so just go through that file instead for section in audiofile.analysis.sections[1:]: overlay_sound_file = pickSample(samples_avail) soundpath = os.path.join(str(samples_dir), overlay_sound_file) print soundpath sample = audio.LocalAudioFile(soundpath) # Mix the audio volume = 0.9 pan = 0 startsample = int(section.start * out.sampleRate) seg = sample[0:] seg.data *= (volume - (pan * volume), volume + (pan * volume) ) # pan + volume if out.data.shape[0] - startsample > seg.data.shape[0]: out.data[startsample:startsample + len(seg.data)] += seg.data[0:] out.encode(output_filename)
def run(self, mix=0.5, envelope=False): dur = len(self.input_a.data) + 100000 # another two seconds # determine shape of new array. # do everything in mono; I'm not fancy. new_shape = (dur,) new_channels = 1 self.input_a = action.make_mono(self.input_a) self.input_b = action.make_mono(self.input_b) out = audio.AudioData(shape=new_shape, sampleRate=self.input_b.sampleRate, numChannels=new_channels) for a in self.segs_a: seg_index = a.absolute_context()[0] # find best match from segs in B distance_matrix = self.calculate_distances(a) distances = [numpy.sqrt(x[0]+x[1]+x[2]) for x in distance_matrix] match = self.segs_b[distances.index(min(distances))] segment_data = self.input_b[match] reference_data = self.input_a[a] if segment_data.endindex < reference_data.endindex: if new_channels > 1: silence_shape = (reference_data.endindex,new_channels) else: silence_shape = (reference_data.endindex,) new_segment = audio.AudioData(shape=silence_shape, sampleRate=out.sampleRate, numChannels=segment_data.numChannels) new_segment.append(segment_data) new_segment.endindex = len(new_segment) segment_data = new_segment elif segment_data.endindex > reference_data.endindex: index = slice(0, int(reference_data.endindex), 1) segment_data = audio.AudioData(None,segment_data.data[index], sampleRate=segment_data.sampleRate) chopvideo = self.slave.video[match] # get editableframes object masterchop = self.master.video[a] startframe = self.master.video.indexvoodo(a.start) # find start index endframe = self.master.video.indexvoodo(a.start + a.duration) for i in xrange(len(chopvideo.files)): if startframe+i < len(self.master.video.files): self.master.video.files[startframe+i] = chopvideo.files[i] last_frame = chopvideo.files[i] for i in xrange(len(chopvideo.files), len(masterchop.files)): if startframe+i < len(self.master.video.files): self.master.video.files[startframe+i] = last_frame if envelope: # db -> voltage ratio http://www.mogami.com/e/cad/db.html linear_max_volume = pow(10.0,a.loudness_max/20.0) linear_start_volume = pow(10.0,a.loudness_begin/20.0) if(seg_index == len(self.segs_a)-1): # if this is the last segment linear_next_start_volume = 0 else: linear_next_start_volume = pow(10.0,self.segs_a[seg_index+1].loudness_begin/20.0) pass when_max_volume = a.time_loudness_max # Count # of ticks I wait doing volume ramp so I can fix up rounding errors later. ss = 0 # Set volume of this segment. Start at the start volume, ramp up to the max volume , then ramp back down to the next start volume. cur_vol = float(linear_start_volume) # Do the ramp up to max from start samps_to_max_loudness_from_here = int(segment_data.sampleRate * when_max_volume) if(samps_to_max_loudness_from_here > 0): how_much_volume_to_increase_per_samp = float(linear_max_volume - linear_start_volume)/float(samps_to_max_loudness_from_here) for samps in xrange(samps_to_max_loudness_from_here): try: segment_data.data[ss] *= cur_vol except IndexError: pass cur_vol = cur_vol + how_much_volume_to_increase_per_samp ss = ss + 1 # Now ramp down from max to start of next seg samps_to_next_segment_from_here = int(segment_data.sampleRate * (a.duration-when_max_volume)) if(samps_to_next_segment_from_here > 0): how_much_volume_to_decrease_per_samp = float(linear_max_volume - linear_next_start_volume)/float(samps_to_next_segment_from_here) for samps in xrange(samps_to_next_segment_from_here): cur_vol = cur_vol - how_much_volume_to_decrease_per_samp try: segment_data.data[ss] *= cur_vol except IndexError: pass ss = ss + 1 mixed_data = audio.mix(segment_data,reference_data,mix=mix) out.append(mixed_data) self.master.audio = out self.master.save(self.output_filename)
def main(input_filename, output_filename, break_filename, break_parts, measures, mix): # This takes the input tracks, sends them to the analyzer, and returns the results. audiofile = audio.LocalAudioFile(input_filename) sample_rate = audiofile.sampleRate breakfile = audio.LocalAudioFile(break_filename) # This converts the break to stereo, if it is mono if breakfile.numChannels == 1: breakfile = mono_to_stereo(breakfile) # This gets the number of channels in the main file num_channels = audiofile.numChannels # This splits the break into each beat drum_data = split_break(breakfile, break_parts) hits_per_beat = int(break_parts/(4 * measures)) # This gets the bars from the input track bars = audiofile.analysis.bars # This creates the 'shape' of new array. # (Shape is a tuple (x, y) that indicates the length per channel of the audio file) out_shape = (len(audiofile)+100000,num_channels) # This creates a new AudioData array to write data to out = audio.AudioData(shape=out_shape, sampleRate=sample_rate, numChannels=num_channels) if not bars: # If the analysis can't find any bars, stop! # (This might happen with really ambient music) print "Didn't find any bars in this analysis!" print "No output." sys.exit(-1) # This is where the magic happens: # For every beat in every bar except the last bar, # map the tatums of the break to the tatums of the beat for bar in bars[:-1]: # This gets the beats in the bar, and loops over them beats = bar.children() for i in range(len(beats)): # This gets the index of matching beat in the break try: break_index = ((bar.local_context()[0] %\ measures) * 4) + (i % 4) except ValueError: break_index = i % 4 # This gets the tatums from the beat of the break tats = range((break_index) * hits_per_beat, (break_index + 1) * hits_per_beat) # This gets the number of samples in each tatum drum_samps = sum([len(drum_data[x]) for x in tats]) # This gets the number of sample and the shape of the beat from the original track beat_samps = len(audiofile[beats[i]]) beat_shape = (beat_samps,num_channels) # This get the shape of each tatum tat_shape = (float(beat_samps/hits_per_beat),num_channels) # This creates the new AudioData that will be filled with chunks of the drum break beat_data= audio.AudioData(shape=beat_shape, sampleRate=sample_rate, numChannels=num_channels) for j in tats: # This creates an audioData for each tatum tat_data= audio.AudioData(shape=tat_shape, sampleRate=sample_rate, numChannels=num_channels) # This corrects for length / timing: # If the original is shorter than the break, truncate drum hits to fit beat length if drum_samps > beat_samps/hits_per_beat: tat_data.data = drum_data[j].data[:len(tat_data)] # If the original is longer, space out drum hits to fit beat length elif drum_samps < beat_samps/hits_per_beat: tat_data.append(drum_data[j]) # This adds each new tatum to the new beat. tat_data.endindex = len(tat_data) beat_data.append(tat_data) del(tat_data) # This corrects for rounding errors beat_data.endindex = len(beat_data) # This mixes the new beat data with the input data, and appends it to the final file mixed_beat = audio.mix(beat_data, audiofile[beats[i]], mix=mix) del(beat_data) out.append(mixed_beat) # This works out the last beat and appends it to the final file finale = bars[-1].start + bars[-1].duration last = audio.AudioQuantum(audiofile.analysis.bars[-1].start, audiofile.analysis.duration - audiofile.analysis.bars[-1].start) last_data = audio.getpieces(audiofile,[last]) out.append(last_data) # This writes the newly created audio to the given file. out.encode(output_filename)
def run(self, mix=0.5, envelope=False): # This chunk creates a new array of AudioData to put the resulting resynthesis in: # Add two seconds to the length, just in case dur = len(self.input_a.data) + 100000 # This determines the 'shape' of new array. # (Shape is a tuple (x, y) that indicates the length per channel of the audio file) # If we have a stereo shape, copy that shape if len(self.input_a.data.shape) > 1: new_shape = (dur, self.input_a.data.shape[1]) new_channels = self.input_a.data.shape[1] # If not, make a mono shape else: new_shape = (dur,) new_channels = 1 # This creates the new AudioData array, based on the new shape out = audio.AudioData(shape=new_shape, sampleRate=self.input_b.sampleRate, numChannels=new_channels) # Now that we have a properly formed array to put chunks of audio in, # we can start deciding what chunks to put in! # This loops over each segment in file A and finds the best matching segment from file B for a in self.segs_a: seg_index = a.absolute_context()[0] # This works out the distances distance_matrix = self.calculate_distances(a) distances = [numpy.sqrt(x[0]+x[1]+x[2]) for x in distance_matrix] # This gets the best match match = self.segs_b[distances.index(min(distances))] segment_data = self.input_b[match] reference_data = self.input_a[a] # This corrects for length: if our new segment is shorter, we add silence if segment_data.endindex < reference_data.endindex: if new_channels > 1: silence_shape = (reference_data.endindex,new_channels) else: silence_shape = (reference_data.endindex,) new_segment = audio.AudioData(shape=silence_shape, sampleRate=out.sampleRate, numChannels=segment_data.numChannels) new_segment.append(segment_data) new_segment.endindex = len(new_segment) segment_data = new_segment # Or, if our new segment is too long, we make it shorter elif segment_data.endindex > reference_data.endindex: index = slice(0, int(reference_data.endindex), 1) segment_data = audio.AudioData(None,segment_data.data[index], sampleRate=segment_data.sampleRate) # This applies the volume envelopes from each segment of A to the segment from B. if envelope: # This gets the maximum volume and starting volume for the segment from A: # db -> voltage ratio http://www.mogami.com/e/cad/db.html linear_max_volume = pow(10.0,a.loudness_max/20.0) linear_start_volume = pow(10.0,a.loudness_begin/20.0) # This gets the starting volume for the next segment if(seg_index == len(self.segs_a)-1): # If this is the last segment, the next volume is zero linear_next_start_volume = 0 else: linear_next_start_volume = pow(10.0,self.segs_a[seg_index+1].loudness_begin/20.0) pass # This gets when the maximum volume occurs in A when_max_volume = a.time_loudness_max # Count # of ticks I wait doing volume ramp so I can fix up rounding errors later. ss = 0 # This sets the starting volume volume of this segment. cur_vol = float(linear_start_volume) # This ramps up to the maximum volume from start samps_to_max_loudness_from_here = int(segment_data.sampleRate * when_max_volume) if(samps_to_max_loudness_from_here > 0): how_much_volume_to_increase_per_samp = float(linear_max_volume - linear_start_volume)/float(samps_to_max_loudness_from_here) for samps in xrange(samps_to_max_loudness_from_here): try: # This actally applies the volume modification segment_data.data[ss] *= cur_vol except IndexError: pass cur_vol = cur_vol + how_much_volume_to_increase_per_samp ss = ss + 1 # This ramp down to the volume for the start of the next segent samps_to_next_segment_from_here = int(segment_data.sampleRate * (a.duration-when_max_volume)) if(samps_to_next_segment_from_here > 0): how_much_volume_to_decrease_per_samp = float(linear_max_volume - linear_next_start_volume)/float(samps_to_next_segment_from_here) for samps in xrange(samps_to_next_segment_from_here): cur_vol = cur_vol - how_much_volume_to_decrease_per_samp try: # This actally applies the volume modification segment_data.data[ss] *= cur_vol except IndexError: pass ss = ss + 1 # This mixes the segment from B with the segment from A, and adds it to the output mixed_data = audio.mix(segment_data,reference_data,mix=mix) out.append(mixed_data) # This writes the newly created audio to the given file. Phew! out.encode(self.output_filename)
config.ECHO_NEST_API_KEY = "O63DZS2LNNEWUO9GC" """ This file is useless, just used to try out API calls """ #reload(audio) audio_file = audio.LocalAudioFile("mp3/Calibria.mp3") beats = audio_file.analysis.beats[128:159] collect = [] for beat in beats: beat_audio = beat.render() scaled_beat = dirac.timeScale(beat_audio.data, 1.2) ts = audio.AudioData(ndarray=scaled_beat, shape=scaled_beat.shape, sampleRate=audio_file.sampleRate, numChannels=scaled_beat.shape[1]) collect.append(ts) print collect out = audio.assemble(collect, numChannels=2) # audio_file2 = audio.LocalAudioFile("mp3/Bastille.mp3") # beats2 = audio_file2.analysis.beats[128:159] # data1 = audio.getpieces(audio_file, beats) # # print type(data1) # # print isinstance(data1, audio.AudioData) # #out = modify.Modify().shiftTempo(data1, 1) # data2 = audio.getpieces(audio_file2, beats2) # out = action.Crossfade([data1, data2], [0.0, 0.0], 30).render()