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

124
LINES

< > BotCompany Repo | #1027055 // Android Raspi Bridge v3 [for Bluetooth headset, with voice output, basically works]

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (6136L/41K/139K).

1  
!7
2  
3  
// TODO: better cleanup
4  
5  
set flag Android. set flag NoAWT.
6  
7  
import android.media.*;
8  
import android.content.*;
9  
10  
static int bufferSize = 8192;
11  
static int maxAudioQueueSize = 50;
12  
13  
// raspi home, X20
14  
static LS ips = ll("192.168.2.165", "192.168.43.244");
15  
16  
static AudioManager am;
17  
static AudioRecord recorder;
18  
static new L<StefansOS_ConnectToServer> senders;
19  
static new L<Q> qSendAudio;
20  
static new Q qVoiceOut;
21  
static bool connected;
22  
23  
p {
24  
  am = (AudioManager) androidContext().getSystemService(Context.AUDIO_SERVICE);
25  
  printStreamInfos();
26  
  androidSay_keepEngine = true;
27  
  //set androidSay_keepEngine_verbose;
28  
  set androidSay_keepEngine_inCall;
29  
  
30  
  for (S ip : ips) {
31  
    print("Connecting to " + ip);
32  
    new StefansOS_ConnectToServer sender;
33  
    sender.server = ip;
34  
    sender.onLine = voidfunc(S s) {
35  
      final new Matches m;
36  
      if (startsWith_trim(s, "voiceOutput:", m))
37  
        say(m.rest());
38  
    };
39  
    sender.tryToConnect = () -> !connectToServer_anyConnected(senders);
40  
    sender.startWithSubs("voiceOutput");
41  
    senders.add(sender);
42  
    qSendAudio.add(new Q);
43  
  }
44  
  
45  
  BroadcastReceiver brr = new {
46  
    @Override
47  
    public void onReceive(Context context, Intent intent) {
48  
      int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,-1);
49  
      print(+state);
50  
      if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
51  
        print("Bluetooth Connected");
52  
        androidContext().unregisterReceiver(this);
53  
        printStreamInfos();
54  
        thread { startRecording(); }
55  
      }
56  
    }
57  
  };
58  
  
59  
  androidContext().registerReceiver(brr, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
60  
  print("Starting Bluetooth");
61  
  //am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
62  
  am.setBluetoothScoOn(true);
63  
  //am.setMode(AudioManager.MODE_IN_CALL);
64  
  am.startBluetoothSco();
65  
  print("Can BT record from mic? " + am.isBluetoothScoAvailableOffCall());
66  
}
67  
68  
svoid startRecording {
69  
  print("Making recorder");
70  
  
71  
  recorder = new AudioRecord.Builder()
72  
    .setAudioSource(
73  
      //MediaRecorder.AudioSource.VOICE_COMMUNICATION
74  
      MediaRecorder.AudioSource.MIC
75  
    )
76  
    .setAudioFormat(new AudioFormat.Builder()
77  
      .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
78  
      .setSampleRate(16000)
79  
      .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
80  
      .build())
81  
   .setBufferSizeInBytes(bufferSize)
82  
   .build();
83  
  print("Made recorder");
84  
  
85  
  recorder.startRecording();
86  
  print("Started recording");
87  
  
88  
  byte[] buf = new[bufferSize];
89  
  
90  
  while licensed {
91  
    int n = recorder.read(buf, 0, l(buf), AudioRecord.READ_BLOCKING);
92  
    //printWithTime("Read bytes: " + n);
93  
    fS line = "audio: " + base64(buf);
94  
    for i over qSendAudio: {
95  
      final Q q = qSendAudio.get(i);
96  
      final StefansOS_ConnectToServer sender = senders.get(i);
97  
      if (!sender.connected()) continue;
98  
      addToQWithMaxSize(maxAudioQueueSize, q, r {
99  
        sender.sendLine(line)
100  
      });
101  
    }
102  
    if (connectToServer_anyConnected(senders) != connected) {
103  
      connected = !connected;
104  
      say(connected ? "Connected" : "Lost connection");
105  
    }
106  
  }
107  
  
108  
  /*recorder.release();
109  
  print("Released recorder");*/
110  
}
111  
112  
svoid cleanMeUp {
113  
  if (recorder != null) recorder.release();
114  
  dispose senders;
115  
}
116  
117  
svoid printStreamInfos {
118  
  print("Stream muted: " + am.isStreamMute(AudioManager.STREAM_MUSIC) + ", vol: " + am.getStreamVolume(AudioManager.STREAM_MUSIC));
119  
}
120  
121  
svoid say(S s) {
122  
  print("> " + s);
123  
  addToQWithMaxSize_verbose(5, +qVoiceOut, r { androidSayInEnglish(s); });
124  
}

Author comment

Began life as a copy of #1027051

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, mqqgnosmbjvj, odkhaoglnmdk, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027055
Snippet name: Android Raspi Bridge v3 [for Bluetooth headset, with voice output, basically works]
Eternal ID of this version: #1027055/29
Text MD5: f8b1995256cf813c953b2374fdffe7e1
Transpilation MD5: 5221962aec736205af5718d54bbb33e6
Author: stefan
Category: javax / android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-17 10:04:12
Source code size: 3606 bytes / 124 lines
Pitched / IR pitched: No / No
Views / Downloads: 266 / 571
Version history: 28 change(s)
Referenced in: [show references]