音声認識をしたいです

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
人工無能をつくりたいです

音声認識をしたいです

#1

投稿記事 by 人工無能をつくりたいです » 9年前

続けて質問させていただきます。
http://www.buildinsider.net/small/perc/05
上のリンク先の記事を参考にして、Intel Perceptual Computing SDKによる音声認識を
目指しています。

コード:

// aitanaka.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <Windows.h>
#include <stdio.h>
#include <time.h>
#include "util_pipeline.h"
#include "voice_out.h"
#pragma comment( lib, "winmm.lib" )

using namespace std;

//
//class Pipeline: public UtilPipeline {
//public:
//
//  // 1.コンストラクタ
//  Pipeline(void)
//    : UtilPipeline()
//  {
//    // 必要なデータを有効にする
//    EnableVoiceRecognition();
//  }
//
//  // 2.音声エンジンのセットアップを行う
//  virtual void  OnVoiceRecognitionSetup(PXCVoiceRecognition::ProfileInfo * finfo)
//  {
//    // 日本語の音声エンジンを探す
//    auto voiceRecognition = QueryVoiceRecognition();
//    for ( int i = 0; ; ++i ) {
//      PXCVoiceRecognition::ProfileInfo pinfo = { 0 };
//      auto ret = voiceRecognition->QueryProfile( i, &pinfo );
//      if ( ret != PXC_STATUS_NO_ERROR ) {
//        break;
//      }
//
//      if ( pinfo.language == PXCVoiceRecognition::ProfileInfo::LANGUAGE_JP_JAPANESE ) {
//        *finfo = pinfo;
//        std::cout << "日本語の音声エンジンを設定しました" << std::endl;
//      }
//    }
//
//    std::cout << "音声認識を開始します" << std::endl;
//  }
//
//  // 3.音声認識されたテキストを取得する
//  virtual void PXCAPI OnRecognized( PXCVoiceRecognition::Recognition *cmd ) {
//    std::wcout << L"認識した文: " << cmd->dictation << std::endl;
//  }
//};


void widen(const std::string &src, std::wstring &dest) {
	size_t returnvalue;
	wchar_t *wcs = new wchar_t[src.length() + 1];
	mbstowcs_s(&returnvalue,wcs,src.length() + 1, src.c_str(), src.length() + 1);
	dest = wcs;
	delete [] wcs;
}


void voicesyn(wstring message){
	
	// 2.wide-charactorを扱えるようにする
  std::locale::global(std::locale("japanese"));

  // 3. UtilPipelineクラスをそのまま利用する
  UtilPipeline pipeline;
  pipeline.Init();

  // 4. 音声合成の機能を利用可能にする
  PXCVoiceSynthesis* synthesis = nullptr;
  pipeline.QuerySession()->CreateImpl<PXCVoiceSynthesis>( &synthesis );

  // 5. 音声エンジンを設定する
  PXCVoiceSynthesis::ProfileInfo pinfo = { 0 };
  for ( int i = 0; ; ++i ) {
    auto ret = synthesis->QueryProfile( i, &pinfo );
    if ( ret != PXC_STATUS_NO_ERROR ) {
      break;
    }

    if ( pinfo.language == PXCVoiceRecognition::ProfileInfo::LANGUAGE_JP_JAPANESE ) {
      synthesis->SetProfile( &pinfo );
      break;
    }
  }

   // 6.テキストを音声化し、スピーカーから出力する
  VoiceOut voice( &pinfo );
    // 音声合成のキューに入れる
    pxcUID id=0;
    synthesis->QueueSentence( (wchar_t*)message.c_str(), message.size(), &id );

    for (;;) {
      PXCSmartSP sp;
      PXCAudio *sample;

      // 音声合成を行う
      auto ret = synthesis->ProcessAudioAsync(id, &sample, &sp);
      if ( ret<PXC_STATUS_NO_ERROR ) {
        break;
      }

      ret = sp->Synchronize();
      if ( ret<PXC_STATUS_NO_ERROR ) {
        break;
      }

      // 音声データをスピーカーに出力する
      voice.RenderAudio(sample);
		}
	}

int _tmain(int argc, _TCHAR* argv[])
{
	string line,str;
	wstring message,name2,str2;
	int a,nameNo,lineNo,random;
	setlocale(LC_CTYPE, "JPN");
	

	cout<<"田中:会話しましょう"<<endl;
	message=L"会話しましょう";
	voicesyn(message);

	cout<<"あなた:";
	while(cin){
		a=0;
		cin>>line;
		ifstream fs1("greeting.txt");
		ifstream fs2("abuse.txt");
		ifstream fs4("name.txt");
		ifstream fs5("random.txt");

		if (fs1.fail())
		{
			cerr << "失敗" << endl;
			return -1;
		}
		if (fs2.fail())
		{
			cerr << "失敗" << endl;
			return -1;
		}
		if (fs4.fail())
		{
			cerr << "失敗" << endl;
			return -1;
		}
		if (fs5.fail())
		{
			cerr << "失敗" << endl;
			return -1;
		}

		while(getline(fs1,str)){
			if((line.find(str)!=-1)&&(a==0)){
			cout<<"田中:どうも"<<endl;
			message=L"どうも";
			voicesyn(message);
			a++;
			}
		}

		while(getline(fs2,str)){
		if((line.find(str)!=-1)&&(a==0)){
			cout<<"田中:ひどいですね"<<endl;
			message=L"ひどいですね";
			voicesyn(message);
			a++;
			}
		}
		
		if((line.find("私の名前")!=-1)&&(a==0)){
			nameNo=line.find("です")-line.find("名前は")-6;
			string name(line,10,nameNo);
			ifstream fs4("name.txt");
			while(getline(fs4,str)){
				if(name.find(str)!=-1){
				cout<<"田中:また会いましたね、"<<name<<"さん"<<endl;
				message=L"また会いましたね";
				voicesyn(message);
				a=1;
				}
			}
			
			if(a==0){
				ofstream fs3("name.txt",ios::app);
				fs3<<name<<endl;
				cout<<"田中:こんにちは、"<<name<<"さん"<<endl;
				message=L"こんにちは";
				voicesyn(message);
			}
			a++;
		}

		if(a==0){
		lineNo=0;
		srand((unsigned int)time(NULL));
		random=rand()%5+1;
			
			while(getline(fs5,str)){
				lineNo++;
				if(lineNo==random){
					cout<<"田中:"<<str<<endl;
					widen(str,str2);
					message=str2;
					voicesyn(message);
				}

			}
		}

		cout<<"あなた:";
	}
	
	return 0;
}

上手くいかないのでコメントアウトしています。
実行すると、
日本語の音声エンジンを設定しました
音声認識を開始します
までは表示されるのですが、そのあといくらマイクに話しても
何も表示されません。
実は、http://www.buildinsider.net/small/perc/02
のsampleを実行した際、commandモードはうまくいくのですが、
dictationモードは、反応しません。英語の場合はうまくいきますし、
日本語エンジンは認識されているはずと思い質問させていただきましたが、
percsdkの問題なのでしょうか?
よろしくお願いします。

人工無能をつくりたいです

Re: 音声認識をしたいです

#2

投稿記事 by 人工無能をつくりたいです » 9年前

質問の内容が直接c言語に関わっていないかもしれないので、
質問を取り消します。ありがとうございました。

閉鎖

“C言語何でも質問掲示板” へ戻る