44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import alsaaudio
|
|
import numpy as np
|
|
import wave
|
|
import opuslib
|
|
import socket
|
|
|
|
# Network
|
|
# UDP_IP = "172.16.11.27"
|
|
UDP_IP = "127.0.0.1"
|
|
|
|
UDP_PORT = 5005
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
encoder = opuslib.Encoder(48000, 1, opuslib.APPLICATION_VOIP)
|
|
inp = alsaaudio.PCM(
|
|
alsaaudio.PCM_CAPTURE, # capture mode
|
|
# alsaaudio.PCM_NONBLOCK, # non-blocking
|
|
alsaaudio.PCM_NORMAL,
|
|
|
|
channels=1, # mono
|
|
rate=48000, # sample rate (Hz)
|
|
format=alsaaudio.PCM_FORMAT_S16_LE, # 16-bit
|
|
periodsize=960, # frames per read
|
|
device='default'
|
|
)
|
|
|
|
# frames = []
|
|
|
|
try:
|
|
while True:
|
|
length, data = inp.read() # read audio chunk
|
|
if length > 0:
|
|
# convert raw bytes to numpy array
|
|
audio = np.frombuffer(data, dtype=np.int16)
|
|
opus_data = encoder.encode(data, 960)
|
|
sock.sendto(opus_data, (UDP_IP, UDP_PORT))
|
|
|
|
# sock.sendto(data, (UDP_IP, UDP_PORT))
|
|
print(f"Frames: {length} | Max amplitude: {np.max(np.abs(audio))}")
|
|
except KeyboardInterrupt:
|
|
print("Stopped")
|
|
|
|
finally:
|
|
sock.close()
|
|
print("Stopped") |