Browse Source

Audio

pull/42/head
Jure Šorn 5 years ago
parent
commit
6e597cad2f
2 changed files with 48 additions and 26 deletions
  1. 40
      README.md
  2. 34
      index.html

40
README.md

@ -2622,7 +2622,8 @@ img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()])
img.convert(mode='RGB').save('test.png')
```
### Animation
Animation
---------
#### Creates a GIF of a bouncing ball:
```python
# $ pip3 install imageio
@ -2644,27 +2645,39 @@ imageio.mimsave('test.gif', frames, duration=0.03)
Audio
-----
```python
import wave
from struct import pack, iter_unpack
import wave, struct
```
```python
<Wave_read> = wave.open('<path>', 'rb')
<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.
```
### Read Frames from WAV File
```python
def read_wav_file(filename):
with wave.open(filename, 'rb') as wf:
frames = wf.readframes(wf.getnframes())
return [a[0] for a in iter_unpack('<h', frames)]
with wave.open(filename, 'rb') as file:
frames = file.readframes(file.getnframes())
return [a[0] for a in struct.iter_unpack('<h', frames)]
```
### Write Frames to WAV File
```python
def write_to_wav_file(filename, frames_int, mono=True):
frames_short = (pack('<h', a) for a in frames_int)
with wave.open(filename, 'wb') as wf:
wf.setnchannels(1 if mono else 2)
wf.setsampwidth(2)
wf.setframerate(44100)
wf.writeframes(b''.join(frames_short))
frames_short = (struct.pack('<h', a) for a in frames_int)
with wave.open(filename, 'wb') as file:
file.setnchannels(1 if mono else 2)
file.setsampwidth(2)
file.setframerate(44100)
file.writeframes(b''.join(frames_short))
```
### Examples
@ -2684,7 +2697,8 @@ frames_i = (add_noise(a) for a in read_wav_file('test.wav'))
write_to_wav_file('test.wav', frames_i)
```
### Synthesizer
Synthesizer
-----------
#### Plays Popcorn by Gershon Kingsley:
```python
# $ pip3 install simpleaudio

34
index.html

@ -2234,7 +2234,7 @@ img.putdata([(add_noise(h), s, v) <span class="hljs-keyword">for</span> h, s, v
img.convert(mode=<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
</code></pre></div>
<div><h3 id="animation">Animation</h3><div><h4 id="createsagifofabouncingball">Creates a GIF of a bouncing ball:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install imageio</span>
<div><h2 id="animation"><a href="#animation" name="animation">#</a>Animation</h2><div><h4 id="createsagifofabouncingball">Creates a GIF of a bouncing ball:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install imageio</span>
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw
<span class="hljs-keyword">import</span> imageio
WIDTH, R = <span class="hljs-number">126</span>, <span class="hljs-number">10</span>
@ -2250,23 +2250,31 @@ imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<s
</code></pre></div></div>
<div><h2 id="audio"><a href="#audio" name="audio">#</a>Audio</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> wave
<span class="hljs-keyword">from</span> struct <span class="hljs-keyword">import</span> pack, iter_unpack
<div><h2 id="audio"><a href="#audio" name="audio">#</a>Audio</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> wave, struct
</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>)
&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>
</code></pre>
<div><h3 id="readframesfromwavfile">Read 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-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> wf:
frames = wf.readframes(wf.getnframes())
<span class="hljs-keyword">return</span> [a[<span class="hljs-number">0</span>] <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> iter_unpack(<span class="hljs-string">'&lt;h'</span>, frames)]
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
frames = file.readframes(file.getnframes())
<span class="hljs-keyword">return</span> [a[<span class="hljs-number">0</span>] <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> struct.iter_unpack(<span class="hljs-string">'&lt;h'</span>, frames)]
</code></pre></div>
<div><h3 id="writeframestowavfile">Write Frames to WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_wav_file</span><span class="hljs-params">(filename, frames_int, mono=True)</span>:</span>
frames_short = (pack(<span class="hljs-string">'&lt;h'</span>, a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> frames_int)
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> wf:
wf.setnchannels(<span class="hljs-number">1</span> <span class="hljs-keyword">if</span> mono <span class="hljs-keyword">else</span> <span class="hljs-number">2</span>)
wf.setsampwidth(<span class="hljs-number">2</span>)
wf.setframerate(<span class="hljs-number">44100</span>)
wf.writeframes(<span class="hljs-string">b''</span>.join(frames_short))
frames_short = (struct.pack(<span class="hljs-string">'&lt;h'</span>, a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> frames_int)
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
file.setnchannels(<span class="hljs-number">1</span> <span class="hljs-keyword">if</span> mono <span class="hljs-keyword">else</span> <span class="hljs-number">2</span>)
file.setsampwidth(<span class="hljs-number">2</span>)
file.setframerate(<span class="hljs-number">44100</span>)
file.writeframes(<span class="hljs-string">b''</span>.join(frames_short))
</code></pre></div>
<div><h3 id="examples-1">Examples</h3><div><h4 id="savesasinewavetoamonowavfile">Saves a sine wave to a mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> pi, sin
@ -2282,7 +2290,7 @@ frames_i = (add_noise(a) <span class="hljs-keyword">for</span> a <span class="h
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_i)
</code></pre></div>
<div><h3 id="synthesizer">Synthesizer</h3><div><h4 id="playspopcornbygershonkingsley">Plays Popcorn by Gershon Kingsley:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
<div><h2 id="synthesizer"><a href="#synthesizer" name="synthesizer">#</a>Synthesizer</h2><div><h4 id="playspopcornbygershonkingsley">Plays Popcorn by Gershon Kingsley:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
<span class="hljs-keyword">import</span> simpleaudio, math, struct
<span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> chain, repeat
F = <span class="hljs-number">44100</span>

Loading…
Cancel
Save