1111from .errors import DeepgramMicrophoneError
1212from .constants import LOGGING , CHANNELS , RATE , CHUNK
1313
14-
1514class Microphone :
1615 """
1716 This implements a microphone for local audio input. This uses PyAudio under the hood.
@@ -25,6 +24,7 @@ def __init__(
2524 chunk = CHUNK ,
2625 channels = CHANNELS ,
2726 ):
27+ # dynamic import of pyaudio as not to force the requirements on the SDK (and users)
2828 import pyaudio
2929
3030 self .logger = logging .getLogger (__name__ )
@@ -40,6 +40,9 @@ def __init__(
4040 self .stream = None
4141
4242 def is_active (self ):
43+ """
44+ returns True if the stream is active, False otherwise
45+ """
4346 self .logger .debug ("Microphone.is_active ENTER" )
4447 if self .stream is None :
4548 self .logger .error ("stream is None" )
@@ -52,6 +55,9 @@ def is_active(self):
5255 return
5356
5457 def start (self ):
58+ """
59+ starts the microphone stream
60+ """
5561 self .logger .debug ("Microphone.start ENTER" )
5662
5763 if self .stream is not None :
@@ -76,14 +82,17 @@ def start(self):
7682 self .lock = threading .Lock ()
7783
7884 self .stream .start_stream ()
79- self .thread = threading .Thread (target = self .processing )
85+ self .thread = threading .Thread (target = self ._processing )
8086 self .thread .start ()
8187
8288 self .logger .notice ("start succeeded" )
8389 self .logger .debug ("Microphone.start LEAVE" )
8490
85- def processing (self ):
86- self .logger .debug ("Microphone.processing ENTER" )
91+ def _processing (self ):
92+ """
93+ the main processing loop for the microphone
94+ """
95+ self .logger .debug ("Microphone._processing ENTER" )
8796
8897 try :
8998 while True :
@@ -106,15 +115,18 @@ def processing(self):
106115 self .logger .verbose ("regular threaded callback" )
107116 self .push_callback (data )
108117
109- self .logger .notice ("processing exiting..." )
110- self .logger .debug ("Microphone.processing LEAVE" )
118+ self .logger .notice ("_processing exiting..." )
119+ self .logger .debug ("Microphone._processing LEAVE" )
111120
112121 except Exception as e :
113122 self .logger .error ("Error while sending: %s" , str (e ))
114- self .logger .debug ("Microphone.processing LEAVE" )
123+ self .logger .debug ("Microphone._processing LEAVE" )
115124 raise
116125
117126 def finish (self ):
127+ """
128+ Stops the microphone stream
129+ """
118130 self .logger .debug ("Microphone.finish ENTER" )
119131
120132 self .lock .acquire ()
@@ -125,7 +137,7 @@ def finish(self):
125137 if self .thread is not None :
126138 self .thread .join ()
127139 self .thread = None
128- self .logger .notice ("processing /send thread joined" )
140+ self .logger .notice ("_processing /send thread joined" )
129141
130142 if self .stream is not None :
131143 self .stream .stop_stream ()
0 commit comments