!7 // TODO: better cleanup set flag Android. set flag NoAWT. import android.media.*; import android.content.*; static int bufferSize = 8192; static int maxAudioQueueSize = 50; // raspi home, X20 static LS ips = ll("192.168.2.165", "192.168.43.244"); static AudioManager am; static AudioRecord recorder; static new L senders; static new L qSendAudio; static new Q qVoiceOut; static bool connected; p { am = (AudioManager) androidContext().getSystemService(Context.AUDIO_SERVICE); printStreamInfos(); androidSay_keepEngine = true; //set androidSay_keepEngine_verbose; set androidSay_keepEngine_inCall; for (S ip : ips) { print("Connecting to " + ip); new StefansOS_ConnectToServer sender; sender.server = ip; sender.onLine = voidfunc(S s) { final new Matches m; if (startsWith_trim(s, "voiceOutput:", m)) say(m.rest()); }; sender.tryToConnect = () -> !connectToServer_anyConnected(senders); sender.startWithSubs("voiceOutput"); senders.add(sender); qSendAudio.add(new Q); } BroadcastReceiver brr = new { @Override public void onReceive(Context context, Intent intent) { int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,-1); print(+state); if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) { print("Bluetooth Connected"); androidContext().unregisterReceiver(this); printStreamInfos(); thread { startRecording(); } } } }; androidContext().registerReceiver(brr, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)); print("Starting Bluetooth"); //am.setStreamSolo(AudioManager.MODE_IN_CALL, true); am.setBluetoothScoOn(true); //am.setMode(AudioManager.MODE_IN_CALL); am.startBluetoothSco(); print("Can BT record from mic? " + am.isBluetoothScoAvailableOffCall()); } svoid startRecording { print("Making recorder"); recorder = new AudioRecord.Builder() .setAudioSource( //MediaRecorder.AudioSource.VOICE_COMMUNICATION MediaRecorder.AudioSource.MIC ) .setAudioFormat(new AudioFormat.Builder() .setEncoding(AudioFormat.ENCODING_PCM_16BIT) .setSampleRate(16000) .setChannelMask(AudioFormat.CHANNEL_IN_MONO) .build()) .setBufferSizeInBytes(bufferSize) .build(); print("Made recorder"); recorder.startRecording(); print("Started recording"); byte[] buf = new[bufferSize]; while licensed { int n = recorder.read(buf, 0, l(buf), AudioRecord.READ_BLOCKING); //printWithTime("Read bytes: " + n); fS line = "audio: " + base64(buf); for i over qSendAudio: { final Q q = qSendAudio.get(i); final StefansOS_ConnectToServer sender = senders.get(i); if (!sender.connected()) continue; addToQWithMaxSize(maxAudioQueueSize, q, r { sender.sendLine(line) }); } if (connectToServer_anyConnected(senders) != connected) { connected = !connected; say(connected ? "Connected" : "Lost connection"); } } /*recorder.release(); print("Released recorder");*/ } svoid cleanMeUp { if (recorder != null) recorder.release(); dispose senders; } svoid printStreamInfos { print("Stream muted: " + am.isStreamMute(AudioManager.STREAM_MUSIC) + ", vol: " + am.getStreamVolume(AudioManager.STREAM_MUSIC)); } svoid say(S s) { print("> " + s); addToQWithMaxSize_verbose(5, +qVoiceOut, r { androidSayInEnglish(s); }); }