and again dll and market - page 27

 
Alexsandr San:

here's the first run - loaded a text file with Russian text, some scribbles - but it has, save the file as .wav


copied and pasted - got a.wav file in the archive saved in.wav.

Snapshot2

Files:
 
Nikolai Karetnikov:

well, here's the thing about Google

They give the stream in Base64. I was able to convert it to mp3, but not with LINEAR16.

LINEAR16 should be converted to wav

The audio content returned as LINEAR16 also contains a WAV header.
Method: text.synthesize  |  Cloud Text-to-Speech  |  Google Cloud
Method: text.synthesize  |  Cloud Text-to-Speech  |  Google Cloud
  • cloud.google.com
Synthesizes speech synchronously: receive results after all text input has been processed. Request body The request body contains data with the following structure: Fields Response body If successful, the response body contains data with the following structure: The message returned to the client by the method. Fields The audio data bytes...
 

question to the experts

Referring to a Google service in code

In Google

1. only one header

2. key is passed via url

3. We control the engine via json file.

In curl it is like this

curl -X POST -H "Content-Type: application/json" -d @request.json https://texttospeech.googleapis.com/v1/text:synthesize?key=AIzaSyCaLxPh84wXpLkT-zOE04MlvHj3JhLXU0w

request.json

{"input":{"text":"M"},"voice":{"languageCode":"en-gb"},"audioConfig":{"audioEncoding":"LINEAR16"}}

curl gets the correct response



Now implement this with WebRequest


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {

   char    post[],result[];
   string  url="https://texttospeech.googleapis.com/v1/text:synthesize?key=AIzaSyCaLxPh84wXpLkT-zOE04MlvHj3JhLXU0w";
   string  headers;
   string  result_headers;
   int     status;
   
   
   string jsonbody;
   headers = "Content-Type: application/json";
//---

// original json file
//{"input":{"text":"M"},"voice":{"languageCode":"en-gb"},"audioConfig":{"audioEncoding":"LINEAR16"}}
////

  jsonbody = "{\"input\":{\"text\":\"M\"},\"voice\":{\"languageCode\":\"en-gb\"},\"audioConfig\":{\"audioEncoding\":\"LINEAR16\"}}";
  StringToCharArray(jsonbody,post);
  status=WebRequest("POST",url,headers,100000,post,result,result_headers);
   
   if(status==-1)
     {
      Print("Ошибка в WebRequest. Код ошибки  =",GetLastError());
      //---
      StringSetLength(url,StringFind(url,"/",8));
      MessageBox("Необходимо добавить адрес '"+url+"' в список разрешенных URL во вкладке 'Советники'","Ошибка",MB_ICONINFORMATION);
     }
   else
     {
      if(status==200)
        {
         //--- успешная загрузка
         PrintFormat("Файл успешно загружен, размер %d байт.",ArraySize(result));
         PrintFormat("Заголовки сервера: %s",result_headers);
         //--- сохраняем данные в файл
         int filehandle=FileOpen("result.wav",FILE_WRITE|FILE_BIN);
         if(filehandle!=INVALID_HANDLE)
           {
            //--- сохраняем содержимое массива result[] в файл
            FileWriteArray(filehandle,result,0,ArraySize(result));
            //--- закрываем файл
            FileClose(filehandle);
            PlaySound("\\Files\\test.mp3");
           }
         else
            Print("Ошибка в FileOpen. Код ошибки =",GetLastError());
        }
      else
         PrintFormat("Ошибка загрузки '%s', код %d",url,status);
     }
  }

But the response comes back

2020.06.02 11:52:15.887 GoogleVoice (EURUSD,H1) Ошибка загрузки 'https://texttospeech.googleapis.com/v1/text:synthesize?key=AIzaSyCaLxPh84wXpLkT-zOE04MlvHj3JhLXU0w', код 400

as if the server does not understand the array sent to it in the json variable short

Either I am forming the array incorrectly or something else?

 
TheXpert:

LINEAR16 must be wavable

must! ) And it is.

The reason is.

if you remove extra characters and "feed" the cleaned string into Base64, you get a PlaySound-readable wav file

 
Nikolai Karetnikov:

must! ) And cited.

The reason is.

if you remove extra characters and "feed" the cleaned string to Base64, you get a PlaySound wav file

it's a json :-) in a nice way, you should get the value from audioContent key

 
Nikolai Karetnikov:


You may not be able to read it, so

         int filehandle=FileOpen("result.wav",FILE_WRITE|FILE_BIN);
         if(filehandle!=INVALID_HANDLE)
           {
            //--- сохраняем содержимое массива result[] в файл
            FileWriteArray(filehandle,result,0,ArraySize(result));
            //--- закрываем файл
            FileClose(filehandle);
            PlaySound("\\Files\\test.mp3");
you get different files
 
Alexsandr San:

You may not be able to read it, which is why

Files get different

Program execution is interrupted at WebRequest stage, it does not reach the files ))))

 
Maxim Kuznetsov:

it's a json :-) in a nice way, you need to get the value of audioContent

Oh, right! Thanks!!! )))

 
Nikolai Karetnikov:

But the response comes back

as if the server doesn't understand the json array short

Either i am forming the array itself incorrectly or something else?

The problem is the trailing null character.

void OnStart()
  {

   char    post[],result[];
   string  url="https://texttospeech.googleapis.com/v1/text:synthesize?key=AIzaSyCaLxPh84wXpLkT-zOE04MlvHj3JhLXU0w";
   string  headers;
   string  result_headers;
   int     status;
   
   
   string jsonbody;
   headers = "Content-Type: application/json";
//---

// original json file
//{"input":{"text":"M"},"voice":{"languageCode":"en-gb"},"audioConfig":{"audioEncoding":"LINEAR16"}}
////

  jsonbody = "{\"input\":{\"text\":\"M\"},\"voice\":{\"languageCode\":\"en-gb\"},\"audioConfig\":{\"audioEncoding\":\"LINEAR16\"}}";
  ArrayResize(post, StringToCharArray(jsonbody,post) - 1);
  status=WebRequest("POST",url,headers,100000,post,result,result_headers);
   
   if(status==-1)
     {
      Print("Ошибка в WebRequest. Код ошибки  =",GetLastError());
      //---
      StringSetLength(url,StringFind(url,"/",8));
      MessageBox("Необходимо добавить адрес '"+url+"' в список разрешенных URL во вкладке 'Советники'","Ошибка",MB_ICONINFORMATION);
     }
   else
     {
      if(status==200)
        {
         //--- успешная загрузка
         PrintFormat("Файл успешно загружен, размер %d байт.",ArraySize(result));
         PrintFormat("Заголовки сервера: %s",result_headers);
         //--- сохраняем данные в файл
         int filehandle=FileOpen("result.wav",FILE_WRITE|FILE_BIN);
         if(filehandle!=INVALID_HANDLE)
           {
            //--- сохраняем содержимое массива result[] в файл
            FileWriteArray(filehandle,result,0,ArraySize(result));
            //--- закрываем файл
            FileClose(filehandle);
            PlaySound("\\Files\\test.mp3");
           }
         else
            Print("Ошибка в FileOpen. Код ошибки =",GetLastError());
        }
      else
      {
         PrintFormat("Ошибка загрузки '%s', код %d",url,status);
         Print("result: ", CharArrayToString(result));
      }
     }
  }

and if you get an error from webrequest, there is very possible additional information in the result parameter.

For example:

2020.06.02 12:29:27.935 google_speech (USDRUB,M30)      Ошибка загрузки 'https://texttospeech.googleapis.com/v1/text:synthesize?key=AIzaSyCaLxPh84wXpLkT-zOE04MlvHj3JhLXU0w', код 400
2020.06.02 12:29:27.935 google_speech (USDRUB,M30)      result: {
2020.06.02 12:29:27.935 google_speech (USDRUB,M30)        "error": {
2020.06.02 12:29:27.935 google_speech (USDRUB,M30)          "code": 400,
2020.06.02 12:29:27.935 google_speech (USDRUB,M30)          "message": "Invalid JSON payload received. Parsing terminated before end of input.\ncoding\":\"LINEAR16\"}}\u0000\n                    ^",
2020.06.02 12:29:27.935 google_speech (USDRUB,M30)          "status": "INVALID_ARGUMENT"
2020.06.02 12:29:27.935 google_speech (USDRUB,M30)        }
2020.06.02 12:29:27.935 google_speech (USDRUB,M30)      }
 
TheXpert:

the problem is the terminating null character.

and if you get an error from webrequest, there may very well be extra information in the result parameter.

for example:

Thank you! )