-
Notifications
You must be signed in to change notification settings - Fork 3
/
wav_converter.py
138 lines (122 loc) · 4.18 KB
/
wav_converter.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
import multiprocessing
import os
import time
from functools import partial
from os.path import join
import numpy as np
import psutil
import resampy
import soundfile as sf
from stepcovnet import utils
def convert_file(
input_path: str,
output_path: str,
sample_frequency: int,
verbose: bool,
file_name: str,
):
try:
new_file_name = utils.standardize_filename(utils.get_filename(file_name, False))
if verbose:
print("Converting " + file_name)
file_input_path = join(input_path, file_name)
file_output_path = join(output_path, new_file_name + ".wav")
input_audio_data, input_audio_sample_rate = sf.read(file_input_path)
if input_audio_data.shape[1] > 1:
input_audio_data = np.mean(input_audio_data, axis=1)
else:
input_audio_data = np.squeeze(input_audio_data)
if input_audio_sample_rate != sample_frequency:
input_audio_data = resampy.resample(
input_audio_data,
sr_orig=input_audio_sample_rate,
sr_new=sample_frequency,
)
sf.write(file_output_path, input_audio_data, sample_frequency)
except Exception as ex:
if verbose:
print("Failed to convert %s: %r" % (file_name, ex))
def run_process(
input_path: str, output_path: str, sample_frequency: int, cores: int, verbose: bool
):
if os.path.isfile(input_path):
convert_file(
os.path.dirname(input_path),
output_path,
sample_frequency,
verbose,
utils.get_filename(input_path),
)
else:
file_names = utils.get_filenames_from_folder(input_path)
func = partial(convert_file, input_path, output_path, sample_frequency, verbose)
with multiprocessing.Pool(cores) as pool:
pool.map_async(func, file_names).get()
def wav_converter(
input_path: str,
output_path: str,
sample_frequency: int = 16000,
cores: int = 1,
verbose_int: int = 0,
):
start_time = time.time()
if verbose_int not in [0, 1]:
raise ValueError(
"%s is not a valid verbose input. Choose 0 for none or 1 for full"
% verbose_int
)
verbose = True if verbose_int == 1 else False
if not os.path.isdir(output_path):
print("Wavs output path not found. Creating directory...")
os.makedirs(output_path, exist_ok=True)
if cores > os.cpu_count() or cores == 0:
raise ValueError(
"Number of cores selected must not be 0 and must be less than the number cpu cores (%d)"
% os.cpu_count()
)
cores = psutil.cpu_count(logical=False) if cores < 0 else cores
if os.path.isfile(input_path) or os.path.isdir(input_path):
if verbose:
print("Starting .wav conversion\n-----------------------------------------")
run_process(input_path, output_path, sample_frequency, cores, verbose)
else:
raise FileNotFoundError(
"Audio file(s) path %s not found" % os.path.abspath(input_path)
)
end_time = time.time()
if verbose:
print("Elapsed time was %g seconds\n" % (end_time - start_time))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Convert audio files to .wav format")
parser.add_argument(
"-i", "--input", type=str, required=True, help="Input audio file/directory path"
)
parser.add_argument(
"-o", "--output", type=str, required=True, help="Output wavs path"
)
parser.add_argument(
"-sf",
"--sample_frequency",
type=int,
default=16000,
help="Sampling frequency to create wavs",
)
parser.add_argument(
"--cores",
type=int,
default=1,
help="Number of processor cores to use for parallel processing: -1 max number of physical cores",
)
parser.add_argument(
"-v",
"--verbose",
type=int,
default=0,
choices=[0, 1],
help="Verbosity: 0 - none, 1 - full",
)
args = parser.parse_args()
wav_converter(
args.input, args.output, args.sample_frequency, args.cores, args.verbose
)