Last active
June 9, 2024 09:34
-
-
Save tuchella/b0795fef73e2be3cda52c255fd166392 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 | |
import os.path as path | |
import math | |
import numpy as np | |
import sys | |
from scipy.io import wavfile | |
def main(): | |
data_dir = sys.argv[1] | |
dest = path.join(data_dir, "processed") | |
if not os.path.exists(dest): | |
os.makedirs(dest) | |
for i, file in enumerate(os.listdir(data_dir)): | |
filename = os.fsdecode(file) | |
if filename.endswith(".wav") or filename.endswith(".WAV"): | |
print("Processing " + filename + "...") | |
process(data_dir, filename, dest) | |
print("...done") | |
print("Processed files written to " + dest) | |
def process(data_dir, filename, dest): | |
sr, data = wavfile.read(path.join(data_dir, filename)) | |
TRIM_START = math.floor(0.035 * sr) | |
FADE_IN = math.floor(0.005 * sr) | |
FADE_OUT = math.floor(0.3 * sr) | |
LENGTH = math.floor(1.0 * sr) | |
data = np.delete(data, range(TRIM_START), 0) | |
data = data[:LENGTH,:] | |
env = np.ones(LENGTH) | |
env[:FADE_IN] = np.arange(0,FADE_IN) / FADE_IN | |
env[LENGTH-FADE_OUT:] = np.flip(np.arange(0,FADE_OUT) / FADE_OUT) | |
env = env.reshape(LENGTH,1) | |
data = np.multiply(data, env) | |
wavfile.write(path.join(dest, filename), sr, data.astype(np.float32)) | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("Usage: python reflektor_process.py [path_take_dir]") | |
exit(1) | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment