From 5f9632678ddd58dea56d36dde181a8a6b683aef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= <sornjure@gmail.com> Date: Sun, 6 Oct 2024 00:41:51 +0200 Subject: [PATCH] Audio, Synthesizer --- README.md | 7 +++---- index.html | 11 +++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3c38a34..23a6d61 100644 --- a/README.md +++ b/README.md @@ -2885,8 +2885,7 @@ import wave * **Bytes object contains a sequence of frames, each consisting of one or more samples.** * **In a stereo signal, the first sample of a frame belongs to the left channel.** * **Each sample consists of one or more bytes that, when converted to an integer, indicate the displacement of a speaker membrane at a given moment.** -* **If sample width is one byte, then the integer should be encoded unsigned.** -* **For all other sizes, the integer should be encoded signed with little-endian byte order.** +* **If sample width is one byte, then the integer should be encoded unsigned. For all other sizes, the integer should be encoded signed with little-endian byte order.** ### Sample Values ```text @@ -2947,7 +2946,7 @@ write_to_wav_file('test.wav', samples_f, params) ```python # $ pip3 install simpleaudio from simpleaudio import play_buffer -with wave.open('test.wav', 'rb') as file: +with wave.open('test.wav') as file: p = file.getparams() frames = file.readframes(-1) play_buffer(frames, p.nchannels, p.sampwidth, p.framerate).wait_done() @@ -2976,7 +2975,7 @@ P2 = '71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71♪,,73♪,,69♪,,71♩,6 get_pause = lambda seconds: it.repeat(0, int(seconds * F)) sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F) get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F))) -get_hz = lambda note: 8.176 * 2 ** (int(note[:2]) / 12) +get_hz = lambda note: 440 * 2 ** ((int(note[:2]) - 69) / 12) get_sec = lambda note: 1/4 if '♩' in note else 1/8 get_samples = lambda note: get_wave(get_hz(note), get_sec(note)) if note else get_pause(1/8) samples_f = it.chain.from_iterable(get_samples(n) for n in (P1+P2).split(',')) diff --git a/index.html b/index.html index afcff5b..7b281a1 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@ <body> <header> - <aside>October 4, 2024</aside> + <aside>October 6, 2024</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </header> @@ -2356,8 +2356,7 @@ imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<s <li><strong>Bytes object contains a sequence of frames, each consisting of one or more samples.</strong></li> <li><strong>In a stereo signal, the first sample of a frame belongs to the left channel.</strong></li> <li><strong>Each sample consists of one or more bytes that, when converted to an integer, indicate the displacement of a speaker membrane at a given moment.</strong></li> -<li><strong>If sample width is one byte, then the integer should be encoded unsigned.</strong></li> -<li><strong>For all other sizes, the integer should be encoded signed with little-endian byte order.</strong></li> +<li><strong>If sample width is one byte, then the integer should be encoded unsigned. For all other sizes, the integer should be encoded signed with little-endian byte order.</strong></li> </ul> <div><h3 id="samplevalues">Sample Values</h3><pre><code class="python hljs">┏━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━┓ ┃ sampwidth │ min │ zero │ max ┃ @@ -2406,7 +2405,7 @@ write_to_wav_file(<span class="hljs-string">'test.wav'</span>, samples_f, params <div><h4 id="playsthewavfile">Plays the WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span> <span class="hljs-keyword">from</span> simpleaudio <span class="hljs-keyword">import</span> play_buffer -<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'test.wav'</span>, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file: +<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'test.wav'</span>) <span class="hljs-keyword">as</span> file: p = file.getparams() frames = file.readframes(<span class="hljs-number">-1</span>) play_buffer(frames, p.nchannels, p.sampwidth, p.framerate).wait_done() @@ -2428,7 +2427,7 @@ P2 = <span class="hljs-string">'71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71 get_pause = <span class="hljs-keyword">lambda</span> seconds: it.repeat(<span class="hljs-number">0</span>, int(seconds * F)) sin_f = <span class="hljs-keyword">lambda</span> i, hz: math.sin(i * <span class="hljs-number">2</span> * math.pi * hz / F) get_wave = <span class="hljs-keyword">lambda</span> hz, seconds: (sin_f(i, hz) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(int(seconds * F))) -get_hz = <span class="hljs-keyword">lambda</span> note: <span class="hljs-number">8.176</span> * <span class="hljs-number">2</span> ** (int(note[:<span class="hljs-number">2</span>]) / <span class="hljs-number">12</span>) +get_hz = <span class="hljs-keyword">lambda</span> note: <span class="hljs-number">440</span> * <span class="hljs-number">2</span> ** ((int(note[:<span class="hljs-number">2</span>]) - <span class="hljs-number">69</span>) / <span class="hljs-number">12</span>) get_sec = <span class="hljs-keyword">lambda</span> note: <span class="hljs-number">1</span>/<span class="hljs-number">4</span> <span class="hljs-keyword">if</span> <span class="hljs-string">'♩'</span> <span class="hljs-keyword">in</span> note <span class="hljs-keyword">else</span> <span class="hljs-number">1</span>/<span class="hljs-number">8</span> get_samples = <span class="hljs-keyword">lambda</span> note: get_wave(get_hz(note), get_sec(note)) <span class="hljs-keyword">if</span> note <span class="hljs-keyword">else</span> get_pause(<span class="hljs-number">1</span>/<span class="hljs-number">8</span>) samples_f = it.chain.from_iterable(get_samples(n) <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> (P1+P2).split(<span class="hljs-string">','</span>)) @@ -2929,7 +2928,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active <footer> - <aside>October 4, 2024</aside> + <aside>October 6, 2024</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </footer>