first try and works
This commit is contained in:
@@ -27,7 +27,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
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 static final int PORT = 50005;
|
||||||
private boolean isStreaming = false;
|
private boolean isStreaming = false;
|
||||||
private Thread recordingThread;
|
private Thread recordingThread;
|
||||||
@@ -97,8 +97,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
clientListenerThread.start();
|
clientListenerThread.start();
|
||||||
|
|
||||||
// Thread: Audio aufnehmen und an alle Clients senden (mit µ-law)
|
// Thread: Audio aufnehmen und an alle Clients senden (mit µ-law)
|
||||||
|
// Thread: Audio aufnehmen und an alle Clients senden (µ-Law, 8kHz)
|
||||||
recordingThread = new Thread(() -> {
|
recordingThread = new Thread(() -> {
|
||||||
int minBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
|
int minBufferSize = AudioRecord.getMinBufferSize(8000, // neue Sample-Rate
|
||||||
AudioFormat.CHANNEL_IN_MONO,
|
AudioFormat.CHANNEL_IN_MONO,
|
||||||
AudioFormat.ENCODING_PCM_16BIT);
|
AudioFormat.ENCODING_PCM_16BIT);
|
||||||
|
|
||||||
@@ -114,7 +115,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
try {
|
try {
|
||||||
recorder = new AudioRecord(
|
recorder = new AudioRecord(
|
||||||
MediaRecorder.AudioSource.MIC,
|
MediaRecorder.AudioSource.MIC,
|
||||||
SAMPLE_RATE,
|
44100, // Aufnahme weiterhin mit nativer Rate
|
||||||
AudioFormat.CHANNEL_IN_MONO,
|
AudioFormat.CHANNEL_IN_MONO,
|
||||||
AudioFormat.ENCODING_PCM_16BIT,
|
AudioFormat.ENCODING_PCM_16BIT,
|
||||||
minBufferSize
|
minBufferSize
|
||||||
@@ -127,23 +128,37 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
byte[] pcmBuffer = new byte[minBufferSize];
|
byte[] pcmBuffer = new byte[minBufferSize];
|
||||||
recorder.startRecording();
|
recorder.startRecording();
|
||||||
|
|
||||||
|
// Buffer für Resampling
|
||||||
|
short[] pcmSamples = new short[minBufferSize / 2];
|
||||||
|
|
||||||
while (isStreaming) {
|
while (isStreaming) {
|
||||||
int read = recorder.read(pcmBuffer, 0, pcmBuffer.length);
|
int read = recorder.read(pcmBuffer, 0, pcmBuffer.length);
|
||||||
if (read > 0) {
|
if (read > 0) {
|
||||||
// PCM → µ-Law konvertieren
|
// PCM Byte-Array → short[]
|
||||||
byte[] muLawData = pcm16ToMuLaw(pcmBuffer, read);
|
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) {
|
synchronized (clients) {
|
||||||
Iterator<Socket> iterator = clients.iterator();
|
Iterator<Socket> iterator = clients.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Socket client = iterator.next();
|
Socket client = iterator.next();
|
||||||
try {
|
try {
|
||||||
OutputStream out = client.getOutputStream();
|
OutputStream out = client.getOutputStream();
|
||||||
out.write(muLawData, 0, muLawData.length);
|
out.write(muLawData);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
try {
|
try { client.close(); } catch (IOException ignored) {}
|
||||||
client.close();
|
|
||||||
} catch (IOException ignored) {}
|
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
showToast("Client getrennt");
|
showToast("Client getrennt");
|
||||||
}
|
}
|
||||||
@@ -156,6 +171,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
recorder.release();
|
recorder.release();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
recordingThread.start();
|
recordingThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user