Skip to content

Commit 6e597ca

Browse files
committed
Audio
1 parent d890a8a commit 6e597ca

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,8 @@ img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()])
26222622
img.convert(mode='RGB').save('test.png')
26232623
```
26242624

2625-
### Animation
2625+
Animation
2626+
---------
26262627
#### Creates a GIF of a bouncing ball:
26272628
```python
26282629
# $ pip3 install imageio
@@ -2644,27 +2645,39 @@ imageio.mimsave('test.gif', frames, duration=0.03)
26442645
Audio
26452646
-----
26462647
```python
2647-
import wave
2648-
from struct import pack, iter_unpack
2648+
import wave, struct
2649+
```
2650+
2651+
```python
2652+
<Wave_read> = wave.open('<path>', 'rb')
2653+
<bytes> = <Wave_read>.readframes(nframes)
2654+
```
2655+
2656+
```python
2657+
<Wave_write> = wave.open('<path>', 'wb')
2658+
<Wave_write>.writeframes(<bytes>)
2659+
<Wave_write>.setnchannels(<int>) # Number of samples per frame.
2660+
<Wave_write>.setsampwidth(<int>) # Sample size in bytes.
2661+
<Wave_write>.setframerate(<int>) # Frames per second.
26492662
```
26502663

26512664
### Read Frames from WAV File
26522665
```python
26532666
def read_wav_file(filename):
2654-
with wave.open(filename, 'rb') as wf:
2655-
frames = wf.readframes(wf.getnframes())
2656-
return [a[0] for a in iter_unpack('<h', frames)]
2667+
with wave.open(filename, 'rb') as file:
2668+
frames = file.readframes(file.getnframes())
2669+
return [a[0] for a in struct.iter_unpack('<h', frames)]
26572670
```
26582671

26592672
### Write Frames to WAV File
26602673
```python
26612674
def write_to_wav_file(filename, frames_int, mono=True):
2662-
frames_short = (pack('<h', a) for a in frames_int)
2663-
with wave.open(filename, 'wb') as wf:
2664-
wf.setnchannels(1 if mono else 2)
2665-
wf.setsampwidth(2)
2666-
wf.setframerate(44100)
2667-
wf.writeframes(b''.join(frames_short))
2675+
frames_short = (struct.pack('<h', a) for a in frames_int)
2676+
with wave.open(filename, 'wb') as file:
2677+
file.setnchannels(1 if mono else 2)
2678+
file.setsampwidth(2)
2679+
file.setframerate(44100)
2680+
file.writeframes(b''.join(frames_short))
26682681
```
26692682

26702683
### Examples
@@ -2684,7 +2697,8 @@ frames_i = (add_noise(a) for a in read_wav_file('test.wav'))
26842697
write_to_wav_file('test.wav', frames_i)
26852698
```
26862699

2687-
### Synthesizer
2700+
Synthesizer
2701+
-----------
26882702
#### Plays Popcorn by Gershon Kingsley:
26892703
```python
26902704
# $ pip3 install simpleaudio

index.html

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@
22342234
img.convert(mode=<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
22352235
</code></pre></div>
22362236

2237-
<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>
2237+
<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>
22382238
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw
22392239
<span class="hljs-keyword">import</span> imageio
22402240
WIDTH, R = <span class="hljs-number">126</span>, <span class="hljs-number">10</span>
@@ -2250,23 +2250,31 @@
22502250
</code></pre></div></div>
22512251

22522252

2253-
<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
2254-
<span class="hljs-keyword">from</span> struct <span class="hljs-keyword">import</span> pack, iter_unpack
2253+
<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
22552254
</code></pre></div>
22562255

2256+
<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>)
2257+
&lt;bytes&gt; = &lt;Wave_read&gt;.readframes(nframes)
2258+
</code></pre>
2259+
<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>)
2260+
&lt;Wave_write&gt;.writeframes(&lt;bytes&gt;)
2261+
&lt;Wave_write&gt;.setnchannels(&lt;int&gt;) <span class="hljs-comment"># Number of samples per frame.</span>
2262+
&lt;Wave_write&gt;.setsampwidth(&lt;int&gt;) <span class="hljs-comment"># Sample size in bytes.</span>
2263+
&lt;Wave_write&gt;.setframerate(&lt;int&gt;) <span class="hljs-comment"># Frames per second.</span>
2264+
</code></pre>
22572265
<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>
2258-
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> wf:
2259-
frames = wf.readframes(wf.getnframes())
2260-
<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)]
2266+
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
2267+
frames = file.readframes(file.getnframes())
2268+
<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)]
22612269
</code></pre></div>
22622270

22632271
<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>
2264-
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)
2265-
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> wf:
2266-
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>)
2267-
wf.setsampwidth(<span class="hljs-number">2</span>)
2268-
wf.setframerate(<span class="hljs-number">44100</span>)
2269-
wf.writeframes(<span class="hljs-string">b''</span>.join(frames_short))
2272+
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)
2273+
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
2274+
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>)
2275+
file.setsampwidth(<span class="hljs-number">2</span>)
2276+
file.setframerate(<span class="hljs-number">44100</span>)
2277+
file.writeframes(<span class="hljs-string">b''</span>.join(frames_short))
22702278
</code></pre></div>
22712279

22722280
<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 @@
22822290
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_i)
22832291
</code></pre></div>
22842292

2285-
<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>
2293+
<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>
22862294
<span class="hljs-keyword">import</span> simpleaudio, math, struct
22872295
<span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> chain, repeat
22882296
F = <span class="hljs-number">44100</span>

0 commit comments

Comments
 (0)