[英]Splitting a wav file in Matlab
我以前問過一個問題,這與我的實際問題有關。 我使用程序audacity將mp3文件轉換為wav文件。 wav文件的持續時間約為10秒。 我想將其分為10個部分,這意味着每個部分需要1秒。 我該如何實現? 即使社群為我提供了一些答案,也請您對我的無知表示歉意。
[y,fs]=wavread('UnchainMyHeart');
t=linspace(0,length(y)/fs,length(y));
plot(t,y)
此處的代碼在時域中顯示了我的wav文件,但由於沒有wav文件,您可能看不到它。 沒關系 現在,是否可能從那里繼續,使我的問題困擾? 我相信這與矩陣有關... Unfortunatley我缺乏知識,因此,我總是感謝您的幫助!
這取決於您下一步要做什么。 獲得部分信號的最簡單方法是:
part=y(1:Fs);
您可能需要這些東西的數組,因此請使用FOR或WHILE結構,如下所示:
%in loop
part(i)=(i*Fs:i*Fs+Fs);
i=i+1;
注意數組長度
這是一個WavReader.m
類,該類以相同長度的塊讀取wav。
要讀取1秒的數據塊,請按以下方式使用它:
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
這是課程:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.