Last active
March 21, 2022 18:43
-
-
Save naoki-mizuno/d244f33f7ae3cf37f1d6a5e54ac68e69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from __future__ import division | |
import sys | |
from numpy import * | |
import scikits.audiolab as al | |
import pyaudio | |
import rosbag | |
in_bag = rosbag.Bag(sys.argv[1]) | |
if __name__ == '__main__': | |
n_step = 160 | |
n_frame = 512 | |
gain = 20 | |
if len(sys.argv) < 3: | |
out_fn = sys.argv[1].replace('.bag', '.wav') | |
else: | |
out_fn = sys.argv[2] | |
print('Output file: {0}'.format(out_fn)) | |
wav = None | |
t_s = in_bag.get_start_time() | |
t_e = in_bag.get_end_time() | |
for topic, msg, t in in_bag.read_messages(topics='/audio'): | |
percentage = (t.to_sec() - t_s) / (t_e - t_s) | |
sys.stdout.write('\r{0:4.2f}%'.format(100 * percentage)) | |
if wav is None: | |
# Use Python lists instead of numpy arrays (for faster appending) | |
wav = [list() for x in range(msg.nch)] | |
new_data = array(msg.data).reshape([msg.len, msg.nch])[:n_step] | |
for i in range(msg.nch): | |
# Flatten list and append | |
# https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python | |
for elem in transpose(new_data)[i]: | |
wav[i].append(elem) | |
sys.stdout.flush() | |
# Columns: channels | |
wav = transpose(array(wav)) | |
al.wavwrite(wav, out_fn, fs=16000, enc="pcm24") | |
print('\nDone!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
convert_to_wav=false | |
launch_file="$HOME/ros/src/raspzx/raspzx/launch/raspzx_dog_mic.launch" | |
_num_channels() { | |
arecord -l | \ | |
egrep -o '[[:digit:]]+[[:space:]]*ch' | \ | |
egrep -o '[[:digit:]]+' | \ | |
uniq | |
} | |
while getopts 'r:h' OPT; do | |
case $OPT in | |
r) | |
filename="$OPTARG" | |
convert_to_wav=true | |
;; | |
h) | |
echo "$0 [-r wav file name] [channels]" | |
exit 0 | |
;; | |
esac | |
done | |
shift $(( OPTIND - 1 )) | |
# Command line arguments | |
channels="${1:-8}" | |
actual_channels="$( _num_channels )" | |
if [[ $actual_channels != $channels ]]; then | |
echo -e "\e[31mInconsistent number of channels: $channels specified but found $actual_channels\e[0m" >&2 | |
echo -e "\e[31mPlease run arecord -l and check the output.\e[0m" >&2 | |
exit 1 | |
fi | |
if $convert_to_wav && ! which wav_saver >/dev/null; then | |
echo -e "\e[31mwav_saver not found\e[0m" >&2 | |
exit 1 | |
fi | |
source /opt/ros/indigo/setup.bash | |
source ~/ros/devel/setup.bash | |
export ROS_HOSTNAME=raspi2_ros | |
export ROS_MASTER_URI=http://raspi2_ros:11311 | |
# Edit the launch file | |
sed -i -e "s/RASPZX[[:digit:]]\+ch/RASPZX${channels}ch/" ${launch_file} | |
sed -i -e "s/n_mic\" value=\"[[:digit:]]/n_mic\" value=\"${channels}/" ${launch_file} | |
sed -i -e "s/list_mic\">\[[0-9,]*\]/list_mic\">[$( seq -s, $(( channels - 1 )) -1 0 | sed -e 's/,$//' )]/" ${launch_file} | |
roslaunch ${launch_file} & | |
disown | |
echo -e "\e[32mWaiting for processes to start up.\e[0m" | |
sleep 10 | |
echo -e "\e[32mWill start recording. Press Ctrl-c to exit.\e[0m" | |
if $convert_to_wav; then | |
exec wav_saver $filename | |
else | |
exec rosbag record -a | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment