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

조회 수: 16 (최근 30일)
Douglas Reeves
Douglas Reeves 2018년 5월 30일
편집: Pawel Jastrzebski 2018년 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