示例#1
0
def get_loops(fileobj, output_name="out.mp3", bars_count=8, bars_start=1):
    print "analyzing"
    audio_file = audio.LocalAudioFile(fileobj.name)
    print "done"
    
    print "%d bars" % len(audio_file.analysis.bars)

    collect = audio.AudioQuantumList()
    
    bars = audio_file.analysis.bars
    repeats = 1
    if len(bars)-bars_start < bars_count:
        bars_count = 4
    if len(bars)-bars_start < bars_count:
        bars_count = 1

    print "actual bar count was %d" % (bars_count)
    for y in xrange(repeats):
        for x in xrange(bars_count):
            collect.append(audio_file.analysis.bars[bars_start+x])
    
    out = audio.getpieces(audio_file, collect)
    output_temp = tempfile.NamedTemporaryFile(mode="w+b", suffix=".mp3")
    out.encode(output_temp.name)
    
    # Do it again
    new_one = audio.LocalAudioFile(output_temp.name)
    analysis = json.loads(urllib.urlopen(new_one.analysis.pyechonest_track.analysis_url).read())
    
    
    return (output_temp, analysis)
示例#2
0
文件: main.py 项目: imclab/snuGIFy
def fruity_loops(fileobj, output_temp):
    options = {
        'plot': None,
        'force': None,
        'verbose': None,
        'graph': None,
        'infinite': True,
        'length': None,
        'minimum': 16,
        'longest': None,
        'duration': 10,
        'pickle': None,
        'shortest': False
    }
    args = [fileobj.name]
    track = LocalAudioFile(args[0], verbose=False)
    # import pdb; pdb.set_trace()
    # this is where the work takes place
    actions = earworm.do_work(track, options)
    render(actions, output_temp.name, verbose=False)
    new_one = audio.LocalAudioFile(output_temp.name)
    analysis = json.loads(
        urllib.urlopen(new_one.analysis.pyechonest_track.analysis_url).read())
    # print os.path.exists(output_temp.name)
    return (output_temp.name, analysis)
示例#3
0
def main(units, inputFile, outputFile):
    # This takes your input track, sends it to the analyzer, and returns the results.  
    audiofile = audio.LocalAudioFile(inputFile)

    # This gets the overall key of the track
    tonic = audiofile.analysis.key['value']
    
    # This gets a list of all of the selected unit in the track.  
    chunks = audiofile.analysis.__getattribute__(units)
    
    # This is a serious line!  
    # It means:  "segments that have the tonic as the max pitch and that overlap the start of the <units>"
    # Note the syntax:  ".that(do_something)". These work just the way you think they should
    # (That is, they act like list comprehensions for the given statement!)
    # Also, note that have_pitch_max and overlap are imported from selection.py
    segs = audiofile.analysis.segments.that(have_pitch_max(tonic)).that(overlap_starts_of(chunks))
    
    # Using the same synatx as the above line:
    # this line gets all rhythmic units that begin with the segment we found above
    outchunks = chunks.that(overlap_ends_of(segs))
    
    # This assembles the pieces of audio defined in collect from the analyzed audio file.
    out = audio.getpieces(audiofile, outchunks)
    
     # This writes the newly created audio to the given file.  
    out.encode(outputFile)
示例#4
0
def main():
    
    try:
        in_filename = sys.argv[1]
    except Exception:
        print USAGE
        sys.exit(-1)

    afile = audio.LocalAudioFile(in_filename)
    vecin = afile.data
    
    # ------------------------------------------------------------------------------------
    # Single time-stretch
    # ------------------------------------------------------------------------------------
    rate = 1.25 # 25% slower
    quality = 0 # fast processing
    
    print "Time Stretching with single rate", rate
    # arguments are:
    # 1) a numpy array of size N x C, where N is a number of samples, and C the number of channels (1 or 2)
    # 2) a float representing the time-stretching rate, e.g. 0.5 for twice as short, and 2.0 for twice as long
    # 3) an integer representing the sample rate of the input signal, e.g. 44100
    # 4) an optional integer representing the processing quality and speed between the default 0 (fastest, lowest quality) and 2 (slowest, highest quality)
    vecout = dirac.timeScale(vecin, rate, afile.sampleRate, quality)
    
    # output audio
    out = audio.AudioData(ndarray=vecout, shape=vecout.shape, sampleRate=afile.sampleRate, numChannels=vecout.shape[1])
    out_filename = 'out_single_rate.wav'
    print "Writing file", out_filename
    out.encode(out_filename)
    
    # ------------------------------------------------------------------------------------
    # Varying time-stretch
    # ------------------------------------------------------------------------------------
    # This example will linearly change the speed from 'rate' to 1.0 in 'numChunks' chunks
    numChunks = 16 # divide the signal into 16 chunks
    sizeChunk = int(vecin.shape[0] / numChunks)
    incRate = (1.0-rate) / (numChunks-1)
    
    # first tuple must start at index 0
    index = 0
    rates = [(index, rate)]
    for i in xrange(numChunks-1):
        index += sizeChunk
        rate += incRate
        rates.append((index, rate))
    
    print "Time Stretching with list of rates", rates
    # arguments are:
    # 1) a numpy array of size N x C, where N is a number of samples, and C the number of channels (1 or 2)
    # 2) a list of tuples each representing (a sample index, a rate). First index must be 0.
    # 3) an integer representing the sample rate of the input signal, e.g. 44100
    # 4) an optional integer representing the processing quality and speed between the default 0 (fastest, lowest quality) and 2 (slowest, highest quality)
    vecout = dirac.timeScale(vecin, rates, afile.sampleRate, quality)
    out = audio.AudioData(ndarray=vecout, shape=vecout.shape, sampleRate=afile.sampleRate, numChannels=vecout.shape[1])

    out_filename = 'out_list_rates.wav'
    print "Writing file", out_filename
    out.encode(out_filename)
示例#5
0
def main(input_filename, output_filename):
    audiofile = audio.LocalAudioFile(input_filename)
    bars = audiofile.analysis.bars
    collect = audio.AudioQuantumList()
    for bar in bars:
        collect.append(bar)
        collect.append(bar)
    out = audio.getpieces(audiofile, collect)
    out.encode(output_filename)
示例#6
0
def split_file_into_bars(track_name, bar_list):
    i = 0
    for bars in bar_list:
        four_bar_chunk = audio.AudioQuantumList()
        for bar in bars:
            four_bar_chunk.append(bar)
        
        audiofile = audio.LocalAudioFile("music/tracks/"+track_name+".mp3")
        out = audio.getpieces(audiofile, four_bar_chunk)
        i = i + 1
        out.encode("music/output/"+track_name+"-chunk-"+str(i))
示例#7
0
def loadav(videofile, verbose=True):
    foo, audio_file = tempfile.mkstemp(".mp3")        
    cmd = "en-ffmpeg -y -i \"" + videofile + "\" " + audio_file
    if verbose:
        print >> sys.stderr, cmd
    out = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    res = out.communicate()
    ffmpeg_error_check(res[1])
    a = audio.LocalAudioFile(audio_file)
    v = sequencefrommov(videofile)
    return SynchronizedAV(audio=a, video=v)
示例#8
0
def loadavfrombundle(dir):
    # video
    videopath = os.path.join(dir, "video")
    videosettings = VideoSettings()
    videosettings.fps = 25
    videosettings.size = (320, 240)
    video = sequencefromdir(videopath, settings=videosettings)
    # audio
    audiopath = os.path.join(dir, "audio.wav")
    analysispath = os.path.join(dir, "analysis.xml")
    myaudio = audio.LocalAudioFile(audiopath, analysis=analysispath, samplerate=22050, numchannels=1)
    return SynchronizedAV(audio=myaudio, video=video)
示例#9
0
def main(inputFile):
    audiofile = audio.LocalAudioFile(inputFile)
    beats = audiofile.analysis.beats
    avgList = []
    time = 0
    output = []
    sum = 0
    for beat in beats:
        time += beat.duration
        avg = runningAverage(avgList, beat.duration)
        sum += avg
        output.append((time, avg))
    base = sum / len(output)
    for d in output:
        print d[0], d[1] - base
示例#10
0
def main(input_filename1, input_filename2, output_filename):
    audiofile1 = audio.LocalAudioFile(input_filename1)
    audiofile2 = audio.LocalAudioFile(input_filename2)

    beats1 = audiofile1.analysis.beats
    beats2 = audiofile2.analysis.beats

    l = min([len(beats1), len(beats2)])

    collect = audio.AudioQuantumList()
    out = None
    for i in xrange(l):
        if i % 2 == 1:
            beat = beats1[i - offset]
            next = audio.getpieces(audiofile1, [beat])
        else:
            beat = beats2[i]
            next = audio.getpieces(audiofile2, [beat])
        if out == None:
            out = next
        else:
            out.append(next)

    out.encode(output_filename)
示例#11
0
def main(inputFilename, outputFilename, windowing):
    print "HERE WE GO!!!!"
    print inputFilename
    print "\n"
    print outputFilename
    print "\n"
    song = audio.LocalAudioFile(inputFilename)

    print "Cool, we loaded the song"

    sample_rate = song.sampleRate
    num_channels = song.numChannels
    out_shape = list(song.data.shape)
    out_shape[0] = 2
    out = audio.AudioData(shape=out_shape,
                          sampleRate=sample_rate,
                          numChannels=num_channels)

    chunks = []
    for section in song.analysis.sections:
        segs = song.analysis.segments.that(are_contained_by(section))

        seglen = len(segs)
        print seglen
        for i in range(0, seglen):
            if i % 2:
                seg = song[segs[i]]
                if windowing:
                    seg.data = window(seg.data)
                out.append(seg)
            else:
                seg = song[segs[seglen - i - 1]]
                try:
                    seg.data = seg.data[::-1]
                    if windowing:
                        seg.data = window(seg.data)
                    out.append(seg)
                    del (seg.data)
                    del (seg)
                except:
                    print "fuckkkkk"
                    seg = song[segs[i]]
                    if windowing:
                        seg.data = window(seg.data)
                    out.append(seg)

    #newAudio = audio.getpieces(song, chunks)
    out.encode(outputFilename)
示例#12
0
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.
    # You can manipulate this just like any other Python list!
    bars = audiofile.analysis.bars

    # This makes a new list of "AudioQuantums".
    # Those are just any discrete chunk of audio:  bars, beats, etc.
    collect = audio.AudioQuantumList()

    # This loop puts the first item in the children of each bar into the new list.
    # A bar's children are beats!  Simple as that.
    for bar in bars:
        collect.append(bar.children()[0])

    # This assembles the pieces of audio defined in collect from the analyzed audio file.
    out = audio.getpieces(audiofile, collect)

    # This writes the newly created audio to the given file.
    out.encode(output_filename)
示例#13
0
文件: main.py 项目: imclab/snuGIFy
def get_loops(fileobj, output_temp, inter=8.0, trans=2.0):
    track = LocalAudioFile(fileobj.name)
    tracks = [track, track, track]  # 3 of em!

    valid = []
    # compute resampled and normalized matrices
    for track in tracks:
        track.resampled = resample_features(track, rate='beats')
        track.resampled['matrix'] = timbre_whiten(track.resampled['matrix'])
        # remove tracks that are too small
        if is_valid(track, inter, trans):
            valid.append(track)
        # for compatibility, we make mono tracks stereo
        track = make_stereo(track)
    tracks = valid

    if len(tracks) < 1: return []
    # Initial transition. Should contain 2 instructions: fadein, and playback.
    start = initialize(tracks[0], inter, trans)

    # Middle transitions. Should each contain 2 instructions: crossmatch, playback.
    middle = []
    [
        middle.extend(make_transition(t1, t2, inter, trans))
        for (t1, t2) in tuples(tracks)
    ]

    # Last chunk. Should contain 1 instruction: fadeout.
    end = terminate(tracks[-1], FADE_OUT)
    actions = start + middle + end

    # output_temp = tempfile.NamedTemporaryFile(mode="w+b", suffix=".mp3")
    render(actions, output_temp.name, False)
    # Do it again
    new_one = audio.LocalAudioFile(output_temp.name)
    analysis = json.loads(
        urllib.urlopen(new_one.analysis.pyechonest_track.analysis_url).read())
    return (output_temp.name, analysis)
示例#14
0
def main(input_filename, output_filename):
    choices = 0
    song = audio.LocalAudioFile(input_filename)
    meter = song.analysis.time_signature.values()[1]
    meter_conf = song.analysis.time_signature.values()[0]
    tempo_conf = song.analysis.tempo.values()[0]
    sections = song.analysis.sections
    last_segment = song.analysis.segments[len(song.analysis.segments) - 1]
    sl = len(sections)
    print "meter confidence"
    print meter_conf
    print "meter"
    print song.analysis.time_signature
    print "number of sections"
    print sl
    outchunks = audio.AudioQuantumList()
    if (meter_conf > 0.2):
        outchunks = strong_meter(choices, song, meter, sections, sl, outchunks)
    else:
        outchunks = weak_meter(choices, song, sections, sl, outchunks)
    outchunks.append(last_segment)
    out = audio.getpieces(song, outchunks)
    out.encode(output_filename)
示例#15
0
def main(toReverse, inputFilename, outputFilename):
    # This takes your input track, sends it to the analyzer, and returns the results.
    audioFile = audio.LocalAudioFile(inputFilename)

    # Checks what sort of reversing we're doing.
    if toReverse == 'beats':
        # This gets a list of every beat in the track.
        chunks = audioFile.analysis.beats
    elif toReverse == 'segments':
        # This gets a list of every segment in the track.
        # Segments are the smallest chunk of audio that Remix deals with
        chunks = audioFile.analysis.segments
    else:
        print usage
        return

    # Reverse the list!
    chunks.reverse()

    # This assembles the pieces of audio defined in chunks from the analyzed audio file.
    reversedAudio = audio.getpieces(audioFile, chunks)
    # This writes the newly created audio to the given file.
    reversedAudio.encode(outputFilename)
示例#16
0
def main(units, inputFile, outputFile):
    # This takes your input track, sends it to the analyzer, and returns the results.
    audiofile = audio.LocalAudioFile(inputFile)

    # This makes a new list of "AudioQuantums".
    # Those are just any discrete chunk of audio:  bars, beats, etc
    collect = audio.AudioQuantumList()

    # If the analysis can't find any bars, stop!
    # (This might happen with really ambient music)
    if not audiofile.analysis.bars:
        print "No bars found in this analysis!"
        print "No output."
        sys.exit(-1)

    # This loop puts all but the last of each bar into the new list!
    for b in audiofile.analysis.bars[0:-1]:
        collect.extend(b.children()[0:-1])

        # If we're using tatums instead of beats, we want all but the last half (round down) of the last beat
        # A tatum is the smallest rhythmic subdivision of a beat -- http://en.wikipedia.org/wiki/Tatum_grid
        if units.startswith("tatum"):
            half = -(len(b.children()[-1].children()) // 2)
            collect.extend(b.children()[-1].children()[0:half])

    # Endings were rough, so leave everything after the start of the final bar intact:
    last = audio.AudioQuantum(
        audiofile.analysis.bars[-1].start,
        audiofile.analysis.duration - audiofile.analysis.bars[-1].start)
    collect.append(last)

    # This assembles the pieces of audio defined in collect from the analyzed audio file.
    out = audio.getpieces(audiofile, collect)

    # This writes the newly created audio to the given file.
    out.encode(outputFile)
示例#17
0
    def remix(self):
        """
            Wub wub wub wub wub wub wub wub wub wub wub wub wub wub wub wub wub wub.
        """
        self.log("Looking up track...", 5)
        self.getTag()
        self.processArt()

        self.log(
            "Listening to %s..." %
            ('"%s"' % self.tag['title'] if 'title' in self.tag else 'song'), 5)
        self.original = audio.LocalAudioFile(self.infile, False)
        if not 'title' in self.tag:
            self.detectSong(self.original)
        self.st = FastModify()

        self.log("Choosing key and tempo...", 10)
        self.tonic = self.original.analysis.key['value']
        self.tempo = self.original.analysis.tempo['value']
        self.bars = self.original.analysis.bars
        self.beats = self.original.analysis.beats
        self.sections = self.original.analysis.sections
        self.tag['key'] = self.keys[
            self.tonic] if self.tonic >= 0 and self.tonic < 12 else '?'
        self.tag['tempo'] = self.template['tempo']

        self.log("Arranging intro...", 40.0 / (len(self.sections) + 1))
        self.partialEncode(self.compileIntro())

        past_progress = 0
        hats = audio.AudioData(self.sample_path + self.template['hats'],
                               sampleRate=44100,
                               numChannels=2,
                               verbose=False)

        i = 0  # Required if there are no sections
        for i, section in enumerate(self.sections):
            self.log(
                "Arranging section %s of %s..." % (i + 1, len(self.sections)),
                40.0 / (len(self.sections) + 1))
            a, b = self.compileSection(i, section, hats)
            self.partialEncode(a)
            self.partialEncode(b)
            del a, b
        del hats
        self.original.unload()

        self.log("Adding ending...", 5)
        self.partialEncode(
            audio.AudioData(self.sample_path + self.template['splash_ends'][
                (i + 1) % len(self.template['splash_ends'])],
                            sampleRate=44100,
                            numChannels=2,
                            verbose=False))

        self.log("Mixing...", 5)
        self.mixwav(self.tempfile)

        if self.deleteOriginal:
            try:
                unlink(self.infile)
            except:
                pass  # File could have been deleted by an eager cleanup script

        self.log("Mastering...", 5)
        self.lame(self.tempfile, self.outfile)
        unlink(self.tempfile)

        self.log("Adding artwork...", 20)
        self.updateTags(titleSuffix=" (Wub Machine Remix)")

        return self.outfile
示例#18
0
 def __init__(self, input_filename_a, input_filename_b, output_filename):
     self.input_a = audio.LocalAudioFile(input_filename_a)
     self.input_b = audio.LocalAudioFile(input_filename_b)
     self.segs_a = self.input_a.analysis.segments
     self.segs_b = self.input_b.analysis.segments
     self.output_filename = output_filename
示例#19
0
 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
示例#20
0
    def generateCues(self, track, replace=False):
        """
        Automatically generates cuepoints for a given track.
        Track can be an lxml element (returned from TraktorDB.getTrack...) or a track name.
        Takes an optional "replace" parameter, that deletes the existing cuepoints and replaces them with the autogenerated ones. (Even if there are less!)

        Uses the Echo Nest Remix API to perform its magic.
        Returns a list of the new CUE_V2 elements.
        """
        self._checkConsistency()
        if not isinstance(track, etree._Element):
            track = self.getTracksByName(track)
            if len(track) > 1:
                raise Exception("Multiple tracks with that name!")
            else:
                track = track[0]
        if not track.get("AUDIO_ID"):
            raise Exception("Track has not been analyzed by Traktor!")

        if not replace and len(track.findall(
                "CUE_V2")) > 1:  #   ignore the usual Autogrid cue
            raise Exception("Track already has cues!")

        #   TODO: Fix this to work on more than Mac
        location = track.find("LOCATION")
        localpath = "/Volumes/" + location.get("VOLUME") + location.get(
            "DIR").replace("/:", "/") + location.get("FILE")

        if not self._justASCII(localpath):
            print "Error: non-ASCII file paths currently break this program. This path didn't work:", localpath
            return track.findall("CUE_V2")

        song = audio.LocalAudioFile(localpath)
        os.unlink(song.convertedfile)

        offset = 0  #   used when the echonest thinks a song starts with a rest

        #   TODO: refactor this to return early
        #   TODO: refine this offset system. Kinda hackish and not very reliable.
        if song.analysis.beats:
            if track.findall("CUE_V2") and track.findall("CUE_V2")[0].get(
                    "NAME") == "AutoGrid":
                #   make use of Traktor's autogrid start point if it exists
                if (song.analysis.beats[0].start * 1000) < float(
                        track.findall("CUE_V2")[0].get("START")):
                    offset = (
                        float(track.findall("CUE_V2")[0].get("START")) / 1000 -
                        song.analysis.beats[0].start)
                    print "EchoNest thinks that the song starts with a rest. Offsetting by %s." % offset  # remove second cue then!!! 2nd cue is redundant
                for cue in track.findall(
                        "CUE_V2")[1:]:  #   Leave first cue, grid cue
                    track.remove(cue)
            else:
                for cue in track.findall("CUE_V2"):
                    track.remove(cue)

            #   Look ma, audio analysis!

            potentialCues = []

            print "Overall analysis confidence: %s" % (
                song.analysis.time_signature['confidence'])
            if song.analysis.time_signature['confidence'] < 0.5:
                print "Analysis not very confident - song may have bad cuepoints."

            firstbeats = song.analysis.beats[::song.analysis.time_signature[
                'value']]  #   grab every nth beat
            beatlength = 60.0 / song.analysis.tempo['value']

            #   Start by iterating through sections...
            for i, section in enumerate(song.analysis.sections):
                closestBeat = None
                closestDistance = song.analysis.duration
                for beat in firstbeats:
                    if abs(beat.start - section.start) < closestDistance:
                        closestBeat = beat
                        closestDistance = abs(beat.start - section.start)
                if closestBeat.start - offset > 0:
                    potentialCues.append({
                        "start": closestBeat.start + offset,
                        "confidence": closestBeat.confidence
                    })

            #   If we have more cues than hotcue spots available, then choose the top 7 most confident.
            if len(potentialCues) > 7:
                potentialCues = sorted(potentialCues,
                                       key=lambda k: k['confidence'])
                potentialCues = potentialCues[:7]
                potentialCues = sorted(potentialCues, key=lambda k: k['start'])

            for i, cue in enumerate(potentialCues):
                print "Confidence of cue %s: %s" % (i, cue['confidence'])

            for i, cue in enumerate(potentialCues):
                cueElement = etree.Element("CUE_V2")
                #<CUE_V2 NAME="AutoGrid" DISPL_ORDER="0" TYPE="4" START="74.359565676842521" LEN="0" REPEATS="-1" HOTCUE="0"></CUE_V2>
                cueElement.set("NAME", "Section " + str(i + 1))
                cueElement.set("DISPL_ORDER", str(0))
                cueElement.set("TYPE", str(0))
                cueElement.set("LEN", str(0))
                cueElement.set("REPEATS", str(-1))
                cueElement.set("HOTCUE", str(i + 1))
                cueElement.set("START", str(cue['start'] * 1000))
                track.append(cueElement)

        return track.findall("CUE_V2")
示例#21
0
import sys
from numpy import vstack,array
from numpy.random import rand
from numpy.linalg import norm
from scipy.cluster.vq import kmeans,vq
from random import choice

# take an input song
input_filename = "mp3/vaetxh-unfolding.mp3";
output_filename = "mp3/qup-creation.mp3";

input_filename = sys.argv[1]
output_filename = sys.argv[2]

song = audio.LocalAudioFile(input_filename)

# set
sample_rate = song.sampleRate
num_channels = song.numChannels
out_shape = list(song.data.shape)
out_shape[0] = 2
out = audio.AudioData(shape=out_shape, sampleRate=sample_rate,numChannels=num_channels)

secs = song.analysis.sections
num_sections = len(secs)
### ZEPTO -> DNB -> ZEPTO -> DNB ??


def loudness(section):
	segs = song.analysis.segments.that(overlap(section))
示例#22
0
def dnbify(randombeat):
	print "dnbify"
	
	dnbfile = "mp3/breaks/RC4_Breakbeat_175 (%i).mp3" % randombeat
	dnbloop = audio.LocalAudioFile(dnbfile)
	
	# how many different groups will we cluster our data into?
	num_clusters = 5

	mix = 1.0
		
	dnbouts = []
	for layer in range(0, 2):
		# best_match = 1  # slower, less varied version. Good for b's which are percussion loops
		# best_match = 0 # faster, more varied version, picks a random segment from that cluster. Good for b's which are sample salads. 
		best_match = layer
		print "layer"
		print layer
		
		song1 = dnbloop
		song2 = song
		
		dnbout = audio.AudioData(shape=out_shape, sampleRate=sample_rate,numChannels=num_channels)
		
		# here we just grab the segments that overlap the section
		sectionsegments = song1.analysis.segments
		#for _ in range(3):
		#	sectionsegments.extend(song1.analysis.segments)
		sectionsegments2 = song2.analysis.segments #.that(overlap(section));
		

		# this is just too easy
		# song.analysis.segments.timbre is a list of all 12-valued timbre vectors. 
		# must be converted to a numpy.array() so that kmeans(data, n) is happy
		data = array(sectionsegments.timbre)
		data2 = array(sectionsegments2.timbre)
		
		"""
		# grab timbre data
		# must be converted to a numpy.array() so that kmeans(data, n) is happy
		data = array(song1.analysis.segments.timbre)
		data2 = array(song2.analysis.segments.timbre)
		"""
		
		
		# computing K-Means with k = num_clusters
		centroids,_ = kmeans(data,num_clusters)
		centroids2,_ = kmeans(data2,num_clusters)
		# assign each sample to a cluster
		idx,_ = vq(data,centroids)
		idx2,_ = vq(data2,centroids2)
		
		collection = []
		for c in range(0, num_clusters):
			ccount = 0
			for i in idx:
				if i==c: 
					ccount += 1
			collection.append([ccount, c])
		collection.sort()
		# list of cluster indices from largest to smallest

		centroid_pairs = []
		for _,c in collection:
			centroid1 = array(centroids[c])
			min_distance = [9999999999,0]
			for ci in range(0,len(centroids2)):
				if ci in [li[1] for li in centroid_pairs]:
					continue
				centroid2 = array(centroids2[ci])
				euclidian_distance = norm(centroid1-centroid2)
				if euclidian_distance < min_distance[0]:
					min_distance = [euclidian_distance, ci]
			centroid_pairs.append([c,min_distance[1]])

		print centroid_pairs
		# now we have a list of paired up cluster indices. Cool.

		# Just so we're clear, we're rebuilding the structure of song1 with segments from song2

		# prepare song2 clusters, 
		segclusters2 = [audio.AudioQuantumList()]*len(centroids2)
		for s2 in range(0,len(idx2)):
			segment2 = song2.analysis.segments[s2]
			cluster2 = idx2[s2]
			segment2.numpytimbre = array(segment2.timbre)
			segclusters2[cluster2].append(segment2)

		
		# for each segment1 in song1, find the timbrely closest segment2 in song2 belonging to the cluster2 with which segment1's cluster1 is paired. 
		for s in range(0,len(idx)):
			segment1 = song1.analysis.segments[s]
			cluster1 = idx[s]
			cluster2 = [li[1] for li in centroid_pairs if li[0]==cluster1][0]

			if(best_match>0):
				# slower, less varied version. Good for b's which are percussion loops
				
				"""
				# there's already a function for this, use that instead: timbre_distance_from
				timbre1 = array(segment1.timbre)
				min_distance = [9999999999999,0]
				for seg in segclusters2[cluster2]:
					timbre2 = seg.numpytimbre
					euclidian_distance = norm(timbre2-timbre1)
					if euclidian_distance < min_distance[0]:
						min_distance = [euclidian_distance, seg]
				bestmatchsegment2 = min_distance[1]
				# we found the segment2 in song2 that best matches segment1
				"""
				
				bestmatches = segclusters2[cluster2].ordered_by(timbre_distance_from(segment1))
				
				if(best_match > 1):
					# if best_match > 1, it randomly grabs from the top best_matches.
					maxmatches = max(best_match, len(bestmatches))
					bestmatchsegment2 = choice(bestmatches[0:maxmatches])
				else:
					# if best_match == 1, it grabs the exact best match
					bestmatchsegment2 = bestmatches[0]
			else:
				# faster, more varied version, picks a random segment from that cluster. Good for sample salads. 
				bestmatchsegment2 = choice(segclusters2[cluster2])
				
			reference_data = song1[segment1]
			segment_data = song2[bestmatchsegment2]
			
			# what to do when segments lengths aren't equal? (almost always)
			# do we add silence? or do we stretch the samples?
			add_silence = True
			
			# This is the add silence solution:
			if add_silence: 
				if reference_data.endindex > segment_data.endindex:
					# we need to add silence, because segment1 is longer
					if num_channels > 1:
						silence_shape = (reference_data.endindex,num_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 reference_data.endindex < segment_data.endindex:
					# we need to cut segment2 shorter, because segment2 is shorter
					index = slice(0, int(reference_data.endindex), 1)
					segment_data = audio.AudioData(None, segment_data.data[index], sampleRate=segment_data.sampleRate)
			else: 		  
				# TODO: stretch samples to fit.
				# haven't written this part yet.
				segment_data = segment_data

			# mix the original and the remix
			mixed_data = audio.mix(segment_data,reference_data,mix=mix)
			dnbout.append(mixed_data)
		
		dnbouts.append(dnbout)
	
	print "YEA"
	mixed_dnbouts = audio.AudioData(shape=out_shape, sampleRate=sample_rate,numChannels=num_channels)
	print len(dnbouts[0])
	print len(dnbouts[1])
	#for s in range(0,len(dnbouts[0])):
	#	print s
#		print dnbouts[0]
	#	print dnbouts[1]
	mixed_data = audio.mix(dnbouts[0], dnbouts[1], 0.5)
	mixed_dnbouts.append(mixed_data)
	print "woah"
	
	dnbrepeatout = audio.AudioData(shape=out_shape, sampleRate=sample_rate,numChannels=num_channels)
	for _ in range(4):
		dnbrepeatout.append(mixed_dnbouts)
	print "oh okay"
	return dnbrepeatout
示例#23
0
inputFilename2 = "mp3/isaaccjlaugh.mp3"

# USAGE/EXAMPLE
# python kcluster_afromb.py INPUT INPUT OUTPUT CLUSTERS MIX
# python kcluster_afromb.py build.mp3 from.mp3 to.mp3 5 0.5

inputFilename = sys.argv[1]
inputFilename2 = sys.argv[2]

outputFilename = sys.argv[3]
# how many different groups will we cluster our data into?
num_clusters = int(sys.argv[4])
# how many different groups will we cluster our data into?
mix = float(sys.argv[5])

song = audio.LocalAudioFile(inputFilename)
song2 = audio.LocalAudioFile(inputFilename2)

sample_rate = song.sampleRate
num_channels = song.numChannels
out_shape = list(song.data.shape)
out_shape[0] = 2
out = audio.AudioData(shape=out_shape,
                      sampleRate=sample_rate,
                      numChannels=num_channels)
"""
outs = [None]*2
for n in range(0,num_clusters):
	outs[n] = audio.AudioData(shape=out_shape, sampleRate=sample_rate,numChannels=num_channels)
"""
示例#24
0
def analyze(filepath):
    """ Create .json blob out of Echo Nest analysis """
    encoder = EchoNestEncoder(ensure_ascii=True)
    data = audio.LocalAudioFile(filepath)
    json = encoder.encode(data.analysis)
    return json
示例#25
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)
示例#26
0
	def __init__(self, filename, target_dirname):
		self.filename = filename
		self.target_dirname = target_dirname
		self.audio_file = audio.LocalAudioFile(filename)
示例#27
0
 def __init__(self, filename):
     self.filename = filename
     self.audio = audio.LocalAudioFile(filename)
示例#28
0
def get_bar_list(input_filename):
    audiofile = audio.LocalAudioFile(input_filename)
    return chunker(audiofile.analysis.bars, 4)