This commit is contained in:
2024-09-20 00:21:49 +02:00
parent 945ed5677b
commit fad17ef9a8
2 changed files with 40 additions and 19 deletions
+7
View File
@@ -0,0 +1,7 @@
import time
first_time = time.time_ns()
time.sleep(1)
time_diff = time.time_ns() - first_time
print(time_diff)
#2000149433
+33 -19
View File
@@ -21,14 +21,14 @@ logger = logging.getLogger("discord")
location = "/home/pi/Conjurer/transcripts/"
class WaveWriter:
def __init__(self, user_id):
def __init__(self, user_id, queue):
self.queue = queue
self.user = user_id
self.username = str(user_id.id)
logger.info("Creating Wavewriter %s", self.username)
logger.info(user_id)
self.present_file_id = 0
self.file_name_past = None
self.file_name_present = location + self.username + "_" + str(self.present_file_id) + ".mp3"
self.transcript_file_present : wave.Wave_write = wave.open(
self.file_name_present, "wb"
@@ -36,7 +36,6 @@ class WaveWriter:
self.transcript_file_present.setnchannels(CHANNELS)
self.transcript_file_present.setsampwidth(SAMPLE_WIDTH)
self.transcript_file_present.setframerate(SAMPLING_RATE)
self.file_name_future = location + self.username + "_" + str(self.present_file_id + 1) + ".mp3"
self.transcript_file_future : wave.Wave_write = wave.open(
self.file_name_future, "wb"
@@ -45,11 +44,13 @@ class WaveWriter:
self.transcript_file_future.setsampwidth(SAMPLE_WIDTH)
self.transcript_file_future.setframerate(SAMPLING_RATE)
def rotate(self):
self.transcript_file_present.close()
self.name_file_past = self.file_name_present
self.present_file_id += 1
if self.present_file_id > 10:
self.present_file_id = 0
else:
self.present_file_id += 1
self.file_name_present = self.file_name_future
self.transcript_file_present = self.transcript_file_future
self.file_name_future = location + self.username + "_" + str(self.present_file_id + 1) + ".mp3"
@@ -59,7 +60,12 @@ class WaveWriter:
self.transcript_file_future.setnchannels(CHANNELS)
self.transcript_file_future.setsampwidth(SAMPLE_WIDTH)
self.transcript_file_future.setframerate(SAMPLING_RATE)
# send present to transcription
operation = {
"type": "user_cleanup",
"user" : self.user,
"filename": self.name_file_past
}
self.queue.put(operation)
def writeframes(self, pcmdata):
logger.info(self.transcript_file_present)
@@ -69,7 +75,6 @@ class WaveWriter:
logger.info("Cleanup for user %s", self.username)
self.transcript_file_present.close()
self.transcript_file_future.close()
# send present to transcription
class SRBuffer(voice_recv.AudioSink):
@@ -78,9 +83,9 @@ class SRBuffer(voice_recv.AudioSink):
"""
# on member join dodajemytypa do listy
# on member disconnect - dropujemy go
def __init__(self):
def __init__(self, queue):
super().__init__()
self.queue = queue
self.wavewriter = {}
def on_user_connect(self, username):
@@ -90,9 +95,6 @@ class SRBuffer(voice_recv.AudioSink):
self.wavewriter[str(username.id)].cleanup()
self.wavewriter.pop(str(username.id))
def rotate_user(self,username):
self.wavewriter[str(username.id)].rotate()
def wants_opus(self) -> bool:
return False
@@ -101,8 +103,8 @@ class SRBuffer(voice_recv.AudioSink):
if user:
self.wavewriter[str(user.id)][0].writeframes(data.pcm)
self.wavewriter[str(user.id)][1] = time.time_ns()
self.wavewriter[str(user.id)][2] = True #still transmiting
self.wavewriter[str(user.id)][3] = True #there is data to write
self.wavewriter[str(user.id)][2] = True
self.wavewriter[str(user.id)][3] = True
def cleanup(self) -> None:
try:
@@ -113,12 +115,17 @@ class SRBuffer(voice_recv.AudioSink):
logger.warning("WaveSink got error closing file on cleanup", exc_info=True)
class TranscriptionOutput(commands.Cog):
def __init__(self, bot):
pass
class Transcriber(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.wsink = SRBuffer()
self.threads = []
self.comm_queue = Queue()
self.wsink = SRBuffer(self.comm_queue)
async def transcribe(file_url):
config = aai.TranscriptionConfig(speaker_labels=True, language_code="pl")
@@ -126,7 +133,7 @@ class Transcriber(commands.Cog):
transcriber = aai.Transcriber()
transcript = transcriber.transcribe(file_url, config=config)
for utterance in transcript.utterances:
print(f"Speaker {utterance.speaker}: {utterance.text}")
logger.info("Speaker %s : %s", utterance.speaker, utterance.text)
@commands.hybrid_command(name="transcribe")
async def test(self, ctx):
@@ -151,14 +158,20 @@ class Transcriber(commands.Cog):
logger.info("Item %s", item)
if not item[2]:
timediff = time.time_ns() - item[1]
if timediff > 50000000 and item[3]: #timeout occured data to transmit exists
if timediff > 3000149433 and item[3]: #timeout occured data to transmit exists
logger.info("File rotation at %s", timediff)
item[3] = False #buffer rotated
item[0].rotate()
#temporary - will be put in separate thread
self.transcribe(item[0].file_name_past)
#ensure only files with minimum length are sent to transcription
item[2] = False
logger.info("Diff : %s", time.time_ns() - item[1])
@tasks.loop(seconds=1)
async def scan_queue(self):
self.comm_queue.get()
@commands.Cog.listener()
async def on_voice_state_update(self, user, before, after):
if user == self.bot.user:
@@ -172,7 +185,8 @@ class Transcriber(commands.Cog):
logger.info("User %s disconnected from channel %s", user, before.channel.name)
operation = {
"type": "user_cleanup",
user : user.id
"user" : user,
"filename": None
}
self.comm_queue.put(operation)
else: