Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Switch AudioTrack to the AudioFormat.ENCODING_PCM_16BIT #179

Merged
merged 26 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
78e0a95
ADC bias injection
dkaukov Jan 1, 2025
58e936d
ADC auto DC removal
dkaukov Jan 1, 2025
32c903f
DRA module retries.
dkaukov Jan 2, 2025
1cfd51e
Android: Switch AudioTrack to the AudioFormat.ENCODING_PCM_16BIT
dkaukov Jan 2, 2025
6ad9e31
Cleanup
dkaukov Jan 7, 2025
9146199
Merge branch 'main' into 02_adc_dc_removal
dkaukov Jan 9, 2025
a305cbe
Merge branch '02_adc_dc_removal' into 03_android_pcm16
dkaukov Jan 9, 2025
07e56ca
Merge branch 'main' into 02_adc_dc_removal
dkaukov Jan 9, 2025
c1ea418
Merge branch '02_adc_dc_removal' into 03_android_pcm16
dkaukov Jan 9, 2025
3017175
Merge branch 'main' into 02_adc_dc_removal
dkaukov Jan 12, 2025
91fdc80
Merge branch '02_adc_dc_removal' into 03_android_pcm16
dkaukov Jan 12, 2025
d91bb51
Merge branch 'main' into 02_adc_dc_removal
dkaukov Jan 12, 2025
8e5728c
Merge branch '02_adc_dc_removal' into 03_android_pcm16
dkaukov Jan 12, 2025
feed317
Fixed merge confilict
dkaukov Jan 12, 2025
d3cfd13
Merge branch '02_adc_dc_removal' into 03_android_pcm16
dkaukov Jan 14, 2025
f04e2a8
Merge remote-tracking branch 'upstream/main'
dkaukov Jan 14, 2025
af6a9c9
Merge branch 'main' into 02_adc_dc_removal
dkaukov Jan 14, 2025
edfe795
Merge branch '02_adc_dc_removal' into 03_android_pcm16
dkaukov Jan 14, 2025
95af01c
signed pcm8 on wire
dkaukov Jan 14, 2025
abaa565
Merge remote-tracking branch 'upstream/main'
dkaukov Jan 19, 2025
9444e5f
Merge branch 'main' into 03_android_pcm16
dkaukov Jan 19, 2025
304c089
Fix for afskDemodulator
dkaukov Jan 20, 2025
a14a4bb
Merge remote-tracking branch 'upstream/main'
dkaukov Jan 21, 2025
25fb5c2
Merge branch 'main' into 03_android_pcm16
dkaukov Jan 21, 2025
f169d46
Merge remote-tracking branch 'upstream/main'
dkaukov Jan 21, 2025
e688927
Merge branch 'main' into 03_android_pcm16
dkaukov Jan 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class RadioAudioService extends Service {
// For transmitting audio to ESP32 / radio
public static final int AUDIO_SAMPLE_RATE = 22050;
public static final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
public static final int audioFormat = AudioFormat.ENCODING_PCM_8BIT;
public static final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
public static final int minBufferSize = AudioRecord.getMinBufferSize(AUDIO_SAMPLE_RATE, channelConfig, audioFormat) * 4;
private UsbManager usbManager;
private UsbDevice esp32Device;
Expand Down Expand Up @@ -1258,6 +1258,18 @@ public static UsbSerialPort getUsbSerialPort() {
return serialPort;
}

public static byte[] convert8BitTo16Bit(byte[] pcm8) {
byte[] pcm16 = new byte[pcm8.length * 2]; // 2 bytes per 16-bit sample
for (int i = 0; i < pcm8.length; i++) {
int unsignedSample = pcm8[i] & 0xFF; // Convert to unsigned
short sample16 = (short)((unsignedSample) << 8); // Scale and shift
// Store as little-endian (least significant byte first)
pcm16[i * 2] = (byte)(sample16 & 0xFF); // LSB
pcm16[i * 2 + 1] = (byte)((sample16 >> 8) & 0xFF); // MSB
}
return pcm16;
}

private void handleESP32Data(byte[] data) {
// Log.d("DEBUG", "Got bytes from ESP32: " + Arrays.toString(data));
/* try {
Expand Down Expand Up @@ -1327,10 +1339,11 @@ private void handleESP32Data(byte[] data) {
synchronized (audioTrack) {
if (afskDemodulator != null) { // Avoid race condition at app start.
// Play the audio.
audioTrack.write(data, 0, data.length);
byte[] pcm16 = convert8BitTo16Bit(data);
audioTrack.write(pcm16, 0, pcm16.length);

// Add the audio samples to the AFSK demodulator.
float[] audioAsFloats = convertPCM8ToFloatArray(data);
float[] audioAsFloats = convertPCM8SignedToFloatArray(data);
afskDemodulator.addSamples(audioAsFloats, audioAsFloats.length);
}

Expand All @@ -1351,7 +1364,8 @@ private void handleESP32Data(byte[] data) {
audioTrack.play();
}
synchronized (audioTrack) {
audioTrack.write(rxBytesPrebuffer, 0, PRE_BUFFER_SIZE);
byte[] pcm16 = convert8BitTo16Bit(rxBytesPrebuffer);
audioTrack.write(pcm16, 0, pcm16.length);
}
}

Expand Down Expand Up @@ -1572,17 +1586,14 @@ private void handleParsedCommand(byte cmd, byte[] param) {
}
}

private float[] convertPCM8ToFloatArray(byte[] pcm8Data) {
private float[] convertPCM8SignedToFloatArray(byte[] pcm8Data) {
// Create a float array of the same length as the input byte array
float[] floatData = new float[pcm8Data.length];

// Iterate through the byte array and convert each sample
for (int i = 0; i < pcm8Data.length; i++) {
// Convert unsigned 8-bit PCM to signed 8-bit value
int signedValue = (pcm8Data[i] & 0xFF) - 128;

// Normalize the signed 8-bit value to the range [-1.0, 1.0]
floatData[i] = signedValue / 128.0f;
floatData[i] = pcm8Data[i] / 128.0f;
}

return floatData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ void loop() {

// Apply attenuation to the sample
int16_t sample = (int32_t)remove_dc(((2048 - (buffer16[i] & 0xfff)) << 4)) * attenuation >> 8;
buffer8[i] = (sample >> 8) + 128; // Unsigned PCM8
buffer8[i] = (sample >> 8); // Signed
}

Serial.write(buffer8, samplesRead);
Expand Down
Loading