Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[audio_to_spectrogram] Add AudioAmplitudePlot node to visualize audio amplitude #2657

Closed
wants to merge 24 commits into from

Conversation

iory
Copy link
Member

@iory iory commented Jan 7, 2022

What is this?

This PR adds a script to publish audio amplitude plot image.
The following video shows the audio amplitude visualized at this node while clapping.
This PR is based on #2654 .

audio_amplitude_plot--SLASH--output--SLASH--viz.mp4

Copy link
Member

@708yamaguchi 708yamaguchi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the nice plotter and documentation!

This is because my code was ad-hoc, but can you make some of the code common?

Comment on lines 27 to 37
# Audio topic config
# The number of channels in audio data
self.n_channel = rospy.get_param('~n_channel', 1)
# Sampling rate of microphone (namely audio topic).
self.mic_sampling_rate = rospy.get_param('~mic_sampling_rate', 16000)
# Bits per one audio data
bitdepth = rospy.get_param('~bitdepth', 16)
if bitdepth == 16:
self.dtype = 'int16'
else:
rospy.logerr("'~bitdepth' {} is unsupported.".format(bitdepth))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are the settings to convert audio stream and used in audio_to_spectrum.py, too.

# Audio topic config
# The number of channels in audio data
self.n_channel = rospy.get_param('~n_channel', 1)
# Sampling rate of microphone (namely audio topic).
mic_sampling_rate = rospy.get_param('~mic_sampling_rate', 16000)
# Period[s] to sample audio data for one fft
fft_sampling_period = rospy.get_param('~fft_sampling_period', 0.3)
# Bits per one audio data
bitdepth = rospy.get_param('~bitdepth', 16)
if bitdepth == 16:
self.dtype = 'int16'
else:
rospy.logerr("'~bitdepth' {} is unsupported.".format(bitdepth))

I think these codes should be shared.

Could you create python class like AudioBuffer, which receives audio_common_msgs/AudioData and create self.audio_buffer buffer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Created AudioBuffer class.

Comment on lines 76 to 87
def audio_cb(self, msg):
# Convert audio buffer to int array
data = msg.data
audio_buffer = np.frombuffer(data, dtype=self.dtype)
# Retreive one channel data
audio_buffer = audio_buffer[0::self.n_channel]
# Save audio msg to audio_buffer
with self.lock:
self.audio_buffer = np.append(
self.audio_buffer, audio_buffer)
self.audio_buffer = self.audio_buffer[
-self.audio_buffer_len:]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in another comment, could you create python class like AudioBuffer?

def audio_cb(self, msg):
# Convert audio buffer to int array
data = msg.data
audio_buffer = np.frombuffer(data, dtype=self.dtype)
# Retreive one channel data
audio_buffer = audio_buffer[0::self.n_channel]
# Save audio msg to audio_buffer
self.audio_buffer = np.append(
self.audio_buffer, audio_buffer)
self.audio_buffer = self.audio_buffer[
-self.audio_buffer_len:]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Created AudioBuffer class.

self.ax.set_ylim((-self.maximum_amplitude, self.maximum_amplitude))

self.ax.legend(loc='upper right')
if self.pub_img.get_num_connections() > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use ConnectionBasedTransport?

In addition, if possible, could you create python class like AudioPlot, which configures matplotlib and can be used in both audio_amplitude_plot.py and spectrum_plot.py

@k-okada
Copy link
Member

k-okada commented Jan 7, 2022

why did you create custom plot node instead of existing plotting tools (rqt / plotjuggler)

@iory
Copy link
Member Author

iory commented Jan 7, 2022

@k-okada

how you created custom plot node instead of existing plotting tools (rqt / plotjuggler)

I'm sorry I don't understand well.
Do you suggest that we should make a plugin such as rqt_plot?

@k-okada
Copy link
Member

k-okada commented Jan 7, 2022

@iory I want to know the reason why you created the nose just to visualize audio amplitude

@iory
Copy link
Member Author

iory commented Jan 7, 2022

Since sound is invisible, it is nice for the user to have it visualized.
The sample rate, number of channels, and other parameters are given manually by the user.
Sometimes, users give these values wrong.
The simplicity of this node, which visualizes the amplitude of sound, makes it easy to notice these mistakes.
In other words, it is useful for debugging purposes.

@k-okada
Copy link
Member

k-okada commented Jan 7, 2022

why you can not use rqt? or other plotting tools?

@knorth55
Copy link
Member

knorth55 commented Jan 7, 2022

FYI:
we have audioinfo message to pass meta data of audio data.
ros-drivers/audio_common#152

@iory
Copy link
Member Author

iory commented Jan 7, 2022

OK. I'll take another way.

@iory iory closed this Jan 7, 2022
@iory iory deleted the visualize-audio-amplitude branch January 7, 2022 06:28
@iory iory restored the visualize-audio-amplitude branch January 7, 2022 08:36
@iory iory reopened this Jan 7, 2022
@iory
Copy link
Member Author

iory commented Jan 7, 2022

@k-okada

why you can not use rqt? or other plotting tools?

I'm sorry, I'd like to ask you one point.
How do you think visualizing audio_common_msgs/AudioData is a good way to do it?
Since audio_common_msgs/AudioData is array data, I think it is necessary to insert another node in order to visualize it with rqt_plot.

$ rosmsg show audio_common_msgs/AudioData
uint8[] data

Also, when it comes out as image data, we are happy to be able to correspond with another image because it has a timestamp.

@k-okada
Copy link
Member

k-okada commented Jun 16, 2022

Since audio_common_msgs/AudioData is array data, I think it is necessary to insert another node in order to visualize it with rqt_plot.

@iory I see, LGTM

@iory iory force-pushed the visualize-audio-amplitude branch from 8f7f018 to 7e09365 Compare June 26, 2022 11:31
k-okada added a commit that referenced this pull request Dec 14, 2022
[audio_to_spectrogram] Add AudioAmplitudePlot node to visualize audio amplitude #2657
@k-okada
Copy link
Member

k-okada commented Dec 14, 2022

@iory merged in #2755 , but I have manually fixed conflict so it might contain wrong code, please check that.

@k-okada k-okada closed this Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants