In [4]:
import tonality as ton
from bregman.suite import * # load the Bregman library toolkit
from pylab import *
%matplotlib inline
rcParams['figure.figsize'] = 12, 6
In [11]:
reload(ton) # Debugging convenience
Out[11]:
In [12]:
# Make a list of audio file names, as a list of strings
flist = sorted(glob.glob('/Users/alexandrarieger/Healing Music/*.wav'))
print flist
In [21]:
try:
import essentia
import essentia.standard
from essentia.standard import *
w = Windowing(type = 'hann')
spectrum = Spectrum() # FFT() would return the complex FFT, here we just want the magnitude spectrum
hpcp = HPCP()
peaks = SpectralPeaks()
hpcps = []
frameSize = 8192
hopSize = 4096
audio, sr, fmt = bregman.sound.wavread('/home/mkc/Music/GoldbergVariations/01 Track 01.wav')
audio = essentia.array(audio.mean(1))
for fstart in range(0, len(audio)-frameSize, hopSize):
frame = audio[fstart:fstart+frameSize]
hpcp_coeffs = hpcp(*peaks(spectrum(w(frame))))
hpcps.append(hpcp_coeffs)
# and plot them...
# as this is a 2D array, we need to use imshow() instead of plot()
imshow(array(hpcps).T, aspect = 'auto', origin='bottom', interpolation='nearest')
title('Harmonic Pitch Class Profile')
ax=yticks(arange(12),ton.pc_labels, fontsize=14)
figure()
stem(arange(12),array(hpcps).mean(0))
title('Average Pitch Class Profile')
axis(xmin=-0.5)
ax=xticks(arange(12),ton.pc_labels, fontsize=14)
X = array(hpcps).T
except:
print "Essentia not installed, defaulting to Bregman Toolkit"
# Extract audio chroma features (pitch-class amplitude profiles) from audio
X = ton.extract_audio_chroma(flist, nSecs=1, nSamps=60)
In [13]:
In [31]:
# Use the tonality tools, jus as with symbolic audio data
ton.plot_mtx(X, 'My Cool Healing Music')
ton.hist_mtx(X, 'Freq')
In [23]:
# Make the centered squared dissimilarity matrix
D = ton.dissimilarity_mtx(X)
B = ton.center_mtx(D**2)
feature_plot(B)
In [24]:
# Eigen analysis of centered dissimilarity matrix
[u,s,v] = svd(B)
stem(arange(50),s[:50])
Out[24]:
In [25]:
# 2D scatter plot of projection coefficients
plot(u[:,0], u[:,1], '.')
plot(u[:,0], u[:,1], '--')
Out[25]:
In [26]:
# 3D scatter plot of projection coefficients
%matplotlib
plot3(u[:,0],u[:,1],u[:,2], linestyle='-', linewidth=0.1, marker='.',markersize=4)
Out[26]:
In [130]:
In [29]:
# Reset inline plotting
%matplotlib inline
rcParams['figure.figsize'] = 12, 6
In [58]:
# Calculate eigenvectors of the pitch-class profiles
p,q,r = svd(X.T,0)
In [59]:
_ = plot(r)
title('Eigenvectors of pitch-class profiles')
xticks(arange(12),ton.pc_labels[:12])
grid()
ax = axis('tight')
In [97]:
# Probabilistic Latent Harmonic Components Analysis
w,z,h, norm, recon, logprob = plca.PLCA.analyze(X, 2)
In [98]:
feature_plot(X)
feature_plot(recon)
In [99]:
for ii, (zz, ww) in enumerate(zip(z,w.T)):
figure(figsize=(5,2))
stem(arange(12),zz*ww)
title('Latent Harmony: Component '+str(ii+1),fontsize=16)
xticks(arange(12),ton.pc_labels, fontsize=14)
In [21]:
In [21]:
In [21]:
In [ ]: