# importimportwave# Open the wave file and extract some propertiesfile_path=f'{dataset_folder}/call_1.wav'withwave.open(file_path,'rb')aswav_file:n_channels=wav_file.getnchannels()sample_width=wav_file.getsampwidth()frame_rate=wav_file.getframerate()n_frames=wav_file.getnframes()comp_type=wav_file.getcomptype()comp_name=wav_file.getcompname()# read the data data=wav_file.readframes(n_frames)# structure the required statswav_file_stats={"Number of Channels":n_channels,"Sample Width":sample_width,"Frame Rate":frame_rate,"Number of Frames":n_frames,"Compression Type":comp_type,"Compression Name":comp_name}# printprint(wav_file_stats)# Example output: # {'Number of Channels': 1,# 'Sample Width': 2,# 'Frame Rate': 22050,# 'Number of Frames': 3821760,# 'Compression Type': 'NONE',# 'Compression Name': 'not compressed'}
We can also scipy package for wav file loading and printing stats
1 2 3 4 5 6 7 8 91011121314151617181920212223
# importfromscipy.ioimportwavfile# let's define a function to print the statsdefprint_wav_stats(sample_rate,data):print(f"Sample rate: {sample_rate} Hz")print(f"Data type: {data.dtype}")print(f"Duration: {data.shape[0]/sample_rate} seconds")print(f"Number of samples: {data.shape[0]}")print(f"Value range: {data.min()} to {data.max()}")print(f"Channels: {data.shape}")# Load the wav filefile_path=f'{dataset_folder}/call_1.wav'sample_rate,data=wavfile.read(file_path)# print stats, example belowprint_wav_stats(sample_rate,data)# Sample rate: 48000 Hz# Data type: int16# Duration: 1.18 seconds# Number of samples: 56640# Value range: -1787 to 1835# Channels: (56640, 2)
Note
scipy returns data in an array with shape (n_samples, n_channels). Whereas wave package returns data in bytes format.
Get WAV file duration in seconds
You can check the above section or below is a readymade function.
1 2 3 4 5 6 7 8 910111213141516
# import importwavedefprint_wav_duration(file_path):# Open the wave filewithwave.open(file_path,'rb')aswav_file:# Extract the frame rate and number of framesframe_rate=wav_file.getframerate()n_frames=wav_file.getnframes()# Calculate durationduration=n_frames/float(frame_rate)print(f"The duration of the file is: {duration} seconds.")# Example usage with a placeholder file path# You would replace 'path/to/file.wav' with the actual file path of your .wav fileprint_wav_duration('path/to/file.wav')
Convert Dual Channel WAV file to Mono
Let's first do this with scipy package
1 2 3 4 5 6 7 8 910111213141516
# importfromscipy.ioimportwavfiledefstereo_to_mono(file_path):# Load the stereo wave filesample_rate,data=wavfile.read(file_path)# Check if it's already monoifdata.shape[1]!=2:return"The file is not a stereo file."# Convert to mono by taking the mean of the two channelsmono_data=data.mean(axis=1)# <--- THIS IS THE ONLY IMPORTANT LINE# Set the file path for outputmono_file_path=file_path.replace(".wav","_mono.wav")# Save the mono filewavfile.write(mono_file_path,sample_rate,mono_data.astype(data.dtype))returnf"Stereo file converted to mono: {mono_file_path}"
importwaveimportnumpyasnpdefstereo_to_mono(file_path):# Open the stereo wave filewithwave.open(file_path,'rb')asstereo_wav:# Check if it's already monoifstereo_wav.getnchannels()!=2:return"The file is not a stereo file."# Read the stereo wave file dataframes=stereo_wav.readframes(stereo_wav.getnframes())# Convert frames to numpy arrayframes=np.frombuffer(frames,dtype=np.int16)# Reshape the data to 2 columns for stereoframes=np.reshape(frames,(-1,2))# Take the mean of the two channels to convert to monomono_frames=frames.mean(axis=1,dtype=np.int16)# Get stereo file params to use for mono fileparams=stereo_wav.getparams()num_frames=len(mono_frames)# Set the file path for outputmono_file_path=file_path.replace(".wav","_mono.wav")# Create a new wave file for monowithwave.open(mono_file_path,'wb')asmono_wav:# Set parameters for mono (nchannels=1)mono_wav.setparams((1,params.sampwidth,params.framerate,num_frames,params.comptype,params.compname))# Write frames for monomono_wav.writeframes(mono_frames.tobytes())returnf"Stereo file converted to mono: {mono_file_path}"# Replace with an actual file path to a stereo wav file# e.g., stereo_to_mono("path/to/stereo_file.wav")stereo_to_mono(f'{dataset_folder}/call_5.wav')
# importfromscipy.ioimportwavfileimportscipy.signalassps# Define a function to downsample a wave filedefdownsample_wav(file_path,target_sample_rate=16000):"""Downsample a wave file to a target sample rate. Args: file_path (str): The path to the wave file. target_sample_rate (int): The target sample rate to downsample to. Returns: str: A message indicating the success of the operation. """# Load the wave filesample_rate,data=wavfile.read(file_path)# Check if the target sample rate is the same as the originalifsample_rate==target_sample_rate:return"The file is already at the target sample rate."# Calculate the new number of samplesnew_num_samples=int(data.shape[0]*target_sample_rate/sample_rate)# Resample the datanew_data=sps.resample(data,number_of_samples)# Set the file path for outputdownsampled_file_path=file_path.replace(".wav",f"_downsampled_{target_sample_rate}.wav")# Save the downsampled filewavfile.write(downsampled_file_path,target_sample_rate,new_data.astype(data.dtype))# return a message indicating the success of the operationreturnf"File downsampled to {target_sample_rate} Hz: {downsampled_file_path}"
Warning
Before saving .wav file using scipy, make sure the dtype is int16.