Created
January 8, 2016 15:59
-
-
Save Jip-Hop/c70319c0e0eb5bae0bc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, numpy as np | |
from scipy.io import wavfile | |
def show_info(aname, a): | |
print "Array", aname | |
print "shape:", a.shape | |
print "dtype:", a.dtype | |
print "min, max:", a.min(), a.max() | |
audio_folder_name = 'left_audio_track' | |
# Access all WAV files in directory | |
allfiles=os.listdir(os.path.join(os.getcwd(), audio_folder_name)) | |
wav_filenames = [filename for filename in allfiles if filename[-4:] in [".wav",".WAV"]] | |
rate = 0 | |
max_size = 0 | |
dtype = np.int32 # set as default | |
wav_list = [] | |
for filename in wav_filenames: | |
rate, data = wavfile.read(os.path.join(os.getcwd(), audio_folder_name, filename)) | |
# store the data for later | |
wav_list.append(data) | |
show_info(filename, data) | |
if (data.shape[0] > max_size): | |
max_size = data.shape[0] | |
dtype = data.dtype # override default | |
for wav in wav_list: | |
# resize the data to equal lengths, adding 0's where there was no sample | |
wav.resize((max_size,), refcheck=False) | |
# calculate the median | |
median = np.median(wav_list,axis = 0) | |
# Write the data | |
wavfile.write(os.path.join(os.getcwd(), audio_folder_name + '_median.wav'), rate, median.astype(dtype)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment