4
import os
import scipy.io
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt
dataset_path = os.path.join(os.environ['HOME'], 'shared', 'data', 'assignment_1')
wavedata = os.path.join(dataset_path, 'example.wav')
   
fs, audio_buffer = scipy.io.wavfile.read(wavedata)

And I kinda cannot understand how to further use matplotlib.pyplot Would be grateful for any advice or right documentation!

3
  • 2
    What do you have in mind to plot? Just a regular timeseries plot? It would be nice if would show audio_buffer in the text form. Commented Oct 18, 2020 at 8:48
  • 1
    Link to realtime oscilloscope using openCV & soundcard libraries Commented Oct 18, 2020 at 10:49
  • 1
    recommend librosa.display.waveplot Commented Oct 18, 2020 at 13:17

1 Answer 1

4

this will plot the wav audio file in its native time domain as a time series

import os
import scipy.io
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt

myAudioFilename = 'aaa.wav'  #  plot this wav file     ~/audio/aaa.wav

dataset_path = os.path.join(os.environ['HOME'], 'audio') # homedir -> audiodir -> my wav files
wavedata = os.path.join(dataset_path, myAudioFilename)
   
sampleRate, audioBuffer = scipy.io.wavfile.read(wavedata)

duration = len(audioBuffer)/sampleRate

time = np.arange(0,duration,1/sampleRate) #time vector

plt.plot(time,audioBuffer)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title(myAudioFilename)
plt.show()

Notice the plot accurately renders time which is derived from the WAV file headers which defines the sample rate ... along with bit depth and channel count ... those attributes give the code ability to parse the binary WAV file byte by byte which get rendered as a series of points on the displayed curve ( each point is an audio sample for given channel )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.