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

Conversation

dkaukov
Copy link
Collaborator

@dkaukov dkaukov commented Jan 19, 2025

This PR makes two key changes to improve audio playback and handling:

  1. Switches AudioTrack to use AudioFormat.ENCODING_PCM_16BIT for better device compatibility.
  2. Updates the wire transfer format to int8_t to address issues with Android USB drivers.

Rationale

  1. Better Compatibility:
    Some devices don’t support PCM 8-bit , as documented [here](https://developer.android.com/reference/android/media/AudioFormat#ENCODING_PCM_8BIT). Switching to PCM 16-bit ensures consistent playback across devices.
  2. USB Driver Fix:
    Android USB drivers can add zeros to audio chunks ([issue](Missing/Corrupt data with high baud rates. mik3y/usb-serial-for-android#599)). Using int8_t, where 0 represents silence, minimizes audio artifacts caused by this bug.

@dkaukov dkaukov changed the title 03. Android: Switch AudioTrack to the AudioFormat.ENCODING_PCM_16BIT Android: Switch AudioTrack to the AudioFormat.ENCODING_PCM_16BIT Jan 19, 2025
@dkaukov dkaukov requested a review from VanceVagell January 20, 2025 00:20
@VanceVagell
Copy link
Owner

Please merge the latest changes in main into this and resolve any conflicts. The Android app DB has changed schema, so I can't easily install this to test it (my devices are using the new schema).

@dkaukov
Copy link
Collaborator Author

dkaukov commented Jan 21, 2025

@VanceVagell merged

@VanceVagell
Copy link
Owner

I'm hearing audio artifacts as of this PR, please see attached test video. Listen at 0:16, 0:17, and 0:24. There is some popping.

It was subtle in that recording, but earlier I was hearing a lot of popping when first testing this. Reminds me of small buffer underruns, but not 100% sure that's what this is.

(Note that I merged with the latest in main again to test this, which adds UHF support. Please merge on your side as well.)

screen-20250120-221459.mp4

@VanceVagell
Copy link
Owner

I retested on main without your PR and I still hear the clicking, so it's unrelated, was just a red herring.

Please merge once more and I think we're good to finalize this PR.

@dkaukov
Copy link
Collaborator Author

dkaukov commented Jan 21, 2025

@VanceVagell re-merged and tested. Seems ok to me.
BTW This is was causing crash:

moduleTypeSetting.value.equals("UHF")

in java it is better to do:

   "UHF".equals(moduleTypeSetting.value)

to be null safe (it would not help in this particular case, though)

this is null safe for this case:

radioAudioService.setRadioType(
    "UHF".equals(Optional.ofNullable(moduleTypeSetting).map(setting -> setting.value).orElse("VHF")) 
    ? RadioAudioService.RADIO_MODULE_UHF 
    : RadioAudioService.RADIO_MODULE_VHF
);

@VanceVagell VanceVagell merged commit 5ee8e0d into VanceVagell:main Jan 22, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants