简体   繁体   English

在 MATLAB 中提取一部分 wav 文件并另存为新 wav 文件的想法

[英]Ideas for extracting a portion of a wav file and saving as new wav file in MATLAB

This may be easy but I'm no expert in MatLab. I have a bacth of wav files and I need to extract a 30s sample from each.这可能很简单,但我不是 MatLab 方面的专家。我有一大堆 wav 文件,我需要从每个文件中提取 30 秒的样本。 Example:例子:

1.wav - need 1:34-2:04 1.wav - 需要1:34-2:04

2.wav - need 5:15 - 5:45 2.wav - 需要5:15 - 5:45

Ideally I'd like to run a code which will take each wav file and extract the correct time period according to a pre-generated table and save each snippet as a new wav file (eg, 1_snip.wav would be the 30secs I need to analyze).理想情况下,我想运行一个代码,该代码将获取每个 wav 文件并根据预先生成的表格提取正确的时间段,并将每个片段保存为一个新的 wav 文件(例如,1_snip.wav 将是我需要的 30 秒分析)。 Any points in the right direction would be great.任何指向正确方向的点都会很棒。 Thanks!谢谢!

First you extract raw audio from the recording: [signal,fs] = audioread('file.wav');首先从录音中提取原始音频: [signal,fs] = audioread('file.wav');

The sampling rate is stored inside fs, representing your samples per second.采样率存储在 fs 中,代表每秒的采样数。 So, 5 seconds would be 5*fs.所以,5 秒就是 5*fs。 It is thus possible to extract chunks with specific time stamps: sig_chunk = signal(start_in_s*fs+1: end_in_s*fs);因此可以提取具有特定时间戳的块: sig_chunk = signal(start_in_s*fs+1: end_in_s*fs);

It's also possible to take timestamp data from a table and use the for loop to cut audio.也可以从表中获取时间戳数据并使用 for 循环来剪切音频。

The safest option would be to load them individually and use a mask for each then save.最安全的选择是单独加载它们并为每个使用掩码然后保存。 For example:例如:

[x1,fs] = audioread('fileName1.wav');
tinit = 1*60 + 34; % In seconds
tend  = 2*60 + 4;
ll = floor(tinit*fs) : floor(tend*fs);
x1 = x1(ll); % apply the mask to the segment of audio you want
audiowrite('fileName1edit.wav',x1,fs,'BitsPerSample',24)

However, if you have a big number of files to deal with, a less reliable but more comfortable solution could be to dump all the wav files in a structure但是,如果您有大量文件要处理,一个不太可靠但更舒适的解决方案可能是将所有 wav 文件转储到一个结构中

Files = dir('*.wav');

and load them calling并让他们打电话

[x,fs] = audioread(Files(idx).name);

within a for loop of length(Files) inside which you could prompt a box dialog asking for the minute and second to begin and minute and second to end.在一个length(Files)的for循环中,您可以在其中提示一个框对话框,询问开始的分钟和秒以及结束的分钟和秒。 For example:例如:

for idx = 1 : length(Files)
[x,fs] = audioread(Files(idx).name);
prompt = {'Min start:','Second start:','Min end:','Second end:'};
T      = inputdlg(prompt,'Enter the times',[1,20]);
Ninit  = round(fs*(str2num(T{1})*60 + str2num(T{2})));
Nend   = round(fs*(str2num(T{3})*60 + str2num(T{4})));
ll     = Ninit:Nend;
x      = x(ll); % or x = x(Ninit:Nend);
audiowrite(Files(idx).name,...);
end

See the documentation for inputdlg() for further examples.有关更多示例,请参阅inputdlg()的文档。 If you are not editing the string for the output audio file in audiowrite() with an _edit.mat or similar, make a backup of your files in a folder, for safety.如果您不使用_edit.mat或类似工具在audiowrite()中编辑 output 音频文件的字符串,请在文件夹中备份您的文件,以确保安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM