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

Adding support for AMR format #264

Closed
ariyadey opened this issue Mar 10, 2023 · 13 comments · Fixed by #497
Closed

Adding support for AMR format #264

ariyadey opened this issue Mar 10, 2023 · 13 comments · Fixed by #497
Assignees
Labels
enhancement New feature or request

Comments

@ariyadey
Copy link

It would be great if you add support to record in AMR format. This is a standard format for recording and can be recognized by many third-party apps.

BCR doesn't have a built-in player, so it would be very helpful to support a format that can easily be managed by third-party recording and playing apps.

Thank you!

@chenxiaolong chenxiaolong self-assigned this Mar 17, 2023
@chenxiaolong chenxiaolong added the enhancement New feature or request label Mar 17, 2023
@chenxiaolong
Copy link
Owner

I think this is a good idea and it would be pretty easy to add. However, I don't currently plan on adding it because AMR is still patented and I live in a pretty litigious country. As far as I'm aware, only the decoder can be used royalty-free, not the encoder.

@Derkish
Copy link

Derkish commented May 14, 2023

My preference is lossless, but I think supporting amr would be great (if possible in the future) for people with concerns about file storage because the quality is excellent for such a low bitrate.

@gaaf
Copy link

gaaf commented Sep 29, 2023

IIRC, Android has an AMR encoder builtin, so patents should be covered by the phone vendor already. Just don't add your own encoder.

@chenxiaolong
Copy link
Owner

That was what I thought before too, but I don't think that's a safe assumption to make anymore. Last year, a bunch of Linux distros were forced to remove support for hardware H.264/H.265 video decoding/encoding despite GPU manufacturers having already paid for the patents. I'm not willing to take the risk--I'll just wait for the patents to expire (which, if I remember correctly, is slated for some time in 2024).

@WakoKnight
Copy link

@chenxiaolong In the meantime, would it be possible to add the open-source OGG/Speex audio format to achieve better compression for voice? Speex supports mono/stereo and a bitrate range of 2 kbps to 44 kbps. It could be superior to AMR for call recording as it would allow for the creation of small files with a quality sufficient for archiving phone calls. Thank you.

@chenxiaolong
Copy link
Owner

chenxiaolong commented Dec 5, 2023

I hadn't heard of Speex. That looks like a very nice format. Unfortunately, it doesn't look like Android ships with an encoder for it: https://developer.android.com/guide/topics/media/platform/supported-formats. I currently don't have any plans in the near future to add/bundle external codecs.

In the meantime, I'd suggest using OGG/Opus with a sample rate of 16 kHz (**) and bit rate of 9 kbps if you're aiming for small file sizes. BCR's default bit rate of 48 kbps aims for very good quality without being lossless, but Opus is able to compress speech very well at low bit rates: https://wiki.hydrogenaud.io/index.php?title=Opus#Speech_encoding_quality


(**): BCR defaults to 48 kHz because some devices crash or fail with any other sample rate. If your device works with 16 kHz, it's strongly recommended because anything higher than 16 kHz doesn't capture any more useful data. The sample rate of the actual underlying phone call is 16 kHz (or even 8 kHz if your carrier doesn't support "HD audio").

@rezad1393
Copy link

this is not a related bug report, just a question:
in the changelog here
https://github.com/chenxiaolong/BCR/blob/v1.60/CHANGELOG.md
it is said that "Note that this is a lossy recording even though the raw phone call audio signal is AMR. The modem always decodes the raw AMR audio to PCM. When AMR output in BCR is selected, the PCM audio is reencoded back to AMR."

so the lte/4g/5g audio is in amr format?
but modem wont allow direct access to that and just encodes the audio in PCM?

if so then PCM is the original format of the audio (because we can't access the first AMR file)?
can I record that PCM file directly?
is that for receiving part only? I mean, when I talk my audio is recorded by microphone in what format first and is that PCM too?

can these two be combined?

I am trying to see what the highest fidelity I can achieve for recorded audio of conversation.

@chenxiaolong
Copy link
Owner

so the lte/4g/5g audio is in amr format?

Yep. The data going "over the wire" is AMR. LTE uses AMR-WB and it looks like 5G uses AMR-WB+. The encoding and decoding both happen in the modem. If the call gets recorded, Android only sees PCM.

I mean, when I talk my audio is recorded by microphone in what format first and is that PCM too?

The call recording audio stream comes from the modem, where both sides of the call are already merged together into a single stream. The received audio is always decoded from AMR -> PCM first.

For your own audio, I don't know if the modem combines the microphone's PCM stream directly or if it does something like PCM (microphone) -> AMR (to send over the wire) -> PCM (recording). It probably depends on the device. This can be tested by placing the phone next to something that AMR handles poorly, like music, and seeing if the microphone audio is clear in the recording.

I am trying to see what the highest fidelity I can achieve for recorded audio of conversation.

If you want the best fidelity, pick FLAC or WAV with a 48 kHz sample rate. Both options are equivalent and are an exact copy of the audio as Android sees it.

@rezad1393
Copy link

thanks for great answers.

wav/pcm is that audio stream from modem? then how is my outgoing audio (from mic) gets in there?

flac is losless if I recall correctly. what are the levels then? just cpu/ram usage and size related?

what about sample rate? why I can specially sample rate for wav/pcm if it is from modem (the modem itself (or the radio/lte)) does the sampling)

@chenxiaolong
Copy link
Owner

wav/pcm is that audio stream from modem?

Yes

then how is my outgoing audio (from mic) gets in there?

That is done by the modem. It combines the incoming and outgoing audio and passes the resulting PCM stream to Android.

flac is losless if I recall correctly. what are the levels then? just cpu/ram usage and size related?

That is correct. The audio is identical at every level. The high levels just have smaller file sizes. Just about every device from the past few years should have no trouble using the highest compression level. In my testing, the CPU usage is around 20-30% of one core.

what about sample rate? why I can specially sample rate for wav/pcm if it is from modem (the modem itself (or the radio/lte)) does the sampling)

Every device I've seen uses a native sample rate of 48 kHz for the call recording audio stream. BCR doesn't have a "native" option in the settings because querying the native sample rate requires a call to be active and it can take several seconds on some devices, causing the start of the call audio to be lost.

If you pick any other rate, the audio is resampled.

@chenxiaolong
Copy link
Owner

I just did a quick test. On my Pixel 8 Pro, the outgoing audio from the microphone goes through a bunch of audio processing to remove background noise before being merged with incoming audio into the call recording audio stream. The modem is definitely not recording the raw microphone audio. (Other devices might be different.)

@rezad1393
Copy link

Every device I've seen uses a native sample rate of 48 kHz for the call recording audio stream. BCR doesn't have a "native" option in the settings because querying the native sample rate requires a call to be active and it can take several seconds on some devices, causing the start of the call audio to be lost.

If you pick any other rate, the audio is resampled.

so a wav/pcm with 48Khz and a flac with 48khc are the same ? not in size but in sound quality.
as they are both lossless.

@chenxiaolong
Copy link
Owner

so a wav/pcm with 48Khz and a flac with 48khc are the same ? not in size but in sound quality.
as they are both lossless.

Yep, that's correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants