11 FORMAT = pyaudio.paInt16
16 self.
stream = self.audio.open(format=FORMAT, channels=CHANNELS, rate=RATE,
17 input=
True, frames_per_buffer=CHUNK,
19 self.
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22 self.
_server_name = rospy.get_param(
'/dialogflow_client/server_name',
24 self.
_port = rospy.get_param(
'/dialogflow_client/port', 4444)
26 rospy.loginfo(
"DF_CLIENT: Audio Server Started!")
29 """Create a socket, listen on the server:port and wait for a connection. 32 rospy.loginfo(
"DF_CLIENT: Waiting for connection...")
33 self.serversocket.listen(1)
35 def _callback(self, in_data, frame_count, time_info, status):
36 """PyAudio callback to continuously get audio data from the mic and put 38 :param in_data: Audio data received from mic. 39 :return: A tuple with a signal to keep listening to audio input device 40 :rtype: tuple(None, int) 44 return None, pyaudio.paContinue
47 """Main function that attempts to create a socket and establish a 48 connection with a client.""" 54 readable, writable, errored = select.select(self.
read_list, [], [])
57 (clientsocket, address) = self.serversocket.accept()
58 self.read_list.append(clientsocket)
59 rospy.loginfo(
"DF_CLIENT: Connection from {}".format(
64 self.read_list.remove(s)
65 except KeyboardInterrupt
as k:
66 rospy.logwarn(
"DF_CLIENT: Caught Keyboard Interrupt: {}".format(k))
67 except socket.error
as e:
68 rospy.logwarn(
"DF_CLIENT: Caught Socket Error: {}\n " 69 "Restarting...".format(e))
72 rospy.loginfo(
"DF_CLIENT: Finished recording")
75 self.serversocket.close()
77 self.stream.stop_stream()
79 self.audio.terminate()
82 if __name__ ==
'__main__':
83 rospy.init_node(
'dialogflow_audio_server')
def _callback(self, in_data, frame_count, time_info, status)