PageRenderTime 56ms CodeModel.GetById 45ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/multi/multitest.py

http://echo-nest-remix.googlecode.com/
Python | 79 lines | 69 code | 1 blank | 9 comment | 2 complexity | 890857cdc52490a4ea3902cefd3fbf7b MD5 | raw file
 1#!/usr/bin/env python
 2# encoding: utf=8
 3
 4"""
 5multitest.py
 6
 7Take a whole directory of audio files and smash them together, by
 8beat, with a fairly simple algorithm. Demonstrates the use of 
 9deferred audio file loading used in conjunction with render_serially(),
10which allow one to remix countless files without running out of memory
11too soon. Also outputs a "score" in XML.
12
13Originally by Adam Lindsay, 2000-05-05.
14"""
15
16import os, sys
17import time
18from math import sqrt
19
20from echonest import audio
21
22usage = """
23Usage: 
24    python multitest.py <inputDirectory> <outputFilename> <beats>
25
26Example:
27    python multitest.py ../music mashedbeats.mp3 40
28"""
29
30def main(num_beats, directory, outfile):
31    
32    aud = []
33    ff = os.listdir(directory)
34    for f in ff:
35        # collect the files
36        if f.rsplit('.', 1)[1].lower() in ['mp3', 'aif', 'aiff', 'aifc', 'wav']:
37            aud.append(audio.LocalAudioFile(os.path.join(directory,f)))
38            # mind the rate limit
39    
40    num_files = len(aud)
41    x = audio.AudioQuantumList()
42    
43    print >> sys.stderr, "Assembling beats.",
44    for w in range(num_beats):
45        print >> sys.stderr, '.',
46        ssong = aud[w%num_files].analysis
47        s = ssong.beats[w%len(ssong.beats)]
48        tsong = aud[(w-1)%num_files].analysis
49        t = tsong.beats[w%len(tsong.beats)]
50        
51        x.append(audio.Simultaneous([s,t]))
52    
53    print >> sys.stderr, "\nStarting rendering pass..."
54    
55    then = time.time()
56    # call render_sequentially() with no arguments, and then it calls itself with
57    #  contextual arguments for each source, for each AudioQuantum. It's a lot of
58    #  tree-walking, but each source file gets loaded once (and takes itself from)
59    #  memory when its rendering pass finishes.
60    x.render().encode(outfile)
61    
62    print >> sys.stderr, "%f sec for rendering" % (time.time() - then,)
63    
64    print >> sys.stderr, "Outputting XML: each source makes an API call for its metadata."
65    # output an XML file, for debugging purposes
66    y = open(outfile.rsplit('.', 1)[0] + '.xml', 'w')
67    y.write(x.toxml())
68    y.close()
69
70if __name__ == '__main__':
71    try:
72        directory = sys.argv[-3]
73        outfile = sys.argv[-2]
74        num_beats = int(sys.argv[-1])
75    except:
76        print usage
77        sys.exit(-1)
78    main(num_beats, directory, outfile)
79