61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
#encoding = utf8
|
|
|
|
import logging
|
|
|
|
|
|
from enum import Enum
|
|
|
|
|
|
class HumanStatusEnum(Enum):
|
|
silence = 1
|
|
talking = 2
|
|
|
|
|
|
class HumanStatus:
|
|
def __init__(self, total_frames=0, silence_length=0):
|
|
self._status = HumanStatusEnum.silence
|
|
self._total_frames = total_frames
|
|
self._silence_length = silence_length
|
|
self._talking_length = total_frames - silence_length
|
|
self._current_frame = 0
|
|
self._is_talking = False
|
|
|
|
def get_status(self):
|
|
return self._status
|
|
|
|
def set_status(self, status):
|
|
self._status = status
|
|
return self._status
|
|
|
|
def try_to_talk(self):
|
|
if self._status == HumanStatusEnum.silence:
|
|
if self._current_frame - self._silence_length < 0:
|
|
return False
|
|
self._status = HumanStatusEnum.talking
|
|
return True
|
|
|
|
def get_index(self):
|
|
if not self._is_talking:
|
|
index = self._current_frame % self._silence_length
|
|
|
|
if self._current_frame >= self._silence_length:
|
|
self._is_talking = True
|
|
self._current_frame = 0
|
|
|
|
else:
|
|
index = self._silence_length + (self._current_frame - self._silence_length) % self._talking_length
|
|
|
|
if self._current_frame >= self._silence_length + self._talking_length:
|
|
self._is_talking = False
|
|
self._current_frame = 0
|
|
|
|
self._current_frame = (self._current_frame + 1) % self._total_frames
|
|
return index
|
|
|
|
def start_talking(self):
|
|
self._is_talking = True
|
|
|
|
def stop_talking(self):
|
|
self._is_talking = False
|
|
self._current_frame = 0
|