Skip to content

Commit f8f4e99

Browse files
committed
Fix some Pyright errors in sounddevice.py
1 parent e5eff02 commit f8f4e99

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

sounddevice.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
break
7070
else:
7171
raise OSError('PortAudio library not found')
72-
_lib = _ffi.dlopen(_libname)
72+
_lib: ... = _ffi.dlopen(_libname)
7373
except OSError:
7474
if _platform.system() == 'Darwin':
7575
_libname = 'libportaudio.dylib'
@@ -83,7 +83,7 @@
8383
import _sounddevice_data
8484
_libname = _os.path.join(
8585
next(iter(_sounddevice_data.__path__)), 'portaudio-binaries', _libname)
86-
_lib = _ffi.dlopen(_libname)
86+
_lib: ... = _ffi.dlopen(_libname)
8787

8888
_sampleformats = {
8989
'float32': _lib.paFloat32,
@@ -571,7 +571,7 @@ def query_devices(device=None, kind=None):
571571
if not info:
572572
raise PortAudioError(f'Error querying device {device}')
573573
assert info.structVersion == 2
574-
name_bytes = _ffi.string(info.name)
574+
name_bytes = _ffi_string(info.name)
575575
try:
576576
# We don't know beforehand if DirectSound and MME device names use
577577
# 'utf-8' or 'mbcs' encoding. Let's try 'utf-8' first, because it more
@@ -651,7 +651,7 @@ def query_hostapis(index=None):
651651
raise PortAudioError(f'Error querying host API {index}')
652652
assert info.structVersion == 1
653653
return {
654-
'name': _ffi.string(info.name).decode(),
654+
'name': _ffi_string(info.name).decode(),
655655
'devices': [_lib.Pa_HostApiDeviceIndexToDeviceIndex(index, i)
656656
for i in range(info.deviceCount)],
657657
'default_input_device': info.defaultInputDevice,
@@ -721,7 +721,7 @@ def get_portaudio_version():
721721
(1899, 'PortAudio V19-devel (built Feb 15 2014 23:28:00)')
722722
723723
"""
724-
return _lib.Pa_GetVersion(), _ffi.string(_lib.Pa_GetVersionText()).decode()
724+
return _lib.Pa_GetVersion(), _ffi_string(_lib.Pa_GetVersionText()).decode()
725725

726726

727727
class _StreamBase:
@@ -829,11 +829,11 @@ def __init__(self, kind, samplerate=None, blocksize=None, device=None,
829829
extra_settings, samplerate)
830830
self._device = parameters.device
831831
self._channels = parameters.channelCount
832+
iparameters = _ffi.NULL
833+
oparameters = _ffi.NULL
832834
if kind == 'input':
833835
iparameters = parameters
834-
oparameters = _ffi.NULL
835836
elif kind == 'output':
836-
iparameters = _ffi.NULL
837837
oparameters = parameters
838838

839839
ffi_callback = _ffi.callback('PaStreamCallback', error=_lib.paAbort)
@@ -1204,7 +1204,7 @@ def _raw_read(self, frames):
12041204
"""
12051205
channels, _ = _split(self._channels)
12061206
samplesize, _ = _split(self._samplesize)
1207-
data = _ffi.new('signed char[]', channels * samplesize * frames)
1207+
data = _ffi.new('signed char[]', channels * samplesize * frames) # type: ignore
12081208
err = _lib.Pa_ReadStream(self._ptr, data, frames)
12091209
if err == _lib.paInputOverflowed:
12101210
overflowed = True
@@ -1302,7 +1302,7 @@ def _raw_write(self, data):
13021302
pass # input is not a buffer
13031303
_, samplesize = _split(self._samplesize)
13041304
_, channels = _split(self._channels)
1305-
samples, remainder = divmod(len(data), samplesize)
1305+
samples, remainder = divmod(len(data), samplesize) # type: ignore
13061306
if remainder:
13071307
raise ValueError('len(data) not divisible by samplesize')
13081308
frames, remainder = divmod(samples, channels)
@@ -2239,7 +2239,8 @@ def reset(self):
22392239

22402240
if not hasattr(_ffi, 'I_AM_FAKE'):
22412241
# This object shadows the 'default' class, except when building the docs.
2242-
default = default()
2242+
_default_class = default
2243+
default: _default_class = default()
22432244

22442245

22452246
class PortAudioError(Exception):
@@ -2501,9 +2502,8 @@ class _CallbackContext:
25012502
"""Helper class for reuse in play()/rec()/playrec() callbacks."""
25022503

25032504
blocksize = None
2504-
data = None
2505-
out = None
25062505
frame = 0
2506+
frames: int
25072507
input_channels = output_channels = None
25082508
input_dtype = output_dtype = None
25092509
input_mapping = output_mapping = None
@@ -2550,7 +2550,7 @@ def check_data(self, data, mapping, device):
25502550
if len(mapping) + len(silent_channels) != channels:
25512551
raise ValueError('each channel may only appear once in mapping')
25522552

2553-
self.data = data
2553+
self.data: np.typing.NDArray = data
25542554
self.output_channels = channels
25552555
self.output_dtype = dtype
25562556
self.output_mapping = mapping
@@ -2634,8 +2634,8 @@ def callback_exit(self):
26342634
def finished_callback(self):
26352635
self.event.set()
26362636
# Drop temporary audio buffers to free memory
2637-
self.data = None
2638-
self.out = None
2637+
del self.data
2638+
del self.out
26392639
# Drop CFFI objects to avoid reference cycles
26402640
self.stream._callback = None
26412641
self.stream._finished_callback = None
@@ -2668,6 +2668,10 @@ def wait(self, ignore_errors=True):
26682668
return self.status if self.status else None
26692669

26702670

2671+
def _ffi_string(cdata) -> bytes:
2672+
return _ffi.string(cdata) # type: ignore
2673+
2674+
26712675
def _remove_self(d):
26722676
"""Return a copy of d without the 'self' entry."""
26732677
d = d.copy()
@@ -2742,7 +2746,7 @@ def _get_stream_parameters(kind, device, channels, dtype, latency,
27422746
latency = info['default_' + latency + '_' + kind + '_latency']
27432747
if samplerate is None:
27442748
samplerate = info['default_samplerate']
2745-
parameters = _ffi.new('PaStreamParameters*', (
2749+
parameters: ... = _ffi.new('PaStreamParameters*', (
27462750
device, channels, sampleformat, latency,
27472751
extra_settings._streaminfo if extra_settings else _ffi.NULL))
27482752
return parameters, dtype, samplesize, samplerate
@@ -2798,7 +2802,7 @@ def _check(err, msg=''):
27982802
if err >= 0:
27992803
return err
28002804

2801-
errormsg = _ffi.string(_lib.Pa_GetErrorText(err)).decode()
2805+
errormsg = _ffi_string(_lib.Pa_GetErrorText(err)).decode()
28022806
if msg:
28032807
errormsg = f'{msg}: {errormsg}'
28042808

@@ -2809,7 +2813,7 @@ def _check(err, msg=''):
28092813
# in scenarios where multiple APIs are being used simultaneously.
28102814
info = _lib.Pa_GetLastHostErrorInfo()
28112815
host_api = _lib.Pa_HostApiTypeIdToHostApiIndex(info.hostApiType)
2812-
hosterror_text = _ffi.string(info.errorText).decode()
2816+
hosterror_text = _ffi_string(info.errorText).decode()
28132817
hosterror_info = host_api, info.errorCode, hosterror_text
28142818
raise PortAudioError(errormsg, err, hosterror_info)
28152819

0 commit comments

Comments
 (0)