Ejemplo n.º 1
0
void GateTrigger::processBlock (AudioSampleBuffer& buffer, 
                                MidiBuffer& midiMessages)
{
    float threshold = library->getThreshold();
    int64 releaseTicks = library->getReleaseTicks();
    float velocityScale = library->getVelocityScale();
    
    int windowSize = buffer.getNumSamples();
    for (int i = 0; i < buffer.getNumSamples(); i += windowSize) {
        float rms = 0;
        for (int chan = 0; chan < buffer.getNumChannels(); chan++) {
            rms += buffer.getMagnitude (chan, i, jmin(windowSize, buffer.getNumSamples() - i)); 
        }
        rms = rms / buffer.getNumChannels() * 100;
        if (rms - lastRms > threshold) {
            if (Time::getHighResolutionTicks() - lastTriggerTick > releaseTicks) {
                Pattern* pattern = sequencer->getPattern();
                Instrument* instrument = pattern->getActiveInstrument();
                // play note
                float velocity = (rms - lastRms) / velocityScale;
                DBG("RMS: " + String(rms) + " lastRMS: " + String(lastRms) + " velocity: " + String(velocity));
                int noteNumber = instrument->getNoteNumber();
                MidiMessage m = MidiMessage::noteOn (1, noteNumber, velocity);
                midiMessages.addEvent (m, i);
                // insert into sequencer pattern
                int step = round(sequencer->getPreciseStep());
                step = step % pattern->getNumSteps();
                Cell* cell = pattern->getCellAt (0, step);
                delayedInserterThread->insertNote (cell, velocity, instrument);
                // Retrigger the reset timer
                resetTimer->retrigger();
                lastTriggerTick = Time::getHighResolutionTicks();
            }
        }
        lastRms = rms;
    }   
}
Ejemplo n.º 2
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);
}