compatibility-to-childmonitor-clients #2

Merged
pi merged 2 commits from compatibility-to-childmonitor-clients into master 2025-10-21 17:40:10 +02:00
Showing only changes of commit abced7d982 - Show all commits

View File

@@ -27,7 +27,7 @@ import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int SAMPLE_RATE = 44100;
private static final int SAMPLE_RATE = 8000;
private static final int PORT = 50005;
private boolean isStreaming = false;
private Thread recordingThread;
@@ -97,8 +97,9 @@ public class MainActivity extends AppCompatActivity {
clientListenerThread.start();
// Thread: Audio aufnehmen und an alle Clients senden (mit µ-law)
// Thread: Audio aufnehmen und an alle Clients senden (µ-Law, 8kHz)
recordingThread = new Thread(() -> {
int minBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
int minBufferSize = AudioRecord.getMinBufferSize(8000, // neue Sample-Rate
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
@@ -114,7 +115,7 @@ public class MainActivity extends AppCompatActivity {
try {
recorder = new AudioRecord(
MediaRecorder.AudioSource.MIC,
SAMPLE_RATE,
44100, // Aufnahme weiterhin mit nativer Rate
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize
@@ -127,23 +128,37 @@ public class MainActivity extends AppCompatActivity {
byte[] pcmBuffer = new byte[minBufferSize];
recorder.startRecording();
// Buffer für Resampling
short[] pcmSamples = new short[minBufferSize / 2];
while (isStreaming) {
int read = recorder.read(pcmBuffer, 0, pcmBuffer.length);
if (read > 0) {
// PCM → µ-Law konvertieren
byte[] muLawData = pcm16ToMuLaw(pcmBuffer, read);
// PCM Byte-Array → short[]
ByteBuffer.wrap(pcmBuffer, 0, read).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(pcmSamples, 0, read / 2);
// Resample 44.1kHz -> 8kHz (einfaches Downsampling, z.B. jede 5.5. Probe nehmen)
short[] downsampled = new short[(int) (pcmSamples.length * 8000f / 44100f)];
for (int i = 0; i < downsampled.length; i++) {
downsampled[i] = pcmSamples[(int) (i * 44100f / 8000f)];
}
// PCM → µ-Law
byte[] muLawData = new byte[downsampled.length];
for (int i = 0; i < downsampled.length; i++) {
muLawData[i] = linearToMuLawSample(downsampled[i]);
}
// Senden an alle Clients
synchronized (clients) {
Iterator<Socket> iterator = clients.iterator();
while (iterator.hasNext()) {
Socket client = iterator.next();
try {
OutputStream out = client.getOutputStream();
out.write(muLawData, 0, muLawData.length);
out.write(muLawData);
} catch (IOException e) {
try {
client.close();
} catch (IOException ignored) {}
try { client.close(); } catch (IOException ignored) {}
iterator.remove();
showToast("Client getrennt");
}
@@ -156,6 +171,7 @@ public class MainActivity extends AppCompatActivity {
recorder.release();
});
recordingThread.start();
}