-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pitch_Shift.py
57 lines (48 loc) · 1.93 KB
/
Pitch_Shift.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
from __future__ import print_function
import pandas as pd
import numpy as np
import os, shlex,pandas as pd, subprocess
import json
import matplotlib.pyplot as plt
from glob import glob
import librosa as lr
import librosa.display
from os import path
from pydub import AudioSegment
from audiomentations import PitchShift
import acoustics
import soundfile as sf
from pydub import AudioSegment
from tqdm import tqdm
# Applying Pitch Shift
data_type='LA'
augment_type='PitchShift'
train_dataset_path = r'/scratch/projects/Datasets/ASVSpoof2019/'+data_type+'/ASVspoof2019_'+data_type+'_train/flac/'
audio_files=glob(train_dataset_path + "/*.flac")
path = r"/scratch/projects/Datasets/Augmented_Asvspoof2019/"+data_type+"/Bonafide/"
os.chdir(path)
if not os.path.exists(augment_type):
os.mkdir(augment_type)
print("Directory " , augment_type , " Created ")
else:
print("Directory " , augment_type , " already exists")
path=os.path.join(path,augment_type)
train_labels= []
train_audio=[]
print(" Reading training Samples & Labels")
with open('/scratch/projects/Datasets/ASVSpoof2019/'+data_type+'/ASVspoof2019_'+data_type+'_cm_protocols/ASVspoof2019.'+data_type+'.cm.train.trn.txt', 'r') as f:
for i,line in enumerate(tqdm(f)): #train_raw_labels
t_label = line.split(' ')
t_name = t_label[1]
t_lab = t_label[4].strip()
test_audio, sample_rate_test = lr.load(train_dataset_path+t_name+'.flac',sr=16000)
train_audio.append(test_audio)
if str(t_lab)=='spoof':
train_labels.append(1)
elif str(t_lab)=='bonafide':
train_labels.append(0)
augment = Compose([PitchShift()])
augmented_samples = augment(samples=test_audio, sample_rate=sample_rate_test)
print(t_name+'.flac')
sf.write(os.path.join(path,t_name+'.flac'), augmented_samples, sample_rate_test)
print("Done")