Advertisement
dlangu0393

[GoogleSpeechAPI]Protocol

Jan 24th, 2012
1,548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Protocol::Request_SPEECH(QByteArray & audioData)
  2. {
  3.     if (!Nt_SPEECH)
  4.     {
  5.         QNetworkRequest request;
  6.         QString speechAPI = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1";
  7.         request.setUrl(speechAPI);
  8.  
  9.         request.setRawHeader("User-Agent", "Mozilla/5.0");
  10.         request.setRawHeader("Content-Type", "audio/x-flac; rate=16000");
  11.  
  12.         qDebug(audioData);
  13.  
  14.         Nt_SPEECH = NetworkMGR.post(request, audioData);
  15.         connect(Nt_SPEECH, SIGNAL(readyRead()), this, SLOT(Read_SPEECH()));
  16.     }
  17. }
  18.  
  19.  
  20. void Protocol::Read_SPEECH()
  21. {
  22.     QString content = QString::fromUtf8( Nt_SPEECH->readAll() );
  23.  
  24.     try
  25.     {
  26.         qDebug(content.toAscii());
  27.         replyCheck(Nt_SPEECH);
  28.         contentCheck(content);
  29.  
  30.         Json::Value root;
  31.         Json::Reader reader;
  32.         reader.parse( std::string( content.toAscii().data() ), root);
  33.  
  34.         QList<QString> list;
  35.         list.append(QString("status"));
  36.         list.append(QString("id"));
  37.         list.append(QString("hypotheses"));
  38.         checkJsonKeys(root, list);
  39.  
  40.         jsonConvertableTo(root["status"], Json::intValue);
  41.         jsonConvertableTo(root["id"], Json::stringValue);
  42.         jsonConvertableTo(root["hypotheses"], Json::arrayValue);
  43.  
  44.         switch(root["status"].asInt())
  45.         {
  46.         case SPEECH_INPUT_ERROR_NONE:
  47.             break;
  48.         case SPEECH_INPUT_ERROR_NO_SPEECH:
  49.             throw SPEECH_INPUT_ERROR_NO_SPEECH;
  50.             break;
  51.         case SPEECH_INPUT_ERROR_NO_MATCH:
  52.             throw SPEECH_INPUT_ERROR_NO_MATCH;
  53.             break;
  54.         default:
  55.             throw UndefinedError;
  56.             break;
  57.         }
  58.  
  59.         // Empty response can be handled above
  60.         // So there is no need to check "hypotheses" size
  61.         Json::Value hypotheses = root["hypotheses"];
  62.         Json::Value result = hypotheses[0];
  63.  
  64.         QList<QString> list_2;
  65.         list_2.append(QString("utterance"));
  66.         list_2.append(QString("confidence"));
  67.         checkJsonKeys(result, list_2);
  68.  
  69.         emit Signal_SPEECH(0, QString( result["utterance"].asCString() ), result["confidence"].asDouble() );
  70.     }
  71.     catch (Protocol::RequestError &e)
  72.     {
  73.         emit Signal_SPEECH( (int) e, QString(), 0);
  74.     }
  75.     catch (Protocol::SpeechInputError &e)
  76.     {
  77.         emit Signal_SPEECH( (int) e, QString(), 0);
  78.     }
  79.     catch (...)
  80.     {
  81.         emit Signal_SPEECH( (int) UndefinedError, QString(), 0);
  82.     }
  83.  
  84.     disconnect(Nt_SPEECH, SIGNAL(readyRead()), this, SLOT(Read_SPEECH()));
  85.     //delete Nt_SPEECH;
  86.     Nt_SPEECH = NULL;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement