Example #1
0
void StateWriter::setState (const void* data, int sizeInBytes)
{
    DBG("StateWriter::setState(): Loading state.")

    ValueTree settingsTree = ValueTree::readFromData (data, sizeInBytes);

    ValueTree libTree = settingsTree.getChildWithName ("library");
    library->setThreshold (libTree.getProperty ("threshold"));
    library->setReleaseTicks (libTree.getProperty ("release"));
    library->setVelocityScale (libTree.getProperty ("velocity"));

    ValueTree patternsTree = settingsTree.getChildWithName ("patterns");
    for (int i = 0; i < patternsTree.getNumChildren(); i++) {
        Pattern* pattern = library->getPattern (i);
        ValueTree patternTree = patternsTree.getChild (i);
        ValueTree instrumentsTree = patternTree.getChildWithName ("instruments");
        for (int j = 0; j < instrumentsTree.getNumChildren(); j++) {
            ValueTree instrumentTree = instrumentsTree.getChild (j);
            Instrument* instrument = pattern->getInstrumentAt (j);
            instrument->setIndex (instrumentTree.getProperty ("index"));
            instrument->setName (instrumentTree.getProperty ("name"));
            instrument->setNoteNumber (instrumentTree.getProperty ("noteNumber"));
        }
    }    
    
    ValueTree seqTree = settingsTree.getChildWithName ("sequencer");
    sequencer->setSequencerNum (seqTree.getProperty("sequencerNum"));
}
InstrumentListComponent::InstrumentListComponent (Sequencer* sequencer_) :
Component ("InstrumentListComponent"),
sequencer (sequencer_),
lastPattern (nullptr),
lastActiveInstrument (nullptr)
{
    Pattern* pattern = sequencer->getPattern();
    lastPattern = pattern;
    int numInstruments = pattern->getNumInstruments();    
    for (int i = 0; i < numInstruments; i++) {
        Instrument* instrument = pattern->getInstrumentAt (i);
        InstrumentComponent* instrumentComponent = new InstrumentComponent (instrument);
        addAndMakeVisible (instrumentComponent);
        instrumentComponents.add (instrumentComponent);
    }   
    
    for (int i = 0; i < numInstruments; i++) {
        ToggleButton* btn = new ToggleButton ((String)i);
        btn->setRadioGroupId (kRadioGroupId);
        btn->setColour (ToggleButton::textColourId, Colours::white);
        addAndMakeVisible (btn);
        btn->addListener (this);
        activeButtons.add (btn);
    }   
    
    startTimer (100);
}
// Button::Listener methods
void InstrumentListComponent::buttonClicked (Button* button)
{
    int instrumentNum = button->getButtonText().getIntValue();
    Pattern* pattern = sequencer->getPattern();
    Instrument* instrument = pattern->getInstrumentAt (instrumentNum);
    pattern->setActiveInstrument (instrument);
    lastActiveInstrument = instrument;
}
// Timer methods
void InstrumentListComponent::timerCallback()
{
    Pattern* pattern = sequencer->getPattern();
    if (sequencer->getPattern() != lastPattern) {
        lastPattern = pattern;
        for (int i = 0; i < pattern->getNumInstruments(); i++) {
            Instrument* instrument = pattern->getInstrumentAt (i);
            instrumentComponents[i]->setInstrument (instrument);
        }           
    }
    if (pattern->getActiveInstrument() != lastActiveInstrument) {
        lastActiveInstrument = pattern->getActiveInstrument();
        int i = lastActiveInstrument->getIndex();
        activeButtons[i]->setToggleState (true, false);
    }   
}
Example #5
0
void StateWriter::getState (MemoryBlock& destData)
{
    DBG("StateWriter::getState(): Saving state.")
    
    ValueTree settingsTree ("settings");
    
    ValueTree libTree ("library");
    libTree.setProperty ("threshold", library->getThreshold(), nullptr);
    libTree.setProperty ("release", library->getReleaseTicks(), nullptr);
    libTree.setProperty ("velocity", library->getVelocityScale(), nullptr);
    settingsTree.addChild (libTree, -1, nullptr);

    ValueTree patternsTree ("patterns");
    for (int i = 0; i < library->getNumPatterns(); i++) {
        Pattern* pattern = library->getPattern (i);
        ValueTree patternTree ("pattern");
        ValueTree instrumentsTree ("instruments");
        for (int j = 0; j < pattern->getNumInstruments(); j++) {
            Instrument* instrument = pattern->getInstrumentAt (j);
            ValueTree instrumentTree ("instrument");
            instrumentTree.setProperty ("index", instrument->getIndex(), nullptr);
            instrumentTree.setProperty ("name", instrument->getName(), nullptr);
            instrumentTree.setProperty ("noteNumber", instrument->getNoteNumber(), nullptr);            
            instrumentsTree.addChild (instrumentTree, -1, nullptr);
        }
        patternTree.addChild (instrumentsTree, -1, nullptr);
        patternsTree.addChild (patternTree, -1, nullptr);
    }
    settingsTree.addChild (patternsTree, -1, nullptr);
    
    ValueTree seqTree ("sequencer");
    seqTree.setProperty ("sequencerNum", sequencer->getSequencerNum(), nullptr);
    settingsTree.addChild (seqTree, -1, nullptr);
        
    MemoryOutputStream stream (destData, false);
    settingsTree.writeToStream (stream);
}