e di nuovo dll e mercato - pagina 27

 
Alexsandr San:

ecco la prima esecuzione - ha caricato un file di testo con testo russo, alcuni scarabocchi - ma ha, salvare il file come .wav


copiato e incollato - ha ottenuto un file.wav nell'archivio salvato in.wav.

Istantanea2

File:
 
Nikolai Karetnikov:

Beh, ecco il fatto di Google

Danno il flusso in Base64. Sono riuscito a convertirlo in mp3, ma non con LINEAR16.

LINEAR16 dovrebbe essere convertito in wav

Il contenuto audio restituito come LINEAR16 contiene anche un'intestazione WAV.
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...
 

domanda agli esperti

Fare riferimento a un servizio di Google nel codice

In Google

1. solo un'intestazione

2. la chiave viene passata tramite url

3. Controlliamo il motore tramite il file json.

In curl è così

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

richiesta.json

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

curl ottiene la risposta corretta



Ora implementa questo con 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);
     }
  }

Ma la risposta torna

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

come se il server non capisse l'array inviatogli nella variabile json breve

O sto formando l'array in modo sbagliato o qualcos'altro?

 
TheXpert:

LINEAR16 deve essere ondulabile

deve! ) Ed è così.

La ragione è.

se si rimuovono i caratteri extra e si "alimenta" la stringa pulita in Base64, si ottiene un file wav leggibile da PlaySound

 
Nikolai Karetnikov:

deve! ) E citato.

La ragione è.

se rimuovi i caratteri extra e "dai in pasto" la stringa pulita a Base64, ottieni un file wav PlaySound

è un json :-) in modo carino, dovresti ottenere il valore dalla chiave audioContent

 
Nikolai Karetnikov:


Potreste non essere in grado di leggerlo, quindi

         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");
si ottengono file diversi
 
Alexsandr San:

Potresti non essere in grado di leggerlo, ecco perché

I file diventano diversi

L'esecuzione del programma viene interrotta nella fase WebRequest, non raggiunge i file ))))

 
Maxim Kuznetsov:

è un json :-) in buona fede, è necessario ottenere il valore dalla chiave audioContent

Oh, giusto! Grazie!!! )))

 
Nikolai Karetnikov:

Ma la risposta torna

come se il server non capisse l'array json breve

O sto formando l'array in modo sbagliato o qualcos'altro?

Il problema è il carattere nullo finale.

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));
      }
     }
  }

e se si ottiene un errore da webrequest, è molto probabile che ci siano informazioni aggiuntive nel parametro del risultato.

Per esempio:

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:

il problema è il carattere nullo finale.

e se si ottiene un errore da webrequest, ci possono benissimo essere informazioni extra nel parametro del risultato.

per esempio:

Grazie! )

Motivazione: