mirror of
https://github.com/KrisKennaway/ii-sound.git
synced 2024-06-25 22:29:29 +00:00
0f8ad1bf17
I split out preprocess_audio because scipy/librosa don't run under pypy and I was hoping that would speed up the encoding, but it does not for some reason (maybe numpy interactions?) Anyway, I should be able to vectorize the slow path which will likely help a lot.
28 lines
572 B
Python
28 lines
572 B
Python
import sys
|
|
import librosa
|
|
import numpy
|
|
import soundfile as sf
|
|
|
|
|
|
def preprocess(
|
|
filename: str, target_sample_rate: int,
|
|
normalize: float = 0.5) -> numpy.ndarray:
|
|
data, _ = librosa.load(filename, sr=target_sample_rate, mono=True)
|
|
|
|
max_value = numpy.percentile(data, 90)
|
|
data /= max_value
|
|
data *= normalize
|
|
|
|
return data
|
|
|
|
def main(argv):
|
|
serve_file = argv[1]
|
|
out = argv[2]
|
|
sample_rate = int(1024. * 1000 / 13)
|
|
|
|
sf.write(out, preprocess(serve_file, sample_rate), sample_rate)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv)
|