156 lines
3.9 KiB
Python
156 lines
3.9 KiB
Python
import socket as s
|
|
import sounddevice as sd
|
|
import wavio as wv
|
|
import os
|
|
import wave
|
|
import numpy as np
|
|
import time
|
|
#esp32 default IP address
|
|
HOST = "192.168.4.1"
|
|
#port for UDP audio streaming
|
|
PORT = 3333
|
|
|
|
frequency = 44100 #Hz
|
|
|
|
sd.default.dtype = 'int32'
|
|
def record():
|
|
while True:
|
|
print("Enter duration you would like to record(seconds): ", end="")
|
|
try:
|
|
duration = int(input())
|
|
except ValueError:
|
|
print("Enter a valid integer number!")
|
|
duration = 0
|
|
continue
|
|
|
|
if duration == 0:
|
|
continue
|
|
|
|
|
|
|
|
print("Press enter to start recording for " + str(duration) + " seconds...")
|
|
input()
|
|
|
|
print("\n\nRECORDING STARTED")
|
|
recording = sd.rec(int(duration*frequency),samplerate=frequency, channels=2)
|
|
|
|
sd.wait()
|
|
|
|
print("\n\nRECORDING STOPPED")
|
|
|
|
|
|
|
|
print("Enter a name to save the recording: ",end="")
|
|
name = str(input())
|
|
name = name + ".wav"
|
|
#writes the audio as 32(4byte) samples at a frequency of 44100Hz
|
|
wv.write(name,recording,frequency,sampwidth=4)
|
|
return
|
|
|
|
|
|
|
|
def stream(sock):
|
|
counter = 0
|
|
print("Select a file from current directory or another...")
|
|
sound_list = list()
|
|
for string in os.listdir(os.getcwd()):
|
|
if string.endswith(".wav"):
|
|
sound_list.append(string)
|
|
print("Current directory files:")
|
|
for i in range(len(sound_list)):
|
|
print(str(i+1) + "." + sound_list[i])
|
|
|
|
print()
|
|
|
|
|
|
#Open file based on index
|
|
while True:
|
|
print("Enter the number of the file:", end="")
|
|
try:
|
|
index = int(input())
|
|
except ValueError:
|
|
print("Enter a valid integer number!")
|
|
index = 0
|
|
continue
|
|
|
|
#reduces the input index so that the index matches with the list
|
|
index = index-1
|
|
if index < 0 or index > len(sound_list)-1:
|
|
print("Enter a valid integer number between 1-",len(sound_list))
|
|
continue
|
|
|
|
break
|
|
|
|
file = None
|
|
try:
|
|
file = wave.open(sound_list[index],"rb")
|
|
except:
|
|
print("Could not open the file...Try again!")
|
|
return
|
|
|
|
print("\nFile was opened successfully...Beginning streaming!\n")
|
|
|
|
CHUNK_FRAMES = 128
|
|
sample_rate = file.getframerate()
|
|
channels = file.getnchannels()
|
|
sample_width = file.getsampwidth()
|
|
|
|
print("Sample rate: ",sample_rate)
|
|
print("Channel: " + "Stereo" if channels == 2 else "Mono")
|
|
print("Sample size(bits): ", 32 if sample_width == 4 else 16)
|
|
|
|
for _ in range(50):
|
|
aud_chunk = file.readframes(CHUNK_FRAMES)
|
|
if not aud_chunk:
|
|
break
|
|
|
|
|
|
print(f"Python Sending Hex: {aud_chunk[:4].hex()}")
|
|
sock.sendto(aud_chunk,(HOST,PORT))
|
|
|
|
|
|
|
|
while True:
|
|
aud_chunk = file.readframes(CHUNK_FRAMES)
|
|
if not aud_chunk:
|
|
break
|
|
|
|
|
|
sock.sendto(aud_chunk,(HOST,PORT))
|
|
|
|
time.sleep(CHUNK_FRAMES/sample_rate)
|
|
|
|
wav = wave.open(sound_list[index],"rb")
|
|
data = np.frombuffer(wav.readframes(10),dtype=np.int32)
|
|
print(data[:10])
|
|
|
|
print("File finished streaming...")
|
|
wav.close()
|
|
file.close()
|
|
|
|
def main():
|
|
sock = s.socket(s.AF_INET,s.SOCK_DGRAM)
|
|
|
|
while True:
|
|
print("Options:\n1.Record audio\n2.Select audio\n3.Exit")
|
|
try:
|
|
usr_in = int(input("Enter number: "))
|
|
except ValueError:
|
|
print("Enter a valid integer number!")
|
|
usr_in = 4
|
|
if usr_in < 1 or usr_in > 3:
|
|
continue
|
|
|
|
match usr_in:
|
|
case 1:
|
|
record()
|
|
case 2:
|
|
stream(sock)
|
|
case 3:
|
|
print("Terminating audio streaming application...")
|
|
exit()
|
|
sock.close()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|