0

OS: Windows 11 Python: 3.9

I can extract a number of audio features from .WAV files by using OpenSMILE and plot them with MatPlotLib. I can also select segments by clicking that chart (like, a segment of 3 seconds starting as second 10).

What I need is to playback the audio segment selected from the chart.

As a bonus, while the audio segment plays, I would like to show a vertical line running from the start to the end of the segment so the user can follow the sound playing and compare it with the features plotted.

AlexSC
  • 1,823
  • 3
  • 28
  • 54

1 Answers1

0

You can use the pydub library to play the audio segment selected from the chart. The pydub library allows you to load audio files, manipulate them, and play them back. For instance, you can use it to play a selected audio segment:

from pydub import AudioSegment

audio = AudioSegment.from_file("your_audio_file.wav")

start_time = 10000 # 10 seconds
end_time = 13000 # 3 seconds after the start

segment = audio[start_time:end_time]

segment.play()

To show the vertical line running from the start to the end of the segment while the audio is playing, you can use the matplotlib library to plot the line on top of the chart. Here is an example of how you can do it:

import matplotlib.pyplot as plt

plt.plot(your_chart_data)

# Draw vertical line
plt.axvline(x=start_time, color='r', linestyle='--')
plt.axvline(x=end_time, color='r', linestyle='--')

plt.show()

You can use the start_time and end_time from the audio segment you selected and pass it to the plt.axvline(x=start_time, color='r', linestyle='--') function, this will draw the vertical line for the selected segment.

You can also use the pyplot.pause function from matplotlib to make the program wait while the audio is playing. This way you can ensure that the audio finishes playing before the program continues.

plt.pause(segment.duration_seconds)

It's the duration of the audio segment in seconds, it will make the program wait until the audio finishes playing.

You can also use the tkinter library for this purpose, it has the ability to play audio file with the tkSnack library.

So, you can use the pydub library to play the audio segment, and the matplotlib library to plot the vertical line on top of the chart.

  • Thank you, I would love to use that package, but the code has a bug that prevents it to work on Windows. The code uses the function `NamedTemporaryFile()` in a way it throws an exception. This is explained in https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file – AlexSC Jan 18 '23 at 19:22