Created
January 8, 2016 15:58
-
-
Save Jip-Hop/c69c10da6f5376502b51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = 'right_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 | |
# int64 will overflow and as a result clip the audo if the sum of the sample | |
# values is smaller than -9223372036854775808 or larger than 9223372036854775807 | |
newdata = np.zeros((max_size,), dtype=np.int64) | |
for wav in wav_list: | |
# resize the data to equal lengths and add them together | |
wav.resize((max_size,), refcheck=False) | |
newdata = newdata + wav | |
# average the audio | |
newdata = newdata / len(wav_list) | |
# Write the data | |
wavfile.write(os.path.join(os.getcwd(), audio_folder_name + '_average.wav'), rate, newdata.astype(dtype)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment