Tutorial: Automatic recording of audio#
🏁 Goal of this tutorial
Write a Python script that subscribes to the
/audio/channel0
topic and writes it to a .wav file.
On your development machine, copy-paste the following
record.py
inside ROS package named e.g. audio_recorder
:
#!/usr/bin/env python3
import wave
import rospy
from audio_common_msgs.msg import AudioData
AUDIO_RATE = 16000
AUDIO_CHANNELS = 1
AUDIO_WIDTH = 2
def channel_callback(msg, wf):
wf.writeframes(msg.data)
if __name__ == '__main__':
# call the relevant service
rospy.init_node('record_audio')
wf = wave.open("test_audio.wav", 'wb') #change output directory as desired
wf.setnchannels(AUDIO_CHANNELS)
wf.setsampwidth(AUDIO_WIDTH)
wf.setframerate(AUDIO_RATE)
#Record processed audio, corresponding to channel 0
rospy.Subscriber('/audio/channel0', AudioData, channel_callback, wf)
print("recording...")
rospy.spin()
print("saving...")
wf.close()
Remember to export the ROS_MASTER_URI
of the robot and the ROS_IP
of
your laptop, and test it:
rosrun audio_recorder record.py
If you wish to run it inside the robot, remember to modify the CMakeList.txt
of your ROS package before deploying the code inside the robot and executing the
command:
install(
PROGRAMS
scripts/record.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
If you want to test it on the robot, it already has it installed, it is enough to execute it with:
ssh pal@<robot>-0c
rosrun respeaker_ros record.py
Speak to the robot, and, to stop recording, press CTRL+C. You can then play the recorded file, note the file is saved from wherever you have executed the recording command.
aplay test_audio.wav
If you have recorded it from the robot, you can then copy it back to your laptop. From your laptop directory:
scp pal@<robot>-0c:/home/pal/test_audio.wav ~/desired_dir_on_my_laptop
See also#
To know more about the robot’s microphone array (and how to use the Linux ALSA interface to record directly from it), see Microphone array and audio recording
Overview of the speech and language pipeline on the robot: Dialogue management
For a general introduction to developing on the robot with ROS, refer to ROS development.