PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 12ms app.codeStats 0ms

/examples/videx/vafroma2.py

http://echo-nest-remix.googlecode.com/
Python | 103 lines | 91 code | 2 blank | 10 comment | 0 complexity | b5fb57dfc3e5ad723e2e825b58b159ce MD5 | raw file
  1#!/usr/bin/env python
  2# encoding: utf=8
  3
  4"""
  5vafroma2.py
  6
  7Re-synthesize video A using the segments of song A.
  8Same as vafroma.py, but avoids re-using segments.
  9
 10By Ben Lacker, P. Lamere
 11"""
 12import numpy
 13import sys
 14import time
 15import echonest.audio as audio
 16import echonest.video as video
 17import echonest.modify as modify
 18
 19usage="""
 20Usage:
 21    python vafroma2.py <input_filename>
 22
 23Example:
 24    python vafroma2.py BillieJeanMusicVideo.mp4
 25"""
 26
 27
 28dur_weight = 1000
 29#dur_weight = 100
 30timbre_weight = .001
 31pitch_weight = 10
 32loudness_weight = 1
 33
 34class AfromA(object):
 35    def __init__(self, input_filename, output_filename):
 36        self.av = video.loadav(input_filename)
 37        self.segs = self.av.audio.analysis.segments
 38        self.output_filename = output_filename
 39
 40    def get_distance_from(self, seg):
 41        distances = []
 42        for a in self.segs:
 43            ddur = numpy.square(seg.duration - a.duration)
 44            dloud = numpy.square(seg.loudness_max - a.loudness_max)
 45
 46            timbre_diff = numpy.subtract(seg.timbre, a.timbre)
 47            dtimbre = (numpy.sum(numpy.square(timbre_diff)))
 48
 49            pitch_diff = numpy.subtract(seg.pitches, a.pitches)
 50            dpitch = (numpy.sum(numpy.square(pitch_diff)))
 51
 52            #print dur_weight * ddur, timbre_weight * dtimbre, \
 53            #      pitch_weight * dpitch, loudness_weight * dloud
 54            distance =    dur_weight * ddur \
 55                        + loudness_weight * dloud \
 56                        + timbre_weight * dtimbre \
 57                        + pitch_weight * dpitch;
 58            distances.append(distance)
 59
 60        return distances
 61            
 62
 63    def run(self):
 64        st = modify.Modify()
 65        collect = audio.AudioQuantumList()
 66        used = []
 67        for a in self.segs:
 68            seg_index = a.absolute_context()[0]
 69
 70            distances = self.get_distance_from(a)
 71
 72            distances[seg_index] = sys.maxint
 73            for u in used:
 74                distances[u] = sys.maxint
 75
 76            match_index = distances.index(min(distances))
 77            match = self.segs[match_index]
 78            print seg_index, match_index
 79            # make the length of the new seg match the length
 80            # of the old seg
 81            collect.append(match)
 82            used.append(match_index)
 83        out = video.getpieces(self.av, collect)
 84        out.save(self.output_filename)
 85
 86def main():
 87    try:
 88        input_filename = sys.argv[1]
 89        if len(sys.argv) > 2:
 90            output_filename = sys.argv[2]
 91        else:
 92            output_filename = "aa2_" + input_filename
 93    except:
 94        print usage
 95        sys.exit(-1)
 96    AfromA(input_filename, output_filename).run()
 97
 98
 99if __name__=='__main__':
100    tic = time.time()
101    main()
102    toc = time.time()
103    print "Elapsed time: %.3f sec" % float(toc-tic)