void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
{
    for (int i = 0; i < list.size(); ++i)
    {
        const MidiMessage& mm = list.getUnchecked(i)->message;

        if (mm.isSysEx())
            destSequence.addEvent (mm);
    }
}
//==============================================================================
void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
                                                      MidiMessageSequence& destSequence,
                                                      const bool alsoIncludeMetaEvents) const
{
    for (int i = 0; i < list.size(); ++i)
    {
        const MidiMessage& mm = list.getUnchecked(i)->message;

        if (mm.isForChannel (channelNumberToExtract) || (alsoIncludeMetaEvents && mm.isMetaEvent()))
            destSequence.addEvent (mm);
    }
}
MidiMessageSequence* MidiStochaster::createMelody(int minOctaveOffset, int maxOctaveOffset, double speedFactor) {
    
    int currentOctaveOffset;
    if (minOctaveOffset <= 0) {
        currentOctaveOffset = 0;
    } else { 
        currentOctaveOffset = minOctaveOffset;
    }
    
    int randInt1 = randomInt(100, 200);
    double stopProbability = (double) randInt1 / (randInt1 + 1);
    int randInt2 = randomInt(4, 10);
    double octaveSwitchProbability = (double) randInt2 / (randInt2 + 1);
    
    
    double currentTime = 0.0;
    MidiMessageSequence* melody = new MidiMessageSequence(starter);
    if (!goodSource) {
        return melody;
    }
    
    while (randomDouble(0,1) < stopProbability) {
        int notePitch = pitches->at(randomInt(0, pitches->size() - 1)) + 12 * currentOctaveOffset;
        while (notePitch < 0) {
            notePitch+=12;
            currentOctaveOffset++;
            minOctaveOffset++;
        }
        while (notePitch > 127) {
            notePitch-=12;
            currentOctaveOffset--;
            maxOctaveOffset--;
        }
        int noteVelocity = randomInt(lowVelocity, highVelocity);
        int noteLength = randomDouble(lowNoteLength, highNoteLength) * (1.0 / speedFactor);
        int nextRestLength;
        if (randomDouble(0,1) < negRestProbability) {
            nextRestLength = randomDouble(lowNegRestLength, highNegRestLength) * 1.0 / speedFactor;
        } else {
            nextRestLength = randomDouble(lowRestLength, highRestLength) * 1.0 / speedFactor;
        }
        
        MidiMessage noteDown = MidiMessage::noteOn(1, notePitch, (uint8_t) noteVelocity);
        noteDown.setTimeStamp(currentTime);
        MidiMessage noteUp = MidiMessage::noteOff(1, notePitch);
        noteUp.setTimeStamp(currentTime + noteLength);

        melody->addEvent(noteDown);
        melody->addEvent(noteUp);
        melody->updateMatchedPairs();
        
        currentTime += noteLength + nextRestLength;
        
        if (randomDouble(0,1) > octaveSwitchProbability) {
            currentOctaveOffset = randomInt(minOctaveOffset, maxOctaveOffset);
        }
    }
        
    return melody;
    
    
}