Skip to content

Commit 71edb44

Browse files
committed
Audio
1 parent b140848 commit 71edb44

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

README.md

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ Bytes
11951195
<int> = <bytes>[<index>]
11961196
<bytes> = <bytes>[<slice>]
11971197
<bytes> = b''.join(<coll_of_bytes>)
1198+
<ints> = list(<bytes>)
11981199
```
11991200

12001201
### Encode
@@ -1232,9 +1233,10 @@ Struct
12321233
* **Machine’s native type sizes and byte order are used by default.**
12331234

12341235
```python
1235-
from struct import pack, unpack, calcsize
1236+
from struct import pack, unpack, iter_unpack, calcsize
12361237
<bytes> = pack('<format>', <value_1> [, <value_2>, ...])
12371238
<tuple> = unpack('<format>', <bytes>)
1239+
<iter> = iter_unpack('<format>', <bytes>)
12381240
```
12391241

12401242
### Example
@@ -1700,7 +1702,7 @@ img.putdata([(int(a), 255, 255) for a in pixels])
17001702
img.convert(mode='RGB').save('test.png')
17011703
```
17021704

1703-
#### Adds noise to an image:
1705+
#### Adds noise to PNG image:
17041706
```python
17051707
from random import randint
17061708
add_noise = lambda value: max(0, min(255, value + randint(-20, 20)))
@@ -1719,21 +1721,50 @@ img.convert(mode='RGB').save('test.png')
17191721

17201722
Audio
17211723
-----
1722-
#### Saves a list of floats with values between -1 and 1 to a WAV file:
17231724
```python
1724-
import wave, struct
1725-
samples_l = [struct.pack('<h', int(a * 30000)) for a in <list>]
1726-
samples_b = b''.join(samples_l)
1725+
import wave
1726+
from struct import pack, iter_unpack
1727+
```
1728+
1729+
### Read Frames from WAV File
1730+
```python
1731+
def read_wav_file(filename):
1732+
with wave.open(filename, 'rb') as wf:
1733+
frames = wf.readframes(wf.getnframes())
1734+
return [a[0] for a in iter_unpack('<h', frames)]
1735+
```
1736+
1737+
### Write Frames to WAV File
1738+
```python
1739+
def write_to_wav_file(filename, frames_int):
1740+
frames_short = (pack('<h', a) for a in frames_int)
1741+
with wave.open(filename, 'wb') as wf:
1742+
wf.setnchannels(1)
1743+
wf.setsampwidth(2)
1744+
wf.setframerate(44100)
1745+
wf.writeframes(b''.join(frames_short))
1746+
```
1747+
1748+
### Examples
1749+
#### Saves a sine wave to a WAV file:
1750+
```python
1751+
from math import pi, sin
1752+
sin_f = lambda i: sin(i * 2 * pi * 440 / 44100)
1753+
frames_f = (sin_f(a) for a in range(100000))
1754+
frames_i = (int(a * 30000) for a in frames_f)
1755+
write_to_wav_file('test.wav', frames_i)
1756+
```
17271757

1728-
wf = wave.open('test.wav', 'wb')
1729-
wf.setnchannels(1)
1730-
wf.setsampwidth(2)
1731-
wf.setframerate(44100)
1732-
wf.writeframes(samples_b)
1733-
wf.close()
1758+
#### Adds noise to a WAV file:
1759+
```python
1760+
from random import randint
1761+
add_noise = lambda value: max(-32768, min(32768, value + randint(-500, 500)))
1762+
frames_i = read_wav_file('test.wav')
1763+
frames_i = (add_noise(a) for a in frames_i)
1764+
write_to_wav_file('test.wav', frames_i)
17341765
```
17351766

1736-
### Plays Popcorn
1767+
#### Plays Popcorn:
17371768
```python
17381769
# pip3 install simpleaudio
17391770
import simpleaudio, math, struct
@@ -1747,9 +1778,9 @@ get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
17471778
get_hz = lambda n: 8.176 * 2 ** (int(n) / 12)
17481779
parse_n = lambda note: (get_hz(note[:2]), 0.25 if len(note) > 2 else 0.125)
17491780
get_note = lambda note: get_wave(*parse_n(note)) if note else get_pause(0.125)
1750-
samples_f = chain.from_iterable(get_note(n) for n in f'{P1}{P1}{P2}'.split(','))
1751-
samples_b = b''.join(struct.pack('<h', int(a * 30000)) for a in samples_f)
1752-
simpleaudio.play_buffer(samples_b, 1, 2, F)
1781+
frames_i = chain.from_iterable(get_note(n) for n in f'{P1}{P1}{P2}'.split(','))
1782+
frames_b = b''.join(struct.pack('<h', int(a * 30000)) for a in frames_i)
1783+
simpleaudio.play_buffer(frames_b, 1, 2, F)
17531784
```
17541785

17551786

0 commit comments

Comments
 (0)