Skip to content

Commit 0cb9cfe

Browse files
CODAL v0.3.0: Updates for the audio pipeline refactor. (#101)
* Updates for the audio pipeline refactor (WIP). * SerialStreamer has been moved to codal-core. And the version in codal-core includes a bug fix required in the audio-refactor branch. * Update tests to reflect new audio API * Change back codal-microbit-v2 branch to main, v0.3.0 released with audio refactor. --------- Co-authored-by: Joe Finney <[email protected]>
1 parent 2d23f5f commit 0cb9cfe

File tree

6 files changed

+21
-196
lines changed

6 files changed

+21
-196
lines changed

source/samples/MicrophoneTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mems_mic_test()
7676
void
7777
mems_mic_zero_offset_test()
7878
{
79-
LevelDetectorSPL* levelSPL = new LevelDetectorSPL(uBit.audio.processor->output, 85.0, 65.0, 16.0, 0, DEVICE_ID_SYSTEM_LEVEL_DETECTOR, false);
79+
LevelDetectorSPL* levelSPL = new LevelDetectorSPL(uBit.audio.processor->output, 85.0, 65.0, 16.0, 0, DEVICE_ID_SYSTEM_LEVEL_DETECTOR);
8080
uBit.audio.activateMic();
8181

8282
char float_str[20];

source/samples/OOB_v3.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ static void onButtonLogo(MicroBitEvent) {
212212

213213
int sampleRate = 11000;
214214
static SplitterChannel *splitterChannel = uBit.audio.splitter->createChannel();
215-
splitterChannel->requestSampleRate( sampleRate );
215+
// FIXME: Update this to use the new requestSampleRate
216+
// splitterChannel->requestSampleRate( sampleRate );
216217

217218
// Uncomment these two lines and comment out the *recording declaration after them to insert a low-pass-filter.
218219
// static LowPassFilter *lowPassFilter = new LowPassFilter(*splitterChannel, 0.812313f, false);

source/samples/SerialStreamer.cpp

Lines changed: 6 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -21,126 +21,15 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2121
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2222
DEALINGS IN THE SOFTWARE.
2323
*/
24-
24+
#include "MicroBit.h"
2525
#include "SerialStreamer.h"
2626
#include "Tests.h"
2727

28-
/**
29-
* Creates a simple component that logs a stream of signed 16 bit data as signed 8-bit data over serial.
30-
* @param source a DataSource to measure the level of.
31-
* @param mode the format of the serialised data. Valid options are SERIAL_STREAM_MODE_BINARY (default), SERIAL_STREAM_MODE_DECIMAL, SERIAL_STREAM_MODE_HEX.
32-
*/
33-
SerialStreamer::SerialStreamer(DataSource &source, int mode) : upstream(source)
34-
{
35-
this->mode = mode;
36-
37-
// Register with our upstream component
38-
source.connect(*this);
39-
}
40-
41-
/**
42-
* Callback provided when data is ready.
43-
*/
44-
int SerialStreamer::pullRequest()
45-
{
46-
static volatile int pr = 0;
47-
48-
if(!pr)
49-
{
50-
pr++;
51-
while(pr)
52-
{
53-
lastBuffer = upstream.pull();
54-
streamBuffer(lastBuffer);
55-
pr--;
56-
}
57-
}
58-
else
59-
{
60-
pr++;
61-
}
62-
63-
return DEVICE_OK;
64-
}
65-
66-
/**
67-
* returns the last buffer processed by this component
68-
*/
69-
ManagedBuffer SerialStreamer::getLastBuffer()
70-
{
71-
return lastBuffer;
72-
}
73-
74-
/**
75-
* Callback provided when data is ready.
76-
*/
77-
void SerialStreamer::streamBuffer(ManagedBuffer buffer)
78-
{
79-
int CRLF = 0;
80-
int bps = upstream.getFormat();
81-
82-
// If a BINARY mode is requested, simply output all the bytes to the serial port.
83-
if( mode == SERIAL_STREAM_MODE_BINARY )
84-
{
85-
uint8_t *p = &buffer[0];
86-
uint8_t *end = p + buffer.length();
87-
88-
while(p < end)
89-
uBit.serial.putc(*p++);
90-
}
91-
92-
// if a HEX mode is requested, format using printf, framed by sample size..
93-
if( mode == SERIAL_STREAM_MODE_HEX || mode == SERIAL_STREAM_MODE_DECIMAL )
94-
{
95-
uint8_t *d = &buffer[0];
96-
uint8_t *end = d+buffer.length();
97-
uint32_t data;
98-
99-
while(d < end)
100-
{
101-
data = *d++;
102-
103-
if (bps > DATASTREAM_FORMAT_8BIT_SIGNED)
104-
data |= (*d++) << 8;
105-
if (bps > DATASTREAM_FORMAT_16BIT_SIGNED)
106-
data |= (*d++) << 16;
107-
if (bps > DATASTREAM_FORMAT_24BIT_SIGNED)
108-
data |= (*d++) << 24;
109-
110-
if (mode == SERIAL_STREAM_MODE_HEX) {
111-
uBit.serial.printf("%x ", data);
112-
} else {
113-
// SERIAL_STREAM_MODE_DECIMAL
114-
if (bps == DATASTREAM_FORMAT_8BIT_SIGNED) {
115-
uBit.serial.printf("%d ", (int8_t)(data & 0xFF));
116-
} else if (bps == DATASTREAM_FORMAT_16BIT_SIGNED) {
117-
uBit.serial.printf("%d ", (int16_t)(data & 0xFFFF));
118-
} else if (bps == DATASTREAM_FORMAT_24BIT_SIGNED) {
119-
// Move the sign bit to the most significant bit
120-
int32_t signed_data = data & 0x7FFFFF;
121-
if (data & (1 << 23)) {
122-
signed_data |= 1 << 31;
123-
}
124-
uBit.serial.printf("%d ", signed_data);
125-
} else {
126-
// Cannot print uint32_t correctly as serial.printf
127-
// does not support "%u"
128-
uBit.serial.printf("%d ", data);
129-
}
130-
}
131-
132-
CRLF++;
133-
134-
if (CRLF >= 16){
135-
uBit.serial.putc('\r');
136-
uBit.serial.putc('\n');
137-
CRLF = 0;
138-
}
139-
}
28+
void streamer_serial_test() {
29+
static SplitterChannel *splitterChannel = uBit.audio.splitter->createChannel();
30+
SerialStreamer *streamer = new SerialStreamer(*splitterChannel, SERIAL_STREAM_MODE_DECIMAL);
14031

141-
if (CRLF > 0) {
142-
uBit.serial.putc( '\r' );
143-
uBit.serial.putc( '\n' );
144-
}
32+
while (true) {
33+
uBit.sleep(1000);
14534
}
14635
}

source/samples/SerialStreamer.h

Lines changed: 0 additions & 68 deletions
This file was deleted.

source/samples/StreamAPITest.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,18 @@ void stream_test_record() {
5858
static StreamRecording * recording = new StreamRecording( *input );
5959
static MixerChannel * output = uBit.audio.mixer.addChannel( *recording );
6060

61-
input->requestSampleRate( 11000 );
61+
uBit.audio.mic->setSampleRate( 11000 );
6262
output->setSampleRate( 11000 );
63-
output->setVolume( CONFIG_MIXER_INTERNAL_RANGE * 0.8 ); // 80% volume
63+
64+
output->setVolume( CONFIG_MIXER_INTERNAL_RANGE * 0.2 ); // 20% volume
6465

6566
uBit.display.printChar( '3', 1000 );
6667
uBit.display.printChar( '2', 1000 );
6768
uBit.display.printChar( '1', 1000 );
6869

6970
uBit.display.printChar( 'R' );
70-
input->requestSampleRate( 11000 );
71+
uBit.audio.mic->setSampleRate( 11000 );
72+
7173
recording->recordAsync();
7274
while( recording->isRecording() ) {
7375
uBit.display.printChar( '~' );
@@ -99,13 +101,13 @@ static const int STRSR_SAMPLE_RATE = 11000;
99101

100102
static void strsr_handle_buttonA(MicroBitEvent) {
101103
static SplitterChannel *splitterChannel = uBit.audio.splitter->createChannel();
102-
splitterChannel->requestSampleRate(STRSR_SAMPLE_RATE);
104+
uBit.audio.mic->setSampleRate( STRSR_SAMPLE_RATE );
103105
static StreamRecording *recording = new StreamRecording(*splitterChannel);
104106
static MixerChannel *channel = uBit.audio.mixer.addChannel(*recording, STRSR_SAMPLE_RATE);
105107

106108
DMESG( "Actual sample rate: %d (requested %d)", (int)splitterChannel->getSampleRate(), STRSR_SAMPLE_RATE );
107109

108-
MicroBitAudio::requestActivation();
110+
uBit.audio.requestActivation();
109111
channel->setVolume(75.0);
110112
uBit.audio.mixer.setVolume(512);
111113
uBit.audio.mixer.setSilenceLevel( 0 );
@@ -114,20 +116,20 @@ static void strsr_handle_buttonA(MicroBitEvent) {
114116
uBit.display.clear();
115117
uBit.audio.levelSPL->setUnit(LEVEL_DETECTOR_SPL_8BIT);
116118

117-
splitterChannel->requestSampleRate( STRSR_SAMPLE_RATE );
119+
uBit.audio.mic->setSampleRate( STRSR_SAMPLE_RATE );
118120

119121
DMESG( "RECORDING" );
120122
recording->recordAsync();
121123
bool showR = true;
122124
while (uBit.buttonA.isPressed()) {
123125
if( uBit.logo.isPressed() ) {
124-
splitterChannel->requestSampleRate( abs((uBit.accelerometer.getRoll()-90) * 100) );
126+
uBit.audio.mic->setSampleRate( abs((uBit.accelerometer.getRoll()-90) * 100) );
125127
DMESG( "Sample Rate: %d (mic = %d)", (int)splitterChannel->getSampleRate(), (int)uBit.audio.mic->getSampleRate() );
126128
} else {
127129
if( uBit.buttonB.isPressed() )
128-
splitterChannel->requestSampleRate( 5000 );
130+
uBit.audio.mic->setSampleRate(5000);
129131
else
130-
splitterChannel->requestSampleRate( STRSR_SAMPLE_RATE );
132+
uBit.audio.mic->setSampleRate(STRSR_SAMPLE_RATE);
131133
}
132134

133135
if (showR)

source/samples/Tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,6 @@ void stream_test_getValue_interval();
105105
void stream_test_record();
106106
void stream_test_recording_sample_rates();
107107
void stream_test_all();
108+
void streamer_serial_test();
108109

109110
#endif

0 commit comments

Comments
 (0)