def main(): #### We can't do this for multiple songs. songs = glob.glob("songs/*.mp3") filename = generate(songs) beats = [] audiofile = audio.LocalAudioFile(filename) beats = audiofile.analysis.beats print "Number of beats %s" % len(beats) samples = beats[::SAMPLING_STEP] print "Number of samples to build cluster model %s" % len(samples) cl = cluster.KMeansClustering(samples, distance_beats) clusters = cl.getclusters(K) print "Clustering completed" for c in clusters: c.centroid = None pickle.dump(clusters, open("clustering.c", "wb")) print "Pickled Cluster Model" for c in clusters: c.centroid = cluster.centroid(c) print "Reset the centroids" training_input = [] for beat in beats: training_input.append(get_cluster_index(beat, clusters)) print("Training markovModel") markov_model = MarkovModel() markov_model.learn_ngram_distribution(training_input, NGRAM) #### We should have his function as iterator. print "Generating bunch of music" output_list = markov_model.generate_a_bunch_of_text(len(training_input)) generated_beats = audio.AudioQuantumList() print "Coming back to beats" for index in output_list: generated_beats.append(get_beats_back(index, clusters)) #### We can't do this for multiple songs. print "Saving an Amazing Song" out = audio.getpieces(audiofile, generated_beats) out.encode("bunch_of_music.wav")
# No need to clutter the map with unnecessary placemarks... kml_items = [{'label': label, 'coords': '%s,%s' % coords[0]} for (label, coords) in expanded_coords] # It could also be interesting to include names of your contacts on the map for display for item in kml_items: item['contacts'] = '\n'.join(['%s %s.' % (ec.first_name, ec.last_name[0]) for ec in extended_connections if ec.location == item['label']]) cl = KMeansClustering([coords for (label, coords_list) in expanded_coords for coords in coords_list]) centroids = [{'label': 'CENTROID', 'coords': '%s,%s' % centroid(c)} for c in cl.getclusters(K)] kml_items.extend(centroids) kml = createKML(kml_items) if not os.path.isdir('out'): os.mkdir('out') f = open("out/" + OUT, 'w') f.write(kml) f.close() print >> sys.stderr, 'Data pickled to out/' + OUT
# No need to clutter the map with unnecessary placemarks... kml_items = [{'label': label, 'coords': '%s,%s' % coords[0]} for (label, coords) in expanded_coords] # It could also be interesting to include names of your contacts on the map for display for item in kml_items: item['contacts'] = '\n'.join(['%s %s.' % (ec.first_name, ec.last_name[0]) for ec in extended_connections if ec['location'] == item['label']]) cl = KMeansClustering([coords for (label, coords_list) in expanded_coords for coords in coords_list]) centroids = [{'label': 'CENTROID', 'coords': '%s,%s' % centroid(c)} for c in cl.getclusters(K)] kml_items.extend(centroids) kml = createKML(kml_items) if not os.path.isdir('out'): os.mkdir('out') f = open("out/" + OUT, 'w') f.write(kml) f.close() print >> sys.stderr, 'Data pickled to out/' + OUT
# It could also be interesting to include names of your contacts on the map for display for item in kml_items: item['contacts'] = '\n'.join([ '%s %s.' % (ec.first_name, ec.last_name[0]) for ec in extended_connections if ec.location == item['label'] ]) cl = KMeansClustering([ coords for (label, coords_list) in expanded_coords for coords in coords_list ]) centroids = [{ 'label': 'CENTROID', 'coords': '%s,%s' % centroid(c) } for c in cl.getclusters(K)] kml_items.extend(centroids) kml = createKML(kml_items) if not os.path.isdir('out'): os.mkdir('out') f = open("out/" + OUT, 'w') f.write(kml) f.close() print >> sys.stderr, 'Data pickled to out/' + OUT
def main(): parser = argparse.ArgumentParser( description="Markov") parser.add_argument( "-d", "--directory", default=None, help="Music dir") parser.add_argument( "-f", "--filename", default=None, help="Song file") parser.add_argument( "-p", "--pickle", default=False, action="store_true", help="Pickle") parser.add_argument( "-k", "--clusters", type=int, default=50, help="Clusters") parser.add_argument( "-s", "--sample", type=int, default=2, help="Sampling") parser.add_argument( "-n", "--ngram", type=int, default=10, help="Ngram") parser.add_argument( "-l", "--length", type=int, default=None, help="Length") args = parser.parse_args() if args.directory is not None: args.filename = generate_single_song(args.directory) if args.filename is None: raise Exception("Song not defined") #### We can't do this for multiple songs. beats = [] audiofile = audio.LocalAudioFile(args.filename) beats = audiofile.analysis.beats print "Number of beats %s" % len(beats) internal_filename = os.path.split(args.filename)[1] if not args.pickle: samples = beats[::args.sample] print "Number of samples to build cluster model %s" % len(samples) cl = cluster.KMeansClustering(samples, distance_beats) clusters = cl.getclusters(args.clusters) print "Clustering completed" for c in clusters: c.centroid = None pickle.dump(clusters, open(internal_filename[:-4] + ".pickle", "wb")) print "Pickled Cluster Model" else: clusters = pickle.load(open(internal_filename[:-4] + ".pickle", "rb")) attach_source(clusters, audiofile) print "Resetting the centroids" for c in clusters: c.centroid = cluster.centroid(c) print "Reset the centroids" training_input = [] for beat in beats: training_input.append(get_cluster_index(beat, clusters)) print("Training markovModel") markov_model = MarkovModel() markov_model.learn_ngram_distribution(training_input, args.ngram) #### We should have his function as iterator. print "Generating bunch of music" if args.length is None: args.length = len(training_input) output_list = markov_model.generate_a_bunch_of_text(args.length) generated_beats = audio.AudioQuantumList() print "Coming back to beats" prev_beat = None for index in output_list: curr_beat = get_beats_back(index, clusters, prev_beat) generated_beats.append(curr_beat) prev_beat = curr_beat #### We can't do this for multiple songs. print "Saving an Amazing Song" out = audio.getpieces(audiofile, generated_beats) out.encode(internal_filename[:-4] + ".wav")
for label in coords_freqs: ((lat,lon),f)=coords_freqs[label] expanded_coords.append((label, [(lon, lat)]*f)) # flip lat/lon for google earth # No need to clutter the map with unnecessary placemarks... kml_items = [{'label': label, 'coords': '%s,%s' % coords[0]} for (label, coords) in expanded_coords] # It could also be interesting to include names of your contacts on the map for display for item in kml_items: item['contacts'] = '\n'.join(['%s %s.' % (ec.first_name, ec.last_name[0]) for ec in extended_connections if ec.location == item['label']]) cl = KMeansClustering([coords for (label, coords_list) in expanded_coords for coords in coords_list]) centroids=[{'label': 'CENTROID', 'coords': '%s,%s' % centroid(c)} for c in cl.getclusters(K)] #kml_items.extend(centroids) #kml=createKML(kml_items) if not os.path.isdir('out'): os.mkdir('out') f = open("out/" + OUT, 'w') f.write(centroids) f.close() print >> sys.stderr, "Data pickled to out/" +OUT