Skip to content

Commit 5af311b

Browse files
committed
Audio
1 parent 83ef252 commit 5af311b

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,14 +1571,12 @@ wf.close()
15711571
### Plays Popcorn
15721572
```python
15731573
# pip3 install simpleaudio
1574-
import simpleaudio as sa
1574+
import simpleaudio, math, struct
15751575
from itertools import chain, repeat
1576-
from math import pi, sin
1577-
import struct
15781576
F = 44100
15791577
S1 = '59J,57j,,59J,54j,,50J,54j,,47J,,,'
15801578
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)
1579+
sin_f = lambda frame_no, hz: math.sin(frame_no * 2 * math.pi * hz / F)
15821580
get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
15831581
get_pause = lambda seconds: repeat(0, int(seconds * F))
15841582
get_hz = lambda note: round(16.352 * 2 ** (int(note[:2])/12))
@@ -1587,7 +1585,7 @@ get_note = lambda note: get_wave(*parse_n(note)) if note else get_pause(0.125)
15871585
notes_seq = f'{S1}{S1}{S2}'
15881586
samples_f = chain.from_iterable(get_note(n) for n in notes_seq.split(','))
15891587
samples_b = b''.join(struct.pack('<h', int(a * 30000)) for a in samples_f)
1590-
sa.play_buffer(samples_b, 1, 2, F)
1588+
simpleaudio.play_buffer(samples_b, 1, 2, F)
15911589
```
15921590

15931591

0 commit comments

Comments
 (0)