-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.py
38 lines (30 loc) · 923 Bytes
/
audio.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
import pyaudio
import socket
# Definir constantes
FRAMES = 1024
FORMAT = pyaudio.paInt32
CHANNELS = 8
RATE = 44100
PORT = 12345 # Certifique-se de que isso corresponda ao número da porta usado no script transmissor
# Inicializar PyAudio
audio = pyaudio.PyAudio()
# Abrir um fluxo de saída
flux_audio = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=FRAMES)
# Criar um socket UDP
SOCKET = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Vincular o socket à porta
SERVER_LOCATION = ('', PORT)
SOCKET.bind(SERVER_LOCATION)
print("Ouvindo áudio recebido...")
try:
while True:
# Receber dados de áudio
data, location = SOCKET.recvfrom(65536)
# Reproduzir os dados de áudio
flux_audio.write(location)
finally:
# Limpar recursos
flux_audio.stop_stream()
flux_audio.close()
audio.terminate()
SOCKET.close()