Skip to content

Commit

Permalink
✨ feat: add buffer size parameter to microphone function in mic_strea…
Browse files Browse the repository at this point in the history
…m.dart

The commit adds a new buffer size parameter to the `microphone()` function in `mic_stream.dart`. The default buffer size is set to `4096`, and if the parameter is not provided, the default value will be used. The buffer size determines the amount of data captured per sample and affects the latency and overhead of the audio stream.
  • Loading branch information
yama-yeah committed Mar 15, 2023
1 parent e6a5670 commit 2b000d4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/mic_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MicStream {
ChannelConfig.CHANNEL_IN_MONO;
static const AudioFormat DEFAULT_AUDIO_FORMAT = AudioFormat.ENCODING_PCM_8BIT;
static const int DEFAULT_SAMPLE_RATE = 16000;
static const int DEFAULT_BUFFER_SIZE = 4096;

static const int _MIN_SAMPLE_RATE = 1;
static const int _MAX_SAMPLE_RATE = 100000;
Expand Down Expand Up @@ -70,6 +71,7 @@ class MicStream {
static int? __sampleRate;
static ChannelConfig? __channelConfig;
static AudioFormat? __audioFormat;
static int? __bafferSize;

/// This function manages the permission and ensures you're allowed to record audio
static Future<bool> get permissionStatus async {
Expand All @@ -88,18 +90,21 @@ class MicStream {
/// the returned stream is actually returning 16-bit data, and if so, manually cast uint8List.buffer.asUint16List()
/// audioSource: The device used to capture audio. The default let's the OS decide.
/// sampleRate: The amount of samples per second. More samples give better quality at the cost of higher data transmission
/// bafferSize: The amount of data to be captured per sample. More data per sample means less overhead, but more latency
/// channelConfig: States whether audio is mono or stereo
/// audioFormat: Switch between 8- and 16-bit PCM streams
///
static Future<Stream<Uint8List>?> microphone(
{AudioSource? audioSource,
int? sampleRate,
int? bafferSize,
ChannelConfig? channelConfig,
AudioFormat? audioFormat}) async {
audioSource ??= DEFAULT_AUDIO_SOURCE;
sampleRate ??= DEFAULT_SAMPLE_RATE;
channelConfig ??= DEFAULT_CHANNELS_CONFIG;
audioFormat ??= DEFAULT_AUDIO_FORMAT;
bafferSize ??= DEFAULT_BUFFER_SIZE;

if (sampleRate < _MIN_SAMPLE_RATE || sampleRate > _MAX_SAMPLE_RATE)
throw (RangeError.range(sampleRate, _MIN_SAMPLE_RATE, _MAX_SAMPLE_RATE));
Expand All @@ -110,18 +115,21 @@ class MicStream {
if (audioSource != __audioSource ||
sampleRate != __sampleRate ||
channelConfig != __channelConfig ||
audioFormat != __audioFormat) {
audioFormat != __audioFormat ||
bufferSize != __bafferSize) {
//TODO: figure out whether the old stream needs to be cancelled
_microphone = _microphoneEventChannel.receiveBroadcastStream([
audioSource.index,
sampleRate,
channelConfig == ChannelConfig.CHANNEL_IN_MONO ? 16 : 12,
audioFormat == AudioFormat.ENCODING_PCM_8BIT ? 3 : 2
audioFormat == AudioFormat.ENCODING_PCM_8BIT ? 3 : 2,
bafferSize
]).cast<Uint8List>();
__audioSource = audioSource;
__sampleRate = sampleRate;
__channelConfig = channelConfig;
__audioFormat = audioFormat;
__bafferSize = bafferSize;
}

// sampleRate/bitDepth should be populated before any attempt to consume the stream externally.
Expand Down

0 comments on commit 2b000d4

Please sign in to comment.