2017-09-07 5 views
0

실제로 GitHub of IBM Watson SDK (speech to text service demo)에서 얻은 ExampleSstreaming 클래스가 있습니다. 여기에 그것은IBM Watson Speech to Text 서비스가 Unity3d에서 응답을 제공하지 않습니다.

public class ExampleStreaming : MonoBehaviour 
{ 
    private int m_RecordingRoutine = 0; 
    private string m_MicrophoneID = null; 
    private AudioClip m_Recording = null; 
    private int m_RecordingBufferSize = 5; 
    private int m_RecordingHZ = 22050; 

    private SpeechToText m_SpeechToText = new SpeechToText(); 

    void Start() 
    { 
    LogSystem.InstallDefaultReactors(); 
    Log.Debug("ExampleStreaming", "Start();"); 

    Active = true; 
     Debug.Log("start"); 
    StartRecording(); 
    } 

    public void Update() { 
     Debug.Log(m_SpeechToText.IsListening); 
    } 

    public bool Active 
    { 
    get { return m_SpeechToText.IsListening; } 
    set 
    { 
     if (value && !m_SpeechToText.IsListening) 
     { 
     m_SpeechToText.DetectSilence = true; 
     m_SpeechToText.EnableWordConfidence = false; 
     m_SpeechToText.EnableTimestamps = false; 
     m_SpeechToText.SilenceThreshold = 0.03f; 
     m_SpeechToText.MaxAlternatives = 1; 
     m_SpeechToText.EnableContinousRecognition = true; 
     m_SpeechToText.EnableInterimResults = true; 
     m_SpeechToText.OnError = OnError; 
     m_SpeechToText.StartListening(OnRecognize); 
     } 
     else if (!value && m_SpeechToText.IsListening) 
     { 
     m_SpeechToText.StopListening(); 
     } 
    } 
    } 

    private void StartRecording() 
    { 
    if (m_RecordingRoutine == 0) 
    { 
      Debug.Log("m_RecordingRoutine"); 
      UnityObjectUtil.StartDestroyQueue(); 
     m_RecordingRoutine = Runnable.Run(RecordingHandler()); 
    } 
    } 

    private void StopRecording() 
    { 
    if (m_RecordingRoutine != 0) 
    { 
     Microphone.End(m_MicrophoneID); 
     Runnable.Stop(m_RecordingRoutine); 
     m_RecordingRoutine = 0; 
    } 
    } 

    private void OnError(string error) 
    { 
    Active = false; 

    Log.Debug("ExampleStreaming", "Error! {0}", error); 
    } 

    private IEnumerator RecordingHandler() 
    { 
    Log.Debug("ExampleStreaming", "devices: {0}", Microphone.devices); 

     m_MicrophoneID = Microphone.devices[0]; 
     Debug.Log("m_MicrophoneID : " + m_MicrophoneID); 
     m_Recording = Microphone.Start(m_MicrophoneID, true, m_RecordingBufferSize, m_RecordingHZ); 
    yield return null;  // let m_RecordingRoutine get set.. 
     Debug.Log("m_Recording : " + m_Recording.length); 
     if (m_Recording == null) 
    { 
      Debug.Log("m_Recording is null"); 
      StopRecording(); 
     yield break; 
    } 

    bool bFirstBlock = true; 
    int midPoint = m_Recording.samples/2; 
    float[] samples = null; 

    while (m_RecordingRoutine != 0 && m_Recording != null) 
    { 
     int writePos = Microphone.GetPosition(m_MicrophoneID); 
     if (writePos > m_Recording.samples || !Microphone.IsRecording(m_MicrophoneID)) 
     { 
     Log.Error("MicrophoneWidget", "Microphone disconnected."); 

     StopRecording(); 
     yield break; 
     } 

     if ((bFirstBlock && writePos >= midPoint) 
     || (!bFirstBlock && writePos < midPoint)) 
     { 
     // front block is recorded, make a RecordClip and pass it onto our callback. 
     samples = new float[midPoint]; 
     m_Recording.GetData(samples, bFirstBlock ? 0 : midPoint); 

     AudioData record = new AudioData(); 
     record.MaxLevel = Mathf.Max(samples); 
     record.Clip = AudioClip.Create("Recording", midPoint, m_Recording.channels, m_RecordingHZ, false); 
     record.Clip.SetData(samples, 0); 

     m_SpeechToText.OnListen(record); 

     bFirstBlock = !bFirstBlock; 
     } 
     else 
     { 
     // calculate the number of samples remaining until we ready for a block of audio, 
     // and wait that amount of time it will take to record. 
     int remaining = bFirstBlock ? (midPoint - writePos) : (m_Recording.samples - writePos); 
     float timeRemaining = (float)remaining/(float)m_RecordingHZ; 

     yield return new WaitForSeconds(timeRemaining); 
     } 

    } 

    yield break; 
    } 

    private void OnRecognize(SpeechRecognitionEvent result) 
    { 
     Debug.Log("OnRecognize"); 
     if (result != null && result.results.Length > 0) 
    { 
     foreach (var res in result.results) 
     { 
     foreach (var alt in res.alternatives) 
     { 
      string text = alt.transcript; 
        Debug.Log(text); 

      Log.Debug("ExampleStreaming", string.Format("{0} ({1}, {2:0.00})\n", text, res.final ? "Final" : "Interim", alt.confidence)); 
     } 
     } 
    } 
    } 
} 

이고 이것은 마이크를 추가하기위한 라인입니다. 나는 실제로 0 인 인덱스에서 마이크로폰 장치를 제공하기 위해 편집한다. 실제로는 null이다 (나는 왜 이것을 의도적으로 남겼는지 또는 에러인지 모른다). RecordingHandler.

m_MicrophoneID = Microphone.devices[0]; 

하지만 불행히도 그것은 내가 그것을 실행해야한다고 생각 이벤트 OnRecognize에서 모든 출력 로그를 표시하지 않습니다.

몇 초 후에 이러한 로그를 표시합니다 (오디오의 길이가 5로 주어짐). 내가 뭘 잘못하고 있는지, 그 방법을 이해할 수 없다는 것연설.

[DEBUG] OnListenClosed(), State = DISCONNECTED 
[DEBUG] KeepAlive exited. 

은 또한 그것은 또한 아무것도 표시되지 장면을 텍스트로 IBM 왓슨 음성을 시도했습니다.

답변

1

아직 실시간 출력을 스트리밍 할 수 없지만 오디오 클립을 왓슨 서비스를 통해 텍스트로 변환 할 수있게되었으며 여기에 간단한 코드 (3 일 소요)가 있습니다.

using UnityEngine; 
using System.Collections; 
using IBM.Watson.DeveloperCloud.Services.SpeechToText.v1; 

public class AudioClipToTextWatson : MonoBehaviour { 
    // Non-streaming 
    SpeechToText m_SpeechToText = new SpeechToText(); 
    public AudioClip m_AudioClip = new AudioClip(); 
    public bool on = false; 

    void Start() { 
     m_AudioClip = Microphone.Start(Microphone.devices[0], false, 4, 44100); 

      m_SpeechToText.Recognize(m_AudioClip, OnRecognize); 
      // Streaming 
      m_SpeechToText.StartListening(OnRecognize); 
      // Stop listening 
      m_SpeechToText.StopListening(); 
    } 


    private void OnRecognize(SpeechRecognitionEvent result) 
    { 
     Debug.Log("result : " + result); 
     if (result != null && result.results.Length > 0) 
     { 
      foreach (var res in result.results) 
      { 
       foreach (var alt in res.alternatives) 
       { 
        string text = alt.transcript; 
        Debug.Log(text); 
        Debug.Log(res.final); 
       } 
      } 
     } 
    } 

} 

참고 : 기록하고 오디오 클립 마이크를 사용하여 텍스트로 변환 할 수 있습니다. 이미 오디오가있는 경우 검사관에게 오디오를 놓고 첫 번째 줄을 시작 이벤트로 주석 처리하십시오.

관련 문제