Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for getting data off microphone #3517

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions libs/microphone/micbuffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace input {
//% shim=microphone::_readyEvent
function _readyEvent(): number {
return 0xffff
}

//% shim=microphone::_pull
function _pull(): Buffer {
return null
}

/**
* (beta) Run callback whenever new raw data from microphone is available.
*/
export function onSoundData(cb: (data: Buffer) => void) {
control.onEvent(DAL.DEVICE_ID_NOTIFY, _readyEvent(), () => {
const buf = _pull()
if (buf)
cb(buf)
})
}
}
128 changes: 100 additions & 28 deletions libs/microphone/microphonehw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,63 @@
#include "pxt.h"

#if MICROBIT_CODAL

#include "LevelDetector.h"
#include "LevelDetectorSPL.h"
#include "DataStream.h"

#ifndef MIC_DEVICE
// STM?
class DummyDataSource : public codal::DataSource {
public:
DummyDataSource() {}
};
class PanicPDM {
namespace pxt {

class TeeStream : public DataSink, public DataSource {
public:
uint8_t level;
DummyDataSource source;
codal::DataStream output;
int evid;
DataSource &upstream;
DataStream output;
ManagedBuffer buffer;
bool pxtPending;

PanicPDM(Pin &sd, Pin &sck) : output(source) { target_panic(PANIC_MICROPHONE_MISSING); }
void enable() {}
void disable() {}
};
#define MIC_DEVICE PanicPDM
#endif
TeeStream(int evid, DataSource &src) : evid(evid), upstream(src), output(*this) {
pxtPending = false;
output.setBlocking(false);
upstream.connect(*this);
}

#ifndef MIC_INIT
#define MIC_INIT \
: microphone(*LOOKUP_PIN(MIC_DATA), *LOOKUP_PIN(MIC_CLOCK)) \
, level(microphone.output, 95.0, 75.0, 9, 52, DEVICE_ID_MICROPHONE)
#endif
Buffer pxtBuffer() {
if (!pxtPending)
return nullptr;
pxtPending = false;
return mkBuffer(buffer.getBytes(), buffer.length());
}

#ifndef MIC_ENABLE
#define MIC_ENABLE microphone.enable()
#endif
virtual ManagedBuffer pull() override { return buffer; }
virtual int getFormat() override { return upstream.getFormat(); }
virtual int setFormat(int format) override { return upstream.setFormat(format); }

namespace pxt {
virtual int pullRequest() override {
buffer = upstream.pull();
pxtPending = buffer.length() > 0;
output.pullRequest();
Event(DEVICE_ID_NOTIFY, evid);
return DEVICE_OK;
}
};

class WMicrophone {
public:
MIC_DEVICE microphone;
NRF52ADCChannel *microphone;
StreamNormalizer normalizer;
TeeStream tee;
LevelDetectorSPL level;
WMicrophone() MIC_INIT { MIC_ENABLE; }
WMicrophone()
: microphone(uBit.adc.getChannel(uBit.io.microphone)),
normalizer(microphone->output, 1.0f, true, DATASTREAM_FORMAT_UNKNOWN, 10),
tee(allocateNotifyEvent(), normalizer.output),
level(tee.output, 75.0, 60.0, 9, 52, DEVICE_ID_MICROPHONE) {
uBit.io.runmic.setDigitalValue(1);
uBit.io.runmic.setHighDrive(true);
microphone->setGain(7, 0);
DMESG("microphone: %d Hz", 1000000 / uBit.adc.getSamplePeriod());
}
};
SINGLETON(WMicrophone);

Expand All @@ -53,4 +70,59 @@ codal::LevelDetectorSPL *getMicrophoneLevel() {
}

} // namespace pxt
#endif

#endif

namespace microphone {

//%
int _readyEvent() {
#if MICROBIT_CODAL
return getWMicrophone()->tee.evid;
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
return 0;
#endif
}

//%
Buffer _pull() {
#if MICROBIT_CODAL
return getWMicrophone()->tee.pxtBuffer();
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
return nullptr;
#endif
}

} // namespace microphone

namespace input {

/**
* (beta) Return microphone sampling period in microseconds.
*/
//%
int soundSamplingPeriod() {
#if MICROBIT_CODAL
return uBit.adc.getSamplePeriod();
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
return 0;
#endif
}

/**
* (beta) Set microphone sampling period in microseconds. Typical range 20-200.
*/
//%
void setSoundSamplingPeriod(int us) {
#if MICROBIT_CODAL
if (us != uBit.adc.getSamplePeriod())
uBit.adc.setSamplePeriod(us);
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}

} // namespace input
11 changes: 10 additions & 1 deletion libs/microphone/pxt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/microphone",
"files": [
"README.md",
"microphone.cpp",
"microphonehw.cpp",
"micbuffer.ts",
"enums.d.ts",
"shims.d.ts",
"targetoverrides.ts"
],
"dependencies": {
"core": "file:../core"
}
}
}
14 changes: 14 additions & 0 deletions libs/microphone/shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,19 @@ declare namespace input {
//% group="micro:bit v2" threshold.defl=128 shim=input::setSoundThreshold
function setSoundThreshold(sound: SoundThreshold, threshold?: int32): void;
}
declare namespace input {

/**
* Return microphone sampling period in microseconds.
*/
//% shim=input::soundSamplingPeriod
function soundSamplingPeriod(): int32;

/**
* Set microphone sampling period in microseconds. Typical range 20-200.
*/
//% shim=input::setSoundSamplingPeriod
function setSoundSamplingPeriod(us: int32): void;
}

// Auto-generated. Do not edit. Really.