baseasr.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 torch.multiprocessing as mp
  22. from basereal import BaseReal
  23. class BaseASR:
  24. def __init__(self, opt, parent:BaseReal = None):
  25. self.opt = opt
  26. self.parent = parent
  27. self.fps = opt.fps # 20 ms per frame
  28. self.sample_rate = 16000
  29. self.chunk = self.sample_rate // self.fps # 320 samples per chunk (20ms * 16000 / 1000)
  30. self.queue = Queue()
  31. self.output_queue = mp.Queue()
  32. self.batch_size = opt.batch_size
  33. self.frames = []
  34. self.stride_left_size = opt.l
  35. self.stride_right_size = opt.r
  36. #self.context_size = 10
  37. self.feat_queue = mp.Queue(2)
  38. #self.warm_up()
  39. def flush_talk(self):
  40. self.queue.queue.clear()
  41. def put_audio_frame(self,audio_chunk,datainfo:dict): #16khz 20ms pcm
  42. self.queue.put((audio_chunk,datainfo))
  43. #return frame:audio pcm; type: 0-normal speak, 1-silence; eventpoint:custom event sync with audio
  44. def get_audio_frame(self):
  45. try:
  46. frame,eventpoint = self.queue.get(block=True,timeout=0.01)
  47. type = 0
  48. #print(f'[INFO] get frame {frame.shape}')
  49. except queue.Empty:
  50. if self.parent and self.parent.curr_state>1: #播放自定义音频
  51. frame = self.parent.get_audio_stream(self.parent.curr_state)
  52. type = self.parent.curr_state
  53. else:
  54. frame = np.zeros(self.chunk, dtype=np.float32)
  55. type = 1
  56. eventpoint = None
  57. return frame,type,eventpoint
  58. #return frame:audio pcm; type: 0-normal speak, 1-silence; eventpoint:custom event sync with audio
  59. def get_audio_out(self):
  60. return self.output_queue.get()
  61. def warm_up(self):
  62. for _ in range(self.stride_left_size + self.stride_right_size):
  63. audio_frame,type,eventpoint=self.get_audio_frame()
  64. self.frames.append(audio_frame)
  65. self.output_queue.put((audio_frame,type,eventpoint))
  66. for _ in range(self.stride_left_size):
  67. self.output_queue.get()
  68. def run_step(self):
  69. pass
  70. def get_next_feat(self,block,timeout):
  71. return self.feat_queue.get(block,timeout)