@@ -55,12 +55,17 @@ MicroBitAudio::MicroBitAudio(NRF52Pin &pin, NRF52Pin &speaker, NRF52ADC &adc, NR
5555 if (MicroBitAudio::instance == NULL )
5656 MicroBitAudio::instance = this ;
5757
58+ // Request a periodic callback
59+ status |= DEVICE_COMPONENT_STATUS_SYSTEM_TICK;
60+
5861 synth.allowEmptyBuffers (true );
5962
6063 mic = adc.getChannel (microphone, false );
61- adc. setSamplePeriod ( 1e6 / CONFIG_MIXER_DEFAULT_CHANNEL_SAMPLERATE );
64+ mic-> setSampleRate (CONFIG_AUDIO_DEFAULT_MICROPHONE_SAMPLERATE );
6265 mic->setGain (7 , 0 );
6366
67+ activateMic ();
68+
6469 // Implementers note: The order that the pipeline comes up here is quite sensitive. If we connect up to splitters after starting to
6570 // listen for events from them, we'll see channel startup events (which are valid!) that we don't want. So roughly always follow
6671 // the following schema:
@@ -73,46 +78,48 @@ MicroBitAudio::MicroBitAudio(NRF52Pin &pin, NRF52Pin &speaker, NRF52ADC &adc, NR
7378
7479 // Initilise input splitter
7580 rawSplitter = new StreamSplitter (mic->output );
81+ rawSplitter->filterOn (&micEnabled);
7682
7783 // Initilise stream normalizer
7884 processor = new StreamNormalizer (*rawSplitter->createChannel (), 0 .08f , true , DATASTREAM_FORMAT_8BIT_SIGNED, 10 );
7985
8086 // Initilise level detector SPL and attach to splitter
8187 // levelSPL = new LevelDetectorSPL(*rawSplitter->createChannel(), 85.0, 65.0, 16.0, 0, DEVICE_ID_MICROPHONE, false);
82- levelSPL = new LevelDetectorSPL (*rawSplitter->createChannel (), 85.0 , 65.0 , 16.0 , 52.0 , DEVICE_ID_SYSTEM_LEVEL_DETECTOR, false );
83-
84- // Connect to the rawSplitter. This must come AFTER the processor, to prevent the processor's channel activation starting the microphone
85- if (EventModel::defaultEventBus)
86- EventModel::defaultEventBus->listen (rawSplitter->id , DEVICE_EVT_ANY, this , &MicroBitAudio::onSplitterEvent, MESSAGE_BUS_LISTENER_IMMEDIATE);
88+ levelSPL = new LevelDetectorSPL (*rawSplitter->createChannel (), 85.0 , 65.0 , 16.0 , 35 .0f , DEVICE_ID_SYSTEM_LEVEL_DETECTOR);
8789
8890 // Initilise stream splitter
8991 splitter = new StreamSplitter (processor->output , DEVICE_ID_SPLITTER);
9092
91- // Connect to the splitter - this COULD come after we create it, before we add any stages, as these are dynamic and will only connect on-demand, but just in case
92- // we're going to follow the schema set out above, to be 100% sure.
93- if (EventModel::defaultEventBus) {
94- EventModel::defaultEventBus->listen (DEVICE_ID_SPLITTER, DEVICE_EVT_ANY, this , &MicroBitAudio::onSplitterEvent, MESSAGE_BUS_LISTENER_IMMEDIATE);
95- EventModel::defaultEventBus->listen (DEVICE_ID_NOTIFY, mic->output .emitFlowEvents (), this , &MicroBitAudio::onSplitterEvent, MESSAGE_BUS_LISTENER_IMMEDIATE);
96- }
93+ // Create audio input channels
94+ for (int i=0 ; i<CONFIG_AUDIO_INPUT_CHANNELS; i++)
95+ sampleSource[i] = new SampleSource (mixer, 11000 , 255 );
9796}
9897
99- void MicroBitAudio::onSplitterEvent (MicroBitEvent e){
100- if ( mic->output .isFlowing () || (e.value == SPLITTER_ACTIVATE || e.value == SPLITTER_CHANNEL_CONNECT) )
98+ void MicroBitAudio::periodicCallback ()
99+ {
100+ if (mic->isEnabled () && !micEnabled)
101+ {
102+ // DMESG("MicroBitAudio::periodicCallback: activateMic()...");
101103 activateMic ();
102- else
104+ }
105+
106+ if (!mic->isEnabled () && micEnabled)
107+ {
108+ // DMESG("MicroBitAudio::periodicCallback: deactivateMic()...");
103109 deactivateMic ();
110+ }
104111}
105112
106113void MicroBitAudio::activateMic (){
107114 runmic.setDigitalValue (1 );
108115 runmic.setHighDrive (true );
109116 adc.activateChannel (mic);
117+ adc.getChannel (microphone, false )->setStartDelay (1 );
110118 this ->micEnabled = true ;
111119}
112120
113121void MicroBitAudio::deactivateMic (){
114122 this ->micEnabled = false ;
115- // mic->disable();
116123 runmic.setDigitalValue (0 );
117124 runmic.setHighDrive (false );
118125}
0 commit comments