Beispiel #1
0
    def match_segs(self, mix, envelope):
        for i, a_seg in enumerate(self.segs_a):

            # find the best match
            match_seg = self.find_match(a_seg)
            segment_data = self.input_b[match_seg]
            reference_data = self.input_a[a_seg]

            # fix segment length: if new seg is shorter, add silence
            # if new seg is longer, truncate it
            segment_data = self.correct_segment_length(segment_data, reference_data)
            
            # apply the volume envelope from each seg in A to the matching seg in B
            if envelope:
                segment_data = self.apply_envelope(a_seg, segment_data)

            # mix the seg from B with the seg from A
            mixed_data = audio.mix(segment_data, reference_data, mix=mix)
            
            # self.out.append(mixed_data)
            next_time = a_seg.start
            self.out.add_at(next_time, mixed_data)
Beispiel #2
0
    def match_segs(self, mix, envelope):
        for i, a_seg in enumerate(self.segs_a):

            # find the best match
            match_seg = self.find_match(a_seg)
            segment_data = self.input_b[match_seg]
            reference_data = self.input_a[a_seg]

            # fix segment length: if new seg is shorter, add silence
            # if new seg is longer, truncate it
            segment_data = self.correct_segment_length(segment_data,
                                                       reference_data)

            # apply the volume envelope from each seg in A to the matching seg in B
            if envelope:
                segment_data = self.apply_envelope(a_seg, segment_data)

            # mix the seg from B with the seg from A
            mixed_data = audio.mix(segment_data, reference_data, mix=mix)

            # self.out.append(mixed_data)
            next_time = a_seg.start
            self.out.add_at(next_time, mixed_data)
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
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)
Beispiel #6
0
def Hindustanify_main(inputfile, outputfile, tempoREDpc, taal):
	print 'a'
	soundtouch = modify.Modify()
  	print 'b'
	### Important parameters
	AmplitudeFactorTablaStrokes = 0.35
	print'c'
    ### reading tabla strokes files
	strokes={}
	for bol in numpy.unique(TalaInfoFULL[taal]['normal']['bols'] + TalaInfoFULL[taal]['roll']['bols'] ):
		strokes[bol] = audio.AudioData(TablaStrokesPath[bol])
		strokes[bol].data = AmplitudeFactorTablaStrokes*strokes[bol].data[:,0]
		strokes[bol].numChannels = 1	
	
    
	# reading input file and performning audio analsis
	print 'a'
	audiofile = audio.LocalAudioFile(inputfile)
	print 'b'
	key = audiofile.analysis.key['value']
	mode = audiofile.analysis.mode['value']
    
	get_drone_file, transposition_index = GetDroneFileandTransIndex(key, mode)
	
	#reading audio from drone file
	#C
	dronefile = audio.AudioData(get_drone_file)
	dronefile.data = dronefile.data[:,0]
	dronefile.numChannels =1
	dronefile = soundtouch.shiftPitchSemiTones(dronefile, transposition_index)
	
	#print "Drone file read"
	#print "number of channels " + str(dronefile.numChannels)
	#dronefile = mono_to_stereo(dronefile)
	#print "number of channels " + str(dronefile.numChannels)
	dronefiledutation = dronefile.duration
    
	beats = audiofile.analysis.beats
	outputshape = (len(audiofile.data),)
	output = audio.AudioData(shape=outputshape, numChannels=1, sampleRate=44100)
	final_out = audio.AudioData(shape=outputshape, numChannels=1, sampleRate=44100)
    

	drone_index=0
    
	for i, beat in enumerate(beats):
	    
		#if beat.end > audiofile.duration/6:
			#tempoREDpc=1
		#reading chunk of audio
		data = audiofile[beat]
		#slowing things down
		print tempoREDpc
		new_audio_data = soundtouch.shiftTempo(audiofile[beat],tempoREDpc)

		#reading drone chunk by chunk
		drone_chunk = dronefile[drone_index:drone_index+ new_audio_data.data.shape[0]]
		drone_index = drone_index + new_audio_data.data.shape[0]

		# adding drone signal
		new_audio_data = audio.mix(new_audio_data,drone_chunk, 0.7) 

		output.append(new_audio_data)
	    
	    
		#check if the beat counter for drone might cross the drone duration
		if drone_index > (dronefiledutation-(2*beat.duration))*dronefile.sampleRate:
			drone_index=0
	
	output = AddTabla(output,audiofile.analysis.bars, audiofile.analysis.sections, tempoREDpc, taal, strokes)		
	'''print "started tabla strokes addition"
	for i, tatum in enumerate(audiofile.analysis.tatums[2:-1]):
		
		onset = (tatum.end - tatum.duration)
		print onset
		print type(strokes[tablaaudio2(i,taal)])
		output.add_at(onset,strokes[tablaaudio2(i,taal)])
		'''
	
	output.encode(outputfile)
Beispiel #7
0
                if warmUpCounter == 0:
                    print 'MASHING...'
            else:
                # a) get the section audio
                rgnIdx = clsec.getSongRegionIdx(currSec) # an int
                allSectionsForSong = cluster.getRegionsOfType( \
                    csong.m_adata.analysis, 'sections' ) # array of quanta
                secAData = csong.m_adata[allSectionsForSong[rgnIdx]]
                # b) get the beat concat audio and merge
                (beatAData, indexBars) = addBarsToAudio( \
                    clInfo, csong.m_adata, allSectionsForSong[rgnIdx], \
                        indexBars )

                # Let's not always mix, it can sound messy
                if np.random.random() < 0.25:
                    secnResult = audio.mix( secAData, beatAData )
                else:
                    secnResult = beatAData
                #secAData = addBeatsToAudio( clInfo, csong.m_adata, \
                #                                allSectionsForSong[rgnIdx] )
                playAudioData( secnResult )
        # todo: keep a history and don't go back too early?
        # todo: pick same key?

        # pick a new section in this cluster
        currSecCand = clsec.nextRegion( clSection, currSec )
        if getSongFromCache(clsec.getFilenameOfRegion(currSecCand)) != None:
            # The song is there, we can move to this region.  Otherwise
            # we've prompted it to load, but don't go there right now.
            currSec = currSecCand
            docheck( clsec, clSection, currSec )
Beispiel #8
0
    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)
Beispiel #9
0
                if warmUpCounter == 0:
                    print 'MASHING...'
            else:
                # a) get the section audio
                rgnIdx = clsec.getSongRegionIdx(currSec)  # an int
                allSectionsForSong = cluster.getRegionsOfType( \
                    csong.m_adata.analysis, 'sections' ) # array of quanta
                secAData = csong.m_adata[allSectionsForSong[rgnIdx]]
                # b) get the beat concat audio and merge
                (beatAData, indexBars) = addBarsToAudio( \
                    clInfo, csong.m_adata, allSectionsForSong[rgnIdx], \
                        indexBars )

                # Let's not always mix, it can sound messy
                if np.random.random() < 0.25:
                    secnResult = audio.mix(secAData, beatAData)
                else:
                    secnResult = beatAData
                #secAData = addBeatsToAudio( clInfo, csong.m_adata, \
                #                                allSectionsForSong[rgnIdx] )
                playAudioData(secnResult)
        # todo: keep a history and don't go back too early?
        # todo: pick same key?

        # pick a new section in this cluster
        currSecCand = clsec.nextRegion(clSection, currSec)
        if getSongFromCache(clsec.getFilenameOfRegion(currSecCand)) != None:
            # The song is there, we can move to this region.  Otherwise
            # we've prompted it to load, but don't go there right now.
            currSec = currSecCand
            docheck(clsec, clSection, currSec)
Beispiel #10
0
    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)
Beispiel #11
0
  if d.duration > 0.5:
    if len(audio_file1[ d ]) > 0 and len(audio_file2[ beats2[i]]) > 0:
      interesting_segments.append( audio_file1[ d ])

while i < nb:
    print 'Beat pair %d of %d' % (i,nb)
    # add next beat from song 1
    # alist.append( audio_file2[ beats2[i] ] )
    # add next beat from song 2
    # if audio_file2.analysis.bars[i].confidence > 0.15:
    #   minlen = min(audio_file1.data.shape[0], audio_file2.data.shape[0])
    #   # audiofile = audio_file2
    #   # audiofile.data = audiofile.data[:minlen,:] + audio_file1.data[:minlen,:]

    if random.randrange(100) < 70:
      alist.append( audio.mix(audio_file2[ beats2[i] ], interesting_segments[random.randrange(len(interesting_segments))] ) )
    else:
      alist.append(audio_file2[beats2[i]])

    # else:
      # alist.append( audio_file1[ beats1[i] ] )
    i += 1

# construct output waveform from these audiodata objects.
afout = audio.assemble( alist )

# Write output file
filename = "play"+str(int(time.time()))+".mp3"

afout.encode( filename )
Beispiel #12
0
af1 = audio.LocalAudioFile(f1)
af2 = audio.LocalAudioFile(f2)
equalize_track(af1)
equalize_track(af2)
beats1 = af1.analysis.beats
bars1 = af1.analysis.bars
beats2 = af2.analysis.beats
bars2 = af2.analysis.bars

#pdb.set_trace()
out = audio.AudioData(shape = (44100*30,2),
    sampleRate = af1.sampleRate,
    numChannels = 2
    )
mm = audio.mix(af2,af1)
#mm = audio.mix(af2[beats2[0]:beats2[2]],af2)
for i in range(10):
  out.append(af2[bars2[i]:bars2[i+1]])
  out.append(af1[bars1[i]:bars1[i+1]])
#mm = audio.mix(af1[beats1[0]:beats1[-1]],af2)
#mm = audio.mix(af1[beats1[0]:beats1[2]],af1)
#out.append(mm)
out.encode('../XmasHackCloud/public/tmpmmp.wav')

def equalize_tracks(tracks):
  def db_2_volume(loudness):
      return (1.0 - LOUDNESS_THRESH * (LOUDNESS_THRESH - loudness) / 100.0)
  for track in tracks:
      loudness = track.analysis.loudness
      track.gain = db_2_volume(loudness)
Beispiel #13
0
            interesting_segments.append(audio_file1[d])

while i < nb:
    print 'Beat pair %d of %d' % (i, nb)
    # add next beat from song 1
    # alist.append( audio_file2[ beats2[i] ] )
    # add next beat from song 2
    # if audio_file2.analysis.bars[i].confidence > 0.15:
    #   minlen = min(audio_file1.data.shape[0], audio_file2.data.shape[0])
    #   # audiofile = audio_file2
    #   # audiofile.data = audiofile.data[:minlen,:] + audio_file1.data[:minlen,:]

    if random.randrange(100) < 70:
        alist.append(
            audio.mix(
                audio_file2[beats2[i]], interesting_segments[random.randrange(
                    len(interesting_segments))]))
    else:
        alist.append(audio_file2[beats2[i]])

    # else:
    # alist.append( audio_file1[ beats1[i] ] )
    i += 1

# construct output waveform from these audiodata objects.
afout = audio.assemble(alist)

# Write output file
filename = "play" + str(int(time.time())) + ".mp3"

afout.encode(filename)
Beispiel #14
0
    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)
Beispiel #15
0
 def run(self, mix=0.5, envelope=False):
     dur = len(self.target.data) + 100000 # another two seconds
     # determine shape of new array
     if len(self.target.data.shape) > 1:
         new_shape = (dur, self.target.data.shape[1])
         new_channels = self.target.data.shape[1]
     else:
         new_shape = (dur,)
         new_channels = 1
     out = audio.AudioData(shape=new_shape,
                         sampleRate=self.sources[0].sampleRate,
                         numChannels=new_channels)
     for a in self.target_segs:
         seg_index = a.absolute_context()[0]
         distance_matrices = [self.calculate_distances(a,segs_i) for segs_i in self.source_segs]
         distances_fromi = [[numpy.sqrt(x[0]+x[1]+x[2]) for x in distance_matrix_atoi] for distance_matrix_atoi in distance_matrices]
         minima = [(min(dists), dists.index(min(dists))) for dists in distances_fromi]
         segopts_index = minima.index(min(minima))
         seg_index = minima[segopts_index][1]
         match = self.source_segs[segopts_index][distances_fromi[segopts_index].index(minima[segopts_index][0])]
         segment_data = self.sources[segopts_index][match]
         reference_data = self.target[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)
         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.target_segs)-1): # if this is the last segment
                 linear_next_start_volume = 0
             else:
                 linear_next_start_volume = pow(10.0,self.target_segs[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)
     out.encode(self.output_filename)
Beispiel #16
0
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)
Beispiel #17
0
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);