anarchuser / mic_stream

Flutter plugin to get an infinite audio stream from the microphone
https://pub.dev/packages/mic_stream
GNU General Public License v3.0
100 stars 68 forks source link

Ignoring the given bitrate on ios #72

Open rootux opened 1 year ago

rootux commented 1 year ago

for the following code -

Stream<Uint8List>? stream = await MicStream.microphone(
    sampleRate: 16000,
    channelConfig: ChannelConfig.CHANNEL_IN_MONO,
    audioFormat:AudioFormat.ENCODING_PCM_16BIT
);

final sampleRate = await MicStream.sampleRate;
final bitDepth = await MicStream.bitDepth;
final bufferSize = await MicStream.bufferSize;
logger.i(
  'Microphone settings: sampleRate = $sampleRate, bitDepth = $bitDepth, bufferSize = $bufferSize',
);

I get back this -

flutter: Microphone settings: sampleRate = 48000.0, bitDepth = 16, bufferSize = 1024

Hence the library is ignoring the settings. I know @anarchuser you did not write that part :) Trying to figure out a solution

anarchuser commented 1 year ago

Yeah, I remember there were issues regarding iOS settings/sample rate. You can try out some of the older versions, I think the recording API used in the iOS backend changed in-between

rootux commented 1 year ago

There is this comment in the code that may be related - https://github.com/anarchuser/mic_stream/blob/main/ios/Classes/SwiftMicStreamPlugin.swift#L148

// neither does setting AVLinearPCMBitDepthKey on audioOutput.audioSettings (unavailable on iOS)
// 99% sure it's not possible to set streaming sample rate/bitrate
// try AVAudioSession.sharedInstance().setPreferredOutputNumberOfChannels(numChannels)
rootux commented 1 year ago

@anarchuser Not sure what do you mean. Can you please elaborate?

I see some change in the area here - https://github.com/anarchuser/mic_stream/pull/61/files#diff-92b5a8c491b845cd6d9e20f37fe82c7d10f0410693426936cb7515f4dd23007eR118

anarchuser commented 1 year ago

It will take me some time to look into it. If you want please give it a look yourself. I think it shouldn't be difficult; I just cannot test it myself.

DurandA commented 11 months ago

On iOS, setPreferredSampleRate() is used and there is no guarantee that it will be applied. In fact, it is ignored on most devices and the actual sampling rate is device dependent.

If you require a specific sampling rate for your application, you have to resample the audio.