|
2234 | 2234 | img.convert(mode=<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
|
2235 | 2235 | </code></pre></div>
|
2236 | 2236 |
|
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> |
2238 | 2238 | <span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw
|
2239 | 2239 | <span class="hljs-keyword">import</span> imageio
|
2240 | 2240 | WIDTH, R = <span class="hljs-number">126</span>, <span class="hljs-number">10</span>
|
|
2250 | 2250 | </code></pre></div></div>
|
2251 | 2251 |
|
2252 | 2252 |
|
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 |
2255 | 2254 | </code></pre></div>
|
2256 | 2255 |
|
| 2256 | +<pre><code class="python language-python hljs"><Wave_read> = wave.open(<span class="hljs-string">'<path>'</span>, <span class="hljs-string">'rb'</span>) |
| 2257 | +<bytes> = <Wave_read>.readframes(nframes) |
| 2258 | +</code></pre> |
| 2259 | +<pre><code class="python language-python hljs"><Wave_write> = wave.open(<span class="hljs-string">'<path>'</span>, <span class="hljs-string">'wb'</span>) |
| 2260 | +<Wave_write>.writeframes(<bytes>) |
| 2261 | +<Wave_write>.setnchannels(<int>) <span class="hljs-comment"># Number of samples per frame.</span> |
| 2262 | +<Wave_write>.setsampwidth(<int>) <span class="hljs-comment"># Sample size in bytes.</span> |
| 2263 | +<Wave_write>.setframerate(<int>) <span class="hljs-comment"># Frames per second.</span> |
| 2264 | +</code></pre> |
2257 | 2265 | <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">'<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">'<h'</span>, frames)] |
2261 | 2269 | </code></pre></div>
|
2262 | 2270 |
|
2263 | 2271 | <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">'<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">'<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)) |
2270 | 2278 | </code></pre></div>
|
2271 | 2279 |
|
2272 | 2280 | <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 | 2290 | write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_i)
|
2283 | 2291 | </code></pre></div>
|
2284 | 2292 |
|
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> |
2286 | 2294 | <span class="hljs-keyword">import</span> simpleaudio, math, struct
|
2287 | 2295 | <span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> chain, repeat
|
2288 | 2296 | F = <span class="hljs-number">44100</span>
|
|
0 commit comments