# Visualize Parts of Song as Analyzed by Echonest

This is the a bit of code that works with Echonest API's pyechonest and remix libraries combined with matplotlib.pyplot to offer a simple visual representation of the start and end "parts" of a music track:

#!/usr/bin/env python
# encoding: utf=8
"""
mix_track_utils.py

Analyze music track using echonest API with pyechonest and remix libraries,
show parts using matplotlib graph.

"""
from __future__ import print_function
import echonest.remix.audio as audio
import matplotlib.pyplot as plt

usage = """
Usage:
import mix_track_utils

track1 = audio.LocalAudioFile('some.mp3')

mix_track_utils(track1)
"""

def visualize_analysis(track):

rates = ["tatums", "segments", "beats"]

title = "Start and End Bits of {}".format(track.filename)
plt.figure()
plt.title(title)
plt.axis([-1, 5, -3, 3])
plt.grid(True)

label_height = .2
graph_height = 0
for name in rates:
label_height += .5
graph_height += .5
plt.text(0, label_height, 'Start ' + name.capitalize())
for i in getattr(track.analysis, name)[:8]:
j = (i.start, i.end)
plt.plot(j,[graph_height,graph_height], linewidth=10)

label_height = 0
graph_height = -.2
offset = None

for name in rates:
label_height -= .5
graph_height -= .5
plt.text(0, label_height, 'End ' + name.capitalize())
for i in getattr(track.analysis, name)[:8]:
j = (i.start, i.end)
if offset is None:
offset = j[0]
k = (j[0] - offset, j[1] - offset)
plt.plot(k,[graph_height,graph_height], linewidth=10)

plt.show()

if __name__ == "__main__":
print(usage)
sys.exit(-1)


Getting pyechonest to work also requires adding an ECHONEST_API_KEY key to the environment, as can be seen from the links above.

This code is really nice, and the output is very good looking! I do have a few nitpicky things that I want to cover.

• Why are you storing the usage in a variable named usage? This should be in the file's main docstring. You don't need to print usage under if __name__ == "__main__":.
• Is # encoding: utf=8 really needed? It doesn't seem like you're using any special unicode characters anywhere, so remove it.
• You should have a docstring in visualize_analysis. Describe what the function does, and how it does that, preferably, in detail.

That's about all I see that should be improved! If there's anything else you want me to cover, mention it in the comments, and I'll see what I can do! Hope this helps!

• Thanks, Ethan! In first coment, do you mean I would just include the usage text between the tripple quotes at the top of the script and it will automatically print if script is called wrong? – MikeiLL Jun 17 '15 at 19:37
• @MikeiLL It won't automatically print the file's docstring, but for users who need the info, they can just do print python_file.__doc__ to get the info. – Ethan Bierlein Jun 17 '15 at 19:41
• … or use import mix_track_utils ; help(mix_track_utils). – 200_success Jun 17 '15 at 19:54
• Right. I'm more used to writing scripts to be used from the command line, and this one just needs to exit if called as __main__, as it doesn't do anything anyway. – MikeiLL Jun 17 '15 at 20:54
• @MikeiLL Alrighty. Whatever works for you! – Ethan Bierlein Jun 17 '15 at 20:55