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

Added the sound recognition part #5

Closed

Conversation

vladturcuman
Copy link

@vladturcuman vladturcuman commented Apr 15, 2021

Changed the MicroBitAudioProcessor to also be a DataSource. Added a Hann Window and Harmonic Product Spectrum before and after fft to get more accurate results for square wave detection.

Added MicroBitSoundRecogniser as a DataSink, which recognises given patterns in the frequency data. It connects to the audio processor to get the frequency analysis. The constructor is protected such that the class becomes abstract. EmojiRecogniser inherits it and implements the constructor - adds the happy sound as the sound to be recognised.

To recognise the happy sound the following main.cpp can be used:

#include "MicroBitSerial.h"
#include "MicroBit.h"
#include "CodalDmesg.h"
#include "MicroBitAudioProcessor.h"
#include "EmojiRecogniser.h"
#include "StreamNormalizer.h"
#include "Tests.h"

#include "LevelDetector.h"
#include "StreamSplitter.h"

static NRF52ADCChannel *mic = NULL;
static StreamNormalizer *processor = NULL;
static MicroBitAudioProcessor *fft = NULL;

static LevelDetector *level = NULL;
static StreamSplitter *splitter = NULL;

MicroBitSoundRecogniser *recogniser = NULL;

MicroBit uBit;

void onSound(ManagedString sound) {
    recogniser->stopAnalisying();

    uBit.display.scroll(sound);

    recogniser->startAnalisying(onSound);
}

int main()
{
    uBit.init();

    NRF52ADCChannel *mic = uBit.adc.getChannel(uBit.io.microphone);
    mic->setGain(7,0);
    uBit.io.runmic.setDigitalValue(1);
    uBit.io.runmic.setHighDrive(true);

    StreamNormalizer *processor = new StreamNormalizer(mic->output, 1.2f, true, DATASTREAM_FORMAT_8BIT_SIGNED, 10);
    StreamSplitter *splitter = new StreamSplitter(processor->output);
    fft         = new MicroBitAudioProcessor(*splitter);
    recogniser  = new EmojiRecogniser(*fft, uBit);

    recogniser->startAnalisying(onSound);

    while(1){
        uBit.display.print("-");
        uBit.sleep(1000);
    }

}

Changed the MicroBitAudioProcessor to also be a DataSource. Added a Hann Window and Harmonic Product Spectrum before and after fft to get more accurate results for square wave detection.

Added MicroBitSoundRecogniser as a DataSink, which recognises given patterns in the frequency data. It connects to the audio processor to get the frequency analysis. The constructor is protected such that the class becomes abstract. EmojiRecogniser inherits it and implements the constructor - adds the happy sound as the sound to be recognised.

To recognise the happy sound the following main.cpp can be used:

#include "MicroBitSerial.h"
#include "MicroBit.h"
#include "CodalDmesg.h"
#include "MicroBitAudioProcessor.h"
#include "EmojiRecogniser.h"
#include "StreamNormalizer.h"
#include "Tests.h"

#include "LevelDetector.h"
#include "StreamSplitter.h"

static NRF52ADCChannel *mic = NULL;
static StreamNormalizer *processor = NULL;
static MicroBitAudioProcessor *fft = NULL;

static LevelDetector *level = NULL;
static StreamSplitter *splitter = NULL;

MicroBitSoundRecogniser *recogniser = NULL;

MicroBit uBit;

void onSound(ManagedString sound) {
    recogniser->stopAnalisying();

    uBit.display.scroll(sound);

    recogniser->startAnalisying(onSound);
}

int main()
{
    uBit.init();

    NRF52ADCChannel *mic = uBit.adc.getChannel(uBit.io.microphone);
    mic->setGain(7,0);
    uBit.io.runmic.setDigitalValue(1);
    uBit.io.runmic.setHighDrive(true);

    StreamNormalizer *processor = new StreamNormalizer(mic->output, 1.2f, true, DATASTREAM_FORMAT_8BIT_SIGNED, 10);
    StreamSplitter *splitter = new StreamSplitter(processor->output);
    fft         = new MicroBitAudioProcessor(*splitter);
    recogniser  = new EmojiRecogniser(*fft, uBit);

    recogniser->startAnalisying(onSound);

    while(1){
        uBit.display.print("-");
        uBit.sleep(1000);
    }

}
@vladturcuman vladturcuman changed the title Add MicroBitSoundRecogniser and changed MicroBitAudioProcessor Added the sound recognition part Apr 15, 2021
@vladturcuman
Copy link
Author

Moved the pull request to the lancaster-university / codal-microbit-v2 repo, here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant