-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.py
166 lines (148 loc) · 5.59 KB
/
track.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import wave, os, pydub, datetime, struct
# Input file parameters (name & location)
input_filename_series = False
input_whole_folder = True
input_filename = '4-track.wav'
input_filename_list_range = range(6004, 6010)
input_filename_list = []
input_path = 'F:/03a clean untracked'
output_path = 'Q:/WTUL-1/03 clean'
silence_thresh = -45
audio_min_length = 30000
silence_min_length = 1500
def track_file(input_filename):
def write_header_new(initlength):
WAVE_FORMAT_PCM = 0x0001
assert not new._headerwritten
new._file.write('RIFF')
if not new._nframes:
new._nframes = initlength / (new._nchannels * new._sampwidth)
new._datalength = new._nframes * new._nchannels * new._sampwidth
new._form_length_pos = new._file.tell()
new._file.write(struct.pack('<L4s4sLHHLLHH',
36 + 24*len(cues) + 12 + new._datalength,
'WAVE',
'fmt ',
16,
WAVE_FORMAT_PCM,
new._nchannels,
new._framerate,
new._nchannels * new._framerate * new._sampwidth,
new._nchannels * new._sampwidth,
new._sampwidth * 8,
))
# Cues chunk wrapper (length: 12 + 24n)
new._file.write(struct.pack('<4sLL',
'cue ', #chunkID
24*len(cues) + 4, #chunkDataSize
len(cues))) #cuePointsCount
# Cue chunks (length: 24 each)
for num, cue in enumerate(cue_offsets):
new._file.write(struct.pack('<LL4sLLL',
num, #cuePointID
cue, #playOrderPosition (no playlist) **According to the spec, this should be 0. But who reads specs, anyway?**
'data', #dataChunkID (not silence)
0, #chunkStart (standard 'data' chunk)
0, #blockStart (uncompressed)
cue)) #sampleOffset (cue position)
print 'Cue', num, 'written @', cue, '/', new._nframes, '(' + str(round(cue/float(new._nframes)*100, 2)) + '%)'
# Data chunk header
new._data_length_pos = new._file.tell()
new._file.write(struct.pack('<4sL',
'data',
new._datalength))
new._headerwritten = True
i = 0
markers = [0]
# Add suffix for same-folder output (Can't overwrite)
if input_path == output_path:
output_filename = input_filename.split('.wav')[0]+'_tracked.wav'
else:
output_filename = input_filename
input_filename_fullpath = os.path.join(input_path, input_filename)
output_filename_fullpath = os.path.join(output_path, output_filename)
print 'BLOOOAAARRRRGHHGGGGHHHHH!!! (Please hold...)'
print input_filename_fullpath,'->', output_filename_fullpath
# pyDub open, amplitude stats
audio = pydub.AudioSegment.from_wav(input_filename_fullpath)
print 'audio.rms =', audio.rms,'=', round(audio.dBFS, 2), 'dBFS'
print 'audio.max =', audio.max
print 'audio.max_possible_amplitude =', audio.max_possible_amplitude
# Detect first non-silence
for ms_ord, ms in enumerate(audio):
if ms.dBFS > silence_thresh:
print 'First non-silent slice: %d:%02d' % (divmod(ms_ord / 1000, 60)), ms_ord, 'ms =', ms.rms, '(%.2f dB)' % (ms.dBFS)
break
# Detect tracks!
while True:
# Last track
if i + silence_min_length > len(audio):
current_chunk = audio[i:]
new_marker = len(audio)
markers.append(new_marker)
print 'Track', len(markers),'@', str(datetime.timedelta(milliseconds=new_marker))
break
else:
current_chunk = audio[i:i + silence_min_length]
if i + silence_min_length*2 > len(audio):
next_chunk = audio[i + silence_min_length:]
else:
next_chunk = audio[i + silence_min_length:i + silence_min_length*2]
# If silent
if current_chunk.dBFS < silence_thresh:
silent_chunk = '(Silent)'
if i - markers[-1] >= audio_min_length and not next_chunk.dBFS < silence_thresh:
new_marker = i + silence_min_length
markers.append(new_marker)
print 'Track', len(markers),'@', str(datetime.timedelta(milliseconds=new_marker))
else:
silent_chunk = ''
# Chunk dBFS stats
print str(datetime.timedelta(milliseconds=i))+' - '+str(datetime.timedelta(milliseconds=i+silence_min_length))+': '+str(round(current_chunk.dBFS, 2))+' dBFS', silent_chunk
i += silence_min_length
# Marker stats
print 'Markers:', markers
for num, marker in enumerate(markers):
# Last marker
if num == len(markers) - 1:
track_length = 0
else:
track_length = round(markers[num + 1] - markers[num], -3)
print 'Track', num+1, '@', str(datetime.timedelta(milliseconds=round(marker, -3))), '(length:', str(datetime.timedelta(milliseconds=track_length))+')'
# Write new file with markers
cues = markers
cue_offsets = [int(cue * 44.1) for cue in cues]
new = wave.open(output_filename_fullpath, 'wb')
new._write_header = write_header_new
new.setnchannels(audio.channels)
new.setsampwidth(audio.sample_width)
new.setframerate(audio.frame_rate)
new.setnframes(int(audio.frame_count()))
print '_headerwritten:', new._headerwritten
print '_datawritten:', new._datawritten
new.writeframesraw(audio._data)
print '_headerwritten:', new._headerwritten
print '_datawritten:', new._datawritten
new.close()
# Make input filename list
if input_filename_series and input_whole_folder:
print 'Series or folder. Can\'t have both. Sorry.'
raise SystemError
if input_filename_series:
for f in input_filename_list_range:
f = str(f).zfill(5)
for g in os.listdir(input_path):
if g.startswith(f) and g.endswith('.wav'):
input_filename_list.append(g)
print 'Input filenames (series):', input_filename_list
elif input_whole_folder:
for f in os.listdir(input_path):
if f.endswith('.wav'):
input_filename_list.append(f)
print 'Input filenames (folder):', input_filename_list
else:
input_filename_list = [input_filename]
# RUN THE TRAP
for each in input_filename_list:
print each
track_file(each)