Skip to content

Commit c60c8b0

Browse files
finneyjmicrobit-carlos
authored andcommitted
Introduce multitrack audio playback capability
1 parent 10999d8 commit c60c8b0

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

inc/MicroBitAudio.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ DEALINGS IN THE SOFTWARE.
2828
#include "NRF52PWM.h"
2929
#include "SoundEmojiSynthesizer.h"
3030
#include "SoundExpressions.h"
31+
#include "SampleSource.h"
3132
#include "Mixer2.h"
3233
#include "SoundOutputPin.h"
3334
#include "StreamNormalizer.h"
@@ -39,12 +40,15 @@ DEALINGS IN THE SOFTWARE.
3940
#define MICROBIT_AUDIO_STATUS_DEEPSLEEP 0x0001
4041
#define CONFIG_DEFAULT_MICROPHONE_GAIN 0.1f
4142

42-
4343
// Configurable options
4444
#ifndef CONFIG_AUDIO_MIXER_OUTPUT_LATENCY_US
4545
#define CONFIG_AUDIO_MIXER_OUTPUT_LATENCY_US (uint32_t) ((CONFIG_MIXER_BUFFER_SIZE/2) * (1000000.0f/44100.0f))
4646
#endif
4747

48+
#ifndef CONFIG_AUDIO_INPUT_CHANNELS
49+
#define CONFIG_AUDIO_INPUT_CHANNELS 4
50+
#endif
51+
4852
namespace codal
4953
{
5054
/**
@@ -61,6 +65,7 @@ namespace codal
6165
StreamSplitter *rawSplitter; // Stream Splitter instance (raw input)
6266
LevelDetectorSPL *levelSPL; // Level Detector SPL instance
6367
LowPassFilter *micFilter; // Low pass filter to remove high frequency noise on the mic
68+
SampleSource *sampleSource[CONFIG_AUDIO_INPUT_CHANNELS]; // multichannel sample playback capability
6469

6570
private:
6671
volatile bool micEnabled; // State of on board mic

inc/SampleSource.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef SAMPLE_SOURCE_H
2+
#define SAMPLE_SOURCE_H
3+
4+
#include "MemorySource.h"
5+
#include "DataStream.h"
6+
#include "Mixer2.h"
7+
8+
namespace codal
9+
{
10+
class SampleSource : public MemorySource
11+
{
12+
private:
13+
Mixer2 &mixer;
14+
float sampleRate;
15+
float sampleRange;
16+
MixerChannel *channel;
17+
18+
public:
19+
/**
20+
* Constructor.
21+
* Creates an empty sample source with a Mixer.
22+
* = CONFIG_MIXER_DEFAULT_SAMPLERATE
23+
*/
24+
SampleSource(Mixer2 &mixer, float sampleRate, float sampleRange);
25+
26+
/**
27+
* Destructor
28+
* Removes all resources held by the instance
29+
*/
30+
~SampleSource();
31+
32+
void setSampleRate(float sampleRate);
33+
34+
void setVolume(float volume);
35+
};
36+
}
37+
#endif

model/MicroBit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ DEALINGS IN THE SOFTWARE.
6767
#include "StreamNormalizer.h"
6868
#include "LevelDetector.h"
6969
#include "LevelDetectorSPL.h"
70+
#include "SampleSource.h"
7071
#include "PulseIn.h"
7172
#include "neopixel.h"
7273

source/MicroBitAudio.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ MicroBitAudio::MicroBitAudio(NRF52Pin &pin, NRF52Pin &speaker, NRF52ADC &adc, NR
8888

8989
//Initilise stream splitter
9090
splitter = new StreamSplitter(processor->output, DEVICE_ID_SPLITTER);
91+
92+
// Create audio input channels
93+
for (int i=0; i<CONFIG_AUDIO_INPUT_CHANNELS; i++)
94+
sampleSource[i] = new SampleSource(mixer, 11000, 255);
9195
}
9296

9397
void MicroBitAudio::periodicCallback()

source/SampleSource.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "SampleSource.h"
2+
#include "CodalDmesg.h"
3+
4+
using namespace codal;
5+
6+
SampleSource::SampleSource(Mixer2 &mixer, float sampleRate, float sampleRange) : MemorySource(), mixer(mixer)
7+
{
8+
this->sampleRate = sampleRate;
9+
this->sampleRange = sampleRange;
10+
this->channel = this->mixer.addChannel(*this, sampleRate, sampleRange);
11+
connect(*channel);
12+
}
13+
14+
SampleSource::~SampleSource()
15+
{
16+
this->mixer.removeChannel(this->channel);
17+
}
18+
19+
void SampleSource::setSampleRate(float sr)
20+
{
21+
this->sampleRate = sr;
22+
this->channel->setSampleRate(sr);
23+
}
24+
25+
void SampleSource::setVolume(float volume)
26+
{
27+
this->channel->setVolume(volume);
28+
}

0 commit comments

Comments
 (0)