Skip to content

Commit

Permalink
Merge pull request #3 from groupmm/docstring_corr
Browse files Browse the repository at this point in the history
Docstring corr
  • Loading branch information
yiitozer authored Mar 11, 2024
2 parents 1191cdf + ff6ac82 commit 398d5b1
Show file tree
Hide file tree
Showing 33 changed files with 1,284 additions and 401 deletions.
163 changes: 6 additions & 157 deletions demo_spectrogram.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,48 @@
"id": "bd0baf29",
"metadata": {},
"source": [
<<<<<<< HEAD
"# Sonification of Spectograms (work in progress)"
=======
"# Sonification of Spectograms\n",
"Within this notebook, we explain how ```libsoni.core.spectrum``` can be used to sonify spectrograms."
>>>>>>> yiit_dev
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d488d70",
"metadata": {},
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"source": [
"import librosa\n",
"import librosa.display\n",
<<<<<<< HEAD
"import libfmp\n",
=======
>>>>>>> yiit_dev
"import numpy as np\n",
"import os\n",
"from IPython import display as ipd\n",
"import matplotlib.pyplot as plt\n",
<<<<<<< HEAD
"\n",
"from libsoni.core.spectrum import sonify_spectrum\n",
"\n",
"%matplotlib inline\n",
"Fs = 22050"
=======
"import libfmp\n",
"\n",
"from libsoni.core.spectrogram import sonify_spectrogram, sonify_spectrogram_multi\n",
"from libsoni.core.methods import generate_tone_additive_synthesis\n",
"\n",
"from libsoni.core.spectrogram import sonify_spectrogram_multi\n",
"%matplotlib inline\n",
"\n",
"Fs = 22050\n",
"N = 4096\n",
"H = 2048"
>>>>>>> yiit_dev
]
},
{
"cell_type": "markdown",
<<<<<<< HEAD
"id": "9915b4d8",
"metadata": {},
"source": [
"## Sound Example"
=======
"id": "ca2c4487",
"metadata": {},
"source": [
"## Simple Scenario\n",
"To make the use of the module ```libsoni.core.spectrum``` clear with a simple example, let's first assume a spectogram like an image. The vertical axis is attributed to frequency in conventional formats, while the horizontal axis represents time. Let's say we want to create an image for a C major scale. For this we need a frequency axis that keeps the frequencies of the notes of a C major scale"
>>>>>>> yiit_dev
]
},
{
"cell_type": "code",
"execution_count": null,
<<<<<<< HEAD
"id": "a0bef90a",
"metadata": {},
"outputs": [],
"source": [
"x, Fs = librosa.load(os.path.join('data_audio','samples','01Pia1F060f_np___0.wav'))\n",
"print('Original Audio:')\n",
=======
"id": "0d503155",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -109,54 +79,21 @@
"\n",
"plt.imshow(image, cmap='gray_r', aspect='auto', origin='lower')\n",
"x = sonify_spectrogram_multi(image, c_major_pitch_frequencies, times, num_processes = 4)\n",
>>>>>>> yiit_dev
"ipd.display(ipd.Audio(x, rate=Fs))"
]
},
{
"cell_type": "markdown",
<<<<<<< HEAD
"id": "2c61c79a",
"metadata": {},
"source": [
"## Performing STFT"
=======
"id": "b5b16bce",
"metadata": {},
"source": [
"## Running Example: *Frühlingstraum* by *Franz Schubert*\n",
"<img src=\"figures/demo_spectrum/Schubert_D911-11_score.png\" alt=\"Gute Nacht\" width=\"600\">"
>>>>>>> yiit_dev
]
},
{
"cell_type": "code",
"execution_count": null,
<<<<<<< HEAD
"id": "3df05162",
"metadata": {},
"outputs": [],
"source": [
"N = 128*8\n",
"H = int(N/2)\n",
"\n",
"color = 'gray_r' \n",
"\n",
"X = librosa.stft(x, n_fft=N, hop_length=H, win_length=N, window='hann', pad_mode='constant', center=True)\n",
"\n",
"Y = np.log(1 + 100 * np.abs(X) ** 2)\n",
"\n",
"T_coef = np.arange(X.shape[1]) * H / Fs\n",
"T_coef_librosa = librosa.frames_to_time(np.arange(X.shape[1]), sr=Fs, hop_length=H)\n",
"\n",
"K = N // 2\n",
"F_coef = np.arange(K+1) * Fs / N\n",
"F_coef_librosa = librosa.fft_frequencies(sr=Fs, n_fft=N)\n",
"\n",
"plt.figure(figsize=(6, 3))\n",
"extent = [T_coef[0], T_coef[-1], F_coef[0], F_coef[-1]]\n",
"plt.imshow(Y, cmap=color, aspect='auto', origin='lower', extent=extent)\n",
=======
"id": "b14bd983",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -193,73 +130,33 @@
"plt.figure(figsize=(6, 3))\n",
"extent = [T_coef[0], T_coef[-1], F_coef[0], F_coef[-1]]\n",
"plt.imshow(np.log(1+5*X), cmap='gray_r', aspect='auto', origin='lower', extent=extent)\n",
>>>>>>> yiit_dev
"plt.xlabel('Time (seconds)')\n",
"plt.ylabel('Frequency (Hz)')\n",
"plt.colorbar()\n",
"plt.tight_layout()\n",
<<<<<<< HEAD
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5650d2bf",
"metadata": {},
"outputs": [],
"source": [
"Y_soni = sonify_spectrum(Y, F_coef, T_coef)\n",
"ipd.display(ipd.Audio(Y_soni, rate = Fs))"
=======
"plt.show()\n",
"\n",
"x_resynthesized = sonify_spectrogram_multi(X, F_coef, T_coef, num_processes = 4)\n",
"print('Sonified STFT:')\n",
"ipd.display(ipd.Audio(x_resynthesized, rate = Fs))"
>>>>>>> yiit_dev
]
},
{
"cell_type": "markdown",
<<<<<<< HEAD
"id": "86ac06bb",
"metadata": {},
"source": [
"## Scenario: *Frühlingstraum (Dream of Spring)* by *Franz Schubert*\n",
"<img src=\"figures/demo_spectrum/FMP_B_Sonify_Pitch_Schubert_D911-11_A.png\" alt=\"Frühlingstraum\" width=\"600\">"
=======
"id": "c8675133",
"metadata": {},
"source": [
"## Scenario: CQT"
>>>>>>> yiit_dev
]
},
{
"cell_type": "code",
"execution_count": null,
<<<<<<< HEAD
"id": "dd9e9a4a",
=======
"id": "55f32c47",
>>>>>>> yiit_dev
"metadata": {},
"outputs": [],
"source": [
"# Log-frequency spectrogrm from audio recording\n",
<<<<<<< HEAD
"schubert_audio, Fs = librosa.load(os.path.join('data_audio', 'demo_spectrum', 'FMP_B_Sonify_Pitch_Schubert_D911-11_SC06.wav'), sr=Fs)\n",
"\n",
"H = 512*2\n",
"X_CQT = np.abs(librosa.cqt(y=schubert_audio, sr=Fs, hop_length=H,norm=2))\n",
"num_frames = X_CQT.shape[1]\n",
"X_wav = np.zeros((128, num_frames))\n",
"X_wav[23:23+84, :] = X_CQT\n",
"\n",
"\n",
"#X = libfmp.c3.normalize_feature_sequence(X, norm='2')\n",
=======
"X_CQT = np.abs(librosa.cqt(y=x, sr=Fs, hop_length=H, n_bins=84, bins_per_octave=12, norm=1))\n",
"\n",
"\n",
Expand All @@ -268,42 +165,15 @@
"\n",
"\n",
"\n",
>>>>>>> yiit_dev
"Fs_frame = Fs / H\n",
"\n",
"title='Log-frequency spectrogram (Fs_frame = %.3f)' % Fs_frame\n",
"\n",
<<<<<<< HEAD
"libfmp.b.plot_matrix(X_wav, Fs=Fs_frame, ylabel='MIDI pitch number', \n",
=======
"libfmp.b.plot_matrix(X_CQT, Fs=Fs_frame, ylabel='MIDI pitch number',\n",
>>>>>>> yiit_dev
" title=title, figsize=(8, 4))\n",
"plt.ylim([36, 84])\n",
"plt.show()\n",
"\n",
<<<<<<< HEAD
"freqs = 440 * 2 ** ((np.arange(0,128)-69) / 12)\n",
"\n",
"times = np.arange(start=0, stop=num_frames*H, step = H)/Fs\n",
"\n",
"Y_soni = sonify_spectrum(X_wav, freqs, times)\n",
"\n",
"print('Sonification of log-frequency spectrogram with libsoni:')\n",
"ipd.display(ipd.Audio(Y_soni, rate=Fs))\n",
"\n",
"# Sonification\n",
"harmonics = [1]\n",
"fading_msec = 0.5\n",
"x_pitch_wav, x_pitch_wav_stereo = \\\n",
" libfmp.b.sonify_pitch_activations_with_signal(X_wav, schubert_audio, Fs_frame, Fs, \n",
" fading_msec=fading_msec, \n",
" harmonics_weights=harmonics)\n",
"print('Sonification of log-frequency spectrogram (mono):')\n",
"ipd.display(ipd.Audio(x_pitch_wav, rate=Fs) )\n",
"print('Sonification of log-frequency spectrogram and original audio (stereo):')\n",
"ipd.display(ipd.Audio(x_pitch_wav_stereo, rate=Fs))"
=======
"\n",
"x_resynthesized = sonify_spectrogram_multi(X_CQT, frequencies, times, num_processes = 4)\n",
"print('Sonified CQT:')\n",
Expand Down Expand Up @@ -343,28 +213,7 @@
"\n",
"s = sonify_spectrogram_multi(S, mel_frequencies, times, num_processes = 4)\n",
"ipd.display(ipd.Audio(s, rate = Fs))"
>>>>>>> yiit_dev
]
},
{
"cell_type": "code",
"execution_count": null,
<<<<<<< HEAD
"id": "85be8938",
=======
"id": "762f5e80",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "79ac3fef",
>>>>>>> yiit_dev
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Binary file modified docs/build/doctrees/core/f0.doctree
Binary file not shown.
Binary file added docs/build/doctrees/core/methods.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/core/pianoroll.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/core/tse.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/doctrees/utils.doctree
Binary file not shown.
Loading

0 comments on commit 398d5b1

Please sign in to comment.