Browse Source

Path, SQLite, Synthesizer

pull/115/head
Jure Šorn 3 years ago
parent
commit
85bbdb43d9
2 changed files with 14 additions and 14 deletions
  1. 12
      README.md
  2. 16
      index.html

12
README.md

@ -1645,7 +1645,7 @@ from pathlib import Path
```
```python
<Path> = <Path>.parent # Returns Path without final component.
<Path> = <Path>.parent # Returns Path without the final component.
<str> = <Path>.name # Returns final component as a string.
<str> = <Path>.stem # Returns final component without extension.
<str> = <Path>.suffix # Returns final component's extension.
@ -1864,11 +1864,11 @@ import sqlite3
#### Or:
```python
with <conn>: # Exits the block with commit() or rollback(),
<conn>.execute('<query>') # depending on whether an exception occurred.
<conn>.execute('<query>') # depending on whether any exception occurred.
```
### Placeholders
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetime.**
* **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).**
```python
<conn>.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
@ -2900,15 +2900,15 @@ Synthesizer
import math, struct, simpleaudio
from itertools import repeat, chain
F = 44100
P1 = '71♩,69♪,,71♩,66♪,,62♩,66♪,,59♩,,,'
P2 = '71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71♪,,73♪,,69♪,,71♩,69♪,,71♪,,67♪,,71♩,,,'
P1 = '71♩,69♪,,71♩,66♪,,62♩,66♪,,59♩,,'
P2 = '71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71♪,,73♪,,69♪,,71♩,69♪,,71♪,,67♪,,71♩,,'
get_pause = lambda seconds: 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 key: 8.176 * 2 ** (int(key) / 12)
parse_note = lambda note: (get_hz(note[:2]), 1/4 if '♩' in note else 1/8)
get_samples = lambda note: get_wave(*parse_note(note)) if note else get_pause(1/8)
samples_f = chain.from_iterable(get_samples(n) for n in f'{P1}{P1}{P2}'.split(','))
samples_f = chain.from_iterable(get_samples(n) for n in f'{P1},{P1},{P2}'.split(','))
samples_b = b''.join(struct.pack('<h', int(f * 30000)) for f in samples_f)
simpleaudio.play_buffer(samples_b, 1, 2, F)
```

16
index.html

@ -226,7 +226,7 @@ pre.prettyprint {
<body>
<header>
<aside>October 7, 2021</aside>
<aside>October 8, 2021</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1540,7 +1540,7 @@ value = args.&lt;name&gt;
&lt;Path&gt; = Path.home() <span class="hljs-comment"># Returns user's home directory.</span>
&lt;Path&gt; = Path(__file__).resolve() <span class="hljs-comment"># Returns script's path if cwd wasn't changed.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns Path without final component.</span>
<pre><code class="python language-python hljs">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns Path without the final component.</span>
&lt;str&gt; = &lt;Path&gt;.name <span class="hljs-comment"># Returns final component as a string.</span>
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns final component without extension.</span>
&lt;str&gt; = &lt;Path&gt;.suffix <span class="hljs-comment"># Returns final component's extension.</span>
@ -1698,11 +1698,11 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</code></pre></div>
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;conn&gt;: <span class="hljs-comment"># Exits the block with commit() or rollback(),</span>
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># depending on whether an exception occurred.</span>
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># depending on whether any exception occurred.</span>
</code></pre></div>
<div><h3 id="placeholders">Placeholders</h3><ul>
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li>
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetime.</strong></li>
<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li>
</ul><pre><code class="python language-python hljs">&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;list/tuple&gt;) <span class="hljs-comment"># Replaces '?'s in query with values.</span>
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedtuple&gt;) <span class="hljs-comment"># Replaces ':&lt;key&gt;'s with values.</span>
@ -2498,15 +2498,15 @@ engine.runAndWait()
<span class="hljs-keyword">import</span> math, struct, simpleaudio
<span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> repeat, chain
F = <span class="hljs-number">44100</span>
P1 = <span class="hljs-string">'71♩,69♪,,71♩,66♪,,62♩,66♪,,59♩,,,'</span>
P2 = <span class="hljs-string">'71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71♪,,73♪,,69♪,,71♩,69♪,,71♪,,67♪,,71♩,,,'</span>
P1 = <span class="hljs-string">'71♩,69♪,,71♩,66♪,,62♩,66♪,,59♩,,'</span>
P2 = <span class="hljs-string">'71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71♪,,73♪,,69♪,,71♩,69♪,,71♪,,67♪,,71♩,,'</span>
get_pause = <span class="hljs-keyword">lambda</span> seconds: 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> key: <span class="hljs-number">8.176</span> * <span class="hljs-number">2</span> ** (int(key) / <span class="hljs-number">12</span>)
parse_note = <span class="hljs-keyword">lambda</span> note: (get_hz(note[:<span class="hljs-number">2</span>]), <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(*parse_note(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 = chain.from_iterable(get_samples(n) <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> <span class="hljs-string">f'<span class="hljs-subst">{P1}</span><span class="hljs-subst">{P1}</span><span class="hljs-subst">{P2}</span>'</span>.split(<span class="hljs-string">','</span>))
samples_f = chain.from_iterable(get_samples(n) <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> <span class="hljs-string">f'<span class="hljs-subst">{P1}</span>,<span class="hljs-subst">{P1}</span>,<span class="hljs-subst">{P2}</span>'</span>.split(<span class="hljs-string">','</span>))
samples_b = <span class="hljs-string">b''</span>.join(struct.pack(<span class="hljs-string">'&lt;h'</span>, int(f * <span class="hljs-number">30000</span>)) <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> samples_f)
simpleaudio.play_buffer(samples_b, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, F)
</code></pre></div></div>
@ -3007,7 +3007,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>October 7, 2021</aside>
<aside>October 8, 2021</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save