In [1]:
%pylab inline
from __future__ import print_function
import cPickle as pickle
import os
import IPython
import numpy as np
import scipy
import sklearn.mixture
import mir_eval
In [2]:
# Path to the place where the beatchromlab.zip data was unpacked.
DATA_DIR = '/Users/dpwe/Downloads/prac10/data'
In [3]:
# Read in the list of training file IDs.
def read_file_list(filename):
"""Read a text file with one item per line."""
items = []
with open(filename, 'r') as f:
for line in f:
items.append(line.strip())
return items
train_list_filename = os.path.join(DATA_DIR, 'trainfilelist.txt')
train_ids = read_file_list(train_list_filename)
test_list_filename = os.path.join(DATA_DIR, 'testfilelist.txt')
test_ids = read_file_list(test_list_filename)
print(len(train_ids), "train file IDs and", len(test_ids), "test file IDs read.")
print("Example file ID:", train_ids[134])
In [4]:
def read_beat_chroma_labels(file_id):
"""Read back a precomputed beat-synchronous chroma record."""
filename = os.path.join(os.path.join(DATA_DIR, 'beatchromlabs', file_id + '.pkl'))
with open(filename, "rb") as f:
beat_times, chroma_features, label_indices = pickle.load(f)
#chroma_features = chroma_features**0.25
chroma_features /= np.maximum(0.01, np.max(chroma_features, axis=1))[:, np.newaxis]
return beat_times, chroma_features, label_indices
file_id = train_ids[134]
beat_times, chroma_features, label_indices = read_beat_chroma_labels(file_id)
print(beat_times.shape, chroma_features.shape, label_indices.shape)
In [5]:
def my_imshow(data, **kwargs):
"""Wrapper for imshow that sets common defaults."""
plt.imshow(data, interpolation='nearest', aspect='auto',
origin='bottom', cmap='gray_r', **kwargs)
In [6]:
# Plot the chroma matrix "spectrogram-style", and show how the labels
# line up.
plt.subplot(211)
my_imshow(chroma_features[:50, :].transpose())
plt.subplot(212)
plt.plot(label_indices[:50], '.')
Out[6]:
In [7]:
# Resynthesize chroma feature matrices to audio with Shepard tones.
sr = 16000
y = mir_eval.sonify.chroma(chroma_features.transpose(), beat_times, sr)
IPython.display.Audio(data=y, rate=sr)
Out[7]: