Skip to content

Instantly share code, notes, and snippets.

@schollz
Created October 22, 2024 15:49
Show Gist options
  • Save schollz/e00339570bc5cea189e4595580e34f07 to your computer and use it in GitHub Desktop.
Save schollz/e00339570bc5cea189e4595580e34f07 to your computer and use it in GitHub Desktop.
A gist for aligning offsets in samples
import os
import subprocess
def trim_audio(input_file):
temp_file = "temp.wav"
os.system(f"sox {input_file} 1.wav norm")
cmd = "aubioonset -t 0.1 -H 256 -B 256 -O energy 1.wav"
onsets = subprocess.check_output(cmd, shell=True).decode("utf-8").split("\n")
first_onset = 0
for onset in onsets:
if onset:
onset_float = float(onset)
if onset_float > 0.005:
first_onset = onset_float
break
if first_onset > 0 and first_onset < 0.5:
print(f"sox {input_file} 1.wav trim {first_onset}")
os.system(f"sox {input_file} 1.wav trim {first_onset}")
os.system(f"sox 1.wav {input_file} fade 0.005")
def process_zip(zip_file):
print(f"Processing {zip_file}")
# unzip file
os.system(f"unzip -q {zip_file}")
# get all the wav files
wav_files = [f for f in os.listdir() if f.endswith(".wav")]
print(f"Found {len(wav_files)} wav files")
for wav_file in wav_files:
trim_audio(wav_file)
# remove 1.wav
os.remove("1.wav")
# overwrite the zip file
os.system(f"zip -q -r {zip_file} {' '.join(wav_files)}")
# remove all the wave files
for wav_file in wav_files:
os.remove(wav_file)
def main():
# collect all the zip files
zip_files = [f for f in os.listdir() if f.endswith(".zip")]
print(f"Found {len(zip_files)} zip files")
for zip_file in zip_files:
process_zip(zip_file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment