Skip to content

Commit

Permalink
Merge pull request #284 from 708yamaguchi/dgorissen
Browse files Browse the repository at this point in the history
respeaker_ros for Python 2.x and 3.x
  • Loading branch information
k-okada committed Sep 15, 2021
2 parents 539e1c2 + b7514a2 commit 7fc1c0b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
4 changes: 3 additions & 1 deletion respeaker_ros/launch/sample_respeaker.launch
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<arg name="speech_to_text" default="speech_to_text"/>
<!-- langage used in speech_to_text.py -->
<arg name="language" default="en-US"/>
<!-- self cancellation -->
<arg name="self_cancellation" default="true"/>

<node if="$(arg publish_tf)"
name="static_transformer" pkg="tf" type="static_transform_publisher"
Expand All @@ -29,7 +31,7 @@
<remap from="speech_to_text" to="$(arg speech_to_text)"/>
<rosparam subst_value="true">
language: $(arg language)
self_cancellation: true
self_cancellation: $(arg self_cancellation)
tts_tolerance: 0.5
</rosparam>
</node>
Expand Down
21 changes: 12 additions & 9 deletions respeaker_ros/scripts/respeaker_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ def read(self, name):
rospy.logerr(e)
rospy.signal_shutdown('Shutdown this node because of USBError')

response = struct.unpack(b'ii', response.tostring())
if sys.version_info.major == 2:
response = struct.unpack(b'ii', response.tostring())
else:
response = struct.unpack(b'ii', response.tobytes())

if data[2] == 'int':
result = response[0]
Expand Down Expand Up @@ -237,7 +240,7 @@ def __init__(self, on_audio, channel=0, suppress_error=True):
name = info["name"].encode("utf-8")
chan = info["maxInputChannels"]
rospy.logdebug(" - %d: %s" % (i, name))
if name.lower().find("respeaker") >= 0:
if name.lower().find(b"respeaker") >= 0:
self.channels = chan
self.device_index = i
rospy.loginfo("Found %d: %s (channels: %d)" % (i, name, chan))
Expand Down Expand Up @@ -278,12 +281,12 @@ def __del__(self):

def stream_callback(self, in_data, frame_count, time_info, status):
# split channel
data = np.fromstring(in_data, dtype=np.int16)
chunk_per_channel = len(data) / self.channels
data = np.frombuffer(in_data, dtype=np.int16)
chunk_per_channel = int(len(data) / self.channels)
data = np.reshape(data, (chunk_per_channel, self.channels))
chan_data = data[:, self.channel]
# invoke callback
self.on_audio(chan_data.tostring())
self.on_audio(chan_data.tobytes())
return None, pyaudio.paContinue

def start(self):
Expand All @@ -309,7 +312,7 @@ def __init__(self):
suppress_pyaudio_error = rospy.get_param("~suppress_pyaudio_error", True)
#
self.respeaker = RespeakerInterface()
self.speech_audio_buffer = str()
self.speech_audio_buffer = b""
self.is_speeching = False
self.speech_stopped = rospy.Time(0)
self.prev_is_voice = None
Expand All @@ -327,7 +330,7 @@ def __init__(self):
self.respeaker_audio = RespeakerAudio(self.on_audio, suppress_error=suppress_pyaudio_error)
self.speech_prefetch_bytes = int(
self.speech_prefetch * self.respeaker_audio.rate * self.respeaker_audio.bitdepth / 8.0)
self.speech_prefetch_buffer = str()
self.speech_prefetch_buffer = b""
self.respeaker_audio.start()
self.info_timer = rospy.Timer(rospy.Duration(1.0 / self.update_rate),
self.on_timer)
Expand Down Expand Up @@ -386,7 +389,7 @@ def on_timer(self, event):
doa_rad = math.radians(self.respeaker.direction - 180.0)
doa_rad = angles.shortest_angular_distance(
doa_rad, math.radians(self.doa_yaw_offset))
doa = math.degrees(doa_rad)
doa = int(math.degrees(doa_rad))

# vad
if is_voice != self.prev_is_voice:
Expand Down Expand Up @@ -417,7 +420,7 @@ def on_timer(self, event):
self.is_speeching = True
elif self.is_speeching:
buf = self.speech_audio_buffer
self.speech_audio_buffer = str()
self.speech_audio_buffer = b""
self.is_speeching = False
duration = 8.0 * len(buf) * self.respeaker_audio.bitwidth
duration = duration / self.respeaker_audio.rate / self.respeaker_audio.bitdepth
Expand Down

0 comments on commit 7fc1c0b

Please sign in to comment.