Converting sound wave bytes to integers

Spoken Language Processing in Python

Daniel Bourke

Machine Learning Engineer/YouTube Creator

Converting bytes to integers

  • Can't use bytes
  • Convert bytes to integers using numpy
import numpy as np

# Convert soundwave_gm from bytes to integers signal_gm = np.frombuffer(soundwave_gm, dtype='int16')
# Show the first 10 items signal_gm[:10]
array([ -3,  -5,  -8,  -8,  -9, -13,  -8, -10,  -9, -11], dtype=int16)
Spoken Language Processing in Python

Finding the frame rate

  • Frequency (Hz) = length of wave object array/duration of audio file (seconds)
# Get the frame rate
framerate_gm = good_morning.getframerate()

# Show the frame rate framerate_gm
48,000
  • Duration of audio file (seconds) = length of wave object array/frequency (Hz)
Spoken Language Processing in Python

Finding sound wave timestamps

# Return evenly spaced values between start and stop
np.linspace(start=1, stop=10, num=10)
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
# Get the timestamps of the good morning sound wave
time_gm = np.linspace(start=0, 
                      stop=len(soundwave_gm)/framerate_gm,
                      num=len(soundwave_gm))
Spoken Language Processing in Python

Finding sound wave timestamps

# View first 10 time stamps of good morning sound wave
time_gm[:10]
array([0.00000000e+00, 2.08334167e-05, 4.16668333e-05, 6.25002500e-05,
       8.33336667e-05, 1.04167083e-04, 1.25000500e-04, 1.45833917e-04,
       1.66667333e-04, 1.87500750e-04])
Spoken Language Processing in Python

Let's practice!

Spoken Language Processing in Python

Preparing Video For Download...