Skip to content

Commit 98384e8

Browse files
committed
Audio
1 parent 6854285 commit 98384e8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,28 @@ wf.writeframes(b''.join(samples))
15681568
wf.close()
15691569
```
15701570

1571+
### Plays Popcorn
1572+
```python
1573+
# pip3 install simpleaudio
1574+
import simpleaudio as sa
1575+
from itertools import chain, repeat
1576+
from math import pi, sin
1577+
import struct
1578+
F = 44100
1579+
S1 = '59J,57j,,59J,54j,,50J,54j,,47J,,,'
1580+
S2 = '59J,61j,,62J,61j,,62j,,59j,,61J,59j,,61j,,57j,,59J,57j,,59j,,55j,,59J,,,'
1581+
sin_f = lambda frame_no, hz: sin(frame_no * 2 * pi * hz / F)
1582+
get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
1583+
get_pause = lambda seconds: repeat(0, int(seconds * F))
1584+
get_hz = lambda note: round(16.352 * 2 ** (int(note[:2])/12))
1585+
parse_n = lambda note: (get_hz(note), 0.25 if note[2] == 'J' else 0.125)
1586+
get_note = lambda note: get_wave(*parse_n(note)) if note else get_pause(0.125)
1587+
notes_seq = f'{S1}{S1}{S2}{S1}{S1}{S2}'
1588+
samples_f = chain.from_iterable(get_note(n) for n in notes_seq.split(','))
1589+
samples_b = b''.join(struct.pack('<h', int(a * 30000)) for a in samples_f)
1590+
sa.play_buffer(samples_b, 1, 2, F)
1591+
```
1592+
15711593

15721594
Url
15731595
---

0 commit comments

Comments
 (0)