Skip to content

Commit 2645337

Browse files
committed
modified the audio snippets
1 parent c650555 commit 2645337

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed

docs/audio_intelligence/audio_snippets.md

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Audio Intelligence Snippets
22
========================
33

4-
Here are some code snippets that you might find useful when playing around with audio files :wink:
4+
Here are some code snippets that you might find useful when playing around with audio files 😉
55

6-
## Check WAV file's stats
6+
## Load WAV file and check file stats
77

8-
- To check some basic stats of a wav file, we can use the following code,
8+
- To check some basic stats of a wav file, we can use `wave` package
99

1010
``` python linenums="1"
1111
# import
@@ -20,6 +20,8 @@ with wave.open(file_path, 'rb') as wav_file:
2020
n_frames = wav_file.getnframes()
2121
comp_type = wav_file.getcomptype()
2222
comp_name = wav_file.getcompname()
23+
# read the data
24+
data = wav_file.readframes(n_frames)
2325
# structure the required stats
2426
wav_file_stats = {
2527
"Number of Channels": n_channels,
@@ -39,8 +41,43 @@ print(wav_file_stats)
3941
# 'Compression Type': 'NONE',
4042
# 'Compression Name': 'not compressed'}
4143
```
44+
45+
- We can also `scipy` package for wav file loading and printing stats
46+
47+
``` python linenums="1"
48+
# import
49+
from scipy.io import wavfile
50+
51+
# let's define a function to print the stats
52+
def print_wav_stats(sample_rate, data):
53+
print(f"Sample rate: {sample_rate} Hz")
54+
print(f"Data type: {data.dtype}")
55+
print(f"Duration: {data.shape[0] / sample_rate} seconds")
56+
print(f"Number of samples: {data.shape[0]}")
57+
print(f"Value range: {data.min()} to {data.max()}")
58+
print(f"Channels: {data.shape}")
59+
60+
# Load the wav file
61+
file_path = f'{dataset_folder}/call_1.wav'
62+
sample_rate, data = wavfile.read(file_path)
63+
# print stats, example below
64+
print_wav_stats(sample_rate, data)
65+
# Sample rate: 48000 Hz
66+
# Data type: int16
67+
# Duration: 1.18 seconds
68+
# Number of samples: 56640
69+
# Value range: -1787 to 1835
70+
# Channels: (56640, 2)
71+
```
72+
73+
!!! Note
74+
`scipy` returns data in an array with shape `(n_samples, n_channels)`. Whereas `wave` package returns data in bytes format.
75+
76+
4277
## Get WAV file duration in seconds
4378

79+
- You can check the above section or below is a readymade function.
80+
4481
``` python linenums="1"
4582
# import
4683
import wave
@@ -62,6 +99,29 @@ print_wav_duration('path/to/file.wav')
6299

63100
## Convert Dual Channel WAV file to Mono
64101

102+
- Let's first do this with `scipy` package
103+
104+
``` python linenums="1"
105+
# import
106+
from scipy.io import wavfile
107+
108+
def stereo_to_mono(file_path):
109+
# Load the stereo wave file
110+
sample_rate, data = wavfile.read(file_path)
111+
# Check if it's already mono
112+
if data.shape[1] != 2:
113+
return "The file is not a stereo file."
114+
# Convert to mono by taking the mean of the two channels
115+
mono_data = data.mean(axis=1) # <--- THIS IS THE ONLY IMPORTANT LINE
116+
# Set the file path for output
117+
mono_file_path = file_path.replace(".wav", "_mono.wav")
118+
# Save the mono file
119+
wavfile.write(mono_file_path, sample_rate, mono_data.astype(data.dtype))
120+
return f"Stereo file converted to mono: {mono_file_path}"
121+
```
122+
123+
- Same can be done with `wave` and `numpy` packages
124+
65125
``` python linenums="1"
66126
import wave
67127
import numpy as np
@@ -101,4 +161,44 @@ def stereo_to_mono(file_path):
101161
# Replace with an actual file path to a stereo wav file
102162
# e.g., stereo_to_mono("path/to/stereo_file.wav")
103163
stereo_to_mono(f'{dataset_folder}/call_5.wav')
104-
```
164+
```
165+
166+
## Downsample WAV file
167+
168+
- Let's first do this with `scipy` package
169+
170+
``` python linenums="1"
171+
# import
172+
from scipy.io import wavfile
173+
import scipy.signal as sps
174+
175+
# Define a function to downsample a wave file
176+
def downsample_wav(file_path, target_sample_rate=16000):
177+
"""Downsample a wave file to a target sample rate.
178+
179+
Args:
180+
file_path (str): The path to the wave file.
181+
target_sample_rate (int): The target sample rate to downsample to.
182+
183+
Returns:
184+
str: A message indicating the success of the operation.
185+
"""
186+
# Load the wave file
187+
sample_rate, data = wavfile.read(file_path)
188+
# Check if the target sample rate is the same as the original
189+
if sample_rate == target_sample_rate:
190+
return "The file is already at the target sample rate."
191+
# Calculate the new number of samples
192+
new_num_samples = int(data.shape[0] * target_sample_rate / sample_rate)
193+
# Resample the data
194+
new_data = sps.resample(data, number_of_samples)
195+
# Set the file path for output
196+
downsampled_file_path = file_path.replace(".wav", f"_downsampled_{target_sample_rate}.wav")
197+
# Save the downsampled file
198+
wavfile.write(downsampled_file_path, target_sample_rate, new_data.astype(data.dtype))
199+
# return a message indicating the success of the operation
200+
return f"File downsampled to {target_sample_rate} Hz: {downsampled_file_path}"
201+
```
202+
203+
!!! Warning
204+
Before saving `.wav` file using `scipy`, make sure the dtype is `int16`.

docs/audio_intelligence/interview_questions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
- Here are some questions and their answers to make you ready for your next interview. Best of luck :wave:
2+
- Here are some questions and their answers to make you ready for your next interview. Best of luck 👋
33

44
!!! Question ""
55
=== "Question"

0 commit comments

Comments
 (0)