from pydub import AudioSegment
import soundfile
import pandas as pd
import csv
import re

def read_txt(file_path):
	'''
	Helper function to read indexes of train, valid and test dataset
	'''
    res = []
    f = open(file_path, "r")
    lines = f.readlines()
    for line in lines:
      number = int(line.strip())-1
      res.append(number)
    return res

def write_tsv(file_name, index_list):
	'''
	Helper function to write tsv files containing file path
	'''
    with open(file_name, "w") as f:
        print('~/yourpath/', file=f)

        for index in index_list:
            wav_path = 'data/'+str(index)+'.wav'
            wav_full_path = '~/yourpath/data/'+str(index)+'.wav'
            frames = soundfile.info(wav_full_path).frames
            print("{}\t{}".format(wav_path, frames), file=f)
    return

# Separate the whole wav file into small clips based on given sub.csv
csv_file = '~/yourpath/knnw/knnw_en_sub.csv'
wav_file = '~/yourpath/knnw/knnw_en_mono.wav'
trans_file = '~/yourpath/trans.txt'

orig_wav = AudioSegment.from_wav(wav_file)
with open(csv_file, mode='r') as f, open(trans_file, mode='w') as trans_f:
    csv_reader = csv.reader(f, delimiter=';')
    next(csv_reader)
    for row in csv_reader:
        id, start_time, end_time, text = row
        wav_file = '~/yourpath/data/'+str(id)+'.wav'
        new_wav = orig_wav[int(start_time):int(end_time)]
        new_wav = new_wav.set_frame_rate(16000)
        new_wav.export(wav_file, format="wav")
        print("{} {}".format(str(id), text), file=trans_f)

train_file = '~/yourpath/train.txt'
val_file = '~/yourpath/val.txt'
test_file = '~/yourpath/test.txt'

train_idx = read_txt(train_file)
val_idx = read_txt(val_file)
test_idx = read_txt(test_file)

train_tsv = '~/yourpath/train.tsv'
val_tsv = '~/yourpath/valid.tsv'
test_tsv = '~/yourpath/test.tsv'

write_tsv(train_tsv, train_idx)
write_tsv(val_tsv, val_idx)
write_tsv(test_tsv, test_idx)

2bdb66b64a335b8f871295a6e2c31fc60fdafb5d
from pydub import AudioSegment
import soundfile
import pandas as pd
import csv
import re

def read_txt(file_path):
	'''
	Helper function to read indexes of train, valid and test dataset
	'''
    res = []
    f = open(file_path, "r")
    lines = f.readlines()
    for line in lines:
      number = int(line.strip())-1
      res.append(number)
    return res

def write_tsv(file_name, index_list):
	'''
	Helper function to write tsv files containing file path
	'''
    with open(file_name, "w") as f:
        print('~/yourpath/', file=f)

        for index in index_list:
            wav_path = 'data/'+str(index)+'.wav'
            wav_full_path = '~/yourpath/data/'+str(index)+'.wav'
            frames = soundfile.info(wav_full_path).frames
            print("{}\t{}".format(wav_path, frames), file=f)
    return

# Separate the whole wav file into small clips based on given sub.csv
csv_file = '~/yourpath/knnw/knnw_en_sub.csv'
wav_file = '~/yourpath/knnw/knnw_en_mono.wav'
trans_file = '~/yourpath/trans.txt'

orig_wav = AudioSegment.from_wav(wav_file)
with open(csv_file, mode='r') as f, open(trans_file, mode='w') as trans_f:
    csv_reader = csv.reader(f, delimiter=';')
    next(csv_reader)
    for row in csv_reader:
        id, start_time, end_time, text = row
        wav_file = '~/yourpath/data/'+str(id)+'.wav'
        new_wav = orig_wav[int(start_time):int(end_time)]
        new_wav = new_wav.set_frame_rate(16000)
        new_wav.export(wav_file, format="wav")
        print("{} {}".format(str(id), text), file=trans_f)

train_file = '~/yourpath/train.txt'
val_file = '~/yourpath/val.txt'
test_file = '~/yourpath/test.txt'

train_idx = read_txt(train_file)
val_idx = read_txt(val_file)
test_idx = read_txt(test_file)

train_tsv = '~/yourpath/train.tsv'
val_tsv = '~/yourpath/valid.tsv'
test_tsv = '~/yourpath/test.tsv'

write_tsv(train_tsv, train_idx)
write_tsv(val_tsv, val_idx)
write_tsv(test_tsv, test_idx)
