I'm trying to make an app that takes audio signal from microphone (using Superpowered) and then shoves it into a Datagram packet to send it. As far as I understood I should use SuperpoweredAndroidAudioIO class for input, but I didn't get how I can read its buffer to send it.
Basically I want to implement something similar to this using Superpowered and C++:
...
recBufSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration,
audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize);
new Thread(){
byte[] buffer = new byte[recBufSize];
public void run(){
try {
datagramSocket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
audioRecord.startRecording();
isRecording = true;
while (isRecording){
int readSize = audioRecord.read(buffer, 0, buffer.length);
try {
DatagramPacket packet = new DatagramPacket(
buffer, readSize, receiverAddress, port);
datagramSocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
datagramSocket.close();
}
}.start();
I'm new to JNI and NDK, so I just want to know whether this is possible or reasonable in order to decrease latency (comparing to using Java code) and if yes maybe a small hint on where to start. Thanks.
This is definitely possible and it will reduce latency as well. However it's magnitudes more complex to implement. Blocking in the audio processing callback is not recommended, therefore you need some lockless mechanism to send audio from the audio processing callback to another thread. In that other thread then use standard BSD sockets to send the data over UDP.