阅读用delphi .wav文件(Read .wav file using delphi)

2019-09-01 04:03发布

我现在目前尝试读取用delphi这里.wav文件是我的代码:

type
  TWaveHeader = packed record
    Marker_RIFF: array [0..3] of char;
    ChunkSize: cardinal;

    Marker_WAVE: array [0..3] of char;
    Marker_fmt: array [0..3] of char;
    SubChunkSize: cardinal;

    FormatTag: word;

    NumChannels: word;
    SampleRate: longint;
    BytesPerSecond: longint;
    BytesPerSample: word;
    BitsPerSample: word;


    Marker_data: array [0..3] of char;
    DataBytes: longint;
  end;

  TChannel = record
  Data : array of double;
end;

一些私人delcaration

private
    wavehdr:TWaveHeader;
    wavedata:array[0..3]of TChannel;
    numsamples:integer;

功能

  FillChar(wavehdr, sizeof(wavehdr), 0);
  Stream.Read(wavehdr, sizeof(wavehdr));

  { Log Header data }
  with memo1.Lines do begin
    Add('Filename : '+od.FileName);
    Add('Header size : '+inttostr(sizeof(wavehdr)));
    tmpstr := wavehdr.Marker_RIFF;
    Add('RIFF ID : '+tmpstr+'');
    Add('Chunk size : '+inttostr(wavehdr.ChunkSize));
    tmpstr := wavehdr.Marker_WAVE;
    Add('WAVE ID : '+tmpstr+'');
    tmpstr := wavehdr.Marker_fmt;
    Add('''fmt '' ID : '+tmpstr+''' ');
    Add('SubChunk size : '+inttostr(wavehdr.SubChunkSize));
    Add('Format : '+inttostr(wavehdr.FormatTag));
    Add('Num Channels : '+inttostr(wavehdr.NumChannels));
    Add('Sample rate : '+inttostr(wavehdr.SampleRate));
    Add('Bytes per second : '+inttostr(wavehdr.BytesPerSecond));
    Add('Bits per sample : '+inttostr(wavehdr.BitsPerSample));
    Add('Block Align : '+inttostr((wavehdr.NumChannels*wavehdr.BitsPerSample)div 8));
  end;

  numsamples := (file.size div (wavehdr.NumChannels*wavehdr.BitsPerSample)div 8) div wavehdr.BytesPerSample;
  case wavehdr.NumChannels of
      1:begin
        SetLength(wavedata[0].Data, numsamples);
        Stream.Read(wavedata[0].Data[0], numsamples);
      end;

      2:begin
        SetLength(wavedata[0].Data, numsamples);
        SetLength(wavedata[1].Data, numsamples);
        for i := 0 to high(wavedata[0].Data) do begin
          Stream.Read(wavedata[0].Data[i], 2);
          Stream.Read(wavedata[1].Data[i], 2);
        end;
      end;
  end;

上面的代码给我有关的.wav头完全相同的信息和细节(同MATLAB一样)是:

  • 文件名:E:\ DEPHI \ classic3.wav
  • RIFF ID:RIFF
  • 块大小:18312354
  • WAVE ID:WAVE
  • 'FMT' ID:FMT'
  • 子块的大小:16
  • 格式:1(PCM)
  • NUM频道:2(立体声)
  • 采样率:44100
  • 每秒字节数:176400
  • 每样本的比特:16
  • 块对齐:4

除我由-44(wavedata的wavedata / blockalign的大小),计算出的总的示例数据44是WAV的报头。 这是不准确的,有时是由小姐5,1,10。 我只用5 sample.And这里举例的测试:

  • classic1.wav MATLAB:3420288,德尔福(我的计算):(4分之13681352)-44 = 3420294
  • classic2.wav MATLAB:2912256,德尔福(我的计算):(四分之一千一百六十四万九千二百〇四)-44 = 2912257

并且还从MATLAB和Delphi样本数据值是不同的等

classic1.wav MATLAB:(第一10值leftchannel和rightchannel)

  1. -3.05175781250000e-05 [] 6.10351562500000e-05
  2. -6.10351562500000e-05 [] 6.10351562500000e-05
  3. -6.10351562500000e-05 [] 3.05175781250000e-05
  4. 0 [] -3.05175781250000e-05
  5. 6.10351562500000e-05 [] -6.10351562500000e-05
  6. 6.10351562500000e-05 [] -6.10351562500000e-05
  7. 3.05175781250000e-05 [] -3.05175781250000e-05
  8. 6.10351562500000e-05 [] -6.10351562500000e-05
  9. 3.05175781250000e-05 [] 0
  10. -3.05175781250000e-05 [] 6.10351562500000e-05

DELPHI:(第一10值leftchannel和rightchannel)

  1. 9.90156960830442E-320 [] 1.00265682167023E-319
  2. 9.90156960830442E-320 [] 9.77113627780233E-320
  3. 3.26083326255223E-322 [] 0
  4. 1.39677298735779E-319 [] 1.37088394751571E-319
  5. 1.45932169812129E-319 [] 1.33373021094845E-319
  6. 1.23175506164681E-319 [] 1.206903559661E-319
  7. 1.28239679034554E-319 [] 1.40932225476216E-319
  8. 1.37068632125737E-319 [] 1.33382902407761E-319
  9. 1.33373021094845E-319 [] 1.25685359645555E-319
  10. 1.40907522193924E-319 [] 1.33358199125469E-319

我的问题是:

  1. 当发现wav文件的总样本,如何正确地做到这一点?
  2. 是MATLAB的方式和Delphi读取wav文件(数据块)以不同的方式? 也许我的代码是一个在这里是错误的?
  3. 有没有一种方法来获得相同的价值像MATLAB呢?

编辑:我跟着MBO咨询和它改为MBO提醒

Data : array of SmallInt;
numsamples := wavehdr.DataBytes div (wavehdr.NumChannels * wavehdr.BitsPerSample div 8);
Stream.Read(wavedata[0].Data[i], SizeOf(SmallInt));

在解释部分我不知道,但我把它改为

floattostr(wavedata[0].Data[i]/32768.0)
floattostr(wavedata[1].Data[i]/32768.0)

结果我得到:

  1. 0.611602783203125 [] 0.61932373046875
  2. 0.611602783203125 [] 0.603546142578125
  3. 0.0023193359375 [] 0
  4. 0.862762451171875 [] 0.846771240234375
  5. 0.901397705078125 [] 0.823822021484375
  6. 0.760833740234375 [] 0.7454833984375
  7. 0.7921142578125 [] 0.870513916015625
  8. 0.799774169921875 [] 0.761016845703125
  9. 0.8238525390625 [] 0.782623291015625
  10. 0.354766845703125 [] 0.76123046875

Answer 1:

WAV文件(每个样本的比特:16)包含有符号的16位整数数据(SMALLINT类型),但你读浮法8个字节的类型双阵列数据。

你可以声明

Data : array of SmallInt;

计算

numsamples := wavehdr.DataBytes div (wavehdr.NumChannels * wavehdr.BitsPerSample div 8);

阅读他们作为

Stream.Read(wavedata[0].Data[0], numsamples * SizeOf(SmallInt))
or multichannel case:
Stream.Read(wavedata[0].Data[i], SizeOf(SmallInt));

然后解释数据值作为浮数据[I] / 32768.0

注意,MATLAB值3.05175781250000e-05 = 1 / 32768.0是16位信号的最小量子



文章来源: Read .wav file using delphi