Skip to content

Commit 5d0b67f

Browse files
authored
Merge pull request #560 from sawada10/change-audio-frames-per-buffer
2 parents a3c5e99 + 77c0e12 commit 5d0b67f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

respeaker_ros/scripts/respeaker_node.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(self):
1616
self.speech_continuation = rospy.get_param("~speech_continuation", 0.5)
1717
self.speech_max_duration = rospy.get_param("~speech_max_duration", 7.0)
1818
self.speech_min_duration = rospy.get_param("~speech_min_duration", 0.1)
19+
self.sample_duration = rospy.get_param("~sample_duration", None) # sec
1920
suppress_pyaudio_error = rospy.get_param("~suppress_pyaudio_error", True)
2021
#
2122
self.respeaker = RespeakerInterface()
@@ -34,7 +35,8 @@ def __init__(self):
3435
self.config = None
3536
self.dyn_srv = Server(RespeakerConfig, self.on_config)
3637
# start
37-
self.respeaker_audio = RespeakerAudio(self.on_audio, suppress_error=suppress_pyaudio_error)
38+
self.respeaker_audio = RespeakerAudio(self.on_audio, suppress_error=suppress_pyaudio_error,
39+
sample_duration=self.sample_duration)
3840
self.speech_prefetch_bytes = int(
3941
self.speech_prefetch * self.respeaker_audio.rate * self.respeaker_audio.bitdepth / 8.0)
4042
self.speech_prefetch_buffer = b""

respeaker_ros/src/respeaker_ros/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def close(self):
232232

233233

234234
class RespeakerAudio(object):
235-
def __init__(self, on_audio, channel=0, suppress_error=True):
235+
def __init__(self, on_audio, channel=0, suppress_error=True, sample_duration=None):
236236
self.on_audio = on_audio
237237
with ignore_stderr(enable=suppress_error):
238238
self.pyaudio = pyaudio.PyAudio()
@@ -242,6 +242,10 @@ def __init__(self, on_audio, channel=0, suppress_error=True):
242242
self.rate = 16000
243243
self.bitwidth = 2
244244
self.bitdepth = 16
245+
if sample_duration is not None:
246+
frames_per_buffer = int(sample_duration * self.rate)
247+
else:
248+
frames_per_buffer = 1024
245249

246250
# find device
247251
count = self.pyaudio.get_device_count()
@@ -272,7 +276,7 @@ def __init__(self, on_audio, channel=0, suppress_error=True):
272276
format=pyaudio.paInt16,
273277
channels=self.channels,
274278
rate=self.rate,
275-
frames_per_buffer=1024,
279+
frames_per_buffer=frames_per_buffer,
276280
stream_callback=self.stream_callback,
277281
input_device_index=self.device_index,
278282
)

0 commit comments

Comments
 (0)