61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
from UniTAP.dev.modules.capturer.result_object import ResultObject
|
|
|
|
from UniTAP.common.audio_mode import AudioMode, AudioFileFormat
|
|
|
|
from UniTAP.dev.ports.modules.ag.ag_utils import save_to_bin_file, save_to_wave_file
|
|
import warnings
|
|
|
|
|
|
class ResultAudioObject(ResultObject):
|
|
"""
|
|
Class `ResultAudioObject` inherited from class `ResultObject`.
|
|
Class `ResultAudioObject` allows saving captured frames to image `save_image_to_file`.
|
|
Also has all the `ResultObject` functionality.
|
|
"""
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.__audio_mode = AudioMode()
|
|
|
|
@property
|
|
def audio_mode(self) -> AudioMode:
|
|
"""
|
|
Returns current audio mode for captured audio frames.
|
|
|
|
Returns:
|
|
object of `AudioMode` type
|
|
"""
|
|
return self.__audio_mode
|
|
|
|
def save_to_file(self, file_format: AudioFileFormat, path: str):
|
|
"""
|
|
Saving audio frames to file. Supported file formats describe in `AudioFileFormat`.
|
|
|
|
Args:
|
|
file_format (`AudioFileFormat`) - file format
|
|
path (str) - path to save
|
|
"""
|
|
if len(self.buffer) > 0:
|
|
if file_format == AudioFileFormat.BIN:
|
|
save_to_bin_file(path=path, data=self.__convert_buffer())
|
|
elif file_format == AudioFileFormat.WAV:
|
|
save_to_wave_file(path=path, audio_mode=self.audio_mode, data=self.__convert_buffer())
|
|
else:
|
|
raise ValueError(f"Incorrect audio format. Available formats: "
|
|
f"{AudioFileFormat.BIN.name}, {AudioFileFormat.WAV.name}.\n"
|
|
f"Transferred audio format: {file_format.name}")
|
|
else:
|
|
warnings.warn("Buffer size is equal 0.")
|
|
|
|
def __convert_buffer(self):
|
|
data = []
|
|
for audio_frame in self.buffer:
|
|
data.append(audio_frame.data)
|
|
return bytearray().join(data)
|
|
|
|
def __str__(self):
|
|
return f"Start capture time: {self.start_capture_time}\n" \
|
|
f"End capture time: {self.end_capture_time}\n" \
|
|
f"Timestamp: {self.timestamp.__str__()}\n" \
|
|
f"Audio mode:\n{self.audio_mode.__str__()}\n"
|
|
|