I previously asked a question, which is related to my actual problem. I converted a mp3 file to a wav file, by using the program audacity. Th duration of the wav-file is about 10 seconds. I want to split it in 10 parts, which means, each part takes 1 second. How can i make it happen? Please apologize my ignorance, even though the community provided me some answer.
[y,fs]=wavread('UnchainMyHeart');
t=linspace(0,length(y)/fs,length(y));
plot(t,y)
The code here, shows my wav file in the time domain, but you probably can't see it, since you don't have the wav file. Doesn't matter. Now, is it possibly to continue from there, concering my question? I believe it has somthing to do with matrix... Unfortunatley I lack the knowledge, therefore I always appreciate your help!
It depends on what you want to do next. The easiest way to get part of signal is:
part=y(1:Fs);
you probaly want array of this things so use FOR, or WHILE sructure, smth like this:
%in loop
part(i)=(i*Fs:i*Fs+Fs);
i=i+1;
be aware of array length
Here is a WavReader.m
class which reads wav in chunks of the same length.
To read 1-second chunks, use it like this:
wr = WavReader('c:/wavs/killyourmama.wav');
wr.SetChunkLengthInMiliSeconds( 1000.0 );
while(true)
[pcm, num_read] = wr.ReadChunk();
if( num_read==0 ); break; end;
fprintf('Progress: %5.2f%%\n', wr.Progress() );
end
Here is the class:
classdef WavReader < handle
%WavReader Reads wav files in chunks
% Flexible wav chunk reader.
%
% 1) Usage steps in case of full file read:
%
% Create wav reader:
% wr = WavReader(path);
% Read all samples:
% [samples, count] = GetAllSamples();
%
% 2) Usage steps in case of range read:
%
% Create wav reader:
% wr = WavReader(path);
%
% Read samples from within a range:
% [samples, count] = GetSamples(seconds_start, seconds_finish);
%
% 3) Usage steps in case chunk-by-chunk read:
%
% Create wav reader:
% wr = WavReader(path);
%
% Set range of interest:
% wr.SetRange( seconds_start, seconds_finish );
%
% Set chunk size you want to read:
% wr.SetChunkLength( chunk_size );
%
% Read chunk-by-chunk:
% while(num_read)
% [pcm, num_read] = wr.ReadChunk();
%
%
properties (Access=private)
% Info
path;
fs;
total_samples;
total_seconds;
% Range of interest
range_length;
range_samples;
% Current state of chunks
chunk_length;
total_chunks;
chunks_read;
end
methods
function obj = WavReader( in_path_wav )
% Constructor
try
obj.path = in_path_wav;
info = audioinfo(obj.path);
obj.fs = info.SampleRate;
obj.total_samples = info.TotalSamples;
obj.total_seconds = info.Duration;
catch
error('Problem with opening wav file. Exiting.')
end
% Defaul reads full range, whole file.
obj.SetRangeFull();
end
%%
function [range_length] = SetRangeFull(obj)
%SetRangeFull Sets the range to full (we are interested in all samples).
try
obj.range_samples = audioread(obj.path);
catch
error('Problem with reading wav file. Exiting.')
end
obj.range_length = obj.total_samples;
range_length = obj.range_length;
end
%%
function [range_length] = SetRange(obj, start_sec, end_sec)
% SetRange Sets target range of interest.
%
%# [range_length] = SetRange(obj, start_sec, end_sec)
%# INPUT start_sec: staring point in target range
%# INPUT end_sec: ending point in target range
%# OUTPUT range_length: number of total samples available in the range
%
% To use full range call SetRangeFull().
% If start beyond end: throws exception.
% If end beyond end: sets end to end.
[start_sec, end_sec] = obj.checks( start_sec,end_sec );
% Full range if both zeroes:
%if(start_sec==0 && end_sec==0)
% start_sample = 1;
% stop_sample = obj.total_samples;
%else
start_sample = obj.seconds_to_samples(start_sec);
stop_sample = obj.seconds_to_samples(end_sec);
%end
obj.range_length = stop_sample - start_sample;
obj.range_samples = 0;
try
obj.range_samples = audioread(obj.path, [start_sample, stop_sample]);
catch
error('Problem with reading wav file. Exiting.')
end
range_length = obj.range_length;
end
%%
function [total_chunks] = SetChunkLength( obj, chunk_length )
% SetChunkLength Defines chunk length for chunk-by-chunk read.
%
%# [total_chunks] = SetChunkLength( obj, chunk_length )
%# INPUT chunk_length: desired chunk length
%# OUTPUT total_chunks: total number of available full chunks
%
% Abandonds last incomplete chunk.
obj.chunk_length = chunk_length;
obj.chunks_read = 0;
obj.total_chunks = round( obj.range_length / obj.chunk_length - 0.5 );
total_chunks = obj.total_chunks;
end
%%
function [total_chunks] = SetChunkLengthInMiliSeconds( obj, chunk_length_ms )
% SetChunkLengthInMiliSeconds Defines chunk length for chunk-by-chunk read.
%
%# [total_chunks] = SetChunkLengthInMiliSeconds( obj, chunk_length_ms )
%# INPUT chunk_length_ms: desired chunk length in mili-seconds
%# OUTPUT total_chunks: total number of available full chunks
%
% Abandonds last incomplete chunk.
seconds = chunk_length_ms/1000.0;
chunk_length_samples = seconds_to_samples( obj, seconds );
total_chunks = SetChunkLength( obj, chunk_length_samples );
end
%%
function [chunk, num_read] = ReadChunk( obj )
% ReadChunk Reads next chunk of blocklength.
%
%# [chunk, num_read] = ReadChunk( obj )
%# OUTPUT chunk: chunk samples
%# OUTPUT num_read: number of read samples.
%
% If next chunk incomplete: returns zero.
% If next chunk not available: returns zero.
start_pos = 1 + obj.chunks_read * obj.chunk_length;
stop_pos = start_pos + obj.chunk_length -1;
if( start_pos>obj.range_length || stop_pos>obj.range_length)
num_read = 0;
chunk = 0;
% warning('Reached the end of samples, nothing read.')
% To do: handle last incomplete frame.
return;
end
chunk = obj.range_samples(start_pos:stop_pos);
num_read = 1 + stop_pos - start_pos;
obj.chunks_read = 1 + obj.chunks_read;
end
%%
function progress = Progress(obj)
% Progress Returns progress in percentages.
progress = 100.0 * double(obj.chunks_read) / double(obj.total_chunks);
end
%%
function range = GetFullRange(obj)
% GetFullRange Returns all samples available.
range = obj.range_samples;
end
%%
function fs = GetSamplingRate(obj)
% GetSamplingRate Returns sampling rate.
fs = obj.fs;
end
%%
function samples = GetNumberOfSamples(obj)
samples = obj.total_samples;
end
%%
function num_chunks_read = GetNumberOfReadChunks(obj)
% GetNumberOfReadChunks Returns number of chunks read so far.
num_chunks_read = obj.chunks_read;
end
%%
function [samples, count] = GetSamples(obj, start_sec, end_sec)
% GetSamples Call to immediately get range of samples.
[start_sec, end_sec] = obj.checks( start_sec,end_sec );
start_sample = obj.seconds_to_samples(start_sec);
stop_sample = obj.seconds_to_samples(end_sec);
count = 1 + stop_sample - start_sample;
try
samples = audioread(obj.path, [start_sample, stop_sample]);
catch
error('Problem with reading wav file. Exiting.')
end
end
%%
function [samples, count] = GetAllSamples(obj)
% GetAllSamples Call to immediately get all of the samples.
try
samples = audioread(obj.path);
catch
error('Problem with reading wav file. Exiting.')
end
count = obj.total_samples;
end
end % methods public
methods (Access=private)
% Secs to samples
function [sample] = seconds_to_samples( obj, seconds )
sample = obj.fs * seconds;
end
% Check range
function [start_checked, end_checked] = checks(obj, start_sec, end_sec)
start_checked = start_sec;
end_checked = end_sec;
% Check fatal errors
if( start_sec>obj.total_seconds ||...
start_sec>=end_sec ||...
start_sec<0 ||...
end_sec<0 )
throw(MException( 'WavReader:FatalError', 'Range error.') );
end
% Handle overflow
if( end_sec > obj.total_seconds )
end_checked = obj.total_seconds;
end
end
end % methods private
end % class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.