plt.plot?

"""
Explanation: &larr; Back to Index
Exercise: Understanding Audio Features through Sonification
This is an exercise notebook. It's a playground for your Python code. Feel free to write and execute your code without fear.
When you see a cell that looks like this:
End of explanation
"""


filename = '125_bounce.wav'
#filename = 'conga_groove.wav'
#filename = '58bpm.wav'
url = 'http://audio.musicinformationretrieval.com/' + filename

urllib.urlretrieve?

"""
Explanation: that is a cue to use a particular command, in this case, plot. Run the cell to see documentation for that command. (To quickly close the Help window, press q.) 
For more documentation, visit the links in the Help menu above. Also see the other notebooks; all the exercises here are covered somewhere else in separate notebooks.
This exercise is loosely based upon "Lab 1" from previous MIR workshops (2010).
Goals
In this exercise, you will segment, feature extract, and analyze audio files.

Detect onsets in an audio signal.
Segment the audio signal at each onset.
Compute features for each segment.
Gain intuition into the features by listening to each segment separately.

Step 1: Retrieve Audio
Download the file simpleLoop.wav onto your local machine.
End of explanation
"""


%ls

"""
Explanation: Make sure the download worked:
End of explanation
"""


librosa.load?

"""
Explanation: Save the audio signal into an array.
End of explanation
"""


print fs

"""
Explanation: Show the sample rate:
End of explanation
"""


IPython.display.Audio?

"""
Explanation: Listen to the audio signal.
End of explanation
"""


librosa.display.waveplot?

"""
Explanation: Display the audio signal.
End of explanation
"""


librosa.stft?

"""
Explanation: Compute the short-time Fourier transform:
End of explanation
"""


librosa.logamplitude?

"""
Explanation: For display purposes, compute the log amplitude of the STFT:
End of explanation
"""


# Play with the parameters, including x_axis and y_axis
librosa.display.specshow?

"""
Explanation: Display the spectrogram.
End of explanation
"""


librosa.onset.onset_detect?

librosa.frames_to_time?

"""
Explanation: Step 2: Detect Onsets
Find the times, in seconds, when onsets occur in the audio signal.
End of explanation
"""


librosa.frames_to_samples?

"""
Explanation: Convert the onset times into sample indices.
End of explanation
"""


# Use the `length` parameter so the click track is the same length as the original signal
mir_eval.sonify.clicks?

# Play the click track "added to" the original signal
IPython.display.Audio?

"""
Explanation: Play a "beep" at each onset.
End of explanation
"""


# Assuming these variables exist:
# x: array containing the audio signal
# fs: corresponding sampling frequency
# onset_samples: array of onsets in units of samples
frame_sz = int(0.100*fs)
segments = numpy.array([x[i:i+frame_sz] for i in onset_samples])

"""
Explanation: Step 3: Segment the Audio
Save into an array, segments, 100-ms segments beginning at each onset.
End of explanation
"""


def concatenate_segments(segments, fs=44100, pad_time=0.300):
    padded_segments = [numpy.concatenate([segment, numpy.zeros(int(pad_time*fs))]) for segment in segments]
    return numpy.concatenate(padded_segments)
concatenated_signal = concatenate_segments(segments, fs)

"""
Explanation: Here is a function that adds 300 ms of silence onto the end of each segment and concatenates them into one signal.
Later, we will use this function to listen to each segment, perhaps sorted in a different order.
End of explanation
"""


IPython.display.Audio?

"""
Explanation: Listen to the newly concatenated signal.
End of explanation
"""


# returns a boolean array
librosa.core.zero_crossings?

# you'll need this to actually count the number of zero crossings per segment
sum?

"""
Explanation: Step 4: Extract Features
For each segment, compute the zero crossing rate.
End of explanation
"""


# zcrs: array, number of zero crossings in each frame
ind = numpy.argsort(zcrs)
print ind

"""
Explanation: Use argsort to find an index array, ind, such that segments[ind] is sorted by zero crossing rate.
End of explanation
"""


concatenated_signal = concatenate_segments(segments[ind], fs)

"""
Explanation: Sort the segments by zero crossing rate, and concatenate the sorted segments.
End of explanation
"""


IPython.display.Audio?

"""
Explanation: Step 5: Listen to Segments
Listen to the sorted segments. What do you hear?
End of explanation
"""


#url = 'http://audio.musicinformationretrieval.com/conga_groove.wav'
#url = 'http://audio.musicinformationretrieval.com/58bpm.wav'

"""
Explanation: More Exercises
Repeat the steps above for the following audio files:
End of explanation
"""


