museasr.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ###############################################################################
  2. # Copyright (C) 2024 LiveTalking@lipku https://github.com/lipku/LiveTalking
  3. # email: lipku@foxmail.com
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. ###############################################################################
  17. import time
  18. import numpy as np
  19. import queue
  20. from queue import Queue
  21. #import multiprocessing as mp
  22. from baseasr import BaseASR
  23. from musetalk.whisper.audio2feature import Audio2Feature
  24. class MuseASR(BaseASR):
  25. def __init__(self, opt, parent,audio_processor:Audio2Feature):
  26. super().__init__(opt,parent)
  27. self.audio_processor = audio_processor
  28. def run_step(self):
  29. ############################################## extract audio feature ##############################################
  30. start_time = time.time()
  31. for _ in range(self.batch_size*2):
  32. audio_frame,type,eventpoint = self.get_audio_frame()
  33. self.frames.append(audio_frame)
  34. self.output_queue.put((audio_frame,type,eventpoint))
  35. if len(self.frames) <= self.stride_left_size + self.stride_right_size:
  36. return
  37. inputs = np.concatenate(self.frames) # [N * chunk]
  38. whisper_feature = self.audio_processor.audio2feat(inputs)
  39. # for feature in whisper_feature:
  40. # self.audio_feats.append(feature)
  41. #print(f"processing audio costs {(time.time() - start_time) * 1000}ms, inputs shape:{inputs.shape} whisper_feature len:{len(whisper_feature)}")
  42. 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 )
  43. #print(f"whisper_chunks len:{len(whisper_chunks)},self.audio_feats len:{len(self.audio_feats)},self.output_queue len:{self.output_queue.qsize()}")
  44. #self.audio_feats = self.audio_feats[-(self.stride_left_size + self.stride_right_size):]
  45. self.feat_queue.put(whisper_chunks)
  46. # discard the old part to save memory
  47. self.frames = self.frames[-(self.stride_left_size + self.stride_right_size):]