-
Notifications
You must be signed in to change notification settings - Fork 0
/
melody2msg.py
60 lines (45 loc) · 1.18 KB
/
melody2msg.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Aron Galdon Gines
# 2017/04/07 First version on Python 2
# 2020/10/30 Migration to Python 3
# melody2msg.py
import sys
import multiprocessing as mp
import time
import pyaudio
import numpy as np
CHUNK = 128
CHANNELS = 1
RATE = 22050
DELAY_SIZE = 1
def feed_queue(q):
FORMAT = pyaudio.paInt16
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
while True:
frame = []
for i in range(10):
frame.append(stream.read(CHUNK))
data_ar = np.fromstring(''.join(frame), 'int16')
if q.full():
q.get_nowait()
q.put(data_ar)
def main(argv):
"""
Función principal.
"""
queue = mp.Queue(maxsize=DELAY_SIZE)
p = mp.Process(target=feed_queue, args=(queue,))
input("Pulse Enter para empezar a escuchar...")
p.start()
for t in range(0, 99):
t += 1
d = queue.get()
# Inicio del programa
if __name__ == '__main__':
main(sys.argv)