Reputation: 77
How do you split an audio file into 1 second long chunks (frames) of audio files in Matlab? Anything helps really, excuse my ignorance as I am not very experienced with Matlab... I have tried
clear all; close all;
[y, fs] = audioread('red.wav');
chunk_size = fs*1;
but then stuck.
Upvotes: 1
Views: 3219
Reputation: 691
You can use audioread to read the file in chunks instead of reading the entire file in all at once. The code below might be helpful.
info = audioinfo('handel.wav');
Fs = info.SampleRate;
chunkDuration = 1; % 1 sec
numSamplesPerChunk = chunkDuration*Fs;
chunkCnt = 1;
for startLoc = 1:numSamplesPerChunk:info.TotalSamples
endLoc = min(startLoc + numSamplesPerChunk - 1, info.TotalSamples);
y = audioread('handel.wav', [startLoc endLoc]);
outFileName = sprintf('outfile%03d.wav', chunkCnt);
audiowrite(outFileName, y, Fs);
chunkCnt = chunkCnt + 1;
end
Hope this helps.
Dinesh
Upvotes: 3
Reputation: 4510
you can do it with a for loop, something like:
[y, fs] = audioread('red.wav');
for t = 0:floor(size(y,2)/fs)-1
z = y(t*fs+1:(t+1)*fs)
filename = ['output' num2str(t)];
wavwrite(z,fs,filename)
end
This should write every (except the last) second in y and save it as output1...outputt. In the loop form, the last second which might not be a whole second but 0.5 or 0.7 of a second could break the loop, so you will have to write another line to get the last second.
This is not an efficient approach for long files! Look at reshape for more efficient approach
Upvotes: 2