In [1]:
# import libraries that we will use
from pylab import *
from bregman.suite import *
import tonality as ton # The Well-Tempered Clavier Tools
import mayavi.mlab as mylab
from pylab import rcParams
rcParams['figure.figsize'] = 14, 8
In [2]:
execfile('tonality.py')
In [3]:
reload(ton)
Out[3]:
In [4]:
help(ton)
1. Analyze music using both audio features and symbolic (score-based) features
2. Analyze a musical corpus by sampling from multiple works
3. Extract the latent geometry in tonal music
In [5]:
prelude1 = loadtxt('01.ascii') # Load the score in matrix form, 128 MIDI notes x time (tactus)
In [6]:
ton.plot_mtx(prelude1, 'Prelude No. 1 in C Major')
In [7]:
# Overlay dissonance plot, scaled to range of pitch values
ton.plot_mtx(prelude1, 'Prelude No. 1 in C Major')
d = ton.dissonance_fun(prelude1)
plot(d*30,'r')
title('Dissonance',fontsize=16)
ax = axis('tight')
In [8]:
# Summarize the work as a histogram, showing only range of active pitches
ton.hist_mtx(prelude1, 'J. S. Bach, Prelude No. 1 in C Major')
In [9]:
D = ton.dissimilarity_mtx(prelude1)
imshow(D)
colorbar()
title('Dissimilarity Matrix: WTC 1: Prelude No. 1 in C Major')
xlabel('Tactus', fontsize=14); ylabel('Tactus', fontsize=14)
Out[9]:
In [10]:
# Learn about the load_wtc function
help(ton.load_wtc)
In [11]:
# Load the first two works in WTC
A = ton.load_wtc(slice(0,10,2)) # sample_len=0
A = hstack(A) # Stack (concatenate) the two works into a single matrix
ton.plot_mtx(A, 'WTC Preludes 1-5')
In [12]:
# Load the first five Fugues in WTC
A = ton.load_wtc(slice(1,10,2)) # sample_len=0
A = hstack(A) # Stack (concatenate) the works into a single matrix
ton.plot_mtx(A, 'WTC Fugues 1-5')
In [13]:
# Load the 24 Preludes in WTC Book 1
A = ton.load_wtc(slice(0,48,2)) # sample_len=0
A = hstack(A) # Stack (concatenate) the two works into a single matrix
#A = ton.scale_mtx(A.T, norm=True).T
ton.plot_mtx(A, 'WTC Preludes 1-24')
print "Matrix Size is:", A.shape
In [14]:
# Make a plot of pitch usage in all Preludes in book 1
ton.hist_mtx(ton.fold_mtx(A), 'WTC Preludes 1-24')
WRITE YOUR ANSWER HERE
In [15]:
# Loading whole works results in very large matrices.
# We need to restrict the size of matrices to have <=5000 time points
# We have two choices:
# 1) Average Adjacent Tactus into pitch profiles spanning Beats, Bars or Phrases
# 2) Randomly sample subsets of Beats, Bars, or Phrases in each work
A = ton.load_wtc(win_len=16, sample_len=20) # Integrate 16 adjacent frames into beat-level
A = hstack(A) # Stack (concatenate) the two works into a single matrix
ton.plot_mtx(A)
In [16]:
# Now that we have fewer time-points due to integration, we can form the pair-wise dissimilarity matrix
D = ton.dissimilarity_mtx(A)
imshow(D**2)
colorbar()
Out[16]:
In [17]:
# Center the Dissimilarity Matrix
B = ton.center_mtx(D**2) # Use squared dissimilarity
# Singular Value Decomposition (Eigenvector Analysis)
u,s,v = svd(B) #
figure(figsize=(12,8))
stem(arange(32),s[:32])
grid()
In [18]:
# Now make a plot of each 'beat' projected onto the first two eigenvector dimensions
figure(figsize=(6,6))
plot(u[:,0], u[:,1],'.')
grid()
figure(figsize=(6,6))
plot3(u[:,0],u[:,1],u[:,2], linestyle='', marker='.')
Out[18]:
In [19]:
mylab.points3d(u[:,0], u[:,1], u[:,2], color=(.5,1,.5), scale_factor=.1)
mylab.show()
In [20]:
sys.path.append('/home/mkc/exp/tsne_python')
In [21]:
import tsne
In [22]:
tsne.tsne?
In [ ]:
In [23]:
p = tsne.tsne(A.T, no_dims=3, perplexity=5)
In [24]:
plot(p[:,0],p[:,1],'.')
Out[24]:
In [25]:
figure(figsize=(12,8))
ax = plot3(p, linestyle='', marker='.')
In [27]:
figure(figsize=(20,6))
subplot(131)
plot(p[:,0],p[:,1],'.')
subplot(132)
plot(p[:,0],p[:,2],'.')
subplot(133)
plot(p[:,1],p[:,2],'.')
Out[27]:
In [28]:
# Interactive 3D plot (would be great to make this interactive audio-visual)
In [33]:
mylab.points3d(p[:,0], p[:,1], p[:,2], color=(0,1,1), scale_factor=2.5)
mylab.show()
In [118]:
A.shape
Out[118]:
In [32]:
mylab.points3d?
In [29]:
In [ ]: