Playing and Recording Sound in Python

In this tutorial, we will learn about Playing and Recording Sound in Python. Many applications, including audio processing, music playback, gaming, and more, commonly involve playing and recording sound in Python. Python provides several libraries and modules that enable sound manipulation, catering to both beginners and advanced users with their versatility.

Playing and Recording Sound in Python

Playing Sound in Python

Certainly! Here are examples of how to play sound using different libraries in Python –

(1) Pygame

Pygame is a popular library for game development, but it can also be used to play sound. It provides a straightforward way to load and play audio files.

Installation

pip install pygame

Example

import pygame

# Initialize pygame
pygame.init()

# Load the sound file
sound = pygame.mixer.Sound('path_to_sound_file.wav')

# Play the sound
sound.play()

# You can add a delay or use a loop to keep the program running while the sound plays
pygame.time.delay(sound.get_length() * 1000)  # Delay in milliseconds

# Quit pygame
pygame.quit()
  • Import the pygame library.
  • Initialize pygame with pygame.init().
  • Load the sound file using pygame.mixer.Sound('path_to_sound_file.wav').
  • Play the sound using sound.play().
  • You can add a delay to keep the program running while the sound plays.
  • Finally, quit pygame with pygame.quit().

(2) Playsound

Playsound is a simple Python library for playing sound without the need for external dependencies. It’s straightforward and useful for basic sound playback.

Installation

pip install playsound

Example

from playsound import playsound

# Play the sound
playsound('path_to_sound_file.mp3')
  • Import playsound from the library.
  • Use playsound('path_to_sound_file.mp3') to play the sound file.

(3) Pydub

The pydub library in Python is primarily used for audio file manipulation, including playing sound. It provides a simple and user-friendly interface for working with audio files.

Pydub, on the other hand, simplifies the process of working with audio files in Python, including the capability to play them back seamlessly. Furthermore, it’s a versatile library that excels not only in playback but also in managing an array of audio operations, including conversion, trimming, and concatenation. Consequently, Pydub emerges as an invaluable tool for tackling a wide spectrum of audio processing tasks.

Here’s how to play sound using pydub –

Installation

pip install pydub

Example

from pydub import AudioSegment
from pydub.playback import play

# Load the audio file
audio = AudioSegment.from_file('path_to_sound_file.wav')

# Play the audio
play(audio)
  • Importing Modules: We start by importing AudioSegment and play from the pydub library. AudioSegment is used to load and manipulate audio files, while play is used to play them.
  • Loading Audio: We load the audio file we want to play using AudioSegment.from_file('path_to_sound_file.wav'). Make sure to replace 'path_to_sound_file.wav' with the actual path to your sound file.
  • Playing Audio: To play the loaded audio, we simply call play(audio), where audio is the AudioSegment object representing the sound file. This will play the audio in the default audio player for your operating system.

(4) Sounddevice

Sounddevice is a library that provides low-latency audio input and output for Python. It’s a more advanced option for handling audio, suitable for audio processing applications.

Installation

pip install sounddevice

Example

import sounddevice as sd
import numpy as np

# Define the sample rate and duration
sample_rate = 44100  # 44.1 kHz
duration = 5  # seconds

# Generate a simple sine wave as audio data
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
audio_data = 0.5 * np.sin(2 * np.pi * 440 * t)

# Play the audio data
sd.play(audio_data, sample_rate)
sd.wait()
  • Import sounddevice as sd and numpy as np.
  • Define the sample rate (sample_rate) and duration of the audio.
  • Generate audio data (e.g., a simple sine wave in this example).
  • Play the audio data using sd.play(audio_data, sample_rate) and wait for it to finish with sd.wait().

Recording Sound in Python

You can record audio in Python using two libraries: python-sounddevice and pyaudio.

  • python-sounddevice records audio into NumPy arrays.
  • pyaudio records audio into bytes objects.

You can save audio recorded with python-sounddevice as a WAV file using the scipy library. For pyaudio, you can use the wave library to save it as a WAV file.

(1) PyAudio for Sound Recording

PyAudio is a Python library that provides an interface to work with audio devices, making it suitable for both recording and playback. It supports various audio formats and allows you to customize audio parameters.

import pyaudio
import wave

# Initialize PyAudio
audio = pyaudio.PyAudio()

# Parameters for recording
FORMAT = pyaudio.paInt16  # Sample format
CHANNELS = 1  # Number of audio channels (1 for mono, 2 for stereo)
RATE = 44100  # Sample rate (samples per second)
CHUNK = 1024  # Frames per buffer
RECORD_SECONDS = 5  # Duration of the recording in seconds
OUTPUT_FILENAME = "recorded_audio.wav"  # Name of the output file

# Create an audio stream
stream = audio.open(format=FORMAT, channels=CHANNELS,
                    rate=RATE, input=True,
                    frames_per_buffer=CHUNK)

print("Recording...")

frames = []

# Record audio
for _ in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("Recording finished.")

# Stop and close the audio stream
stream.stop_stream()
stream.close()

# Terminate PyAudio
audio.terminate()

# Save the recorded audio to a WAV file
with wave.open(OUTPUT_FILENAME, 'wb') as wf:
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(audio.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
  • Sample Rate (RATE): The number of audio samples per second (measured in Hertz, Hz).
  • Sample Format (FORMAT): The format used to represent audio samples (e.g., paInt16 for 16-bit signed integers).
  • Channels (CHANNELS): The number of audio channels (e.g., 1 for mono, 2 for stereo).
  • Frames per Buffer (CHUNK): The number of frames (audio samples) to read at a time.
  • Stream: A connection to an audio input or output device.

(2) Sounddevice for Sound Recording

Sounddevice is a Python library that simplifies audio input and output tasks. It provides a high-level interface to record and play audio with ease.

import sounddevice as sd
import numpy as np

# Parameters for recording
samplerate = 44100  # Sample rate (samples per second)
channels = 1  # Number of audio channels (1 for mono, 2 for stereo)
duration = 5  # Duration of the recording in seconds
OUTPUT_FILENAME = "recorded_audio.wav"  # Name of the output file

# Record audio
print("Recording...")
audio_data = sd.rec(int(samplerate * duration), samplerate=samplerate, channels=channels)
sd.wait()  # Wait until recording is finished

print("Recording finished.")

# Save the recorded audio to a WAV file
sd.write(OUTPUT_FILENAME, audio_data)

# Optionally, you can play the recorded audio as well
# sd.play(audio_data, samplerate)
# sd.wait()
  • Sample Rate (samplerate): The number of audio samples per second (measured in Hertz, Hz).
  • Channels (channels): The number of audio channels (e.g., 1 for mono, 2 for stereo).
  • Duration (duration): The duration of the recording in seconds.