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

5 views (last 30 days)
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.

Answers (1)

Pawel Jastrzebski
Pawel Jastrzebski on 30 May 2018
Edited: Pawel Jastrzebski on 30 May 2018
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

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!