livetalking/museasr.py

37 lines
1.8 KiB
Python
Raw Normal View History

2024-05-26 11:10:03 +08:00
import time
import numpy as np
import queue
from queue import Queue
2024-06-09 09:04:04 +08:00
import multiprocessing as mp
from baseasr import BaseASR
2024-05-26 11:10:03 +08:00
from musetalk.whisper.audio2feature import Audio2Feature
class MuseASR(BaseASR):
2024-08-03 12:58:49 +08:00
def __init__(self, opt, parent,audio_processor:Audio2Feature):
super().__init__(opt,parent)
2024-05-26 11:10:03 +08:00
self.audio_processor = audio_processor
def run_step(self):
############################################## extract audio feature ##############################################
start_time = time.time()
for _ in range(self.batch_size*2):
audio_frame,type=self.get_audio_frame()
2024-06-22 09:02:01 +08:00
self.frames.append(audio_frame)
2024-05-26 11:10:03 +08:00
self.output_queue.put((audio_frame,type))
2024-06-22 09:02:01 +08:00
if len(self.frames) <= self.stride_left_size + self.stride_right_size:
return
inputs = np.concatenate(self.frames) # [N * chunk]
2024-05-26 11:10:03 +08:00
whisper_feature = self.audio_processor.audio2feat(inputs)
2024-06-22 09:02:01 +08:00
# for feature in whisper_feature:
# self.audio_feats.append(feature)
2024-05-26 11:10:03 +08:00
#print(f"processing audio costs {(time.time() - start_time) * 1000}ms, inputs shape:{inputs.shape} whisper_feature len:{len(whisper_feature)}")
2024-06-22 09:02:01 +08:00
whisper_chunks = self.audio_processor.feature2chunks(feature_array=whisper_feature,fps=self.fps/2,batch_size=self.batch_size,start=self.stride_left_size/2 )
2024-05-26 11:10:03 +08:00
#print(f"whisper_chunks len:{len(whisper_chunks)},self.audio_feats len:{len(self.audio_feats)},self.output_queue len:{self.output_queue.qsize()}")
2024-06-22 09:02:01 +08:00
#self.audio_feats = self.audio_feats[-(self.stride_left_size + self.stride_right_size):]
2024-06-09 09:04:04 +08:00
self.feat_queue.put(whisper_chunks)
2024-06-22 09:02:01 +08:00
# discard the old part to save memory
self.frames = self.frames[-(self.stride_left_size + self.stride_right_size):]