在Matlab中拆分wav文件

时间:2016-10-09 09:54:44

标签: matlab wav

我之前问过一个与我实际问题有关的问题。我使用程序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我缺乏知识,所以我总是感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

这取决于你接下来想做什么。获取部分信号的最简单方法是:

part=y(1:Fs);

你可能想要这些东西的数组所以使用FOR或者WHILE sructure,就像这样:

%in loop
part(i)=(i*Fs:i*Fs+Fs);
i=i+1;

注意数组长度

答案 1 :(得分:0)

这是一个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