1
1
Audio Intelligence Snippets
2
2
========================
3
3
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 😉
5
5
6
- ## Check WAV file's stats
6
+ ## Load WAV file and check file stats
7
7
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
9
9
10
10
``` python linenums="1"
11
11
# import
@@ -20,6 +20,8 @@ with wave.open(file_path, 'rb') as wav_file:
20
20
n_frames = wav_file.getnframes()
21
21
comp_type = wav_file.getcomptype()
22
22
comp_name = wav_file.getcompname()
23
+ # read the data
24
+ data = wav_file.readframes(n_frames)
23
25
# structure the required stats
24
26
wav_file_stats = {
25
27
" Number of Channels" : n_channels,
@@ -39,8 +41,43 @@ print(wav_file_stats)
39
41
# 'Compression Type': 'NONE',
40
42
# 'Compression Name': 'not compressed'}
41
43
```
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
+
42
77
## Get WAV file duration in seconds
43
78
79
+ - You can check the above section or below is a readymade function.
80
+
44
81
``` python linenums="1"
45
82
# import
46
83
import wave
@@ -62,6 +99,29 @@ print_wav_duration('path/to/file.wav')
62
99
63
100
## Convert Dual Channel WAV file to Mono
64
101
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
+
65
125
``` python linenums="1"
66
126
import wave
67
127
import numpy as np
@@ -101,4 +161,44 @@ def stereo_to_mono(file_path):
101
161
# Replace with an actual file path to a stereo wav file
102
162
# e.g., stereo_to_mono("path/to/stereo_file.wav")
103
163
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 ` .
0 commit comments