@@ -7,9 +7,21 @@ import 'package:flutter/services.dart';
77// In reference to the implementation of the official sensors plugin
88// https://github.com/flutter/plugins/tree/master/packages/sensors
99
10- enum AudioSource {DEFAULT , MIC , VOICE_UPLINK , VOICE_DOWNLINK , VOICE_CALL , CAMCORDER , VOICE_RECOGNITION , VOICE_COMMUNICATION , REMOTE_SUBMIX , UNPROCESSED , VOICE_PERFORMANCE }
11- enum ChannelConfig {CHANNEL_IN_MONO , CHANNEL_IN_STEREO }
12- enum AudioFormat {ENCODING_PCM_8BIT , ENCODING_PCM_16BIT }
10+ enum AudioSource {
11+ DEFAULT ,
12+ MIC ,
13+ VOICE_UPLINK ,
14+ VOICE_DOWNLINK ,
15+ VOICE_CALL ,
16+ CAMCORDER ,
17+ VOICE_RECOGNITION ,
18+ VOICE_COMMUNICATION ,
19+ REMOTE_SUBMIX ,
20+ UNPROCESSED ,
21+ VOICE_PERFORMANCE
22+ }
23+ enum ChannelConfig { CHANNEL_IN_MONO , CHANNEL_IN_STEREO }
24+ enum AudioFormat { ENCODING_PCM_8BIT , ENCODING_PCM_16BIT }
1325
1426const AudioSource _DEFAULT_AUDIO_SOURCE = AudioSource .DEFAULT ;
1527const ChannelConfig _DEFAULT_CHANNELS_CONFIG = ChannelConfig .CHANNEL_IN_MONO ;
@@ -19,15 +31,21 @@ const int _DEFAULT_SAMPLE_RATE = 16000;
1931const int _MIN_SAMPLE_RATE = 1 ;
2032const int _MAX_SAMPLE_RATE = 100000 ;
2133
22- const EventChannel _microphoneEventChannel = EventChannel ('aaron.code.com/mic_stream' );
34+ const EventChannel _microphoneEventChannel =
35+ EventChannel ('aaron.code.com/mic_stream' );
2336
2437Permissions _permission;
2538Stream <dynamic > _microphone;
2639
2740// This function manages the permission and ensures you're allowed to record audio
2841Future <bool > get permissionStatus async {
29- _permission = (await Permission .getPermissionsStatus ([PermissionName .Microphone ])).first;
30- if (_permission.permissionStatus != PermissionStatus .allow) _permission = (await Permission .requestPermissions ([PermissionName .Microphone ])).first;
42+ _permission =
43+ (await Permission .getPermissionsStatus ([PermissionName .Microphone ]))
44+ .first;
45+ if (_permission.permissionStatus != PermissionStatus .allow)
46+ _permission =
47+ (await Permission .requestPermissions ([PermissionName .Microphone ]))
48+ .first;
3149 return (_permission.permissionStatus == PermissionStatus .allow);
3250}
3351
@@ -37,12 +55,26 @@ Future<bool> get permissionStatus async {
3755/// sampleRate: The amount of samples per second. More samples give better quality at the cost of higher data transmission
3856/// channelConfig: States whether audio is mono or stereo
3957/// audioFormat: Switch between 8- and 16-bit PCM streams
40- Stream <List <int >> microphone ({AudioSource audioSource: _DEFAULT_AUDIO_SOURCE , int sampleRate: _DEFAULT_SAMPLE_RATE , ChannelConfig channelConfig: _DEFAULT_CHANNELS_CONFIG , AudioFormat audioFormat: _DEFAULT_AUDIO_FORMAT }) async * {
41- if (sampleRate < _MIN_SAMPLE_RATE || sampleRate > _MAX_SAMPLE_RATE ) throw (RangeError .range (sampleRate, _MIN_SAMPLE_RATE , _MAX_SAMPLE_RATE ));
58+ Stream <List <int >> microphone (
59+ {AudioSource audioSource: _DEFAULT_AUDIO_SOURCE ,
60+ int sampleRate: _DEFAULT_SAMPLE_RATE ,
61+ ChannelConfig channelConfig: _DEFAULT_CHANNELS_CONFIG ,
62+ AudioFormat audioFormat: _DEFAULT_AUDIO_FORMAT }) async * {
63+ if (sampleRate < _MIN_SAMPLE_RATE || sampleRate > _MAX_SAMPLE_RATE )
64+ throw (RangeError .range (sampleRate, _MIN_SAMPLE_RATE , _MAX_SAMPLE_RATE ));
4265 if (! (await permissionStatus)) throw (PlatformException );
43- if (_microphone == null ) _microphone = _microphoneEventChannel
44- .receiveBroadcastStream ([audioSource.index, sampleRate, channelConfig == ChannelConfig .CHANNEL_IN_MONO ? 16 : 12 , audioFormat == AudioFormat .ENCODING_PCM_8BIT ? 3 : 2 ]);
45- yield * (audioFormat == AudioFormat .ENCODING_PCM_8BIT ) ? _parseStream (_microphone) : _squashStream (_microphone);
66+
67+ if (_microphone == null )
68+ _microphone = _microphoneEventChannel.receiveBroadcastStream ([
69+ audioSource.index,
70+ sampleRate,
71+ channelConfig == ChannelConfig .CHANNEL_IN_MONO ? 16 : 12 ,
72+ audioFormat == AudioFormat .ENCODING_PCM_8BIT ? 3 : 2
73+ ]);
74+
75+ yield * (audioFormat == AudioFormat .ENCODING_PCM_8BIT )
76+ ? _parseStream (_microphone)
77+ : _squashStream (_microphone);
4678}
4779
4880// I'm getting a weird stream (_BroadcastStream<dynamic>), so to work with this, I cast it to Stream<List<int>>
@@ -58,7 +90,6 @@ List<int> _parseList(var samples) {
5890 return sampleList;
5991}
6092
61-
6293// The following is needed for 16bit PCM transmission, as I can only transmit byte arrays from java to dart
6394// This function then squashes two bytes together to one short
6495Stream <List <int >> _squashStream (Stream audio) {
@@ -73,8 +104,7 @@ List<int> _squashList(var byteSamples) {
73104 for (var sample in byteSamples) {
74105 if (isFirstElement) {
75106 sum += sample * 256 ;
76- }
77- else {
107+ } else {
78108 sum += sample;
79109 shortSamples.add (sum - 32767 );
80110 sum = 0 ;
0 commit comments