ryu преди 2 години
ревизия
527d69b896
променени са 5 файла, в които са добавени 805 реда и са изтрити 0 реда
  1. 350 0
      audio_augmentation/aug_multi.py
  2. 170 0
      audio_augmentation/script/failure_check.py
  3. 194 0
      audio_augmentation/script/postprocess.py
  4. 90 0
      audio_augmentation/script/script.py
  5. 1 0
      data_process

+ 350 - 0
audio_augmentation/aug_multi.py

@@ -0,0 +1,350 @@
+import argparse
+import numpy
+import random
+import os
+import time
+import math
+import glob
+import soundfile
+from scipy import signal
+from scipy.io import wavfile
+import soundfile as sf
+import librosa
+
+import torch
+# import torchaudio
+import torchaudio.transforms as T
+import torch.nn.functional as F
+
+from tqdm import tqdm
+import multiprocessing as mp
+from multiprocessing import Pool
+from typing import Any, Coroutine, Iterable, List, Tuple
+
+def loadWAV(filepath, max_audio, evalmode=False, num_eval=10):
+    """
+    Ignore 'evalmode' and 'num_eval' argument. We not use in this code.
+    """
+
+    # Read wav file and convert to torch tensor
+    audio, sample_rate = soundfile.read(filepath)
+    audiosize = audio.shape[0]
+
+    if audiosize <= max_audio:
+        shortage    = max_audio - audiosize + 1 
+        audio       = numpy.pad(audio, (0, shortage), 'wrap') # repeat wav to extend the wav length to max length
+        audiosize   = audio.shape[0]
+
+    if evalmode:
+        startframe = numpy.linspace(0,audiosize-max_audio,num=num_eval)
+    else:
+        startframe = numpy.array([numpy.int64(random.random()*(audiosize-max_audio))])
+    
+    feats = []
+    if evalmode and max_frames == 0:
+        feats.append(audio)
+    else:
+        for asf in startframe:
+            feats.append(audio[int(asf):int(asf)+max_audio])
+
+    feat = numpy.stack(feats,axis=0).astype(numpy.float64)
+
+    return feat;
+
+
+def loadWav_samplerate(filepath, sample_rate, transform=None):
+    waveform, _sample_rate = soundfile.read(filepath)
+
+    if _sample_rate != sample_rate:
+        t = torch.from_numpy(waveform).type(torch.float32)
+        if transform is None:
+            transform = T.Resample(_sample_rate, sample_rate)
+        waveform = transform(t).numpy()
+
+    return waveform
+
+
+    
+
+def augment_2ogg(root_path, file_path, sample_rate = 16000, args = None):
+    
+    sub_path = file_path.rsplit('.', 1)[0].split(root_path)[-1] + '.ogg'
+
+    conv_list = ['ogg']
+    conv_list.extend([f'aug{i+1}' for i in range(4)])
+
+    org = None
+
+    for idx, conv in enumerate(conv_list):
+        conv_path = root_path + conv + '/' + sub_path
+
+        if os.path.isfile(conv_path):
+            continue
+        
+        os.makedirs(conv_path.rsplit('/', 1)[0], exist_ok=True)
+        
+        if org is None:
+            org = loadWav_samplerate(file_path, sample_rate)
+
+        if conv == 'ogg':
+            sf.write(conv_path, org, sample_rate)
+        else:
+            aug_audio = augmentation.augment_wav_type(org, idx)
+            if len(aug_audio.shape) >= 2:
+                aug_audio = aug_audio.squeeze(0)
+            
+            sf.write(conv_path, aug_audio, sample_rate)
+    
+
+class AugmentWAV(object):
+    def __init__(self, data_list, dest_dir, musan_path, rir_path, log_interval=100, **kwargs):
+        self.data_list = data_list
+        self.dest_dir = dest_dir
+        self.log_interval = log_interval
+
+        self.noisetypes = ['noise','speech','music']
+
+        self.noisesnr   = {'noise':[0,15],'speech':[13,20],'music':[5,15]}
+        self.numnoise   = {'noise':[1,1], 'speech':[3,7],  'music':[1,1] }
+        self.noiselist  = {}
+
+        augment_files = glob.glob(os.path.join(musan_path,'*/*/*.wav'))
+
+        for file in augment_files:
+            if not file.split('/')[-3] in self.noiselist:
+                self.noiselist[file.split('/')[-3]] = []
+            self.noiselist[file.split('/')[-3]].append(file)
+
+        self.rir_files = glob.glob(os.path.join(rir_path,'**/*.wav'), recursive=True)
+
+        # print("in class, rir len = ", len(self.rir_files))
+        # exit()
+
+    def augment_wav_type(self, audio, augtype):
+        if augtype == 1:
+            audio   = self.reverberate(audio)
+        elif augtype == 2:
+            audio   = self.additive_noise('music',audio)
+        elif augtype == 3:
+            audio   = self.additive_noise('speech',audio)
+        elif augtype == 4:
+            audio   = self.additive_noise('noise',audio)
+
+        return audio
+
+
+
+    async def augment_2ogg(self, file_path, root_path, sample_rate=16000):
+
+        org, samplerate = sf.read(file_path)
+        # print(org.shape)
+
+        sub_path = file_path.rsplit('.', 1)[0].split(root_path)[-1] + '.ogg'
+
+        ogg_path = root_path + 'ogg/' + sub_path
+        if os.path.isfile(ogg_path):
+            return
+        
+        os.makedirs(ogg_path.rsplit('/', 1)[0], exist_ok=True)
+        sf.write(ogg_path , org, sample_rate)
+
+        aug_audios = self.augemnt_all(org)
+        for idx, aa in enumerate(aug_audios):
+            aug_path = f"{root_path}aug{idx+1}/{sub_path}"
+            os.makedirs(aug_path.rsplit('/', 1)[0], exist_ok=True)
+
+            if len(aa.shape) >= 2:
+                aa = aa.squeeze(0)
+
+            soundfile.write(aug_path, aa, sample_rate)
+
+
+    def run(self):
+        count = 0 
+        for i, data_path in enumerate(self.data_list):
+            count += 1
+
+            filename = os.path.basename(data_path) # get string 'filename.ext' from 'src_dir/filename.ext'
+            dest_path = os.path.join(self.dest_dir, filename) # make 'dest_dir/filename.ext'
+
+            audio, sample_rate = soundfile.read(data_path)
+
+            aug_audio = self.augment_wav(audio)
+
+            if len(aug_audio.shape) >= 2:
+                aug_audio = aug_audio.squeeze(0)
+
+            soundfile.write(dest_path, aug_audio, sample_rate)
+
+            if i % self.log_interval == 0:
+                print(f'{count}/{len(self.data_list)}...')
+
+
+    def augment_wav(self, audio):
+        augtype = random.randint(0,4) #  augment audio with 4 noise type randomly.
+        if augtype == 1:
+            audio   = self.reverberate(audio)
+        elif augtype == 2:
+            audio   = self.additive_noise('music',audio)
+        elif augtype == 3:
+            audio   = self.additive_noise('speech',audio)
+        elif augtype == 4:
+            audio   = self.additive_noise('noise',audio)
+
+        return audio
+
+    def augemnt_all(self, audio):
+        arr = []
+
+        arr.append(self.reverberate(audio))
+        arr.append(self.additive_noise('music',audio))
+        arr.append(self.additive_noise('speech',audio))
+        arr.append(self.additive_noise('noise',audio))
+
+        return arr
+
+
+    def additive_noise(self, noisecat, audio):
+        max_audio = audio.shape[0]
+        clean_db = 10 * numpy.log10(numpy.mean(audio ** 2)+1e-4) 
+
+        numnoise    = self.numnoise[noisecat]
+        noiselist   = random.sample(self.noiselist[noisecat], random.randint(numnoise[0],numnoise[1]))
+
+        noises = []
+
+        for noise in noiselist:
+            noiseaudio  = loadWAV(noise, max_audio, evalmode=False)
+            noise_snr   = random.uniform(self.noisesnr[noisecat][0],self.noisesnr[noisecat][1])
+            noise_db = 10 * numpy.log10(numpy.mean(noiseaudio[0] ** 2)+1e-4) 
+            noises.append(numpy.sqrt(10 ** ((clean_db - noise_db - noise_snr) / 10)) * noiseaudio)
+
+        return numpy.sum(numpy.concatenate(noises,axis=0),axis=0,keepdims=True) + audio
+
+
+    def reverberate(self, audio, scale=1.0):
+        max_audio = audio.shape[0]
+
+        rir_file    = random.choice(self.rir_files)
+        
+        rir, fs     = soundfile.read(rir_file)
+        rir         = numpy.expand_dims(rir.astype(numpy.float64),0)
+        rir         = rir / numpy.sqrt(numpy.sum(rir**2))
+        
+        rirsize = rir.shape[1]
+
+        if rirsize >= max_audio: # If rir size is longer than target audio length
+            rir = rir[:,:max_audio]
+
+        if len(rir.shape) >= 3 and rir.shape[2] > 1:
+        #     rir = rir[:,:,0]
+            rir = librosa.to_mono(rir.squeeze().T)
+            rir = numpy.expand_dims(rir,0)
+
+        # print(rir.shape, type(rir))
+       
+
+        return signal.convolve(numpy.expand_dims(audio,0), rir, mode='full')[:,:max_audio]
+
+async def augment_func(root_dir, path):
+    await augment_2ogg(root_dir, path)
+
+
+async def aprogress(tasks: Iterable[Coroutine], **pbar_kws: Any) -> Any:
+    if not tasks:
+        return -1
+    
+    pbar = tqdm(asyncio.as_completed(tasks), total=len(tasks), **pbar_kws)
+
+    for task in pbar:
+        await task
+        pbar.update()
+
+def str2bool(v):
+    if isinstance(v, bool):
+       return v
+    if v.lower() in ('yes', 'true', 't', 'y', '1'):
+        return True
+    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
+        return False
+    else:
+        raise argparse.ArgumentTypeError('Boolean value expected.')
+
+
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--filestxt',       default='files.txt', type=str, help='The txt with absolute path of files')
+    parser.add_argument('--dest-dir',       default='output', type=str, help='The destination save path of augmented audio file')
+    parser.add_argument('--musan-path',     default='musan_split/', type=str, help='musan file directory')
+    parser.add_argument('--rir-path',       default='simulated_rirs/', type=str, help='rir file directory')
+    parser.add_argument('--log-interval',   default=50,   type=int)
+
+    parser.add_argument('--root-dir',   default='/mnt/data/',   type=str, help='setting to the root directory')
+    parser.add_argument('--use-list',   default=True,   type=str2bool, help='use a files.txt')
+    
+    args = parser.parse_args()
+
+    # with open(args.filestxt, "r") as f:
+    #     args.data_list = f.read().splitlines()
+
+    args.data_list = []
+
+    augmentation = AugmentWAV(**vars(args))
+
+    # for path in glob.glob(os.path.join(args.root_dir,'**/*.wav'), recursive=True):
+    #     print(path)
+    # print(args.root_dir)
+    # exit()
+
+
+    ### sample test
+    # org = loadWav_samplerate('/root/project/speech_server/sample/9040_5009_5009_9024663400_20220427110527.wav', 16000)
+    # sf.write('/root/project/speech_server/sample/sample.ogg', org, 16000)
+
+    # exit()
+
+    if args.root_dir[-1] != '/':
+        args.root_dir += '/'
+        print(args.root_dir)
+    
+    files_txt = 'files.txt'
+    augment_files = []
+
+    if args.use_list and os.path.exists(args.root_dir + files_txt):
+        with open(args.root_dir + files_txt, 'r') as f:
+            for line in f:
+                augment_files.append(line.split('\n')[0])
+    else:
+        augment_files = glob.glob(os.path.join(args.root_dir,'**/*.wav'), recursive=True)
+        if args.use_list:
+            with open(args.root_dir + files_txt, 'w') as f:
+                for item in augment_files:
+                    f.write(f'{item}\n')
+
+    print("augment len = ", len(augment_files))
+    
+    num_cores = mp.cpu_count()
+    def work_func(path):
+        augment_2ogg(args.root_dir, path, 16000, args)
+
+
+    try:
+        pool = Pool((num_cores)//2)
+
+        with tqdm(total=len(augment_files)) as pbar:
+            for _ in tqdm(pool.imap_unordered(work_func, augment_files)):
+                pbar.update()
+
+        # pool.map(work_func, augment_files)
+
+        pool.close()
+        pool.join()
+        print("Script complete!") 
+    except KeyboardInterrupt:
+        pool.close()
+        pool.join()
+
+    
+
+    

+ 170 - 0
audio_augmentation/script/failure_check.py

@@ -0,0 +1,170 @@
+from tqdm import tqdm
+from postprocess import postprocess
+import glob
+import os
+import re
+import re
+
+from datetime import datetime
+now = datetime.now()
+time_str = now.strftime('%Y_%m_%d_%H_%M_%S')
+
+## none 이면 저장 안됨.
+def broadcast_none_check():
+    total_txt = "/root/mnt/data_aug/audio_augmentation/script/total.txt"
+    failure_txt = f"broadcast_none_{time_str}.txt"
+    ogg_path = '/root/sata/broadcast/split/data'
+    total_ogg_txt = 'broadcast_total_ogg.txt'
+
+    total_txt_set = set()
+    ogg_set = set()
+
+    failure = []
+    cnt = 0
+
+    def split1_split2_compare():
+        failure_txt = f"broadcast_split1_split2_compare_{time_str}.txt"
+        ogg_files = glob.glob(os.path.join('/root/sata/broadcast_split/data', '**/*.ogg'), recursive=True)
+        for ogg in tqdm(ogg_files):
+            txt_file = ogg.strip().replace("ogg", "txt").replace("broadcast_split", "broadcast/split")
+
+            if not os.path.isfile(txt_file):
+                failure.append([ogg, txt_file])
+
+        with open(failure_txt, 'w+') as ft:
+            for fail in failure:
+                ft.write("\t".join(fail) + "\n")
+        print("len failire = ", len(failure))
+
+    
+    def split2_split1_compare():
+        failure_txt = f"broadcast_split2_split1_compare_{time_str}.txt"
+
+        txt_files = glob.glob(os.path.join('/root/sata/broadcast/split/data', '**/*.txt'), recursive=True)
+        for txt in tqdm(txt_files):
+            ogg_file = txt.strip().replace("txt", "ogg").replace("broadcast/split", "broadcast_split" )
+
+            if not os.path.isfile(ogg_file):
+                failure.append([txt, ogg_file])
+
+        with open(failure_txt, 'w+') as ft:
+            for fail in failure:
+                ft.write("\t".join(fail) + "\n")
+        print("len failire = ", len(failure))
+
+
+    
+    split2_split1_compare()
+    return
+
+def broadcast_special_check():
+    failure = []
+    failure_txt = f"broadcast_special_check{time_str}.txt"
+    pattern_1 = r'[%$#]'
+
+    txt_files = glob.glob(os.path.join('/root/sata/broadcast/split/data', '**/*.txt'), recursive=True)
+    for txt in tqdm(txt_files):
+        with open(txt, 'r') as t:
+            tt = t.read().strip()
+            tt = tt.strip()
+            tmp = re.search(pattern=pattern_1, string = tt)
+            if tmp:
+                failure.append([txt, tt])
+
+    with open(failure_txt, 'w+') as ft:
+        for fail in failure:
+            ft.write("\t".join(fail) + "\n")
+    print("len failire = ", len(failure))
+
+def broadcast_post_check():
+    failure_list = []
+    for source_txt in tqdm.tqdm(glob.glob(os.path.join('/root/sata/broadcast/split/data', '**/*.txt'), recursive=True)):
+        with open(source_txt, 'r') as f:
+            content = f.read().strip()
+            post = postprocess(content)
+
+            if post is None:
+                failure_list.append([source_txt, content])
+
+    failure_txt = f"broadcast_post.txt"
+
+    with open(failure_txt, 'w+') as ft:
+        for fail in failure_list:
+            ft.write("\t".join(fail) + "\n")
+
+    print("len = ", len(failure_list))
+
+def broadcast_post_check():
+    total_txt = "/root/mnt/data_aug/audio_augmentation/script/total.txt"
+    failure_txt = f"broadcast_none_{time_str}.txt"
+    ogg_path = '/root/sata/broadcast_split/data'
+    total_ogg_txt = 'broadcast_total_ogg.txt'
+
+    total_txt_set = set()
+    ogg_set = set()
+
+    failure = []
+    cnt = 0
+
+    try:
+        with open(total_ogg_txt, 'r') as tf:
+            for ogg in tqdm(tf):
+                ogg = ogg.strip()
+                txt_file = ogg.replace('ogg', 'txt')
+
+                if not os.path.isfile(txt_file):
+                    failure.append(ogg)
+                else:
+                    cnt += 1
+    except:
+        tf.close()
+    finally:
+        with open(failure_txt, 'w+') as ft:
+            for fail in failure:
+                ft.write(fail + "\n")
+
+        print('cnt', cnt, len(failure))
+
+    return
+
+
+def freetalk_special_check():
+    ogg_path = '/root/nas/data/freetalk/ogg'
+
+    total_txt = 'freetalk_total.txt'
+    failure_txt = f"freetalk_failure_{time_str}.txt"
+    failure = []
+
+    pattern = r"[^\uAC00-\uD7A30-9a-zA-Z\s]" ## 특문
+
+    with open(total_txt, 'w+') as tf:
+        for txt_file in tqdm(glob.glob(os.path.join(ogg_path, '**/*.txt'), recursive=True), leave=True):
+            tf.write(f"{txt_file}\n")
+            with open(txt_file, 'r') as f:
+                content = f.read().strip()
+                
+            
+            exist = re.search(pattern=pattern, string=content)
+            
+            if exist:
+                failure.append((file, content))
+
+            if idx == 100:
+                break
+
+    if len(failure) >= 1:
+        with open(failure_txt, "w+") as f:
+            for _failure in failure:
+                f.write(f"{' '.join(_failure)}\n")
+
+    return
+            
+
+# print('test')
+# freetalk_special_check()
+    
+
+if __name__ == '__main__':
+    # broadcast_none_check()
+    broadcast_special_check()
+    # freetalk_special_check()

+ 194 - 0
audio_augmentation/script/postprocess.py

@@ -0,0 +1,194 @@
+import re
+import glob
+import time
+import tqdm
+from datetime import datetime
+from shutil import copyfile
+import os
+
+a1 = "머리를 (()) 그담에 뭐 하트도 날리고 뽀뽀도 해주고 윙크도 해주고 토닥토닥 안아주고 이 두 가지를 적절하게 잘 사용하면 굉장히 인제 조금 칭찬 방법이 되는 거죠."
+a2 = "니가 이반에서 제일 머리가 좋아 그런 거는 쪼끔 안 좋습니다 왜냐면 인제 자기보다 예쁜 아이가 있는 다른 반에 가지 않을려고 {laughing} 다른 친구보다 다른 형제자매들 보다 훨씬 낫다는 식의 비교 칭찬은 가급적 하지 않는 것이 좋습니다."
+a3 = "그니까 아까 왜 너 얼마나 잘할 거 같애 칠십 퍼센트요 팔십 퍼센트요 그니까 (아이 캔 두잇)(i can do it 나 할 수 있을 거 같애요 라고 하는 건데요."
+a4 = f"    "
+a = [a1, a2, a3, a4]
+
+
+pattern_1 = r'\(\(\)\)' ## (())
+pattern_2 = r'\{(\w*)\}' ## {@@@}
+pattern_3 = r'\((\w*)\)' ## (@@@) 찾기
+
+pattern_4 = r'[\(\)\{\}]' ## (, ), {, } 찾기
+pattern_4_1 = r'&([\w|-]*)&' ## &(id-id123)& 찾기
+
+pattern_5 = r"[^\uAC00-\uD7A30-9a-zA-Z\s]" ## 특문 repl=' '
+pattern_6 = r" {2,}" ## 빈 문자열 2개 이상
+
+
+def postprocess(origin):
+    _a = re.sub(pattern=pattern_1, repl='', string = origin)
+    _a = re.sub(pattern=pattern_2, repl='', string = _a)
+    _a = re.sub(pattern=pattern_3, repl='', string = _a)
+    tmp = re.search(pattern=pattern_4, string = _a)
+    if tmp:
+        return None
+    
+    tmp = re.search(pattern=pattern_4_1, string = _a)
+    if tmp:
+        return None
+
+    _a = re.sub(pattern=pattern_5, repl = " ", string = _a)
+    _a = re.sub(pattern=pattern_6, repl = " ", string = _a)
+    _a = _a.strip()
+
+    if len(_a) == 0:
+        return None
+    else:
+        return _a
+
+# test = '{laughing} 기억하고 있어야지 후생에도 혹시 {laughing} (()) (()) (광)'
+# test = '저희 음악 한 곡 듣고 와서 조금 더 마지막 사연 이야기 나눠볼게요.여러분의 의견도 많이 보내주시길 바래요.&company-name225& &tel-num1& 오십 원 혹은 백 원의 유료문자'
+# test = '저희 음악 한 곡 듣고 와서 조금 더 마지막 사연 이야기 나눠볼게요.여러분의 의견도 많이 보내주시길 바래요.'
+# print(list(map(postprocess, a)))
+# print(re.sub(pattern=pattern_1, repl='', string = test))
+# exit()
+
+root_dir = "/root/sata/broadcast_split/data/"
+total_txt = "/root/mnt/data_aug/audio_augmentation/script/total.txt"
+
+# with open(total_txt, 'w+') as tf:
+#     for idx, content in enumerate(a):
+#         content = content.strip()
+    
+#         post = postprocess(content)
+#         if post is not None:
+#             print(content + "\n  -->  " + post)
+#             tf.write(post + "\n")
+
+
+# exit()
+
+
+## 1 = broadcast_split
+## 2 = broadcast/split 
+## 1에 있는 ogg를 기준으로, txt2를 가져오고, 이를 후처리 해서 txt1에 적용함.
+def broadcast_move_split2_split1(time_str):
+
+    total_txt_file = '/root/mnt/data_aug/audio_augmentation/script/txt/broadcast_total_2.txt'
+    ## for test
+    # total_txt_file = '/root/mnt/data_aug/audio_augmentation/script/txt/broadcast_total_test.txt'
+    
+    ogg_path = '/root/sata/etc/broadcast_split/data'
+    ## for text
+    # ogg_path = '/root/mnt/data_aug/audio_augmentation/sample/broadcast_split'
+
+    post_none_list = []
+    not_exist_list = []
+    ogg_list = []
+
+    print('glob start !!')
+    if os.path.isfile(total_txt_file):
+        with open(total_txt_file, 'r') as f:
+            for ogg in f:
+                tt = ogg.strip()
+                ogg_list.append(tt)
+    else:
+        ogg_list = glob.glob(os.path.join(ogg_path, '**/*.ogg'), recursive=True)
+        with open(total_txt_file, 'w+') as f:
+            for ogg in ogg_list:
+                f.write(ogg + '\n')
+        
+    print('glob end !!', len(ogg_list))
+
+    # return
+
+    for source_ogg in tqdm.tqdm(ogg_list):
+        source_ogg = source_ogg.strip()
+        
+        split1_txt_file = source_ogg.strip().replace("ogg", "txt")
+        split2_txt_file = source_ogg.strip().replace("ogg", "txt").replace("broadcast_split", "broadcast/split")
+
+        if not os.path.isfile(split2_txt_file):
+            not_exist_list.append([source_ogg, split2_txt_file])
+        else:
+            with open(split2_txt_file, 'r') as f:
+                content = f.read().strip()
+
+            post = postprocess(content)
+
+            with open(split1_txt_file, 'w') as f:
+                if post is None:
+                    post_none_list.append([split2_txt_file, content])
+                    if os.path.isfile(split1_txt_file):
+                        os.remove(split1_txt_file)
+                    tmp_ogg = split1_txt_file.replace('txt', 'ogg')
+                    if os.path.isfile(tmp_ogg):
+                        os.remove(tmp_ogg)
+                else:
+                    f.write(post + '\n')
+
+
+    if post_none_list:
+        print('postprocess none = ', len(post_none_list))
+        txt = f'log/post_none_{time_str}.txt'
+
+        with open(txt, 'w+') as f:
+            for item in post_none_list:
+                f.write("\t".join(item) + "\n")
+    
+    if not_exist_list:
+        print('not exist = ', len(not_exist_list))
+        txt = f'log/not_exist_{time_str}.txt'
+
+        for item in post_none_list:
+            with open(txt, 'w+') as f:
+                f.write("\t".join(item) + "\n")
+
+    
+    
+            
+            
+
+
+if __name__ == '__main__':
+
+    now = datetime.now()
+    time_str = now.astimezone().strftime('%Y_%m_%d_%H_%M_%S')
+
+    # print(time_str)
+    # # exit()
+
+    broadcast_move_split2_split1(time_str)
+
+    # failure = []
+    # failure_txt = f"broadcast_failure_{time_str}.txt"
+
+    # with open(total_txt, 'w+') as tf:
+    #     for file in tqdm.tqdm(glob.glob(f'{root_dir}**/*.txt', recursive=True)):
+    #         content = ''
+    #         with open(file, 'r') as f:
+    #             content = f.read().strip()
+            
+    #         post = postprocess(content)
+    #         # print(str(idx) + " : " + content + "\n  -->  " + post)
+
+    #         with open(file, 'w') as f:
+                    
+    #             if post is not None:
+    #                 # tf.write(file + "\n")
+    #                 # f.write(post)
+    #                 pass
+    #             else:
+    #                 failure.append([file, content, post])
+    
+    # # print(failure)
+    # print(len(failure))
+
+    # with open(failure_txt, 'w+') as ft:
+    #     for fail in tqdm.tqdm(failure):
+    #         if fail[2] is None: fail[2] = 'None'
+
+    #         ft.write("\t".join(fail) + "\n")
+            
+#         else:
+#             print(file)
+#             cnt += 1

+ 90 - 0
audio_augmentation/script/script.py

@@ -0,0 +1,90 @@
+import json
+import os
+from tqdm import tqdm
+import multiprocessing as mp
+from multiprocessing import Pool
+import soundfile as sf
+
+import numpy as np
+
+silence, sr = sf.read('/root/nas/data/broadcast/whitenoise_500ms.wav')
+root_dir = '/root/nas/data/broadcast/'
+output_dir = '/root/sata/broadcast/'
+
+def work_func(path):
+    json2txt_broadcast( path, root_dir, output_dir, silence )
+
+def handle_broadcast():
+    
+
+    files_txt = 'files.txt'
+
+    json_files = []
+
+    if os.path.exists(root_dir + files_txt):
+        with open(root_dir + files_txt, 'r') as f:
+            for line in f:
+                json_files.append(line.split('\n')[0])
+    
+
+    num_cores = mp.cpu_count()
+
+    try:
+        pool = Pool(processes = num_cores)
+
+        with tqdm(total=len(json_files)) as pbar:
+            for _ in tqdm(pool.imap_unordered(work_func, json_files)):
+                pbar.update()
+
+        pool.close()
+        pool.join()
+        print("Script complete!") 
+    except KeyboardInterrupt:
+        pool.close()
+        pool.join()
+
+
+## wav_path : wav 파일 경로
+## root_path : input dir 경로
+## output_path : output dir 경로
+## silence : 앞뒤에 추가할 묵음
+def json2txt_broadcast(wav_path, root_path, output_path, silence):
+
+    json_path = wav_path.replace("원천데이터", "라벨링데이터").replace("wav", "json")
+
+    split_root_path = output_dir + "split/" + json_path.split(root_path)[1].rsplit('/', 1)[0] + "/" + json_path.rsplit('/', 1)[1].split('.json')[0] + "/"
+    # print(split_root_path)
+    # print(json_path)
+    # return
+
+    with open(json_path, 'r') as f:
+        json_data = json.load(f) 
+
+    os.makedirs(split_root_path, exist_ok=True)
+
+    # original_sound, sr = sf.read(wav_path)
+
+    for utter in json_data["utterance"]:
+        data = utter["original_form"]
+        start = float(utter['start'])
+        end = float(utter['end'])
+        utter_id = utter['id'].replace(".", "_")
+
+        with open(f"{split_root_path}{utter_id}.txt", 'w+') as f:
+            f.write(f'{data}\n')
+
+        # if not os.path.isfile(f"{split_root_path}{utter_id}.txt"):
+        #     with open(f"{split_root_path}{utter_id}.txt", 'w+') as f:
+        #         f.write(f'{data}\n')
+
+        # if not os.path.isfile(f"{split_root_path}{utter_id}.ogg"):
+        #     # sound = original_sound[int(sr * start) : int(sr * end)+1]
+        #     sound = np.append(silence, original_sound[int(sr * start) : int(sr * end)+1])
+        #     sound = np.append(original_sound[int(sr * start) : int(sr * end)+1], silence)
+
+        #     sf.write(f"{split_root_path}{utter_id}.ogg", sound, sr, format='ogg')
+
+
+        
+if __name__ == '__main__':
+    handle_broadcast()

+ 1 - 0
data_process

@@ -0,0 +1 @@
+Subproject commit 12ef9108332c8a49143aad6dddc37d72e14fc824