Skip to content

Commit 9734bf3

Browse files
committed
Merge branch 'tests/audio'
2 parents 300bcb4 + ceba6d7 commit 9734bf3

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

model/MicroBit.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ void MicroBit::onListenerRegisteredEvent(Event evt)
331331
case DEVICE_ID_SYSTEM_LEVEL_DETECTOR:
332332
// A listener has been registered for the level detector.
333333
// The level detector uses lazy instantiation, we just need to read the data once to start it running.
334-
audio.level->getValue();
334+
//audio.level->getValue();
335+
// The level detector requires that we enable constant listening, otherwise no events will be emitted.
336+
audio.level->activateForEvents( true );
335337
break;
336338

337339
case DEVICE_ID_MICROPHONE:

source/MicroBitAudio.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ MicroBitAudio::MicroBitAudio(NRF52Pin &pin, NRF52Pin &speaker, NRF52ADC &adc, NR
6060
synth.allowEmptyBuffers(true);
6161

6262
mic = adc.getChannel(microphone, false);
63-
adc.setSamplePeriod( 1e6 / 22000 );
64-
mic->setGain(7,0);
63+
adc.setSamplePeriod( 1e6 / 11000 );
64+
mic->setGain(7, 0);
6565

66-
// Implementers note: The order that the pipeline comes up here is quite senitive. If we connect up to splitters after starting to
66+
// Implementers note: The order that the pipeline comes up here is quite sensitive. If we connect up to splitters after starting to
6767
// listen for events from them, we'll see channel startup events (which are valid!) that we don't want. So roughly always follow
6868
// the following schema:
6969
//
@@ -74,14 +74,18 @@ MicroBitAudio::MicroBitAudio(NRF52Pin &pin, NRF52Pin &speaker, NRF52ADC &adc, NR
7474
// This prevents any cases where the pipeline stages cause a connect() message to be emitted, which then auto-activates the mic.
7575

7676
//Initialialise the microphone filter
77-
micFilter = new LowPassFilter(mic->output, 0.1f, true);
77+
//micFilter = new LowPassFilter(mic->output, 0.35f, true);
7878

7979
//Initilise input splitter
80-
rawSplitter = new StreamSplitter(*micFilter);
80+
//rawSplitter = new StreamSplitter(*micFilter);
81+
rawSplitter = new StreamSplitter(mic->output);
8182

8283
//Initilise stream normalizer
8384
processor = new StreamNormalizer(*rawSplitter->createChannel(), 0.08f, true, DATASTREAM_FORMAT_8BIT_SIGNED, 10);
8485

86+
//Initilise level detector SPL and attach to splitter
87+
levelSPL = new LevelDetectorSPL(*rawSplitter->createChannel(), 85.0, 65.0, 16.0, 0, DEVICE_ID_MICROPHONE, false);
88+
8589
// Connect to the rawSplitter. This must come AFTER the processor, to prevent the processor's channel activation starting the microphone
8690
if(EventModel::defaultEventBus)
8791
EventModel::defaultEventBus->listen(rawSplitter->id, DEVICE_EVT_ANY, this, &MicroBitAudio::onSplitterEvent, MESSAGE_BUS_LISTENER_IMMEDIATE);
@@ -92,26 +96,30 @@ MicroBitAudio::MicroBitAudio(NRF52Pin &pin, NRF52Pin &speaker, NRF52ADC &adc, NR
9296
//Initilise level detector and attach to splitter
9397
level = new LevelDetector(*splitter->createChannel(), 150, 75, DEVICE_ID_SYSTEM_LEVEL_DETECTOR, false);
9498

95-
//Initilise level detector SPL and attach to splitter
96-
levelSPL = new LevelDetectorSPL(*rawSplitter->createChannel(), 85.0, 65.0, 16.0, 0, DEVICE_ID_MICROPHONE, false);
97-
9899
// 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
99100
// we're going to follow the schema set out above, to be 100% sure.
100101
if(EventModel::defaultEventBus)
101102
EventModel::defaultEventBus->listen(DEVICE_ID_SPLITTER, DEVICE_EVT_ANY, this, &MicroBitAudio::onSplitterEvent, MESSAGE_BUS_LISTENER_IMMEDIATE);
103+
104+
//activateMic();
102105
}
103106

104107
/**
105108
* Handle events from splitter
106109
*/
107110
void MicroBitAudio::onSplitterEvent(MicroBitEvent e){
108-
if( e.value == SPLITTER_CHANNEL_CONNECT )
111+
112+
DMESGF( "Event! (%d -> %d)", e.source, e.value );
113+
114+
if( e.value == SPLITTER_ACTIVATE || e.value == SPLITTER_CHANNEL_CONNECT )
109115
activateMic();
110116

111117
// Note: The processor will always be present on the rawSplitter, hence the <= 1.
112-
else if( e.value == SPLITTER_CHANNEL_DISCONNECT )
113-
if( splitter->numberActiveChannels <= 0 && rawSplitter->numberActiveChannels <= 1 )
118+
else if( e.value == SPLITTER_DEACTIVATE || e.value == SPLITTER_CHANNEL_DISCONNECT )
119+
{
120+
if( splitter->activeChannels < 1 )
114121
deactivateMic();
122+
}
115123
}
116124

117125
/**
@@ -138,14 +146,14 @@ void MicroBitAudio::deactivateMic(){
138146
*/
139147
void MicroBitAudio::deactivateLevelSPL(){
140148
//levelSPL->disable();
149+
levelSPL->activateForEvents( false );
141150
}
142151

143152
/**
144153
* Set normaliser gain
145154
*/
146155
void MicroBitAudio::setMicrophoneGain(int gain){
147156
processor->setGain(gain/100);
148-
149157
}
150158

151159
/**

0 commit comments

Comments
 (0)