Get startedGet started for free

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.

This exercise is part of the course

Spoken Language Processing in Python

View Course

Exercise instructions

  • Convert the sound wave bytes to integers.
  • Get the frame rate of the good morning audio file using getframerate().
  • Set stop to be the length of soundwave_gm over the frame rate.
  • Set num to be the length of soundwave_gm.

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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])
Edit and Run Code