17 CALLBACK = CFUNCTYPE(
None, POINTER(c_int16))
22 A cross platform Python SDK for PvRecorder to process audio recordings. It lists the available
23 input devices. Also given the audio device index and frame_length, processes the frame and runs
24 a callback each time a frame_length is given.
33 DEVICE_ALREADY_INITIALIZED = 5
34 DEVICE_NOT_INITIALIZED = 6
38 _PVRECORDER_STATUS_TO_EXCEPTION = {
39 PvRecorderStatuses.OUT_OF_MEMORY: MemoryError,
40 PvRecorderStatuses.INVALID_ARGUMENT: ValueError,
41 PvRecorderStatuses.INVALID_STATE: ValueError,
42 PvRecorderStatuses.BACKEND_ERROR: SystemError,
43 PvRecorderStatuses.DEVICE_ALREADY_INITIALIZED: ValueError,
44 PvRecorderStatuses.DEVICE_NOT_INITIALIZED: ValueError,
45 PvRecorderStatuses.IO_ERROR: IOError,
46 PvRecorderStatuses.RUNTIME_ERROR: RuntimeError
52 def __init__(self, device_index, frame_length, buffer_size_msec=1000, log_overflow=True):
56 :param device_index: The device index of the audio device to use. A (-1) will choose default audio device.
57 :param frame_length: The length of the frame to receive at each read call.
58 :param buffer_size_msec: Time in milliseconds indicating the total amount of time to store audio frames.
59 :param log_overflow: Boolean variable to indicate to log overflow warnings. A log warning should indicate
60 read is not being called fast enough from the callers point.
63 init_func = self._LIBRARY.pv_recorder_init
64 init_func.argtypes = [
69 POINTER(POINTER(self.CPvRecorder))
71 init_func.restype = self.PvRecorderStatuses
76 status = init_func(device_index, frame_length, buffer_size_msec, log_overflow, byref(self.
_handle))
105 """Releases any resources used by PV_Recorder."""
110 """Starts recording audio."""
117 """Stops recording audio."""
124 """Reads audio frames and returns a list containing the audio frames."""
134 """Gets the current selected device."""
137 return device_name.decode(
'utf-8')
141 """Gets the current version of pv_recorder library."""
144 return version.decode(
'utf-8')
148 """Gets the audio devices currently available on device.
150 :return: A list of strings, indicating the names of audio devices.
153 get_audio_devices_func = PvRecorder._LIBRARY.pv_recorder_get_audio_devices
154 get_audio_devices_func.argstype = [POINTER(c_int32), POINTER(POINTER(c_char_p))]
157 free_device_list_func = PvRecorder._LIBRARY.pv_recorder_free_device_list
158 free_device_list_func.argstype = [c_int32, POINTER(c_char_p)]
159 free_device_list_func.restype =
None
162 devices = POINTER(c_char_p)()
164 status = get_audio_devices_func(byref(count), byref(devices))
165 if status
is not PvRecorder.PvRecorderStatuses.SUCCESS:
166 raise PvRecorder._PVRECORDER_STATUS_TO_EXCEPTION[status](
"Failed to get device list")
169 for i
in range(count.value):
170 device_list.append(devices[i].decode(
'utf-8'))
172 free_device_list_func(count, devices)
178 """A helper function to get the library path."""
180 if platform.system() ==
"Windows":
181 script_path = os.path.join(os.path.dirname(__file__),
"scripts",
"platform.bat")
183 script_path = os.path.join(os.path.dirname(__file__),
"scripts",
"platform.sh")
185 command = subprocess.run(script_path, stdout=subprocess.PIPE)
187 if command.returncode != 0:
188 raise RuntimeError(
"Current system is not supported.")
189 os_name, cpu = str(command.stdout.decode(
"utf-8")).split(
" ")
191 if os_name ==
"windows":
193 elif os_name ==
"mac":
198 return os.path.join(os.path.dirname(__file__),
"lib", os_name, cpu, f
"libpv_recorder.{extension}")
200 _LIBRARY = cdll.LoadLibrary(_lib_path.__func__())