When working with audio data in Python, it is common to encounter the need to convert NumPy arrays representing audio signals into WAV files. This transformation is essential for audio playback, storage, and further processing. The input is a NumPy array with audio amplitude values, and the desired output is a .wav file containing the corresponding audio.
Method 1: Using scipy.io.wavfile
The scipy.io.wavfile.write function provides an easy way to export a NumPy array as a WAV file. This function takes the sampling rate and the array to be converted as inputs, creating a WAV file. It is essential to ensure that the NumPy array data type is an integer for non-floating point formats.
Here’s an example:
import numpy as np
from scipy.io import wavfile
# Define your sampling rate and audio signal
sample_rate = 44100
audio_signal = np.random.uniform(-1, 1, sample_rate)
# Convert audio signal to 16-bit integers
audio_signal = np.int16(audio_signal * 32767)
# Write the NumPy array to a WAV file
wavfile.write('output.wav', sample_rate, audio_signal)
Output: A WAV file named ‘output.wav’
This code snippet generates an audio signal as a NumPy array and converts it to a 16-bit PCM WAV file by first scaling the audio signal to the 16-bit integer range and then writing it to a file.
Method 2: Using the soundfile Library
The soundfile.write function is another handy tool for creating WAV files from NumPy arrays. This library is capable of handling more audio formats and offers greater flexibility with dtype conversions. Make sure to install the library using pip install soundfile before running the code.
Here’s an example:
import numpy as np
import soundfile as sf
# Define your sampling rate and audio signal
sample_rate = 44100
audio_signal = np.random.uniform(-1, 1, sample_rate)
# Write the NumPy array to a WAV file
sf.write('output.wav', audio_signal, sample_rate)
Output: A WAV file named ‘output.wav’
This example demonstrates how to create a WAV file using the soundfile library. Unlike the scipy approach, there is no need to manually convert the data type of the audio signal.
Method 3: Using wave Module
The wave module in Python’s standard library can also be used to write a NumPy array to a WAV file. This method requires manual handling of opening and closing files and converting NumPy arrays to byte data.
Here’s an example:
import numpy as np
import wave
import struct
# Define your sampling rate and audio signal
sample_rate = 44100
audio_signal = np.random.uniform(-1, 1, sample_rate)
# Convert audio signal to 16-bit integers
audio_signal = np.int16(audio_signal * 32767)
# Open a WAV file
with wave.open('output.wav', 'w') as wav_file:
# Define audio parameters
wav_file.setnchannels(1) # Mono
wav_file.setsampwidth(2) # Two bytes per sample
wav_file.setframerate(sample_rate)
# Convert the NumPy array to bytes and write it to the WAV file
wav_file.writeframes(audio_signal.tobytes())
Output: A WAV file named ‘output.wav’
This code manually manages the writing process to the WAV file using the wave module. It includes setting up the channels, sample width, and frame rate.
Method 4: Using AudioSegment from pydub
Pydub’s AudioSegment class allows for straightforward audio manipulations. Converting a NumPy array to WAV with this library involves creating an AudioSegment instance from raw audio data and exporting it as a WAV file. The library can be installed using pip install pydub.
Here’s an example:
import numpy as np
from pydub import AudioSegment
# Define your sampling rate and audio signal
sample_rate = 44100
audio_signal = np.random.uniform(-1, 1, sample_rate)
# Convert audio signal to 16-bit integers
audio_signal = audio_signal.astype(np.int16)
# Create the audio segment
audio_segment = AudioSegment(audio_signal.tobytes(), frame_rate=sample_rate, sample_width=2, channels=1)
# Export as WAV
audio_segment.export('output.wav', format='wav')
Output: A WAV file named ‘output.wav’
This snippet creates an AudioSegment object with the raw audio data and saves it as a WAV file.
Bonus One-Liner Method 5: Using audiowaveform (Command Line)
Finally, the command-line tool audiowaveform is particularly efficient for generating waveform images, but can also convert audio formats. As a one-liner, it requires running a single command in the terminal after the NumPy array has been saved as an intermediate raw audio file.
Here’s an example:
audiowaveform -i audio.raw -o output.wav --pixels-per-second 100 -b 8
Output: A WAV file named ‘output.wav’
This command assumes the availability of an audio.raw file containing the raw audio data from the NumPy array. It uses the audiowaveform tool to generate a WAV file.
Summary/Discussion
- Method 1: scipy.io.wavfile. Strengths: Part of SciPy package, convenient for scientific computing. Weaknesses: Requires manual dtype conversion.
- Method 2: soundfile library. Strengths: Flexible, supports various audio formats. Weaknesses: Additional library installation required.
- Method 3: wave module. Strengths: Part of standard library, fine control over parameters. Weaknesses: Verbose and manual process.
- Method 4: Pydub. Strengths: Easy manipulation of audio, good for complex operations. Weaknesses: Requires pydub installation, possible external dependencies for audio formats.
- Method 5: audiowaveform. Strengths: Useful for waveform visualization, productive for batch conversions. Weaknesses: Not a Python solution, command-line utility, intermediate raw file needed.
