@@ -1195,6 +1195,7 @@ Bytes
1195
1195
< int > = < bytes > [< index> ]
1196
1196
< bytes > = < bytes > [< slice > ]
1197
1197
< bytes > = b ' ' .join(< coll_of_bytes> )
1198
+ < ints> = list (< bytes > )
1198
1199
```
1199
1200
1200
1201
### Encode
@@ -1232,9 +1233,10 @@ Struct
1232
1233
* ** Machine’s native type sizes and byte order are used by default.**
1233
1234
1234
1235
``` python
1235
- from struct import pack, unpack, calcsize
1236
+ from struct import pack, unpack, iter_unpack, calcsize
1236
1237
< bytes > = pack(' <format>' , < value_1> [, < value_2> , ... ])
1237
1238
< tuple > = unpack(' <format>' , < bytes > )
1239
+ < iter > = iter_unpack(' <format>' , < bytes > )
1238
1240
```
1239
1241
1240
1242
### Example
@@ -1700,7 +1702,7 @@ img.putdata([(int(a), 255, 255) for a in pixels])
1700
1702
img.convert(mode = ' RGB' ).save(' test.png' )
1701
1703
```
1702
1704
1703
- #### Adds noise to an image:
1705
+ #### Adds noise to PNG image:
1704
1706
``` python
1705
1707
from random import randint
1706
1708
add_noise = lambda value : max (0 , min (255 , value + randint(- 20 , 20 )))
@@ -1719,21 +1721,50 @@ img.convert(mode='RGB').save('test.png')
1719
1721
1720
1722
Audio
1721
1723
-----
1722
- #### Saves a list of floats with values between -1 and 1 to a WAV file:
1723
1724
``` 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
+ ```
1727
1757
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)
1734
1765
```
1735
1766
1736
- ### Plays Popcorn
1767
+ #### Plays Popcorn:
1737
1768
``` python
1738
1769
# pip3 install simpleaudio
1739
1770
import simpleaudio, math, struct
@@ -1747,9 +1778,9 @@ get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
1747
1778
get_hz = lambda n : 8.176 * 2 ** (int (n) / 12 )
1748
1779
parse_n = lambda note : (get_hz(note[:2 ]), 0.25 if len (note) > 2 else 0.125 )
1749
1780
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)
1753
1784
```
1754
1785
1755
1786
0 commit comments