Created
July 9, 2017 06:43
-
-
Save aqzlpm11/251c11dd41792b613b7e1c1d62518521 to your computer and use it in GitHub Desktop.
python: cooledit2_labelling_helper. 把所有的wav合成一个,做好标记后,再分开。
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 librosa | |
import wave | |
from pathlib import Path | |
import numpy as np | |
import os | |
all_in_one_wav = "@all_in_one.wav" | |
all_in_one_wav_info = "@all_in_one.txt" | |
all_in_one_label = "@all_in_one_vad.txt" | |
label_file = "@label.txt" | |
def generate_all_in_one_wav(): | |
sample_rate = None | |
all_wav = [] | |
record = [] | |
for wav in Path('.').glob("*.wav"): | |
wav = str(wav) | |
if wav == all_in_one_wav: | |
continue | |
if sample_rate is None: | |
with wave.open(wav, 'r') as wav_file: | |
sample_rate = wav_file.getframerate() | |
y, _ = librosa.load(wav, sample_rate) | |
# print(type(y), y.shape) | |
start_time = len(all_wav) / sample_rate | |
all_wav += list(y) + [100, -100]*100 | |
end_time = len(all_wav) / sample_rate | |
record.append([wav, start_time, end_time]) | |
all_wav = np.array(all_wav) | |
# print(type(all_wav), all_wav.shape) | |
librosa.output.write_wav(all_in_one_wav, all_wav, sample_rate) | |
with open(all_in_one_wav_info, 'w') as f: | |
for r in record: | |
f.write(" ".join([str(i) for i in r]) + "\n") | |
def generate_all_in_one_all_in_one_label(): | |
if not os.path.exists(all_in_one_label): | |
f = open(all_in_one_label, 'a') | |
f.close() | |
def generate_label(): | |
res = {} | |
record = [] | |
for line in open(all_in_one_wav_info): | |
wav, start_time, end_time = line.strip().split() | |
record.append([wav, float(start_time), float(end_time)]) | |
for line in open(all_in_one_label): | |
line = line.strip() | |
if len(line) == 0: | |
continue | |
start_time, end_time = [float(i) for i in line.split()] | |
for r in record: | |
if r[1] <= start_time and start_time <= r[2]: | |
if r[1] <= end_time and end_time <= r[2]: | |
s = start_time - r[1] | |
e = end_time - r[1] | |
if r[0] not in res: | |
res[r[0]] = [] | |
res[r[0]].append([s, e]) | |
else: | |
print('ERROR cross wav!') | |
print('all_in_one_label: ', start_time, end_time) | |
print('wav_duration:', r) | |
assert(0) | |
with open(label_file, 'w') as f: | |
for r in res: | |
f.write(r + "\n") | |
for info in res[r]: | |
f.write("{:.2f} {:.2f}\n".format(info[0], info[1])) | |
f.write('.\n') | |
if __name__ == '__main__': | |
generate_all_in_one_wav() | |
generate_all_in_one_all_in_one_label() | |
generate_label() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment