Get startedGet started for free

Bytes to integers

You've seen how to import and read an audio file using Python's wave module and the readframes() method. But doing that results in an array of bytes.

To convert the bytes into something more useful, we'll use NumPy's frombuffer() method.

Passing frombuffer() our sound waves bytes and indicating a dtype of 'int16', we can convert our bytes to integers. Integers are much easier to work with than bytes.

The Python wave library has already been imported along with the good_morning.wav audio file.

This exercise is part of the course

Spoken Language Processing in Python

View Course

Exercise instructions

  • Import the numpy package with its common alias np.
  • Open and read the good morning audio file.
  • Convert the signal_gm bytes to int16 integers.
  • View the first 10 sound wave values.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

import ____ as ____

# Open good morning sound wave and read frames as bytes
good_morning = wave.open(____, 'r')
signal_gm = good_morning.readframes(-1)

# Convert good morning audio bytes to integers
soundwave_gm = np.frombuffer(____, dtype=_____)

# View the first 10 sound wave values
print(soundwave_gm[:____])
Edit and Run Code