Cut random 30secs of audio from an audio recording .wav

5 次查看(过去 30 天)
Hi I would like to know how to: A) Cut a random 10 seconds out of a 10 minute audio file after, i load audio file. B) I would like to repeat this for all files in a linked folder. C) Save the cut sections as a single audio file in .wav format.
Cheers.

回答(1 个)

Pawel Jastrzebski
Pawel Jastrzebski 2018-5-30
编辑:Pawel Jastrzebski 2018-5-30
This code will allow you to import and export all of the audio files in your current folder:
% get file names in the current folder
fileNames = dir('*.wav'); % structure
fileNames = {fileNames.name}; % actual names extracted to cell array
% read and write file in the loop
for i = 1:numel(fileNames)
[y, Fs] = audioread(fileNames{i});
% code that randomly cuts the the sample
audiowrite(['out_' fileNames{i}],y,Fs);
end
To get the portion of the file use the:
If you don't know the duration of each of the files these are the steps you need to take:
  • Get the total duration of a file
durationInSeconds = size(y,1)/Fs
  • Specify the duration of your sound bite i.e 10s
soundDuration = 10;
  • Having all this information, you know that your sound bite can start at any point between 0s and durationInSeconds - soundDuration
  • Now you need to generate the random soundbite start using:
randomStart = randi([0, durationInSeconds - soundDuration]);
  • Once you've got the start, add the sound bite duration to it, convert to new y and write as a new sample

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by