PageRenderTime 223ms CodeModel.GetById 99ms app.highlight 8ms RepoModel.GetById 106ms app.codeStats 0ms

/examples/stretch/beatshift.py

http://echo-nest-remix.googlecode.com/
Python | 50 lines | 37 code | 2 blank | 11 comment | 0 complexity | c6b0105106e0db6e5b64760eec7c90c5 MD5 | raw file
 1#!/usr/bin/env python
 2# encoding: utf-8
 3"""
 4beatshift.py
 5
 6Pitchshift each beat based on its position in the bar.
 7Beat one is unchanged, beat two is shifted down one half step,
 8beat three is shifted down two half steps, etc.
 9
10Created by Ben Lacker on 2009-06-24.
11"""
12import numpy
13import os
14import random
15import sys
16import time
17
18import soundtouch
19from echonest import audio, modify
20
21USAGE = """
22Usage:
23    python beatshift.py <input_filename> <output_filename>
24Exampel:
25    python beatshift.py CryMeARiver.mp3 CryMeAShifty.mp3
26"""
27
28def main():
29    try:
30        in_filename = sys.argv[1]
31        out_filename = sys.argv[2]
32    except Exception:
33        print USAGE
34        sys.exit(-1)
35    st = modify.Modify()
36    afile = audio.LocalAudioFile(in_filename)
37    beats = afile.analysis.beats
38    out_shape = (len(afile.data),)
39    out_data = audio.AudioData(shape=out_shape, numChannels=1, sampleRate=44100)
40    for i, beat in enumerate(beats):
41        data = afile[beat].data
42        number = beat.local_context()[0] % 12
43        new_ad = st.shiftPitchSemiTones(afile[beat], number*-1)
44        out_data.append(new_ad)
45    out_data.encode(out_filename)
46
47
48if __name__ == '__main__':
49    main()
50