Get Started

From AudioFile to AudioData

As you saw earlier, there are some transformation steps we have to take to make our audio data useful. The same goes for SpeechRecognition.

In this exercise, we'll import the clean_support_call.wav audio file and get it ready to be recognized.

We first read our audio file using the AudioFile class. But the recognize_google() method requires an input of type AudioData.

To convert our AudioFile to AudioData, we'll use the Recognizer class's method record() along with a context manager. The record() method takes an AudioFile as input and converts it to AudioData, ready to be used with recognize_google().

SpeechRecognition has already been imported as sr.

This is a part of the course

“Spoken Language Processing in Python”

View Course

Exercise instructions

  • Pass the AudioFile class clean_support_call.wav.
  • Use the context manager to open and read clean_support_call as source.
  • Record source and run the code.

Hands-on interactive exercise

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

# Instantiate Recognizer
recognizer = sr.Recognizer()

# Convert audio to AudioFile
clean_support_call = sr.AudioFile(____)

# Convert AudioFile to AudioData
with ____ as source:
    clean_support_call_audio = recognizer.record(____)

# Transcribe AudioData to text
text = recognizer.recognize_google(clean_support_call_audio,
                                   language="en-US")
print(text)

This exercise is part of the course

Spoken Language Processing in Python

AdvancedSkill Level
4.7+
3 reviews

Learn how to load, transform, and transcribe speech from raw audio files in Python.

Speech recognition is still far from perfect. But the SpeechRecognition library provides an easy way to interact with many speech-to-text APIs. In this section, you'll learn how to use the SpeechRecognition library to easily start converting the spoken language in your audio files to text.

Exercise 1: SpeechRecognition Python libraryExercise 2: Pick the wrong speech_recognition APIExercise 3: Using the SpeechRecognition libraryExercise 4: Using the Recognizer classExercise 5: Reading audio files with SpeechRecognitionExercise 6: From AudioFile to AudioData
Exercise 7: Recording the audio we needExercise 8: Dealing with different kinds of audioExercise 9: Different kinds of audioExercise 10: Multiple Speakers 1Exercise 11: Multiple Speakers 2Exercise 12: Working with noisy audio

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free