Search code examples
pythonaudiospectrogramm4a

spectrogram of an .m4a file


To solve my main problem, I have recorded some .m4a audio files (sample). I want to first get an spectrogram like this:


Generated by Audacity.

From here I can import the file:

from pydub import AudioSegment
seg = AudioSegment.from_file("voice.m4a")
print("Information:")
print("Channels:", seg.channels)
print("Bits per sample:", seg.sample_width * 8)
print("Sampling frequency:", seg.frame_rate)
print("Length:", seg.duration_seconds, "seconds")

and I know these spectrogram ploting functions:

  • scipy.signal.spectrogram
  • matplotlib.pyplot.specgram

for which there are a lot of examples here. But I don't know the steps between. I would appreciate if you could help me know how should I get the above plot. It doesn't have to be PyBud. Any other library (or even other language as far as it is Free Software!) is also fine. Thansk for your support in advance.

P.S. Next step would be to analyse the ambient noise in real-time like the one here. So any help in that direction would also be highly appreciated.


Solution

  • Here is an example straight from the documentation (see page 10):

    # Example for plotting a spectrogram using this function
    import audiosegment
    import matplotlib.pyplot as plt
    #...
    seg = audiosegment.from_file("voice.m4a")
    freqs, times, amplitudes = seg.spectrogram(window_length_s=0.03, overlap=0.5)
    amplitudes = 10 * np.log10(amplitudes + 1e-9)
    # Plot
    plt.pcolormesh(times, freqs, amplitudes)
    plt.xlabel("Time in Seconds")
    plt.ylabel("Frequency in Hz")
    plt.show()
    
    • You need to have [AudioSegment library] installed (e.g. pip install AudioSegment)
    • FFmpeg library needs to also be installed. On Windows one can use Chocolatey: choco install ffmpeg