Program Listing for File SoundEmojiSynthesizer.cpp#

Return to documentation for file (libraries/codal-microbit-v2/source/SoundEmojiSynthesizer.cpp)

/*
The MIT License (MIT)

Copyright (c) 2017 Lancaster University.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "SoundEmojiSynthesizer.h"
#include "Synthesizer.h"
#include "CodalFiber.h"
#include "CodalUtil.h"
#include "ErrorNo.h"
#include "MicroBitAudio.h"

using namespace codal;

SoundEmojiSynthesizer::SoundEmojiSynthesizer(uint16_t id, int sampleRate) : CodalComponent(id, 0), buffer(EMOJI_SYNTHESIZER_BUFFER_SIZE), emptyBuffer(0)
{
    this->downStream = NULL;
    this->bufferSize = EMOJI_SYNTHESIZER_BUFFER_SIZE;
    this->position = 0.0f;
    this->effect = NULL;
    this->partialBuffer = NULL;
    this->playbackCompleteIn = 0;
    this->buffer2 = ManagedBuffer(bufferSize);

    this->samplesToWrite = 0;
    this->samplesWritten = 0;

    setSampleRate(sampleRate);
    setSampleRange(1023);
    setOrMask(0);
}

SoundEmojiSynthesizer::~SoundEmojiSynthesizer()
{
}

void SoundEmojiSynthesizer::connect(DataSink &sink)
{
    this->downStream = &sink;
}

bool SoundEmojiSynthesizer::isConnected()
{
    return this->downStream != NULL;
}

int SoundEmojiSynthesizer::getFormat()
{
    return DATASTREAM_FORMAT_16BIT_UNSIGNED;
}

int SoundEmojiSynthesizer::setBufferSize(int size)
{
    if (bufferSize <= 0)
        return DEVICE_INVALID_PARAMETER;

    this->bufferSize = size;
    return DEVICE_OK;
}

int SoundEmojiSynthesizer::play(ManagedBuffer sound)
{
    // Enable audio pipeline if needed.
    MicroBitAudio::requestActivation();

    // Validate inputs
    if (sound.length() < (int) sizeof(SoundEffect))
        return DEVICE_INVALID_PARAMETER;

    // If a playout is already in progress, block until it has been scheduled.
    lock.wait();

    // Store the requested sequence of sound effects.
    effectBuffer = sound;

    // Scheduled this sound effect for playout.
    // Generation will start the next time a pull() operation is called from downstream.
    nextSoundEffect();

    // Perform on demand activiation if this is the first time this compoennt has been used.
    // Simply issue a pull request to start the process.
    if (!(status & EMOJI_SYNTHESIZER_STATUS_ACTIVE))
    {
        status |= EMOJI_SYNTHESIZER_STATUS_ACTIVE;
        downStream->pullRequest();
    }

    return DEVICE_OK;
}

void SoundEmojiSynthesizer::stop() {
    if (effect)
        status |= EMOJI_SYNTHESIZER_STATUS_STOPPING;
}

bool SoundEmojiSynthesizer::nextSoundEffect()
{
    const bool hadEffect = effect != NULL;
    if (status & EMOJI_SYNTHESIZER_STATUS_STOPPING)
    {
        effect = NULL;
        effectBuffer = emptyBuffer;
    }

    // If a sequence of SoundEffects are being played, attempt to move on to the next.
    // If not, select the first in the buffer.
    if (effect)
        effect++;
    else
        effect = (SoundEffect *) &effectBuffer[0];

    // Validate that we have a valid sound effect. If not, record that we have nothing to play.
    if ((uint8_t *)effect >= &effectBuffer[0] + effectBuffer.length())
    {
        // if we have an effect with a negative duration, reset the buffer (unless there is an update pending)
        effect = (SoundEffect *) &effectBuffer[0];

        if (effect->duration >= 0 || lock.getWaitCount() > 0)
        {
            effect = NULL;
            effectBuffer = emptyBuffer;
            samplesWritten = 0;
            samplesToWrite = 0;
            return hadEffect;
        }
    }

    // We have a valid buffer. Set up our synthesizer to the requested parameters.
    samplesToWrite = determineSampleCount(effect->duration);
    frequency = effect->frequency;
    volume = effect->volume;
    samplesWritten = 0;

    // validate and initialise per effect rendering state.
    for (int i=0; i<EMOJI_SYNTHESIZER_TONE_EFFECTS; i++)
    {
        effect->effects[i].step = 0;
        effect->effects[i].steps = max(effect->effects[i].steps, 1);
        samplesPerStep[i] = (float) samplesToWrite / (float) effect->effects[i].steps;
    }
    return false;
}

ManagedBuffer SoundEmojiSynthesizer::pull()
{
    ManagedBuffer output = buffer2;

    // If the last DMA buffer was only partially filled, try to fill it.
    if (partialBuffer)
        fillOutputBuffer();

    // We only allow one chance at filling a DMA buffer, so that we don't stall the pipeline.
    partialBuffer = NULL;
    buffer2 = buffer;

    // Generate another buffer if possible. This may be empty if there is no sound effect scheduled.
    fillOutputBuffer();

    // Issue a Pull Request so that we are always receiver driven, and we're done.
    downStream->pullRequest();

    // Issue a deferred DEVICE_SOUND_EMOJI_SYNTHESIZER_EVT_PLAYBACK_COMPLETE event if requested.
    if (playbackCompleteIn > 0)
    {
        playbackCompleteIn--;

        if (playbackCompleteIn == 0)
            Event(id, DEVICE_SOUND_EMOJI_SYNTHESIZER_EVT_PLAYBACK_COMPLETE);
    }

    return output;
}

ManagedBuffer SoundEmojiSynthesizer::fillOutputBuffer()
{
    // Generate a buffer on demand. This is likely to be in interrupt context, so
    // the receiver driven nature reduces glitching on audio output.
    bool done = false;
    uint16_t *sample = NULL;
    uint16_t *bufferEnd;

    while (!done)
    {
        if (samplesWritten == samplesToWrite || status & EMOJI_SYNTHESIZER_STATUS_STOPPING)
        {
            bool renderComplete = nextSoundEffect();

            // If we have just completed active playout of an effect, and there are no more effects scheduled,
            // unblock any fibers that may be waiting to play a sound effect.
            if (samplesToWrite == 0 || status & EMOJI_SYNTHESIZER_STATUS_STOPPING)
            {
                done = true;
                if (renderComplete || status & EMOJI_SYNTHESIZER_STATUS_STOPPING)
                {
                    if (renderComplete)
                        partialBuffer = sample;

                    // Flip our status bit and fire the event
                    status &= ~EMOJI_SYNTHESIZER_STATUS_STOPPING;
                    playbackCompleteIn = CONFIG_EMOJI_SYNTHESIZER_OUTPUT_BUFFER_DEPTH+2;
                    Event(id, DEVICE_SOUND_EMOJI_SYNTHESIZER_EVT_DONE);
                    lock.notify();
                }
            }
        }

        // If we have something to do, ensure our buffers are created.
        // We defer creation to avoid unecessary heap allocation when genertaing silence.
        if (((samplesWritten < samplesToWrite) || !(status & EMOJI_SYNTHESIZER_STATUS_OUTPUT_SILENCE_AS_EMPTY)) && sample == NULL)
        {
            // If we completed the last sound effect in the middl eof a DMA buffer, continue from where we left off
            if (partialBuffer)
            {
                sample = partialBuffer;
                partialBuffer = NULL;
            }
            else
            {
                buffer = ManagedBuffer(bufferSize);
                sample = (uint16_t *) &buffer[0];
            }

            bufferEnd = (uint16_t *) (&buffer[0] + buffer.length());
        }

        // Generate some samples with the current effect parameters.
        while(samplesWritten < samplesToWrite)
        {
            float skip = ((EMOJI_SYNTHESIZER_TONE_WIDTH_F * frequency) / sampleRate);
            float gain = (sampleRange * volume) / 1024.0f;
            float offset = 512.0f - (512.0f * gain);

            int effectStepEnd[EMOJI_SYNTHESIZER_TONE_EFFECTS];

            for (int i = 0; i < EMOJI_SYNTHESIZER_TONE_EFFECTS; i++)
            {
                effectStepEnd[i] = (int) (samplesPerStep[i] * (effect->effects[i].step));
                if (effect->effects[i].step == effect->effects[i].steps - 1)
                    effectStepEnd[i] = samplesToWrite;
            }

            int stepEndPosition = effectStepEnd[0];
            for (int i = 1; i < EMOJI_SYNTHESIZER_TONE_EFFECTS; i++)
                stepEndPosition = min(stepEndPosition, effectStepEnd[i]);

            // Write samples until the end of the next effect-step
            while (samplesWritten < stepEndPosition)
            {
                // Stop processing when we've filled the requested buffer
                if (sample == bufferEnd)
                    return buffer;

                // Synthesize a sample
                float s = effect->tone.tonePrint(effect->tone.parameter, (int) position);

                // Apply volume scaling and OR mask (if specified).
                *sample = ((uint16_t) ((s * gain) + offset)) | orMask;

                // Move on our pointers.
                sample++;
                samplesWritten++;
                position += skip;

                // Keep our toneprint pointer in range
                while(position > EMOJI_SYNTHESIZER_TONE_WIDTH_F)
                    position -= EMOJI_SYNTHESIZER_TONE_WIDTH_F;
            }

            // Invoke the effect function for any effects that are due.
            for (int i = 0; i < EMOJI_SYNTHESIZER_TONE_EFFECTS; i++)
            {
                if (samplesWritten == effectStepEnd[i])
                {
                    if (effect->effects[i].step < effect->effects[i].steps)
                    {
                        if (effect->effects[i].effect)
                            effect->effects[i].effect(this, &effect->effects[i]);

                        effect->effects[i].step++;
                    }
                }
            }
        }
    }

    // if we have no data to send, return an empty buffer (if requested)
    if (sample == NULL)
    {
        buffer = ManagedBuffer();
    }
    else
    {
        // Pad the output buffer with silence if necessary.
        uint16_t silence = ((uint16_t) (sampleRange *0.5f)) | orMask;
        while(sample < bufferEnd)
        {
            *sample = silence;
            sample++;
        }
    }

    return buffer;
}

float SoundEmojiSynthesizer::getSampleRate()
{
    return sampleRate;
}

int SoundEmojiSynthesizer::setSampleRate(int sampleRate)
{
    if( sampleRate < 1 )
        return ErrorCode::DEVICE_INVALID_PARAMETER;
    this->sampleRate = sampleRate;
    return DEVICE_OK;
}

int SoundEmojiSynthesizer::determineSampleCount(float playoutTime)
{
    if (playoutTime < 0)
        playoutTime = -playoutTime;

    float seconds = playoutTime / 1000.0f;
    return (int) ((float)sampleRate * seconds);
}

uint16_t SoundEmojiSynthesizer::getSampleRange()
{
    return (uint16_t)sampleRange;
}

int SoundEmojiSynthesizer::setSampleRange(uint16_t sampleRange)
{
    this->sampleRange = (float)sampleRange;
    return DEVICE_OK;
}

int SoundEmojiSynthesizer::setOrMask(uint16_t mask)
{
    orMask = mask;
    return DEVICE_OK;
}


void SoundEmojiSynthesizer::allowEmptyBuffers(bool mode)
{
    if (mode)
        this->status |= EMOJI_SYNTHESIZER_STATUS_OUTPUT_SILENCE_AS_EMPTY;
    else
        this->status &= ~EMOJI_SYNTHESIZER_STATUS_OUTPUT_SILENCE_AS_EMPTY;


}