forked from locaal-ai/lexisynth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_player.py
40 lines (32 loc) · 955 Bytes
/
audio_player.py
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
import queue
import time
from pydub import AudioSegment
from pydub.playback import play
import io
from PyQt6.QtCore import QThread
class AudioBuffer:
class AudioBufferType:
RAW = 0
MP3 = 1
def __init__(self, type, bytes):
self.buffer = queue.Queue()
self.type = type
self.bytes = bytes
class AudioPlayer(QThread):
def __init__(self):
super().__init__()
self.queue = queue.Queue()
self.isRunning = False
def add_to_queue(self, audio: AudioBuffer):
self.queue.put(audio)
def stop(self):
self.isRunning = False
def run(self):
while self.isRunning:
if self.queue.empty():
time.sleep(0.1)
continue
audio = self.queue.get()
if audio.type == AudioBuffer.AudioBufferType.MP3:
audio = AudioSegment.from_mp3(io.BytesIO(audio.bytes))
play(audio)