title: Google Speech to Text integration with Asterisk live calls
body: <p>i have setup google speech to test with asterisk. i have done following to get speech to text.</p>
  
  <p>when call comes in i record the message,
  convert to google supported bit rate audio file using sox
  send the audio file to google cloud speech and get the response.</p>
  
  <p>is there anyway we can send live call audio instead of recording to google cloud speed and get response?
  please see my following code.</p>
  
  <pre><code>&lt;?php
          require_once('/scripts/phpagi/phpagi.php');
  
  $agi = new AGI();
  $agi-&gt;answer();
  
  $unique_id = "$argv[1]";
  $caller_id = "$argv[2]";
  
  $agi-&gt;record_file("/scripts/google-speech-to-text/recordings/$unique_id-$caller_id","wav",' ',-1,null,false,5);
  
  
  echo system("sox /scripts/google-speech-to-text/recordings/$unique_id-$caller_id.wav -b 16 -s -c 1 -r 16k -t raw /scripts/google-speech-to-text/recordings/$unique_id-$caller_id.raw");
  
  putenv('GOOGLE_APPLICATION_CREDENTIALS=/scripts//file.json');
  
  # Includes the autoloader for libraries installed with composer
  require __DIR__ . '/vendor/autoload.php';
  
  # Imports the Google Cloud client library
  use Google\Cloud\Speech\SpeechClient;
  
  # Your Google Cloud Platform project ID
  $projectId = 'myprojectid';
  
  # Instantiates a client
  $speech = new SpeechClient([
      'projectId' =&gt; $projectId,
      'languageCode' =&gt; 'en-US',
  ]);
  
  # The name of the audio file to transcribe
  $fileName = '/scripts/google-speech-to-text/recordings/$unique_id-$caller_id.raw';
  
  # The audio file's encoding and sample rate
  $options = [
      'encoding' =&gt; 'LINEAR16',
      'sampleRateHertz' =&gt; 16000,
      'model' =&gt; 'phone_call',
  ];
  
  # Detects speech in the audio file
  $results = $speech-&gt;recognize(fopen($fileName, 'r'), $options);
  
  foreach ($results as $result) {
  $getresult =  $result-&gt;alternatives()[0]['transcript'] ;
  
  }
  
  echo $getresult;
  ?&gt;
  </code></pre>