Browse Source

Audio

pull/44/head
Jure Šorn 5 years ago
parent
commit
b147b3bc06
2 changed files with 21 additions and 21 deletions
  1. 22
      README.md
  2. 20
      index.html

22
README.md

@ -2340,7 +2340,7 @@ rotation=<int>|<datetime.timedelta>|<datetime.time>|<str>
* **`'<str>'` - Any of above as a string: `'100 MB'`, `'1 month'`, `'monday at 12:00'`, ...**
### Retention
**Sets a condition which old log files are deleted.**
**Sets a condition which old log files get deleted.**
```python
retention=<int>|<datetime.timedelta>|<str>
```
@ -2613,7 +2613,7 @@ from PIL import Image
<tuple/int> = <Image>.getpixel((x, y)) # Returns a pixel.
<Image>.putpixel((x, y), <tuple/int>) # Writes a pixel to image.
<ImagingCore> = <Image>.getdata() # Returns a sequence of pixels.
<Image>.putdata(<list/tuple>) # Writes a sequence of pixels.
<Image>.putdata(<list/tuple/ImagingCore>) # Writes a sequence of pixels.
<Image>.paste(<Image>, (x, y)) # Writes an image to image.
```
@ -2629,9 +2629,9 @@ from PIL import Image
```python
WIDTH, HEIGHT = 100, 100
size = WIDTH * HEIGHT
hue = [255 * i/size for i in range(size)]
hues = [255 * i/size for i in range(size)]
img = Image.new('HSV', (WIDTH, HEIGHT))
img.putdata([(int(h), 255, 255) for h in hue])
img.putdata([(int(h), 255, 255) for h in hues])
img.convert('RGB').save('test.png')
```
@ -2723,7 +2723,7 @@ nframes = <Wave_read>.getnframes() # Number of frames.
+-----------+-------------+------+-------------+
```
### Read Float Frames from WAV File
### Read Float Samples from WAV File
```python
def read_wav_file(filename):
def get_int(a_bytes):
@ -2732,13 +2732,13 @@ def read_wav_file(filename):
with wave.open(filename, 'rb') as file:
frames = file.readframes(file.getnframes())
width = file.getsampwidth()
chunks = (frames[i: i + width] for i in range(0, len(frames), width))
return [get_int(a) / pow(2, width * 8 - 1) for a in chunks]
samples = (frames[i: i + width] for i in range(0, len(frames), width))
return [get_int(a) / pow(2, width * 8 - 1) for a in samples]
```
### Write Float Frames to WAV File
### Write Float Samples to WAV File
```python
def write_to_wav_file(filename, frames_float, nchannels=1, sampwidth=2, framerate=44100):
def write_to_wav_file(filename, float_samples, nchannels=1, sampwidth=2, framerate=44100):
def get_bytes(a_float):
a_float = max(-1, min(1 - 2e-16, a_float))
a_float += sampwidth == 1
@ -2748,7 +2748,7 @@ def write_to_wav_file(filename, frames_float, nchannels=1, sampwidth=2, framerat
file.setnchannels(nchannels)
file.setsampwidth(sampwidth)
file.setframerate(framerate)
file.writeframes(b''.join(get_bytes(a) for a in frames_float))
file.writeframes(b''.join(get_bytes(a) for a in float_samples))
```
### Examples
@ -2762,7 +2762,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: value + (random()-0.5) * 0.03
add_noise = lambda value: value + (random() - 0.5) * 0.03
frames_f = (add_noise(a) for a in read_wav_file('test.wav'))
write_to_wav_file('test.wav', frames_f)
```

20
index.html

@ -2022,7 +2022,7 @@ logger.&lt;level&gt;(<span class="hljs-string">'A logging message.'</span>)
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;time&gt;'</span></code> - Time of day.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;str&gt;'</span></code> - Any of above as a string: <code class="python hljs"><span class="hljs-string">'100 MB'</span></code>, <code class="python hljs"><span class="hljs-string">'1 month'</span></code>, <code class="python hljs"><span class="hljs-string">'monday at 12:00'</span></code>, …</strong></li>
</ul>
<div><h3 id="retention">Retention</h3><p><strong>Sets a condition which old log files are deleted.</strong></p><pre><code class="python language-python hljs">retention=&lt;int&gt;|&lt;datetime.timedelta&gt;|&lt;str&gt;
<div><h3 id="retention">Retention</h3><p><strong>Sets a condition which old log files get deleted.</strong></p><pre><code class="python language-python hljs">retention=&lt;int&gt;|&lt;datetime.timedelta&gt;|&lt;str&gt;
</code></pre></div>
@ -2228,7 +2228,7 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</s
<pre><code class="python language-python hljs">&lt;tuple/int&gt; = &lt;Image&gt;.getpixel((x, y)) <span class="hljs-comment"># Returns a pixel.</span>
&lt;Image&gt;.putpixel((x, y), &lt;tuple/int&gt;) <span class="hljs-comment"># Writes a pixel to image.</span>
&lt;ImagingCore&gt; = &lt;Image&gt;.getdata() <span class="hljs-comment"># Returns a sequence of pixels.</span>
&lt;Image&gt;.putdata(&lt;list/tuple&gt;) <span class="hljs-comment"># Writes a sequence of pixels.</span>
&lt;Image&gt;.putdata(&lt;list/tuple/ImagingCore&gt;) <span class="hljs-comment"># Writes a sequence of pixels.</span>
&lt;Image&gt;.paste(&lt;Image&gt;, (x, y)) <span class="hljs-comment"># Writes an image to image.</span>
</code></pre>
<div><h3 id="modes-1">Modes</h3><ul>
@ -2239,9 +2239,9 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</s
<li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - 3x8-bit pixels, Hue, Saturation, Value color space.</strong></li>
</ul><div><h3 id="examples">Examples</h3><div><h4 id="createsapngimageofarainbowgradient">Creates a PNG image of a rainbow gradient:</h4><pre><code class="python language-python hljs">WIDTH, HEIGHT = <span class="hljs-number">100</span>, <span class="hljs-number">100</span>
size = WIDTH * HEIGHT
hue = [<span class="hljs-number">255</span> * i/size <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(size)]
hues = [<span class="hljs-number">255</span> * i/size <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(size)]
img = Image.new(<span class="hljs-string">'HSV'</span>, (WIDTH, HEIGHT))
img.putdata([(int(h), <span class="hljs-number">255</span>, <span class="hljs-number">255</span>) <span class="hljs-keyword">for</span> h <span class="hljs-keyword">in</span> hue])
img.putdata([(int(h), <span class="hljs-number">255</span>, <span class="hljs-number">255</span>) <span class="hljs-keyword">for</span> h <span class="hljs-keyword">in</span> hues])
img.convert(<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
</code></pre></div></div></div>
@ -2320,18 +2320,18 @@ nframes = &lt;Wave_read&gt;.getnframes() <span class="hljs-comment"
+-----------+-------------+------+-------------+
</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>
<div><h3 id="readfloatsamplesfromwavfile">Read Float Samples 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>)
<span class="hljs-keyword">return</span> an_int - <span class="hljs-number">128</span> * (width == <span class="hljs-number">1</span>)
<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())
width = file.getsampwidth()
chunks = (frames[i: i + width] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, len(frames), width))
<span class="hljs-keyword">return</span> [get_int(a) / pow(<span class="hljs-number">2</span>, width * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> chunks]
samples = (frames[i: i + width] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, len(frames), width))
<span class="hljs-keyword">return</span> [get_int(a) / pow(<span class="hljs-number">2</span>, width * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> samples]
</code></pre></div>
<div><h3 id="writefloatframestowavfile">Write Float 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_float, nchannels=<span class="hljs-number">1</span>, sampwidth=<span class="hljs-number">2</span>, framerate=<span class="hljs-number">44100</span>)</span>:</span>
<div><h3 id="writefloatsamplestowavfile">Write Float Samples 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, float_samples, nchannels=<span class="hljs-number">1</span>, sampwidth=<span class="hljs-number">2</span>, framerate=<span class="hljs-number">44100</span>)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_bytes</span><span class="hljs-params">(a_float)</span>:</span>
a_float = max(<span class="hljs-number">-1</span>, min(<span class="hljs-number">1</span> - <span class="hljs-number">2e-16</span>, a_float))
a_float += sampwidth == <span class="hljs-number">1</span>
@ -2341,7 +2341,7 @@ nframes = &lt;Wave_read&gt;.getnframes() <span class="hljs-comment"
file.setnchannels(nchannels)
file.setsampwidth(sampwidth)
file.setframerate(framerate)
file.writeframes(<span class="hljs-string">b''</span>.join(get_bytes(a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> frames_float))
file.writeframes(<span class="hljs-string">b''</span>.join(get_bytes(a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> float_samples))
</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
@ -2351,7 +2351,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: value + (random()<span class="hljs-number">-0.5</span>) * <span class="hljs-number">0.03</span>
add_noise = <span class="hljs-keyword">lambda</span> value: 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>))
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_f)
</code></pre></div>

|||||||
100:0
Loading…
Cancel
Save