lamorfini

server / client - microphone sounddevice

Jul 24th, 2021
970
304 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # server.py
  2. import socket, sys, time
  3.  
  4. x = socket.socket()
  5. h_name = socket.gethostname()
  6. print("Your host name is : ", h_name)
  7. port = 8000
  8. x.bind(('',port))
  9. x.listen(1)
  10. connection, address = x.accept()
  11. print("Server is ONLINE ! ! !")
  12.  
  13. try:
  14.      while 1:
  15.           display_mess = input(str(">> "))
  16.           display_mess = display_mess.encode()
  17.           connection.send(display_mess)
  18.           in_message = connection.recv(1024)
  19.           in_message = in_message.decode()
  20.           print()
  21.           print("Client said: ",in_message)
  22.           print()
  23. except:
  24.      print("Client went OFFLINE")
  25.  
  26.  
  27. # client.py
  28.  
  29.  
  30. import socket, sys, time
  31.  
  32. x = socket.socket()
  33. h_name = input(str("Enter hostname:- "))
  34. port = 8000
  35. x.connect((h_name, port))
  36. print("Server Connected...!")
  37. print("Let the Server Type First")
  38.  
  39. try:
  40.      while 1:
  41.           incoming_message = x.recv(1024)
  42.           incoming_message = incoming_message.decode()
  43.           print()
  44.           print("Server said: ",incoming_message)
  45.           print()
  46.           message = input(str(">>>> "))
  47.           message = message.encode()
  48.           x.send(message)
  49. except:
  50.      print("Server went OFFLINE")
  51.  
  52.  
  53.  
  54. # Microphone.py
  55.  
  56. import sounddevice as sd
  57. import soundfile as sf
  58.  
  59. class Rec():
  60.     def __init__(self, name, duration):
  61.         """ inizializzazione (nome  e durata di registrazione) """
  62.         self.duration = duration
  63.         self.name = name
  64.     def recoding(self):
  65.         """ registrazione + salvataggio file .wav """
  66.         myrecording = sd.rec(int(self.duration * 10000), samplerate=10000, channels=1)
  67.         sd.wait()
  68.         sf.write(self.name, myrecording, 10000)
  69.     def play(self):
  70.         """ play """
  71.         data, fs = sf.read(self.name)
  72.         sd.play(data, fs)
  73.         sd.wait()
  74.     def stop(self):
  75.         """ stop """
  76.         sd.stop()  
  77. # test
  78. n = Rec("prova.wav", 3)
  79. n.recoding()
  80. n.play()
  81. n.stop()
  82.  
RAW Paste Data