XR4-display / src / XRSequenceInstrumentTrack.cpp
XRSequenceInstrumentTrack.cpp
Raw
#include <sstream>
#include "XRSequenceInstrumentTrack.h"

void SequenceInstrumentTrack::setDeviceActive(bool selDeviceActive)
{
   this->deviceActive = selDeviceActive;
}

void SequenceInstrumentTrack::setDeviceOptions(const std::vector<std::string>& deviceOptions)
{
    this->configPropOptions.devices = deviceOptions;
}

void SequenceInstrumentTrack::setConfigProp(unsigned int prop, unsigned int val)
{    
    unsigned int &d = this->device;
    unsigned int &ch = this->channel;
    unsigned int &c = this->cue;
    unsigned int &l = this->loop;
    unsigned int &f = this->force;

    switch (prop) {
        case 0 :
            if (val > 0) {
                this->deviceActive = true;
            } else {
                this->deviceActive = false;
            }
            d = val;
            break;
        case 1 :
            ch = val;
            break;
        case 2 :
            c = val;
            break;
        case 3 :
            l = val;
            break;
        case 4 :
            f = val;
            break;
        default:
            break;
    }
}

std::string SequenceInstrumentTrack::getConfigPropSettingValue(unsigned int prop) const
{
    std::string numStr;
    std::ostringstream numStrTmp;

    switch (prop) {
        case (unsigned int)InsConfigPropSettingsType::DEVICE :
            return this->getConfigPropSettingOption(prop, this->device);
        case (unsigned int)InsConfigPropSettingsType::CHANNEL :
            numStrTmp << this->channel;
            numStr = numStrTmp.str();
            return numStr;
        case (unsigned int)InsConfigPropSettingsType::CUE :
            return this->getConfigPropSettingOption(prop, this->cue);
        case (unsigned int)InsConfigPropSettingsType::LOOP :
            return this->getConfigPropSettingOption(prop, this->loop);
        case (unsigned int)InsConfigPropSettingsType::FORCE :
            return this->getConfigPropSettingOption(prop, this->force);
        default:
            return "N/A";
    }
}

std::string SequenceInstrumentTrack::getConfigPropSettingOption(unsigned int propIdx, unsigned int opt) const
{
    if (propIdx != (unsigned int)InsConfigPropSettingsType::CHANNEL) {
        InsConfigPropSettingsTemplate propOpts = this->configPropOptions;
        std::vector<std::string> opts;

        if (propIdx == (unsigned int)InsConfigPropSettingsType::DEVICE) {
            opts = propOpts.devices;
        } else if (propIdx == (unsigned int)InsConfigPropSettingsType::CUE) {
            opts = propOpts.cue;
        } else if (propIdx == (unsigned int)InsConfigPropSettingsType::LOOP) {
            opts = propOpts.loop;
        } else if (propIdx == (unsigned int)InsConfigPropSettingsType::FORCE) {
            opts = propOpts.force;
        }

        if (!opts.empty() && opts.size() >= opt) {
            std::string cOpt = opts.at(opt);

            return cOpt;
        }
    }

    return "N/A";
}

unsigned int SequenceInstrumentTrack::getConfigPropSettingMax(unsigned int prop) const
{
    InsConfigPropSettingsTemplate propOpts = this->configPropOptions;

    unsigned int max = 16; // default max for channel prop

    if (prop != (unsigned int)InsConfigPropSettingsType::CHANNEL) {
        std::vector<std::string> opts;

        if (prop == (unsigned int)InsConfigPropSettingsType::DEVICE) {
            opts = propOpts.devices;
        } else if (prop == (unsigned int)InsConfigPropSettingsType::CUE) {
            opts = propOpts.cue;
        } else if (prop == (unsigned int)InsConfigPropSettingsType::LOOP) {
            opts = propOpts.loop;
        } else if (prop == (unsigned int)InsConfigPropSettingsType::FORCE) {
            opts = propOpts.force;
        }

        max = opts.size();
    }

    return max;
}

unsigned int SequenceInstrumentTrack::getConfigPropCount()
{
    return SequenceInstrumentTrack::configPropCount;
}

const char* SequenceInstrumentTrack::getConfigPropDefault(unsigned int prop)
{
    return this->configPropValueDefaults[prop];
}

const char* SequenceInstrumentTrack::getConfigPropName(unsigned int prop)
{
    return this->configPropNames[prop];
}

const char* SequenceInstrumentTrack::getConfigPropNameShort(unsigned int prop)
{
    return this->configPropNamesShort[prop];
}

int SequenceInstrumentTrack::getCueVal() const
{
    int val = 0;
    
    if (this->cue == 1) {
        return -4;
    } else if (this->cue == 2) {
        return -8;
    } else if (this->cue == 3) {
        return -14;
    }

    return val;
}

void SequenceInstrumentTrack::initializeProgramAt(unsigned int prgIdx)
{
//    Serial.print("initializeProgramAt() -> prgIdx: ");
//    Serial.println(prgIdx);

    auto *nPrg = new SequenceInstrumentTrackProgram(
        (int) this->tIndex,
        (int) prgIdx
    );

    if (this->instrumentTrackPrograms.size() >= prgIdx) {
        Serial.println("This is an edit, don't create a new program");
        // This is an edit, don't create a new program
        return;
    }

    this->instrumentTrackPrograms.reserve(this->instrumentTrackPrograms.size()+1);
    this->instrumentTrackPrograms.push_back(nPrg);
}

void SequenceInstrumentTrack::discardProgramAt(unsigned int prgIdx, bool onNewProgram)
{
    Serial.print("discardProgramAt() -> prgIdx: ");
    Serial.println(prgIdx);

    bool firstProgramButMoreExist = (prgIdx == 1 && this->instrumentTrackPrograms.size() > 1);
    bool notFirstProgramButNotNew = (prgIdx > 1 && prgIdx <= this->instrumentTrackPrograms.size() && !onNewProgram);

    if (firstProgramButMoreExist || notFirstProgramButNotNew) {
        // re-apply old fields to program / undo new changes
        for (auto &prg : this->instrumentTrackPrograms) {
            if (prg->pIndex == (int) prgIdx) {
                prg->undoChanges();
            }
        }

        return;
    }

    // else destroy the new program
    this->instrumentTrackPrograms.erase(this->instrumentTrackPrograms.begin() + (prgIdx-1));
}