MATLAB: Split file.wav

split wav

Hi,
I'm trying to split a file .wav because its size is very large. For example I have a file x.wav with size of 100mb (mega byte) and then I would like to spli it in 5 mb pieces.
I 've tried with a tip that I find it in the section related to the answers, I paste below the link, but It doesn't work. Maybe I do something wrong.
Do you have any suggestion o other way?
Thank for your help

Best Answer

elis - the answer you included was to split the wav file into 1 second chunks. You want to do something different and split the data into 5Mb files. In order to do this (and it will give a rough estimate only), you will need to know some information about your wav file: the number of channels, the number of bits per sample, and the sampling frequency (I'm guessing a bit here based on what I've read online). Luckily, you can use audioinfo to get that information. So we'll do this by an example. Let's create a sample wav file using the handel.mat data
>> handelData = load('handel.mat');
which has a sampling frequency of 8192 and 73113 samples (single channel) and is roughly 8.9 seconds long. Let's increase this to ten minutes (by repeating) and write to file
>> tenMinuteData = repmat(handelData.y, 68, 1);
>> audiowrite('handel.wav', tenMinuteData, handelData.Fs);
which creates a ~10Mb file. Using audioinfo gives us
>> handelAudioInfo = audioinfo('handel.wav')
handelAudioInfo =
Filename: '/Users/.../handel.wav'
CompressionMethod: 'Uncompressed'
NumChannels: 1
SampleRate: 8192
TotalSamples: 4971684
Duration: 606.89501953125
Title: []
Comment: []
Artist: []
BitsPerSample: 16
Now we are going to split this file into roughly 5Mb chunks which means we need to calculate the number of samples that we write to each of these files. If we (safely?) assume that audio data (excluding wav file header and other information) size of the wav file can be estimated by
SampleRate * NumChannels * (BitsPerSample / 8) * Duration
then a 5Mb data size corresponds to
>> numSamplesPer5MbFile = 5000000 / (handelAudioInfo.NumChannels * handelAudioInfo.BitsPerSample/8)
numSamplesPer5MbFile =
2500000
which seems valid since there are ~5000000 samples in the ten minute file. So we can try the following
handelAudioInfo = audioinfo('handel.wav');
numSamplesPer5MbFile = 5000000 / (handelAudioInfo.NumChannels * handelAudioInfo.BitsPerSample/8);
[y,Fs] = audioread('handel.wav');
n = length(y);
atBlock = 1;
for k = 1:numSamplesPer5MbFile:n
audioBlock = y(k:min(k + numSamplesPer5MbFile - 1, n), :);
audiowrite(sprintf('handel%03d.wav', atBlock), audioBlock, Fs);
atBlock = atBlock + 1;
end
For me, this created two new files
-rw-r--r-- 1 staff 5000044 9 Apr 13:54 handel001.wav
-rw-r--r-- 1 staff 4943412 9 Apr 13:54 handel002.wav
each roughly 5Mb in size.