help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: wav error


From: Robert A. Macy
Subject: Re: wav error
Date: Wed, 14 Dec 2005 12:25:17 -0800

Here is a "recent" copy.  Someone updated it to accept new
formats, I think.  

The Header of your .wav file may be contradictory. Have you
checked it?

* * * * * *
function [sig, sf, bits]=wavread2(wavefile, siz, fmt24bit)

% WAVREAD  Load Microsoft Windows .WAV format sound files.
%
%  [sig, sf, bits] = wavread(wavfile [, siz])
%   
%        sig : signal
%         sf : sampling freq.
%       bits : the number of bits per sample (8, 16,
24bits)
%    wavfile : file name 
%              (The ".wav" extension is appended if no
extension is given.)
%        siz : 'size' returns the size of the audio data
%            : [N1 N2] returns only samples N1 through N2
%            : N1 returns the first N1 samples
%   fmt24bit :  0 = digion format
%            :  1 =  2^23 format for 24 bit data
%            : Default is 1 

% Modified By: Doug Stewart May 10, 5005

if nargin < 1
  help wavread;
  return;
end

if nargin < 3
   fmt24bit = 1;
end

% fmt24bit = 1; % fmt24bit =0: digion format, fmt24bit=1:
2^23 format for 24 bit data
if fmt24bit==1
  cnst = 1;
else
  cnst = 2;
end

if isempty(findstr(wavefile,'.'))
  wavefile=[wavefile,'.wav'];
end

fid=fopen(wavefile,'rb','ieee-le');
if fid == -1
  error('Can''t open .WAV file for input!');
end;

% read riff chunk ChunkID 4 bytes
RIFF_str=(char(fread(fid,4,'uchar')))';
if (strcmp(RIFF_str,'RIFF')==0) 
  fclose(fid);
  error(sprintf('%s is not a WAV file',wavefile));
  return;
end
first=RIFF_str
disp(sprintf('Input file \'%s\' is RIFF
format:',wavefile));

% ChunkSize 4 bytes
nbytes=fread(fid,1,'ulong');
disp(sprintf('\tTotal length = %d bytes',nbytes));

% Format 4 bytes
WAVE_str=(char(fread(fid,4,'uchar')))';
disp(sprintf('Subformat = \'%s\' ',WAVE_str));
if (strcmp(WAVE_str,'WAVE')==0) 
  fclose(fid);
  error('*** Cannot handle subformat %s',WAVE_str);
  return;
end


% read format subchunk1ID 4 bytes "fmt "
fmt_str=(char(fread(fid,4,'uchar')))';
if (strcmp(fmt_str,'fmt ')==0)
  fclose(fid);
  error(sprintf('*** Expected \'fmt \' in header, but got
\'', fmt_str));
  return;
end

% Subchunk1Size 4 bytes should be 16 for PCM
fslen=fread(fid,1,'ulong');

% audioformat 2 byts    1 = PCM, other than 1 is some form
of compresion
audioformat=fread(fid,1,'ushort');                %
AudioFormat 

if (audioformat != 1) 
  fclose(fid);
  disp(sprintf('*** This Version does not support audio
compresion'));
  error(sprintf('*** Expected 01 in audio format, but got
%d',audioformat));

end

ch=fread(fid,1,'ushort');                % NumChannels  2
bytes
disp(sprintf('\tNumber of chanels = %d ', ch));
sf=fread(fid,1,'ulong');                 % Samples per
second 4 bytes
ByteRate=fread(fid,1,'ulong');             % ByteRate  ==
SampleRate * NumChannels * BitsPerSample/8  4 bytes
block=fread(fid,1,'ushort');              % BlockAlign  ==
NumChannels * BitsPerSample/8 2 bytes
bits=fread(fid,1,'ushort');                % Bits per
sample   2 bytes
disp(sprintf('\tBits per sample = %d ', bits));

% throw away the rest of this chunk  
if fslen-16 > 0
  dum = fread(fid, fslen-16, 'uchar');
end


% now look for the data chunk.

do
id = char( fread( fid, 4, "uchar") );
if (strcmp(id,'data')==0) % if not 'data' then read size
and throw away this section
   cl2 = fread(fid,1,'ulong');
   cl3 = fread(fid,cl2,'uchar');
endif
until (strcmp(id,'data')!=0) 

if (strcmp(id,'data')==0) 
  fclose(fid);
  error(sprintf('Did not find \'data\' ID where
expected'));
end

%  now read the data chunk size  4 bytes
nbyteforsamples=fread(fid,1,'ulong');

nsamples=nbyteforsamples/block;
disp(sprintf('\tData length = %d samples', nsamples));


if(nargin < 2)
  siz = [1 nsamples];
end

if(strcmp(siz, 'size'))
  sig = [nsamples, ch];
  fclose(fid);
  return;
end

if length(siz)==1
     st = 1;
     et = siz;
  else
     st = siz(1);  %  start of data to return
     et = siz(2);  % end of data to return
end

if bits == 8
  fseek(fid, (st-1)*ch, 0);
  [sig, cnt] = fread(fid, [ch, et-st+1],'uchar');
  sig = (sig-128)/128;
end

if bits == 16
  fseek(fid, (st-1)*2*ch, 0);
  [sig, cnt] = fread(fid, [ch, et-st+1], 'short');
  sig = sig/32768;     % MATLAB compatible
%  sig = (sig+32768)/32767.5-1;
end

if bits == 24
  fseek(fid, (st-1)*3*ch, 0);
  len = et-st+1;
  tmpsig = fread(fid, [1, (et-st+1)*3*ch], 'uchar');
  for k=1:ch
    idx = (0:len-1)*3*ch+(k-1)*3+1;
    signm = tmpsig(idx+2) > 127;
    sig(k,:) =
-signm.*((255-tmpsig(idx+2))*2^16+cnst+(255-tmpsig(idx))+(255-tmpsig(idx+1))*2^8)+(1-signm).*(tmpsig(idx+2)*2^16+tmpsig(idx)+tmpsig(idx+1)*2^8);
  end
  if fmt
    sig = sig/2^23;
  else
    sig = (sig+2^23)/(2^23-0.5)-1;
  end
end


sig = sig'; % transpose for Matlab compatability
fclose(fid);


* * * * * *


On Wed, 14 Dec 2005 14:04:48 -0500
 "Cesar Augusto Rodriguez" <address@hidden>
wrote:
> Hi... 
> | 
> | I need your help. 
> | 
> | I can't read .wav files using wavread.m. Octave starts
> into int then crashes 
> | 
> | the error is like that : 
> | 
> | [s1,fs]=wavread('H:/biovoz/delphi/ocho.wav') 
> | Input file 'H:/biovoz/delphi/ocho.wav' is RIFF format: 
> |        Total length = 48036 bytes 
> |        Number of channels = 1 
> |        Sampling rate = 12500 Hz 
> | error: *** 8000 bytes per second looks wrong. Expect
> 25000 
> | error: evaluating if command near line 48, column 1 
> | error: called from `wavread' in file
> `/usr/share/octave/2.1.36/m/audio/wavread.m' 
> | 
> | thanks for your help. 
> 
> 
> 



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]