-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsamples_to_audio_sprite.py
77 lines (67 loc) · 2.48 KB
/
samples_to_audio_sprite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
import argparse
import inspect
import os
from pprint import pprint
import sys
from lib.audio_mixer import *
from lib.audio_utils import *
from lib.collection_utils import *
from lib.io_utils import *
from lib.math_utils import *
from lib.processing_utils import *
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILE", default="path/to/samples.csv", help="Input csv file")
parser.add_argument('-dir', dest="INPUT_DIR", default="path/to/audio/", help="Directory of input audio files")
parser.add_argument('-out', dest="OUT_AUDIO", default="output/sprite.mp3", help="Output audio file")
parser.add_argument('-data', dest="OUT_DATA", default="output/sprite.json", help="Output data file")
parser.add_argument('-sort', dest="SORT", default="", help="Query string to sort by")
parser.add_argument('-filter', dest="FILTER", default="", help="Query string to filter by")
parser.add_argument('-lim', dest="LIMIT", default=-1, type=int, help="Target total sample count, -1 for everything")
parser.add_argument('-reverb', dest="REVERB", default=0, type=int, help="Add reverb (0-100)")
parser.add_argument('-probe', dest="PROBE", action="store_true", help="Just show the stats")
a = parser.parse_args()
fieldNames, samples = readCsv(a.INPUT_FILE)
sampleCount = len(samples)
fxPad = 500
print("Found %s samples" % sampleCount)
if len(a.SORT) > 0:
samples = sortByQueryString(samples, a.SORT)
if len(a.FILTER) > 0:
samples = filterByQueryString(samples, a.FILTER)
sampleCount = len(samples)
print("%s samples after filtering" % sampleCount)
if a.LIMIT > 0 and sampleCount > a.LIMIT:
samples = samples[:a.LIMIT]
sampleCount = len(samples)
print("%s samples after limiting" % sampleCount)
ms = 0
pad = 10
instructions = []
jsonData = {}
for i, s in enumerate(samples):
fn = a.INPUT_DIR + s["filename"]
start = s["start"]
dur = s["dur"]
instruction = {
"filename": fn,
"start": start,
"dur": dur,
"ms": ms
}
if a.REVERB > 0:
instruction["reverb"] = a.REVERB
dur += fxPad
instructions.append(instruction)
jsonData[str(i)] = [ms, dur]
ms += pad + dur
totalDuration = ms
print("Total duration: %s" % formatSeconds(roundInt(totalDuration / 1000.0)))
if a.PROBE:
sys.exit()
if a.REVERB > 0:
mixAudio(instructions, totalDuration, a.OUT_AUDIO, sfx=True, fxPad=fxPad)
else:
mixAudio(instructions, totalDuration, a.OUT_AUDIO, sfx=False)
writeJSON(a.OUT_DATA, jsonData)