-
Notifications
You must be signed in to change notification settings - Fork 1
/
sectionize.py
executable file
·103 lines (79 loc) · 2.79 KB
/
sectionize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# encoding: utf=8
"""
sections.py
Analyze with Echonest, print out details about track and potentially render various sections.
[http://developer.echonest.com/docs/v4/_static/AnalyzeDocumentation.pdf]
tatums
: list of tatum markers, in seconds. Tatums represent the lowest regular pulse train that a listener intuitively
infers from the timing of perceived musical events (segments).
‣
beats
: list of beat markers, in seconds. A beat is the basic time unit of a piece of music; for example, each tick of
a metronome. Beats are typically multiples of tatums.
‣
bars
: list of bar markers, in seconds. A bar (or measure) is a segment of time defined as a given number of beats.
Bar offsets also indicate downbeats, the first beat of the measure.
‣
sections
: a set of section markers, in seconds. Sections are defined by large variations in rhythm or timbre, e.g.
chorus, verse, bridge, guitar solo, etc. Each section contains its own descriptions of tempo, key, mode,
time_signature, and loudness.
By Mike iLL/mZoo.org, 2014-07-20.
"""
from __future__ import print_function
import echonest.remix.audio as audio
import sys, os
import pickle
usage = """
Usage:
python sectionize.py <input_filename>
Example:
python sectionize.py audio/Gondoliers11.mp3
python sectionize.py audio/Gondoliers39.mp3
"""
try:
input_filename = sys.argv[1]
except:
print(usage)
sys.exit(-1)
spacer = "*" + (' ' * 58) + "*"
border = "*" * 60
def track_details(track):
"""
Print echonest analysis details of track to stdout.
"""
segments = track.segments
sections = track.sections
heading = " Some details about " + input_filename + " "
print(heading.center(60,"*"), spacer)
print("Track Duration: {0:14.4f}".format(track.duration))
if track.sections:
sections = track.sections
print("* Counted {0:6d} sections".format(len(sections)))
print(spacer, os.linesep, os.linesep, spacer, border)
else:
no_beats = "********No Sections Detected********"
print(no_beats)
print(spacer)
print("*" * len(no_beats))
print(spacer)
def main(input_filename):
"""
"""
try:
with open(input_filename + '.analysis.en') as f:
audiofile = pickle.load(f)
except IOError:
audiofile = audio.LocalAudioFile(input_filename)
audiofile.save()
track = audiofile.analysis
track_details(track)
# Zero-pad all section names to the maximum needed length :)
fmt = "%0" + str(len(str(len(track.sections)))) + "d.mp3"
for name, section in enumerate(track.sections, 1):
section.render().encode(fmt % name)
print(spacer, os.linesep, border)
if __name__ == "__main__":
main(input_filename)