Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def torchaudio_info(path):
import torchaudio
# get length of file in samples
info = {}
si, _ = torchaudio.info(str(path))
info['samplerate'] = si.rate
info['samples'] = si.length // si.channels
info['duration'] = info['samples'] / si.rate
return info
def test_biquad_delay(self):
si, _ = torchaudio.info(self.test_filepath)
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("biquad", ["0.25136437", "0.50272873", "0.25136437",
"1.0", "-0.17123075", "0.17668821"])
E.append_effect_to_chain("delay", ["15000s"])
x, _ = E.sox_build_flow_effects()
# check if effect worked
self.assertTrue(x.size(1) == (si.length / si.channels) + 15000)
def test_z_get_info(self):
input_path = os.path.join(self.test_dirpath, 'assets', 'sinewave.wav')
channels, samples, rate, precision = (1, 64000, 16000, 32)
si, ei = torchaudio.info(input_path)
self.assertEqual(si.channels, channels)
self.assertEqual(si.length, samples)
self.assertEqual(si.rate, rate)
self.assertEqual(ei.bits_per_sample, precision)
def __init__(self):
self.asset_dirpath = os.path.join(self.test_dirpath, "assets")
sound_files = ["sinewave.wav", "steam-train-whistle-daniel_simon.mp3"]
self.data = [os.path.join(self.asset_dirpath, fn) for fn in sound_files]
self.si, self.ei = torchaudio.info(os.path.join(self.asset_dirpath, "sinewave.wav"))
self.si.precision = 16
self.E = torchaudio.sox_effects.SoxEffectsChain()
self.E.append_effect_to_chain("rate", [self.si.rate]) # resample to 16000hz
self.E.append_effect_to_chain("channels", [self.si.channels]) # mono signal
self.E.append_effect_to_chain("trim", [0, "16000s"]) # first 16000 samples of audio
def test_silence_contrast(self):
si, _ = torchaudio.info(self.test_filepath)
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("silence", [1, 100, 1])
E.append_effect_to_chain("contrast", [])
x, sr = E.sox_build_flow_effects()
# check if effect worked
self.assertLess(x.numel(), si.length)
"sinewave.wav")
sr = 16000
freq = 440
volume = 0.3
y = (torch.cos(
2 * math.pi * torch.arange(0, 4 * sr).float() * freq / sr))
y.unsqueeze_(1)
# y is between -1 and 1, so must scale
y = (y * volume * 2**31).long()
save(sinewave_filepath, y, sr)
self.assertTrue(os.path.isfile(sinewave_filepath))
# test precision
new_filepath = os.path.join(self.test_dirpath, "test.wav")
si, ei = torchaudio.info(sinewave_filepath)
save(new_filepath, y, sr, precision=16)
si16, ei16 = torchaudio.info(new_filepath)
self.assertEqual(si.precision, 32)
self.assertEqual(si16.precision, 16)
os.unlink(new_filepath)
def test_band_chorus(self):
si_in, ei_in = torchaudio.info(self.test_filepath)
ei_in.encoding = torchaudio.get_sox_encoding_t(1)
E = torchaudio.sox_effects.SoxEffectsChain(out_encinfo=ei_in, out_siginfo=si_in)
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("band", ["-n", "10k", "3.5k"])
E.append_effect_to_chain("chorus", [.5, .7, 55, 0.4, .25, 2, '-s'])
E.append_effect_to_chain("rate", [si_in.rate])
E.append_effect_to_chain("channels", [si_in.channels])
x, sr = E.sox_build_flow_effects()
# The chorus effect will make the output file longer than the input
self.assertEqual(x.size(0), si_in.channels)
self.assertGreaterEqual(x.size(1) * x.size(0), si_in.length)
speakerTalk = {}
nData = len(list_metadata)
bar = progressbar.ProgressBar(maxval=nData)
bar.start()
for index, pathMetadata in enumerate(list_metadata):
bar.update(index)
with open(pathMetadata, 'rb') as file:
locMetadata = json.load(file)
speaker_name = locMetadata['speaker']
path_audio_data = os.path.splitext(pathMetadata)[0] + audio_extension
info = torchaudio.info(path_audio_data)[0]
totAudio = info.length / (info.rate * 3600.)
if speaker_name is None:
speaker_name = 'null'
if speaker_name not in speakerTalk:
speakerTalk[speaker_name] = 0
speakerTalk[speaker_name] += totAudio
bar.finish()
return speakerTalk
speakerData = getJSON(os.path.join(path_dir,
get_speaker_data_name(metadataName)))
dirWav = os.path.join(pathWav, wavName)
if not os.path.isdir(dirWav):
continue
listWav = [f'{f}.wav' for f in speakerData["names"]]
for index, wavFile in enumerate(listWav):
locPath = os.path.join(dirWav, wavFile)
if not os.path.isfile(locPath):
continue
info = torchaudio.info(locPath)
size = (info[0].length / info[0].rate) / 3600
speakers = speakerData['readers'][index]
if speakers is None:
speakers = ['null']
if len(speakers) > 1:
multiples += size
for IDspeaker in speakers:
if IDspeaker not in speakerTalk:
speakerTalk[IDspeaker] = 0
speakerTalk[IDspeaker] += size