Last active
May 29, 2020 15:46
-
-
Save eamonnbell/a642fa41a7793a530ccca2fb3bb6fae3 to your computer and use it in GitHub Desktop.
Segments an audiofile in to chunks according to the result of some MIR algos
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 itertools | |
import os | |
import sys | |
SPLITFILE = 'splits.txt' | |
binaries = [ 'DBNBeatTracker', | |
'SpectralOnsetDetection', | |
'CNNOnsetDetector', | |
'DBNDownBeatTracker', | |
] | |
try: | |
INFILE = sys.argv[1] | |
BINARY = sys.argv[2] | |
except IndexError: | |
print('must be invoked with an INFILE and a BINARY') | |
print('allowed binaries: {} '.format(" ".join(binaries))) | |
print('$ python process.py INFILE BINARY') | |
quit() | |
BASENAME = os.path.splitext(INFILE)[0] | |
### COMPUTE ACTIVATIONS | |
binary = BINARY | |
extra_args = [] | |
if binary == 'DBNDownBeatTracker': | |
extra_args.append('--downbeats') | |
activate_cmd = "{} -v {} single {} -o {}".format(binary, " ".join(extra_args), INFILE, SPLITFILE) | |
os.system('rm splits.txt') | |
os.system(activate_cmd) | |
### SET THINGS UP | |
os.system('rm -rfi {}_{}'.format(BASENAME,binary)) | |
os.system('mkdir {}_{}'.format(BASENAME,binary)) | |
## sox forth.wav 10.wav trim 303.463 =353.790 | |
#COMMAND_TEMPLATE = "ffmpeg -i {} -acodec copy -ss {} -to {} {}/{}" | |
COMMAND_TEMPLATE = "sox {} {}_{}/{} trim {} ={}" | |
### GET THE SPLITS OUT OF THE FILE | |
with open(SPLITFILE) as f: | |
lines = f.readlines() | |
trimmed = [l.strip() for l in lines] | |
### ASSEMBLE THE COMMANDS FOR SPLITTING | |
i = 0 | |
commands = [] | |
while i < len(trimmed) - 1: | |
start, end = trimmed[i], trimmed[i+1] | |
fn = f"{i:04}{INFILE}" | |
cmd = COMMAND_TEMPLATE.format(INFILE, BASENAME, binary, fn, start, end) | |
commands.append(cmd) | |
i += 1 | |
### LIMIT THE SEGMENTS | |
print('found {} segments using {}. how many do you want?'.format(len(commands), binary)) | |
wanted_n = input('> ') | |
### EXECUTE THE SPLIT COMMANDS | |
for cmd in commands[:int(wanted_n)]: | |
os.system(cmd) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment