Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

141
LINES

< > BotCompany Repo | #1001276 // Unterhaltung

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (703L/6K/18K).

!747

import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.media.*;

main {
  static Activity androidContext;
  
  static SpeechRecognizer sr;
  static final String TAG = "MyStt3Activity";
  static boolean loop = true;
  static S language = "de-DE";
  static int extraResults = 1;
  
  static S voice = "Leopold";
  
  static new L<S> history;
   
  p {
    say("hallo");
    
    androidContext.runOnUiThread(runnable {
      //androidMuteAudio(androidContext);
      newRecognizer();
    });
    sleep();
  }
  
  static void newRecognizer() {
    sr = SpeechRecognizer.createSpeechRecognizer(androidContext);
    sr.setRecognitionListener(new listener());        
    recog();
  }

  static class listener implements RecognitionListener {
    public void onReadyForSpeech(Bundle params) {
      print("SAG WAS.");
    }
    
    public void onBeginningOfSpeech() {}
    public void onRmsChanged(float rmsdB) {}
    public void onBufferReceived(byte[] buffer) {}
    public void onEndOfSpeech() {}
    
    public void onError(int error) {
       //print( "error " +  error); // screw the errors!
       //if (loop) recog();
       try {
         sr.destroy();
       } catch (Throwable e) {
         print(e);
       }
       if (loop)
         newRecognizer();
    }
    
    public void onResults(Bundle results) {
      //print("onResults " + results);
      ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
      final S s = data.get(0);
      print("Ich habe geh\u00f6rt: " + quote(s));
      //print("I heard: " +structure(data));
  
      // TODO: fix the java strings umlaut problem
       
      final boolean goodbye = match3("goodbye", s) || match3("bye", s) || match3("tsch\u00fcss", s);
      
      thread {
        // get answer
        
        history.add(s);
        
        S answer;
        try {
          answer = goodbye ? "tsch\u00fcss" : answer(s, history);
        } catch (Throwable e) {
          e.printStackTrace();
          answer = "Fehler";
        }
        
        if (answer != null)
          say(answer);
          
        androidContext.runOnUiThread(runnable {
          if (goodbye) {
            print("\nGOODBYE!");
            sr.destroy();
          } else if (loop) {
            sr.stopListening();
            recog();
          }
        });
      } // end of thread
    }
    
    public void onPartialResults(Bundle partialResults) {}
    public void onEvent(int eventType, Bundle params) {}
  }
   
  static void recog() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, extraResults); 
    sr.startListening(intent);
    //print("started listening");
  }
  
  static synchronized S answer(S s, L<S> history) {
    return s;
  }
  
  static void say(S s) {
    androidUnmuteAudio(androidContext);
    File mp3 = new File(getProgramDir(), "bla.mp3");
    makeSpeechCereproc(s, voice, mp3);
    androidPlayMp3(androidContext, mp3);
    androidMuteAudio(androidContext);
  }
  
  static void partialMute(Activity androidContext) {
    AudioManager amanager = (AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE);
    amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
    amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
    //amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
    amanager.setStreamMute(AudioManager.STREAM_RING, true);
    amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
  }
}

Author comment

Began life as a copy of #1001259

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, ufhdzwgrioxs, vouqrxazstgt

No comments. add comment

Snippet ID: #1001276
Snippet name: Unterhaltung
Eternal ID of this version: #1001276/1
Text MD5: 3bae88c282ae0b0bb2255710f6ac34ae
Transpilation MD5: fea884f9516cfc9c341d1cdf16ed18e7
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-06 13:22:38
Source code size: 4239 bytes / 141 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 649 / 728
Referenced in: #1001278 - Unterhaltung mit Leopold 2
#3000189 - Answer for stefanreich(>> t bla)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)