BCR icon indicating copy to clipboard operation
BCR copied to clipboard

Adding support for AMR format

Open ariyadey opened this issue 1 year ago • 6 comments

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!

ariyadey avatar Mar 10 '23 10:03 ariyadey

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.

chenxiaolong avatar Mar 17 '23 21:03 chenxiaolong

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.

Derkish avatar May 14 '23 20:05 Derkish

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

gaaf avatar Sep 29 '23 15:09 gaaf

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).

chenxiaolong avatar Sep 29 '23 16:09 chenxiaolong

@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.

WakoKnight avatar Dec 04 '23 19:12 WakoKnight

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").

chenxiaolong avatar Dec 05 '23 00:12 chenxiaolong

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.

rezad1393 avatar Feb 14 '24 18:02 rezad1393

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.

chenxiaolong avatar Feb 14 '24 18:02 chenxiaolong

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)

rezad1393 avatar Feb 14 '24 19:02 rezad1393

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 avatar Feb 14 '24 22:02 chenxiaolong

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.)

chenxiaolong avatar Feb 14 '24 22:02 chenxiaolong

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.

rezad1393 avatar Feb 15 '24 05:02 rezad1393

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.

chenxiaolong avatar Feb 15 '24 16:02 chenxiaolong