使用Python每次使用Sox修剪和PAD WAV文件,而无需每次保存WAV文件

发布于 2025-01-19 11:41:31 字数 644 浏览 4 评论 0原文

我发现修剪和垫子wav文件的最佳方法是使用sox。但是,与其每次保存转换后的文件,如何仅仅为转换的wav文件生成变量?也就是说,不要每次将转换的wav文件保存到硬盘驱动器中,而是在python中使用变量。

代码

import librosa
import sox

# get the sample rate
sample_rate = sox.file_info.sample_rate(input_file)
# create transformer
tfm = sox.Transformer()
# trim the audio between 0 and 0.25 seconds.
tfm.trim(0, 0.25)

xx = 'test.wav'
tfm.build(input_file, xx)  # create the output file

tfm.pad(0, 0.75)
tfm.build(input_file, xx)  # create the output file
duration2 = sox.file_info.duration(xx)

任何帮助和指导都是真诚地赞赏的!

谢谢!

I found that the best way to trim and pad wav files is to use sox. However, instead of saving the transformed file each time, how to go about just generating a variable for the transformed wav file? That is, do not save the transformed wav file to the harddrive each time, but instead use a variable within Python.

CODE

import librosa
import sox

# get the sample rate
sample_rate = sox.file_info.sample_rate(input_file)
# create transformer
tfm = sox.Transformer()
# trim the audio between 0 and 0.25 seconds.
tfm.trim(0, 0.25)

xx = 'test.wav'
tfm.build(input_file, xx)  # create the output file

tfm.pad(0, 0.75)
tfm.build(input_file, xx)  # create the output file
duration2 = sox.file_info.duration(xx)

Any help and guidance is sincerely appreciated!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2025-01-26 11:41:31

您可以完全省略使用 sox 并使用 librosa 返回的 numpy 数组。如果文件短于所需长度,librosa.util.fix_length 可以方便地用零填充。

durationSeconds = 0.5
data, sr = librosa.load("test.wav", sr=None, mono=False)
trimmed = librosa.util.fix_length(data, int(sr * durationSeconds))

如果您稍后想要保存填充后的 wav 文件:

librosa.output.write_wav("padded.wav", trimmed, sr)

you can entirely omit using sox and work with the numpy array returned by librosa. librosa.util.fix_length conveniently pads with zeros if the file is shorter than the desired length.

durationSeconds = 0.5
data, sr = librosa.load("test.wav", sr=None, mono=False)
trimmed = librosa.util.fix_length(data, int(sr * durationSeconds))

if you later want to save the padded wav file:

librosa.output.write_wav("padded.wav", trimmed, sr)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文