17
17
from typing import Any , Callable , Optional
18
18
19
19
import numpy as np
20
+ import sounddevice as sd
20
21
from numpy ._typing import NDArray
21
22
from pydub import AudioSegment
22
23
from scipy .signal import resample
@@ -85,10 +86,6 @@ def __init__(self, config: SoundDeviceConfig):
85
86
self .device_name = ""
86
87
87
88
self .config = config
88
- try :
89
- import sounddevice as sd
90
- except ImportError :
91
- raise SoundDeviceError ("SoundDeviceAPI requires sound_device module!" )
92
89
sd .default .latency = ("low" , "low" ) # type: ignore
93
90
if config .device_name :
94
91
self .device_name = config .device_name
@@ -143,10 +140,7 @@ def write(self, data: AudioSegment, blocking: bool = False, loop: bool = False):
143
140
"""
144
141
if not self .write_flag :
145
142
raise SoundDeviceError (f"{ self .device_name } does not support writing!" )
146
- try :
147
- import sounddevice as sd
148
- except ImportError :
149
- raise SoundDeviceError ("SoundDeviceAPI requires sound_device module!" )
143
+
150
144
audio = np .array (data .get_array_of_samples ())
151
145
sd .play (
152
146
audio ,
@@ -187,10 +181,7 @@ def read(self, time: float, blocking: bool = False) -> AudioSegment:
187
181
188
182
if not self .read_flag :
189
183
raise SoundDeviceError (f"{ self .device_name } does not support reading!" )
190
- try :
191
- import sounddevice as sd
192
- except ImportError :
193
- raise SoundDeviceError ("SoundDeviceAPI requires sound_device module!" )
184
+
194
185
frames = int (time * self .sample_rate )
195
186
recording = sd .rec (
196
187
frames = frames ,
@@ -217,10 +208,6 @@ def stop(self):
217
208
- This is a convenience function to stop the sound device from playing or recording.
218
209
- It will stop any sound that is currently playing and any recording currently happening.
219
210
"""
220
- try :
221
- import sounddevice as sd
222
- except ImportError :
223
- raise SoundDeviceError ("SoundDeviceAPI requires sound_device module!" )
224
211
sd .stop ()
225
212
226
213
def wait (self ):
@@ -232,10 +219,6 @@ def wait(self):
232
219
- This is a convenience function to wait for the sound device to finish playing or recording.
233
220
- It will block until the sound is played or recorded.
234
221
"""
235
- try :
236
- import sounddevice as sd
237
- except ImportError :
238
- raise SoundDeviceError ("SoundDeviceAPI requires sound_device module!" )
239
222
sd .wait ()
240
223
241
224
def open_write_stream (
@@ -250,10 +233,6 @@ def open_write_stream(
250
233
f"{ self .device_name } does not support streaming writing!"
251
234
)
252
235
253
- try :
254
- import sounddevice as sd
255
- except ImportError :
256
- raise SoundDeviceError ("SoundDeviceAPI requires sound_device module!" )
257
236
from sounddevice import CallbackFlags
258
237
259
238
def callback (indata : NDArray , frames : int , time : Any , status : CallbackFlags ):
@@ -323,10 +302,6 @@ def callback(indata: NDArray, frames: int, _, status: CallbackFlags):
323
302
}
324
303
on_feedback (indata , flag_dict )
325
304
326
- try :
327
- import sounddevice as sd
328
- except ImportError :
329
- raise SoundDeviceError ("SoundDeviceAPI requires sound_device module!" )
330
305
try :
331
306
if self .config .consumer_sampling_rate is None :
332
307
window_size_samples = self .config .block_size * self .sample_rate
0 commit comments