Browse Source

Audio

pull/42/head
Jure Šorn 5 years ago
parent
commit
8700ca2d90
2 changed files with 54 additions and 12 deletions
  1. 34
      README.md
  2. 32
      index.html

34
README.md

@ -2651,15 +2651,38 @@ import wave, struct
```python
<Wave_read> = wave.open('<path>', 'rb')
framerate = <Wave_read>.getframerate() # Number of frames per second.
nchannels = <Wave_read>.getnchannels() # Number of samples per frame.
sampwidth = <Wave_read>.getsampwidth() # Sample size in bytes.
nframes = <Wave_read>.getnframes() # Number of frames.
<bytes> = <Wave_read>.readframes(nframes)
```
```python
<Wave_write> = wave.open('<path>', 'wb')
<Wave_write>.writeframes(<bytes>)
<Wave_write>.setnchannels(<int>) # Number of samples per frame.
<Wave_write>.setsampwidth(<int>) # Sample size in bytes.
<Wave_write>.setframerate(<int>) # Frames per second.
<Wave_write>.setframerate(<int>) # 44100 for CD, 48000 for video.
<Wave_write>.setnchannels(<int>) # 1 for mono, 2 for stereo.
<Wave_write>.setsampwidth(<int>) # 2 for CD quality sound.
<Wave_write>.writeframes(<bytes>)
```
* **Bytes object contains a seqence of frames, each consisting of one or more samples.**
* **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, then the integer is interpreted as unsigned.**
* **For all other sample sizes the integer is interpreted as signed with little-endian byte order.**
* **In stereo signal first sample of a frame belongs to the left channel.**
### Sample Values
```text
┏━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━━━┓
┃ sampwidth │ min │ zero │ max ┃
┠───────────┼─────────────┼──────┼─────────────┨
┃ 1 │ 0 │ 128 │ 255 ┃
┃ 2 │ -32768 │ 0 │ 32767 ┃
┃ 3 │ -8388608 │ 0 │ 8388607 ┃
┃ 4 │ -2147483648 │ 0 │ 2147483647 ┃
┗━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━┛
```
### Read Float Frames from WAV File
@ -2701,8 +2724,7 @@ write_to_wav_file('test.wav', frames_f)
#### Adds noise to a mono WAV file:
```python
from random import random
add_noise = lambda value: max(-1, min(1, value + (random()-0.5)*0.03))
frames_f = (add_noise(a) for a in read_wav_file('test.wav'))
frames_f = (a + (random()-0.5) * 0.03 for a in read_wav_file('test.wav'))
write_to_wav_file('test.wav', frames_f)
```

32
index.html

@ -2254,14 +2254,35 @@ imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<s
</code></pre></div>
<pre><code class="python language-python hljs">&lt;Wave_read&gt; = wave.open(<span class="hljs-string">'&lt;path&gt;'</span>, <span class="hljs-string">'rb'</span>)
framerate = &lt;Wave_read&gt;.getframerate() <span class="hljs-comment"># Number of frames per second.</span>
nchannels = &lt;Wave_read&gt;.getnchannels() <span class="hljs-comment"># Number of samples per frame.</span>
sampwidth = &lt;Wave_read&gt;.getsampwidth() <span class="hljs-comment"># Sample size in bytes.</span>
nframes = &lt;Wave_read&gt;.getnframes() <span class="hljs-comment"># Number of frames.</span>
&lt;bytes&gt; = &lt;Wave_read&gt;.readframes(nframes)
</code></pre>
<pre><code class="python language-python hljs">&lt;Wave_write&gt; = wave.open(<span class="hljs-string">'&lt;path&gt;'</span>, <span class="hljs-string">'wb'</span>)
&lt;Wave_write&gt;.writeframes(&lt;bytes&gt;)
&lt;Wave_write&gt;.setnchannels(&lt;int&gt;) <span class="hljs-comment"># Number of samples per frame.</span>
&lt;Wave_write&gt;.setsampwidth(&lt;int&gt;) <span class="hljs-comment"># Sample size in bytes.</span>
&lt;Wave_write&gt;.setframerate(&lt;int&gt;) <span class="hljs-comment"># Frames per second.</span>
&lt;Wave_write&gt;.setframerate(&lt;int&gt;) <span class="hljs-comment"># 44100 for CD, 48000 for video.</span>
&lt;Wave_write&gt;.setnchannels(&lt;int&gt;) <span class="hljs-comment"># 1 for mono, 2 for stereo.</span>
&lt;Wave_write&gt;.setsampwidth(&lt;int&gt;) <span class="hljs-comment"># 2 for CD quality sound.</span>
&lt;Wave_write&gt;.writeframes(&lt;bytes&gt;)
</code></pre>
<ul>
<li><strong>Bytes object contains a seqence of frames, each consisting of one or more samples.</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, then the integer is interpreted as unsigned.</strong></li>
<li><strong>For all other sample sizes the integer is interpreted as signed with little-endian byte order.</strong></li>
<li><strong>In stereo signal first sample of a frame belongs to the left channel.</strong></li>
</ul>
<div><h3 id="samplevalues">Sample Values</h3><pre><code class="text language-text">┏━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━━━┓
┃ sampwidth │ min │ zero │ max ┃
┠───────────┼─────────────┼──────┼─────────────┨
┃ 1 │ 0 │ 128 │ 255 ┃
┃ 2 │ -32768 │ 0 │ 32767 ┃
┃ 3 │ -8388608 │ 0 │ 8388607 ┃
┃ 4 │ -2147483648 │ 0 │ 2147483647 ┃
┗━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━┛
</code></pre></div>
<div><h3 id="readfloatframesfromwavfile">Read Float Frames from WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_wav_file</span><span class="hljs-params">(filename)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_int</span><span class="hljs-params">(a_bytes)</span>:</span>
an_int = int.from_bytes(a_bytes, <span class="hljs-string">'little'</span>, signed=width!=<span class="hljs-number">1</span>)
@ -2293,8 +2314,7 @@ write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_f)
<div><h4 id="addsnoisetoamonowavfile">Adds noise to a mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> random
add_noise = <span class="hljs-keyword">lambda</span> value: max(<span class="hljs-number">-1</span>, min(<span class="hljs-number">1</span>, value + (random()<span class="hljs-number">-0.5</span>)*<span class="hljs-number">0.03</span>))
frames_f = (add_noise(a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> read_wav_file(<span class="hljs-string">'test.wav'</span>))
frames_f = (a + (random()<span class="hljs-number">-0.5</span>) * <span class="hljs-number">0.03</span> <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> read_wav_file(<span class="hljs-string">'test.wav'</span>))
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_f)
</code></pre></div>

Loading…
Cancel
Save