-
Notifications
You must be signed in to change notification settings - Fork 3
/
rosbagAudio.py
executable file
·150 lines (124 loc) · 4.29 KB
/
rosbagAudio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
import os
import sys
import yaml
import numpy as np
import wave
import subprocess
import matplotlib.pyplot as plt
import runFunction
from audioGlobals import audioGlobals
from termcolor import colored
programmName = os.path.basename(sys.argv[0])
#input parameters
def parse_arguments():
inputFile = sys.argv[-1]
#print inputFile
return inputFile
#isolate audio from bag file
def audio_bag_file(bagFile):
#save topic and message
topicKey = 0
topic = 0
bag = bagFile
info_dict = yaml.load(bag._get_yaml_info())
topics = info_dict['topics']
for key in xrange(len(topics)):
if topics[key]['topic'] == '/audio':
topicKey = key
topic = topics[topicKey]
topic_type = topic['type']
frequency = topic['frequency']
#Checking if the topic is audio
if 'audio_common_msgs' in topic_type:
sound = True
#print "Audio found"
else:
sound = False
#print "Such a pity..."
audio = []
if sound == True:
for topic, msg, t in bag.read_messages(topics=['/audio']):
audio += msg.data
#print "total number of compressed bytes {0:d}".format(nBytes)
#print "total number of compressed bytes {0:d}".format(nSamples)
#print "total duration {0:.2f}".format(duration)
#print "average bit rate {0:.2f}".format(float(nBytes) * 8.0 / float(duration))
frequency = frequency * 1000
#bag.close()
return audio, frequency
#save mp3 file
def write_mp3_file(audioData, bagFile):
mp3FileName = bagFile.replace(".bag",".mp3")
mp3_file = open(mp3FileName, 'w')
mp3_file.write(''.join(audioData))
mp3_file.close()
return mp3FileName
#convert mp3 to wav
def mp3_to_wav(mp3Path, frequency):
#call arg not file...
wavFileName = mp3Path.replace(".mp3",".wav")
#write 1.6 kHz
subprocess.call(['ffmpeg', '-loglevel', 'quiet', '-i', mp3Path, '-y', '-ar', '16000', '-ac', '1', wavFileName])
return wavFileName
#convert mp4 to wav
def video_to_wav(video_path, frequency):
#call arg not file...
name, extension = os.path.splitext(video_path)
wavFileName = name + ".wav"
#write 1.6 kHz
subprocess.call(['ffmpeg', '-loglevel', 'quiet', '-i', video_path, '-y', '-ar', '16000', '-vn', '-ac', '1', wavFileName])
return wavFileName
#play wav file
def play_wav(wavFileName):
#-nodisp : display not in new window
#-autoexit : stop automatically
#-ss : start time
#-t : duration
subprocess.call(['ffplay', '-nodisp', '-autoexit','-ss','0', '-t', '3.5', wavFileName])
#Plot waveform in GUI
#create waveform of wav file
def createWaveform(wavFileName):
spf = wave.open(wavFileName,'r')
#Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int16')
#If Stereo
if spf.getnchannels() == 2:
print 'Just mono files'
sys.exit(0)
plt.title('Signal Wave...')
plt.plot(signal)
play_wav(wavFileName)
plt.grid()
plt.show()
#main
def runMain(bag, fileName):
video_extensions = [".mp4", ".avi", ".mkv"]
if bag:
#read bag file
audioGlobals.bagFile = fileName
audioFileName = fileName.replace(".bag",".wav")
if os.path.isfile(audioFileName):
print colored('Load WAV File', 'yellow')
audioGlobals.wavFileName = audioFileName
audioGlobals.saveAudio = True
else:
print colored('Get audio data from ROS', 'green')
audioData, frequency = audio_bag_file(bag)
# get audio data
mp3FileName = write_mp3_file(audioData, audioGlobals.bagFile)
audioGlobals.wavFileName = mp3_to_wav(mp3FileName, frequency)
audioGlobals.saveAudio = False
else:
name, extension = os.path.splitext(fileName)
audioGlobals.saveAudio = False
if extension == ".wav":
print colored('Load WAV File', 'yellow')
audioGlobals.wavFileName = fileName
elif extension == ".mp3":
audioGlobals.wavFileName = video_to_wav(fileName, 16000)
elif extension in video_extensions:
audioGlobals.wavFileName = video_to_wav(fileName, 16000)
print audioGlobals.wavFileName
runFunction.run(audioGlobals.wavFileName, audioGlobals.bagFile)