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

Get supported sample rates for devices #216

Open
alexobviously opened this issue Oct 10, 2023 · 2 comments
Open

Get supported sample rates for devices #216

alexobviously opened this issue Oct 10, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@alexobviously
Copy link
Contributor

Is your feature request related to a problem? Please describe.
When recording, I need to specify a sample rate. In most cases, we want to use 48khz, but this isn't supported by bluetooth mics which seem to be limited to 8khz or 16khz depending on the platform. We don't know whether the user has a bluetooth mic or not (although if we had this method, we could sort of determine it).

Describe the solution you'd like
Either the devices returned by listInputDevices() contains a sample rate (and obviously needs to not return null on android), or some other method that gives me a sample rate that is guaranteed to work. Or alternatively, a way of knowing whether the user is using a bluetooth device.

Describe alternatives you've considered
Using 8khz all the time, but this obviously isn't great.

Additional context
To be clear, I'm talking about android/ios here. I've been looking mostly at ios because that's where our main problems are, and it seems like the problem is AVCaptureDevice doesn't have any information about supported sample rates. I'm gonna keep looking into this and will update this if I find anything out.

@alexobviously alexobviously changed the title Get supported sample rate(s) for devices Get supported sample rates for devices Oct 10, 2023
@alexobviously
Copy link
Contributor Author

alexobviously commented Oct 10, 2023

I figured out roughly how to do this on android:

import android.content.Context
import android.media.AudioDeviceInfo
import android.media.AudioManager

// ...

            "listInputDevices" -> {
                val audioManager: AudioManager = activity!!.getSystemService(Context.AUDIO_SERVICE) as AudioManager;
                val devices: Array<AudioDeviceInfo> = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);
                val mapped = devices
                    .filter({it.isSource})
                    .map({mapOf(
                        "id" to it.getId().toString(),
                        "label" to it.getType().toString(),
                        "sampleRate" to it.getSampleRates().first(),
                    )});
                result.success(mapped)
            }

This is a work in progress - I don't really know Kotlin or Android native and I don't have a dev environment set up for it or anything, so sorry for the ugly/unidiomatic code lol

Problems so far:

  • The device type is an integer, which sucks (does this language not have enums?). I don't know if there's any good way to convert it to a meaningful name other than maintaining our own list (reference)
  • I don't know if any of this stuff can be null or empty (can activity be null?)
  • AudioDeviceInfo.getSampleRates() actually returns a list of supported sample rates, so the platform interface would have to change if you want to support it properly?
  • Obviously if we are returning devices for android, we have to also allow selecting one
  • I don't know which of these devices returned is the default one (that we currently use), it looks like it's just the first one but is that a rule?

I can finish this feature and make a PR for it (and then look at ios, but separately I guess), just wondering if you have any thoughts first.

There is also this: https://developer.android.com/reference/android/media/AudioManager#PROPERTY_OUTPUT_SAMPLE_RATE

@llfbandit
Copy link
Owner

Thanks for the try.
Unfortunatly only Android is able to provide acceptable values so I removed corresponding fields on InputDevice for now as I will release v5 really soon.
On iOS, sample rates range is from 8k to 48k with arbitrary values in between like 16k and 44.1k.
On Android, bluetooth is not supported by this package for now and sample rates range is pretty much the same as iOS.

So yeah, there's room for improvements to include Android capabilities in further versions.

I let this issue opened to track listInputDevices enhancement.

@llfbandit llfbandit added the enhancement New feature or request label Oct 14, 2023
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

No branches or pull requests

2 participants