Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 01fe156c rédigé par mathieu Lagrange's avatar mathieu Lagrange
Parcourir les fichiers

fix

parent 75a59fb8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
%% Cell type:markdown id: tags:
%% Cell type:markdown id:b513b4fa tags:
## Import tools
%% Cell type:code id: tags:
%% Cell type:code id:6ca01fe4 tags:
``` python
# deal with matrices
import numpy as np
# progress bar
import tqdm
# principal component analysis
from sklearn.decomposition import PCA
# handle musical datasets
import mirdata
# deal with audio data
import librosa
from librosa.display import specshow
# play audio
import IPython.display as ipd
# handle display
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import cm
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:e75f9acd tags:
## Retrieve the tinysol dataset
https://forum.ircam.fr/projects/detail/tinysol
%% Cell type:code id: tags:
%% Cell type:code id:e04e4326 tags:
``` python
tinysol = mirdata.initialize('tinysol')
tinysol.download()
# run this line in case of inconsistant results that may be due to database corruption
# tinysol.validate()
```
%% Cell type:code id: tags:
%% Cell type:code id:76448ed6 tags:
``` python
# listen a random example track
example_track = tinysol.choice_track()
ipd.Audio(example_track.audio_path)
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:0a254d5d tags:
## Tones of 4 instruments in the C4-C5 pitch range
%% Cell type:code id: tags:
%% Cell type:code id:162d8c5f tags:
``` python
# select pitch range
low_pitch = librosa.note_to_midi("C4")
high_pitch = librosa.note_to_midi("C5")
# select instruments
my_instruments = [
"Clarinet in Bb", "Flute", "Violin", "Cello"
]
# build selector
my_tracks = {
track_id: track
for track_id, track in tinysol.load_tracks().items()
if low_pitch<=track.pitch_id<high_pitch
and track.instrument_full in my_instruments
}
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:f14792ce tags:
**Question:** listen to a B4 Flute tone
%% Cell type:code id: tags:
%% Cell type:code id:fd8ddc05 tags:
``` python
# answer here
####
```
%% Cell type:code id: tags:
%% Cell type:code id:4cf6149e tags:
``` python
# frame rate of representations in samples
hop_length = 512
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:8d587d16 tags:
**Question:** define a function selectRms(y, sr) that computes the amplitude envelope of this tone using the librosa.feature.rms method and select the audio corresponding to the time frame of 8 hop_length before the maximum value of the envelope and 16 hop_length after.
%% Cell type:code id: tags:
%% Cell type:code id:6637c013 tags:
``` python
def selectRms(y, # audio signal
sr # sampling rate
):
# answer here
####
####
ys = selectRms(*fluteB4[0].audio)
print(f'Original size: {y.shape[0]}. New size: {ys.shape[0]}')
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:faa4a89b tags:
## Feature set (MFCCs and Chromas)
**Question:** compute the MFCCs and the Chroma representations for each track. Once computed
%% Cell type:code id: tags:
%% Cell type:code id:b5063953 tags:
``` python
for track in tqdm.tqdm(my_tracks.values()):
# select audio
y = selectRms(*track.audio)
# compute MFCC
mfcc = librosa.feature.mfcc(y, sr, n_mels=40, n_mfcc=12)
# average over time
track.avg_mfcc = np.mean(mfcc, axis=-1)
# compute chroma
chroma = librosa.feature.chroma_cqt(y, sr)
# average over time
track.avg_chroma = np.mean(chroma, axis=-1)
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:98036f27 tags:
## Display of the features sets
**Question:** from the data in track.avg_chroma, build a matrix X of shape [len(my_tracks), 12], and standardize it row-wise.
Display the dataset by considering the first and second features.
Apply a Principal Component Analysis (PCA) and display the dataset considering the coordinates of each track along the first and second eigenvectors.
Is the display improved ?
Why ?
%% Cell type:code id: tags:
%% Cell type:code id:22340a35 tags:
``` python
# CHROMA
# answer here
Y = np.random.randn(len(my_tracks), 2) # replace this line
####
####
# pitch labels
pitch_ids = np.array([track.pitch_id for track in my_tracks.values()])
# prepare figure
plt.figure(figsize=(5, 5))
plt.xlim(-2.75, 2.75)
plt.ylim(-2.75, 2.75)
# plot each pitch class
for pitch_class in range(low_pitch, high_pitch):
sub_Y = Y[pitch_ids==pitch_class, :]
hue = (pitch_class-low_pitch) / 12
color = cm.hsv(hue)
plt.scatter(
sub_Y[:, 0], sub_Y[:, 1],
color=color, label=librosa.midi_to_note(pitch_class)[:-1])
plt.grid(linestyle="--")
plt.legend()
plt.tight_layout()
#plt.gca().set_facecolor("black")
#plt.legend()
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:1164aab6 tags:
**Answer:** here
%% Cell type:markdown id: tags:
%% Cell type:markdown id:e4856c4f tags:
**Question:** from the data in track.avg_mfcc, build a matrix X of shape [len(my_tracks), 12], and standardize it row-wise.
Display the dataset by considering the first and second features.
Apply a Principal Component Analysis (PCA) and display the dataset considering the coordinates of each track along the first and second eigenvectors.
Is the display improved ?
Why ?
%% Cell type:code id: tags:
%% Cell type:code id:efa62ea2 tags:
``` python
# MFCC
# answer here
Y = np.random.randn(len(my_tracks), 2) # replace this line
####
####
# instruments labels and markers
instruments = np.array([track.instrument_full for track in my_tracks.values()])
markers = ["o", "s", "v", "x", "^"]
# prepare figure
plt.figure(figsize=(6, 6))
plt.xlim(-6, 6)
plt.ylim(-6, 6)
# plot each instrument
for instrument_id, my_instrument in enumerate(my_instruments):
sub_Y = Y[instruments==my_instrument, :]
plt.plot(
sub_Y[:, 0], sub_Y[:, 1],
markers[instrument_id],
label=my_instrument)
plt.legend()
plt.grid(linestyle="--")
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:ee0406ae tags:
**Answer:** here
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter