python-scipyHow do I use Python and SciPy to write a WAV file?
To write a WAV file with Python and SciPy, you can use the scipy.io.wavfile.write
function. This example code will create a WAV file with a sine wave of frequency 440 Hz and a duration of 2 seconds:
import scipy.io.wavfile
import numpy as np
# Sampling rate of the sine wave
sampling_rate = 44100.0
# Duration of the sine wave
duration = 2.0
# Sine wave frequency
frequency = 440.0
# Generate time points
time_points = np.linspace(0, duration, int(sampling_rate * duration))
# Generate sine wave
sine_wave = np.sin(frequency * time_points * 2 * np.pi)
# Convert to 16-bit data
sine_wave = np.int16(sine_wave * 32767)
# Write the WAV file
scipy.io.wavfile.write("sine_wave.wav", sampling_rate, sine_wave)
This code will create a WAV file called sine_wave.wav
in the current directory.
The code consists of the following parts:
- Import the necessary modules:
scipy.io.wavfile
andnumpy
. - Set the sampling rate, duration, and frequency of the sine wave.
- Generate the time points for the sine wave.
- Generate the sine wave.
- Convert the sine wave to 16-bit data.
- Write the WAV file.
For more information, see the SciPy documentation.
More of Python Scipy
- How can I use Python Scipy to zoom in on an image?
- How do I use Python Scipy to perform a Z test?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I create a 2D array of zeros using Python and NumPy?
- How to use Python, XML-RPC, and NumPy together?
- How can I use Python and SciPy to find the zeros of a function?
- How can I use Python and Numpy to zip files?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I use Python and SciPy to visualize data?
See more codes...