-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg2melody.py
67 lines (52 loc) · 1.45 KB
/
msg2melody.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Aron Galdon Gines
# 2017/04/07 First version on Python 2
# 2020/10/30 Migration to Python 3
# msg2melody.py mensaje
import sys
import math
import pyaudio # C:\Python27\scripts\pip install pyaudio
# Calidad: bits por segundo
BITRATE = 22050
# Tono musical base en Hz
FREC_BASE = 8.13
# Duración del tono en segundos
DURACION = 3
def generar_tono(n):
datos_onda = ''
nframes = int(BITRATE * DURACION)
for x in range(nframes):
a = FREC_BASE * n
b = (BITRATE / a) / math.pi
c = x / b
valor = int(math.sin(c) * 127 + 128)
datos_onda = datos_onda + chr(valor)
resto = nframes % BITRATE
for x in range(resto):
datos_onda = datos_onda + chr(128)
return datos_onda
def main(argv):
"""
Función principal.
"""
if not argv[1:]:
argv.append('abc')
input("Pulse Enter para empezar a reproducir...")
audio = ''
for c in argv[1]:
n = ord(c)
print ('{0} {1}'.format(c, n))
audio = audio + generar_tono(n)
PyAudio = pyaudio.PyAudio
p = PyAudio()
stream = p.open(
format = p.get_format_from_width(1),
channels = 1, rate = BITRATE, output = True)
stream.write(audio)
stream.stop_stream()
stream.close()
p.terminate()
# Inicio del programa
if __name__ == '__main__':
main(sys.argv)