HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Visualize Parts of Song as Analyzed by Echonest

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
visualizeanalyzedsongechonestparts

Problem

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.

Solution

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!

Context

StackExchange Code Review Q#78264, answer score: 6

Revisions (0)

No revisions yet.