0

From SO I get the next unit code to capture Mic Audio. I´d like to use the audio captured to send to google speech recognize api.

The google speech api accepts only some encoded formats.

I need the audio in LINEAR16 Uncompressed 16-bit signed little-endian samples. How could I modify this code to do it?

Sample of use:

    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
        SNDProcWaveIn(ind);
        //code for proc data in "ind" buffer
    end;

unit WaveSound;

interface

uses
  SysUtils, MMSystem;

const
  NUMSAMPLES = 1024;    // Number of Samples

type
  TIndata      = array[0 .. NUMSAMPLES - 1] of Integer;
  PIndata      = ^TIndata;

  TFrec= record
    Fx, dx  :Integer;
  end;

  function SNDInitWaveIn: Cardinal;
  procedure SNDProcWaveIn(var Indata : TIndata);
  procedure SNDStopWave;

implementation

var
  DevHandle   : Integer;
  WAVEFORMAT1 : TWAVEFORMATEX;
  Wave        : WAVEHDR;

function SNDInitWaveIn: Cardinal;
begin
    with WAVEFORMAT1 do begin
        wFormatTag := WAVE_FORMAT_PCM;
        nChannels := 1;
        nSamplesPerSec := 44100;// 11025; //11khz
        wBitsPerSample := 16;
        nBlockAlign := (nChannels * wBitsPerSample) div 8;
        nAvgBytesPerSec := nBlockAlign * nSamplesPerSec;
        cbSize := 0;
    end;
    Result:= waveInOpen(@DevHandle, cardinal(-1),@WAVEFORMAT1, cardinal(0), cardinal(0), cardinal(0));
    If not(DevHandle = 0) Then waveInStart(DevHandle);
end;

procedure SNDProcWaveIn(var Indata : TIndata);
begin
  //lpdata requires the address of an array to fill up data with
      Wave.lpData := @Indata;
  //the buffer length
      Wave.dwBufferLength := NUMSAMPLES;
      Wave.dwFlags := 0;
   //prepare device for input
      waveInPrepareHeader(DevHandle, @Wave, sizeof(Wave));
      waveInAddBuffer(DevHandle, @Wave, sizeof(Wave));
      // if the following statement is removed, the vis. will be a lot faster (avs style)
      // but uses up 100% of cpu!
      // this is why i hate avs
      Sleep(10); // give device a breather
      // the following loop is quite useless, but anyway...
      repeat
          //Just wait for the blocks to be done or the device to close
      until (((Wave.dwFlags and WHDR_DONE)= WHDR_DONE) or (DevHandle = 0));
      If (DevHandle = 0) Then Exit;  //Cut out if the device is closed
      waveInUnprepareHeader(DevHandle, @Wave, sizeof(Wave));
end;

procedure SNDStopWave;
begin
    waveInReset(DevHandle);
    waveInClose(DevHandle);
    DevHandle := 0;
end;
end.
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Luiz Alves
  • 2,575
  • 4
  • 34
  • 75
  • Hey, Luiz, in your [previous question about creating flac](https://stackoverflow.com/questions/52236347/creating-flac-file-or-flac-stream-using-bass-dll-with-delphi/52237682#comment91444125_52237682) you have provided code that records input audio from microphone. As I can see that code *already* records audio in 16-bit range. Why don't use that code instead of using this one? I believe it will solve your problem. Or you have restriction to not use any libraries? – Josef Švejk Sep 16 '18 at 14:13
  • @Dima Hi, I am just trying other code to compare the performance of my goodle speech app. I am using bass dll and I´d like to compare the performance using other units. I found no detailed info about LINEAR16. – Luiz Alves Sep 16 '18 at 16:44

0 Answers0