0
0
SciPydata~10 mins

WAV audio file handling in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WAV audio file handling
Import scipy.io.wavfile
Read WAV file with read()
Get sample rate and data array
Process or analyze data
Write data back with write()
Save new WAV file
This flow shows how to read a WAV file, get its sample rate and data, optionally process it, and then save it back.
Execution Sample
SciPy
from scipy.io import wavfile
sample_rate, data = wavfile.read('input.wav')
print(sample_rate, data.shape)
wavfile.write('output.wav', sample_rate, data)
Reads a WAV file, prints sample rate and data shape, then writes the same data to a new file.
Execution Table
StepActionResultData Type/Value
1Import wavfile from scipy.ioModule readywavfile module
2Read 'input.wav' using wavfile.read()Returns sample rate and data arraysample_rate=44100, data=array(shape=(441000,))
3Print sample_rate and data.shapeOutputs sample rate and data shape44100 (441000,)
4Write data to 'output.wav' with wavfile.write()File saved with same sample rate and dataoutput.wav created
5End of scriptProcess completeNo errors
💡 All steps executed successfully; WAV file read and written.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
sample_rateundefined44100441004410044100
dataundefinedarray(shape=(441000,))array(shape=(441000,))array(shape=(441000,))array(shape=(441000,))
Key Moments - 3 Insights
Why do we get two outputs from wavfile.read()?
wavfile.read() returns the sample rate (how many samples per second) and the data array (the actual sound samples). See execution_table step 2.
What does the data array shape mean?
The shape shows how many samples and channels the audio has. For example, (441000,) means 441000 samples in one channel (mono). See variable_tracker for data shape.
Can we write the data back without changes?
Yes, wavfile.write() saves the data with the given sample rate. This is shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the sample rate after reading the WAV file?
A22050
B48000
C44100
DNone
💡 Hint
Check execution_table row 2 under 'Data Type/Value' for sample_rate.
At which step is the WAV file data shape printed?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table rows and find where print happens.
If the data array had shape (441000, 2), what would that mean?
AStereo audio with 441000 samples per channel
BMono audio with 441000 samples
CAudio with 2 samples total
DSample rate is 2 Hz
💡 Hint
Refer to key_moments about data array shape meaning.
Concept Snapshot
Use scipy.io.wavfile to read and write WAV files.
read() returns sample rate and data array.
Data array shape shows samples and channels.
write() saves data with sample rate.
Useful for audio processing and analysis.
Full Transcript
This lesson shows how to handle WAV audio files using scipy.io.wavfile in Python. First, import wavfile. Then read a WAV file with wavfile.read(), which gives you the sample rate and the audio data array. The sample rate tells how many samples per second the audio has. The data array contains the sound samples, and its shape tells you how many samples and channels are present. You can print these values to understand the audio file. Finally, you can write the audio data back to a new WAV file using wavfile.write(), keeping the same sample rate and data. This process is useful for audio analysis and processing tasks.