38 lines
979 B
Python
38 lines
979 B
Python
import alsaaudio
|
|
import numpy as np
|
|
import wave
|
|
|
|
import socket
|
|
|
|
# Network
|
|
UDP_IP = "172.16.11.27"
|
|
UDP_PORT = 5005
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
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)
|
|
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") |