In [1]:
# import libraries that we will use
from pylab import *
from bregman.suite import *
import tonality as ton # The Well-Tempered Clavier Tools
from pylab import rcParams
%matplotlib inline
rcParams['figure.figsize'] = 14, 8
In [3]:
# %cd /Path/to/WTCexample, for example:
# %cd /home/mkc/src/BregmanToolkit/bregman/examples/WTCexamples
In [4]:
help(ton)
In [77]:
execfile('tonality.py') # Calling the module as a script runs a demo
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 [7]:
prelude1 = loadtxt('01.ascii') # Load the score in matrix form, 128 MIDI notes x time (tactus)
In [13]:
ton.plot_mtx(prelude1, 'Prelude No. 1 in C Major')
In [14]:
# 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 [15]:
# 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 [16]:
prelude1.shape
Out[16]:
In [17]:
x2 = prelude1[:,2]
x3 = prelude1[:,3]
print sqrt(sum((x3 - x2)**2))
In [18]:
prelude1b = ton.fold_mtx(prelude1)
D = ton.dissimilarity_mtx(prelude1b)
imshow(D)
colorbar()
title('Dissimilarity Matrix: WTC 1: Prelude No. 1 in C Major')
xlabel('Tactus', fontsize=14); ylabel('Tactus', fontsize=14)
Out[18]:
In [19]:
# Learn about the load_wtc function
help(ton.load_wtc)
In [20]:
# Load the first two works in WTC
A = ton.load_wtc(slice(0,10,2)) # sample_len=0
A = hstack(A) # Stack (concatenate) all works into a single matrix
ton.plot_mtx(A, 'WTC Preludes 1-5')
In [21]:
# Load the first five Fugues in WTC
A = ton.load_wtc(slice(1,10,2)) # sample_len=0
A = hstack(A) # Stack (concatenate)
ton.plot_mtx(A, 'WTC Fugues 1-5')
In [22]:
# Load the 24 Preludes in WTC Book 1
A = ton.load_wtc(slice(0,48,2)) # sample_len=0
A = hstack(A) # Stack (concatenate) all 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 [23]:
# 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 [24]:
# 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) all works into a single matrix
ton.plot_mtx(A)
In [25]:
# Now that we have fewer time-points due to integration, we can form the pair-wise dissimilarity matrix
D = ton.dissimilarity_mtx(A)
figure(figsize=(12,12))
imshow(D**2)
colorbar()
Out[25]:
In [26]:
# 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 [27]:
# Now make a plot of each 'beat' projected onto the first two eigenvector dimensions
figure(figsize=(10,10))
plot(u[:,0], u[:,1],'.') # u are the projection of the data onto the principal axes
grid()
In [28]:
# Setup matplotlib for external window (deafult mode)
%matplotlib
rcParams['figure.figsize'] = 12, 12
plot3(u[:,0],u[:,1],u[:,2],linestyle='none',marker='.')
title('J. S. Bach, Well Tempered Clavier: 96 Works, first 3 Principal Components')
Out[28]:
In [29]:
# Reset matplotlib for inline drawing
%matplotlib inline
rcParams['figure.figsize'] = 14, 8
In [29]:
In [52]:
In [ ]: