-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
30 lines (22 loc) · 1.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python3
import pyaudio, numpy
sampling_rate: int = 44100
def freq2sound(freq: float, duration: float = 0.25, volume: float = 0.5, sampling_rate: int = sampling_rate) -> bytes:
samples = (numpy.sin(2 * numpy.pi * numpy.arange(sampling_rate * duration) * freq / sampling_rate)).astype(numpy.float32)
return (volume * samples).tobytes()
def stream_sound(audio: bytes, output_format = pyaudio.paFloat32, channels = 1, sampling_rate: int = sampling_rate, output: bool = True) -> None:
p = pyaudio.PyAudio()
stream = p.open(format = output_format, channels = channels, rate = sampling_rate, output = output)
stream.write(audio)
stream.stop_stream()
stream.close()
p.terminate()
_ch: list[str] = [chr(i) for i in range(128)]
_freq = range(200,455,2)
ch_freq_map: dict[str,int] = dict(zip(_ch, _freq))
ch_sound_map: dict[str, bytes] = {ch:freq2sound(freq) for (ch, freq) in ch_freq_map.items()}
text: str = input('Enter string:\n > ')
sounds = bytearray()
for ch in text:
sounds.extend(bytearray(ch_sound_map[ch]))
stream_sound(bytes(sounds))