Skip to main content

SoundCard Examples

SoundCard Library How To Examples in Python

The SoundCard library in Python provides a simple and intuitive way to play and record audio using the sound card on your computer. This tutorial will cover various examples to help you understand how to use the SoundCard library effectively.

Installation

Before we dive into the examples, let's start with installing the SoundCard library. Open your terminal and run the following command:

pip install soundcard

Ensure that you have the necessary privileges to install packages on your system.

Now that we have installed the SoundCard library, let's move on to the examples.

Example 1: Playing a Sound File

In this example, we will learn how to play a sound file using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound output devices
speakers = sc.all_speakers()
print("Available sound output devices:")
for speaker in speakers:
print(speaker)

# Select the first sound output device
speaker = speakers[0]

# Load the sound file
sound_file = "path/to/sound/file.wav"

# Play the sound file
with speaker.player(samplerate=44100) as sp:
sp.play_file(sound_file)

Replace "path/to/sound/file.wav" with the actual path to your sound file.

When you run this code, it will first display a list of available sound output devices. It then selects the first device from the list and plays the specified sound file using that device.

Example 2: Recording Audio

In this example, we will learn how to record audio using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound input devices
microphones = sc.all_microphones()
print("Available sound input devices:")
for microphone in microphones:
print(microphone)

# Select the first sound input device
microphone = microphones[0]

# Record audio for 5 seconds
frames = microphone.record(samplerate=44100, numframes=220500)

# Save the recorded audio to a file
sound_file = "path/to/save/recorded/audio.wav"
sc.write(sound_file, frames, samplerate=44100)

Replace "path/to/save/recorded/audio.wav" with the desired path to save the recorded audio.

When you run this code, it will first display a list of available sound input devices. It then selects the first device from the list and records audio for 5 seconds using that device. The recorded audio is then saved to the specified file.

Example 3: Playing and Recording Simultaneously

In this example, we will learn how to play and record audio simultaneously using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound input and output devices
speakers = sc.all_speakers()
microphones = sc.all_microphones()
print("Available sound output devices:")
for speaker in speakers:
print(speaker)
print("Available sound input devices:")
for microphone in microphones:
print(microphone)

# Select the first sound output and input devices
speaker = speakers[0]
microphone = microphones[0]

# Load the sound file
sound_file = "path/to/sound/file.wav"

# Play the sound file and record audio simultaneously
with speaker.player(samplerate=44100) as sp, microphone.recorder(samplerate=44100) as mic:
sp.play_file(sound_file)
frames = mic.record(numframes=220500)

# Save the recorded audio to a file
recorded_sound_file = "path/to/save/recorded/audio.wav"
sc.write(recorded_sound_file, frames, samplerate=44100)

Replace "path/to/sound/file.wav" and "path/to/save/recorded/audio.wav" with the actual paths for the sound file and the recorded audio file respectively.

When you run this code, it will first display a list of available sound output and input devices. It then selects the first devices from each list and plays the specified sound file using the output device while simultaneously recording audio using the input device. The recorded audio is then saved to the specified file.

Example 4: Adjusting Volume

In this example, we will learn how to adjust the volume of audio using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound output devices
speakers = sc.all_speakers()
print("Available sound output devices:")
for speaker in speakers:
print(speaker)

# Select the first sound output device
speaker = speakers[0]

# Load the sound file
sound_file = "path/to/sound/file.wav"

# Play the sound file with reduced volume
with speaker.player(samplerate=44100, volume=0.5) as sp:
sp.play_file(sound_file)

Replace "path/to/sound/file.wav" with the actual path to your sound file.

When you run this code, it will first display a list of available sound output devices. It then selects the first device from the list and plays the specified sound file with reduced volume (0.5 times the original volume) using that device.

Example 5: Mixing Multiple Audio Streams

In this example, we will learn how to mix multiple audio streams using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound output devices
speakers = sc.all_speakers()
print("Available sound output devices:")
for speaker in speakers:
print(speaker)

# Select the first sound output device
speaker = speakers[0]

# Load the sound files
sound_file1 = "path/to/sound/file1.wav"
sound_file2 = "path/to/sound/file2.wav"

# Play the sound files simultaneously
with speaker.player(samplerate=44100) as sp:
sp.play_file(sound_file1)
sp.play_file(sound_file2)

Replace "path/to/sound/file1.wav" and "path/to/sound/file2.wav" with the actual paths to your sound files.

When you run this code, it will first display a list of available sound output devices. It then selects the first device from the list and plays the specified sound files simultaneously using that device.

Example 6: Streaming Audio from a Microphone

In this example, we will learn how to stream audio from a microphone using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound input devices
microphones = sc.all_microphones()
print("Available sound input devices:")
for microphone in microphones:
print(microphone)

# Select the first sound input device
microphone = microphones[0]

# Stream audio from the microphone
with microphone.recorder(samplerate=44100) as mic:
for _ in range(1000):
frames = mic.record(numframes=4410)
# Process the frames here

When you run this code, it will first display a list of available sound input devices. It then selects the first device from the list and starts streaming audio from the microphone using that device. You can process the audio frames inside the for loop according to your requirements.

Example 7: Changing the Sample Rate

In this example, we will learn how to change the sample rate of audio using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound output devices
speakers = sc.all_speakers()
print("Available sound output devices:")
for speaker in speakers:
print(speaker)

# Select the first sound output device
speaker = speakers[0]

# Load the sound file
sound_file = "path/to/sound/file.wav"

# Play the sound file with a different sample rate
with speaker.player(samplerate=22050) as sp:
sp.play_file(sound_file)

Replace "path/to/sound/file.wav" with the actual path to your sound file.

When you run this code, it will first display a list of available sound output devices. It then selects the first device from the list and plays the specified sound file with a sample rate of 22050 Hz using that device.

Example 8: Looping Playback

In this example, we will learn how to loop the playback of a sound file using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc

# Get a list of available sound output devices
speakers = sc.all_speakers()
print("Available sound output devices:")
for speaker in speakers:
print(speaker)

# Select the first sound output device
speaker = speakers[0]

# Load the sound file
sound_file = "path/to/sound/file.wav"

# Loop the playback of the sound file
with speaker.player(samplerate=44100, loop=True) as sp:
sp.play_file(sound_file)

Replace "path/to/sound/file.wav" with the actual path to your sound file.

When you run this code, it will first display a list of available sound output devices. It then selects the first device from the list and continuously plays the specified sound file in a loop using that device.

Example 9: Frequency Modulation

In this example, we will learn how to apply frequency modulation to audio using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc
import numpy as np

# Create a carrier signal
duration = 5 # seconds
samplerate = 44100
t = np.linspace(0, duration, int(samplerate * duration), endpoint=False)
carrier_frequency = 440 # Hz
carrier_signal = np.sin(2 * np.pi * carrier_frequency * t)

# Create a modulating signal
modulating_frequency = 5 # Hz
modulating_signal = np.sin(2 * np.pi * modulating_frequency * t)

# Apply frequency modulation
modulated_signal = np.sin(2 * np.pi * (carrier_frequency + modulating_signal) * t)

# Play the modulated signal
speakers = sc.all_speakers()
speaker = speakers[0]
with speaker.player(samplerate=samplerate) as sp:
sp.play(modulated_signal)

When you run this code, it will create a carrier signal and a modulating signal. It then applies frequency modulation by adding the modulating signal to the carrier frequency. The resulting modulated signal is played using the first available sound output device.

Example 10: Generating a Sine Wave

In this example, we will learn how to generate a sine wave using the SoundCard library. Create a new Python file and add the following code:

import soundcard as sc
import numpy as np

# Generate a sine wave
duration = 5 # seconds
samplerate = 44100
t = np.linspace(0, duration, int(samplerate * duration), endpoint=False)
frequency = 440 # Hz
sine_wave = np.sin(2 * np.pi * frequency * t)

# Play the sine wave
speakers = sc.all_speakers()
speaker = speakers[0]
with speaker.player(samplerate=samplerate) as sp:
sp.play(sine_wave)

When you run this code, it will generate a sine wave with a frequency of 440 Hz. The sine wave is then played using the first available sound output device.

These examples should give you a good starting point for using the SoundCard library in Python. Experiment with different settings and explore the library's documentation for more advanced features and functionalities.