Finding the time stamps
We know the frequency of our sound wave is 48 kHz, but what if we didn't? We could find it by dividing the length of our sound wave array by the duration of our sound wave. However, Python's wave module has a better way. Calling getframerate() on a wave object returns the frame rate of that wave object.
We can then use NumPy's linspace() method to find the time stamp of each integer in our sound wave array. This will help us visualize our sound wave in the future.
The linspace() method takes start, stop and num parameters and returns num evenly spaced values between start and stop.
In our case, start will be zero, stop will be the length of our sound wave array over the frame rate (or the duration of our audio file) and num will be the length of our sound wave array.
Cet exercice fait partie du cours
Spoken Language Processing in Python
Instructions
- Convert the sound wave bytes to integers.
- Get the frame rate of the good morning audio file using
getframerate(). - Set
stopto be the length ofsoundwave_gmover the frame rate. - Set
numto be the length ofsoundwave_gm.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Read in sound wave and convert from bytes to integers
good_morning = wave.open('good_morning.wav', 'r')
signal_gm = good_morning.readframes(-1)
soundwave_gm = np.frombuffer(____, dtype='int16')
# Get the sound wave frame rate
framerate_gm = good_morning.____
# Find the sound wave timestamps
time_gm = np.linspace(start=0,
stop=____/____,
num=____)
# Print the first 10 timestamps
print(time_gm[:10])