【问题标题】:Splitting a wav file in Matlab在 Matlab 中拆分 wav 文件
【发布时间】:2016-10-09 09:54:43
【问题描述】:

我之前问了一个问题,这与我的实际问题有关。我使用程序 audacity 将 mp3 文件转换为 wav 文件。 wav 文件的持续时间约为 10 秒。我想把它分成 10 个部分,这意味着,每个部分需要 1 秒。我怎样才能让它发生?请原谅我的无知,即使社区为我提供了一些答案。

[y,fs]=wavread('UnchainMyHeart');
t=linspace(0,length(y)/fs,length(y));
plot(t,y)

此处的代码显示了我在时域中的 wav 文件,但您可能看不到它,因为您没有 wav 文件。没关系。现在,关于我的问题,是否有可能从那里继续?我相信它与矩阵有关... 不幸的是,我缺乏知识,因此我总是感谢您的帮助!

【问题讨论】:

    标签: matlab wav


    【解决方案1】:

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

    part=y(1:Fs);
    

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

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

    注意数组长度

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-13
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 2017-12-07
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多