-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new notebooks to test the new features of maelzel.snd.generate, maelz…
…el.snd.filters, and maelzel.snd.vamptools
- Loading branch information
1 parent
555bc25
commit ac72085
Showing
5 changed files
with
15,063 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,320 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"id": "3d2406e2-24db-4456-930d-8c9db8735880", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from maelzel.snd import vamptools\n", | ||
"from maelzel.snd import audiosample\n", | ||
"from maelzel.snd import deverb\n", | ||
"import pitchtools as pt\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import sndfileio\n", | ||
"from math import nan" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "5119d97c-4541-44b2-ab3e-d5d6da3f8d8a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"samples, sr = sndfileio.sndread(\"../snd/piano-pitchtest-Cmaj.flac\")\n", | ||
"samples2 = deverb.removeSustain(samples, sr)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "ac2997b7-7b13-4602-bbc1-cd0056d12b16", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Duplicate times: t0=9.827845805, t1=9.827845805\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"mnOut size: 1789\n", | ||
"m_pitchTrack size: 1789\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"PyinResult(voicedProbabilityCurve=Linear[0.023219955:10.402539683], smoothPitchCurve=Linear[0.029024944:10.402539683], f0curve=Linear[0.023219955:10.396734694], f0candidates=[(0.023s, [525.3, 261.0, 174.2, 131.6, …], [0.1242, 0.0119, 0.0008, 0.0010, …]), (0.029s, [524.5, 261.5, 174.0, 131.3, …], [0.1391, 0.0118, 0.0027, 0.0023, …]), (0.035s, [528.6, 263.7, 175.8, 131.9, …], [0.1468, 0.0080, 0.0021, 0.0025, …]), (0.041s, [526.0, 262.4, 175.4, 131.5, …], [0.2193, 0.0182, 0.0055, 0.0047, …]), (0.046s, [526.8, 263.3, 175.0, 131.2, …], [0.2278, 0.0290, 0.0149, 0.0102, …]), (0.052s, [528.3, 263.6, 175.7, 131.5, …], [0.1444, 0.0139, 0.0088, 0.0088, …]), (0.058s, [527.7, 263.5, 175.5, 131.5, …], [0.1061, 0.0092, 0.0092, 0.0101, …]), (0.064s, [528.6, 264.2, 175.8, 131.7, …], [0.1022, 0.0087, 0.0087, 0.0101, …]), (0.070s, [529.9, 264.2, 176.0, 131.9, …], [0.0947, 0.0065, 0.0076, 0.0092, …]), (0.075s, [529.0, 264.0, 175.8, 131.7, …], [0.0945, 0.0688, 0.0688, 0.1007, …]), ...]" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"r = vamptools.pyin(samples2, sr, overlap=8, voicedThresholdPercentile=0.1, onsetSensitivity=0.1, threshDistr='beta15', lowAmpSuppressionDb=-70, preciseTime=True, maxRelativeSkew=0.1, rmsPeriod=0.020, minRmsPercentile=0.01)\n", | ||
"r" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "cf54baa8-9aa6-4167-99d3-541f3afe79da", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"QSocketNotifier: Can only be used with threads started with QThread\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<Axes: >" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"%matplotlib qt \n", | ||
"import matplotlib.pyplot as plt\n", | ||
"axes = audiosample.Sample(samples, sr).plotSpectrogram(maxfreq=6000)\n", | ||
"#(r.voicedProbabilityCurve * 6000).plot(axes=axes)\n", | ||
"# r.f0curve.plot(axes=axes, linewidth=2, color='#ffff00', show=False)\n", | ||
"c = r.smoothPitchCurveNan * (r.numCandidates > 0).nanmask()\n", | ||
"c.plot(axes=axes, show=False, n=10000)\n", | ||
"# r.smoothPitchCurveNan.plot(axes=axes, linewidth=2, color='#00ffff', show=False, n=10000)\n", | ||
"# (r.smoothPitchCurve*2).plot(axes=axes, linewidth=2, color='#ffff00', show=False, n=1000)\n", | ||
"# r.f0curve.plot(axes=axes, show=False)\n", | ||
"# (r.voicedProbabilityCurve*10000).plot(axes=axes, show=False, n=10000)\n", | ||
"# (r.numCandidates * 1000).plot(axes=axes, show=False, n=1000)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 28, | ||
"id": "09dac2d2-f11e-491e-8bf9-95e4f83b80c2", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Linear[0.023219955:1.263219955] 6\n", | ||
"Linear[1.313219955:2.4232199550000004] 6\n", | ||
"Linear[2.4932199550000003:4.873219955] 24\n", | ||
"Linear[5.273219955:6.583219955000001] 4\n", | ||
"Linear[6.623219955000001:7.123219955000001] 2\n", | ||
"Linear[7.513219955:8.063219955000001] 9\n", | ||
"Linear[8.313219955000001:8.723219955000001] 7\n", | ||
"Linear[8.793219955:9.023219955] 4\n", | ||
"Linear[9.093219955:9.333219955] 4\n", | ||
"Linear[9.373219955:9.573219954999999] 4\n", | ||
"Linear[9.633219955:9.853219955] 5\n", | ||
"Linear[9.883219955:10.383219955] 11\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"qt.qpa.wayland: Wayland does not support QWindow::requestActivate()\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"parts = [b.aslinear().simplify(threshold=0.1) for b in c[::0.01].split()]\n", | ||
"axes = plt.subplot()\n", | ||
"for part in parts:\n", | ||
" print(part, len(part.points()[0]))\n", | ||
" part.plot(axes=axes, show=False)\n", | ||
"plt.show()\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "36633240-bda7-4f7e-841f-751a21c6eb60", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from math import isnan\n", | ||
"\n", | ||
"def split_fragments(b):\n", | ||
" xs, ys = b.points()\n", | ||
" parts = []\n", | ||
" lastpart: list[tuple[float, float]] = None\n", | ||
" for x, y in zip(xs, ys):\n", | ||
" if not isnan(y):\n", | ||
" if lastpart is None:\n", | ||
" lastpart = []\n", | ||
" parts.append(lastpart)\n", | ||
" lastpart.append((x, y))\n", | ||
" else:\n", | ||
" lastpart = None\n", | ||
" cls = b.__class__\n", | ||
" bpfs = []\n", | ||
" for part in parts:\n", | ||
" xs, ys = zip(*part)\n", | ||
" if len(xs) >= 2:\n", | ||
" bpfs.append(cls(xs, ys))\n", | ||
" return bpfs\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "a528cc8b-3fe4-42ac-bd8a-7f61e8c3536f", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
"\n", | ||
"\n", | ||
"--Csound version 6.19 (double samples) Jan 13 2024\n", | ||
"[commit: c53556fbbb69d14554a4a4bd91efa441a1701d70]\n", | ||
"\u001b[mlibsndfile-1.2.0\n", | ||
"\u001b[msr = 44100.0,\u001b[m kr = 689.062,\u001b[m ksmps = 64\n", | ||
"\u001b[m0dBFS level = 1.0,\u001b[m A4 tuning = 440.0\n", | ||
"\u001b[maudio buffered in 256 sample-frame blocks\n", | ||
"\u001b[mreading 2048-byte blks of shorts from adc:Built-in Audio Pro: (RAW)\n", | ||
"writing 512 sample blks of 64-bit floats to dac:Built-in Audio Pro:\n", | ||
"SECTION 1:\n", | ||
"\u001b[m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import csoundengine as ce\n", | ||
"session = ce.Session()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "d697c3af-a425-4354-9a09-b091fa7a24b0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"session.defInstr('vco', r'''\n", | ||
"|kamp=0.1, kfreq=1000, iatt=0.005, irel=0.1, isust=0.7|\n", | ||
"kgain = kfreq > 100 ? 1 : 0\n", | ||
"kgain = lagud:k(kgain, 0.050, 0.100)\n", | ||
"aenv = linsegr:a(0, iatt, 1, 0.010, isust, irel, 0)\n", | ||
"outch 1, vco2:a(kamp, kfreq) * interp(kgain) * aenv\n", | ||
"''')\n", | ||
"\n", | ||
"def schedbpf(bpf, session):\n", | ||
" freqpairs = bpf.flatpairs()\n", | ||
" freqpairs[::2] -= bpf.x0\n", | ||
" synth = session.sched('vco', delay=bpf.x0, dur=bpf.x1 - bpf.x0, kfreq=freqpairs[1]) \n", | ||
" synth.automate('kfreq', freqpairs, delay=bpf.x0)\n", | ||
" return synth" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"id": "ea9f4b78-5943-4568-9957-2db1faeef41c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"qt.qpa.wayland: Wayland does not support QWindow::requestActivate()\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<Axes: >" | ||
] | ||
}, | ||
"execution_count": 16, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"c = r.smoothPitchCurveNan * (r.voicedProbabilityCurve > 0.01)\n", | ||
"c.plot(n=1000)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "76107af5-ebd6-43c0-853c-17e1265e11fe", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import bpf4\n", | ||
"parts = bpf4.util.split_fragments(r.f0curve)\n", | ||
"# parts = split_fragments(r.smoothPitchCurveNan)\n", | ||
"\n", | ||
"with session:\n", | ||
" for part in parts:\n", | ||
" s = schedbpf(part, session=session)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "116fb7bd-41b5-4bc3-a1d5-ed7ebe6fa58c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[<matplotlib.lines.Line2D at 0x799ac4deb090>]" | ||
] | ||
}, | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"prob = r.voicedProbabilityHistogram.percentileToValue(0.1)\n", | ||
"axes.plot((0, r.voicedProbabilityCurve.x1), (prob*10000, prob*10000))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |