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

change buffer size? #52

Closed zhpixel517 closed 1 year ago

zhpixel517 commented 1 year ago

Is it possible to change the buffer size? all the microphone samples in my use case seem to be 1028 bytes. Can I make this smaller? I'm trying to stream this data over the network, so I want the buffer size to be smaller to improve latency

anarchuser commented 1 year ago

The buffer size is chosen by the audio recorders in the backends to fit their needs, based on the given configuration. There is no way to change this.

Instead, I recommend you flatten the list of samples into the stream, i.e., convert the Stream into a Stream. Then you can easily send arbitrary numbers of bytes.

Alternatively, you can just send half a list of samples each, or a quarter, etc.

zhpixel517 commented 1 year ago

Can you elaborate on what you mean by flattening the list of samples and turning the Stream into a Stream? I'm still a little new to using Streams in dart. However, I did try reducing buffer size by halving each sample and sending it like you said. Halving and sending samples didn't really seem to help latency, but only sending the first half of each sample did - at the expense of having buzzing noise. Where is the middle ground?

anarchuser commented 1 year ago

First of all, is your problem latency or bandwidth? If it's latency, splitting each list of samples up as much as possible should minimise response latency. If it's bandwidth, you will want to compress the audio data first and then send it (Only sending the first half of each sample is not a good compressing strategy, as you've noticed). One way to reduce bandwidth would be to use a much lower sample rate.

For flattening streams: If you turn the Stream<Uint8List> into a Stream<Int>, you can work with it more easily, albeit at a minimal processing cost. I'm not exactly sure how to do it syntax-wise, but look for dart documentation on Stream.expand - basically, for every element (a list) in the mic stream Stream<Uint8List>, you want to put (yield) all elements in each list into a new Stream<Int>