void
ManageMetronomeDialog::slotPreviewPitch(int pitch)
{
    RG_DEBUG << "ManageMetronomeDialog::slotPreviewPitch";

    DeviceList *devices = m_doc->getStudio().getDevices();
    DeviceListConstIterator it;
    int count = 0;
    Device *dev = 0;

    for (it = devices->begin(); it != devices->end(); it++) {

        dev = *it;
        if (!isSuitable(dev)) continue;

        if (count == m_metronomeDevice->currentIndex()) break;
        count++;
    }

    if (!dev || !isSuitable(dev)) return;

    const MidiMetronome *metronome = getMetronome(dev);
    if (metronome == 0) return;

    InstrumentList list = dev->getPresentationInstruments();

    Instrument *inst =
        list[m_metronomeInstrument->currentIndex()];
    StudioControl::playPreviewNote(inst, pitch, MidiMaxValue, RealTime(0, 10000000));
}
void
ManageMetronomeDialog::populate(int deviceIndex)
{
    m_metronomeInstrument->clear();

    DeviceList *devices = m_doc->getStudio().getDevices();
    DeviceListConstIterator it;
    int count = 0;
    Device *dev = 0;

    for (it = devices->begin(); it != devices->end(); it++) {

        dev = *it;
        if (!isSuitable(dev)) continue;

        if (count == deviceIndex) break;
        count++;
    }

    // sanity
    if (count < 0 || dev == 0 || !isSuitable(dev)) {
        return ;
    }

    // populate instrument list
    InstrumentList list = dev->getPresentationInstruments();
    InstrumentList::iterator iit;

    const MidiMetronome *metronome = getMetronome(dev);

    // if we've got no metronome against this device then create one
    if (metronome == 0) {
        InstrumentId id = SystemInstrumentBase;

        for (iit = list.begin(); iit != list.end(); ++iit) {
            if ((*iit)->isPercussion()) {
                id = (*iit)->getId();
                break;
            }
        }

        setMetronome(dev, MidiMetronome(id));

        metronome = getMetronome(dev);
    }

    // metronome should now be set but we still check it
    if (metronome) {
        int position = 0;
        int count = 0;

        for (iit = list.begin(); iit != list.end(); ++iit) {

            QString iname(QObject::tr((*iit)->getName().c_str()));
            QString ipname((*iit)->getLocalizedPresentationName());
            QString programName(QObject::tr((*iit)->getProgramName().c_str()));

            QString text;

            if ((*iit)->getType() == Instrument::SoftSynth) {

                iname.replace(QObject::tr("Synth plugin "), "");
                programName = "";

                AudioPluginInstance *plugin = (*iit)->getPlugin
                    (Instrument::SYNTH_PLUGIN_POSITION);
                if (plugin)
                    programName = strtoqstr(plugin->getDisplayName());

            } else {

                iname = ipname;
            }

            if (programName != "") {
                text = tr("%1 (%2)").arg(iname).arg(programName);
            } else {
                text = iname;
            }

            m_metronomeInstrument->addItem(text);

            if ((*iit)->getId() == metronome->getInstrument()) {
                position = count;
            }
            count++;
        }
        m_metronomeInstrument->setCurrentIndex(position);

        m_barPitch = metronome->getBarPitch();
        m_beatPitch = metronome->getBeatPitch();
        m_subBeatPitch = metronome->getSubBeatPitch();
        slotPitchSelectorChanged(0);
        m_metronomeResolution->setCurrentIndex(metronome->getDepth());
        m_metronomeBarVely->setValue(metronome->getBarVelocity());
        m_metronomeBeatVely->setValue(metronome->getBeatVelocity());
        m_metronomeSubBeatVely->setValue(metronome->getSubBeatVelocity());
        m_playEnabled->setChecked(m_doc->getComposition().usePlayMetronome());
        m_recordEnabled->setChecked(m_doc->getComposition().useRecordMetronome());
        slotResolutionChanged(metronome->getDepth());
    }
}
void
ManageMetronomeDialog::slotApply()
{
    Studio &studio = m_doc->getStudio();

    DeviceList *devices = m_doc->getStudio().getDevices();
    DeviceListConstIterator it;
    int count = 0;
    Device *dev = 0;

    for (it = devices->begin(); it != devices->end(); it++) {

        dev = *it;
        if (!isSuitable(dev)) continue;

        if (count == m_metronomeDevice->currentIndex()) break;
        count++;
    }

    if (!dev || !isSuitable(dev)) {
        RG_WARNING << "Warning: ManageMetronomeDialog::slotApply: no " << m_metronomeDevice->currentIndex() << "th device";
        return ;
    }

    DeviceId deviceId = dev->getId();
    studio.setMetronomeDevice(deviceId);

    if (getMetronome(dev) == 0) {
        RG_WARNING << "Warning: ManageMetronomeDialog::slotApply: unable to extract metronome from device " << deviceId;
        return ;
    }
    MidiMetronome metronome(*getMetronome(dev));

    // get instrument
    InstrumentList list = dev->getPresentationInstruments();

    Instrument *inst =
        list[m_metronomeInstrument->currentIndex()];

    if (inst) {
        metronome.setInstrument(inst->getId());
    }

    metronome.setBarPitch(m_barPitch);
    metronome.setBeatPitch(m_beatPitch);
    metronome.setSubBeatPitch(m_subBeatPitch);

    metronome.setDepth(
        m_metronomeResolution->currentIndex());

    metronome.setBarVelocity(
        MidiByte(m_metronomeBarVely->value()));

    metronome.setBeatVelocity(
        MidiByte(m_metronomeBeatVely->value()));

    metronome.setSubBeatVelocity(
        MidiByte(m_metronomeSubBeatVely->value()));

    setMetronome(dev, metronome);

    m_doc->getComposition().setPlayMetronome(m_playEnabled->isChecked());
    m_doc->getComposition().setRecordMetronome(m_recordEnabled->isChecked());

    m_doc->getSequenceManager()->metronomeChanged(inst->getId(), true);
    m_doc->slotDocumentModified();
    setModified(false);
}
        DirectionTypePtr Direction::createDirectionType( std::ostream& message, xml::XElementIterator& subIter, xml::XElementIterator& subIterEnd, bool& isSuccess )
        {
            auto directionType = makeDirectionType();
            
            if( subIter == subIterEnd )
            {
                message << "Direction: well thats weird - should not get here" << std::endl;
                isSuccess = false;
                return directionType;
            }
            
            if( subIter->getName() == "wedge" )
            {
                directionType->setChoice( DirectionType::Choice::wedge );
                isSuccess &= directionType->getWedge()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "dashes" )
            {
                directionType->setChoice( DirectionType::Choice::dashes );
                isSuccess &= directionType->getDashes()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "bracket" )
            {
                directionType->setChoice( DirectionType::Choice::bracket );
                isSuccess &= directionType->getBracket()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "pedal" )
            {
                directionType->setChoice( DirectionType::Choice::pedal );
                isSuccess &= directionType->getPedal()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "metronome" )
            {
                directionType->setChoice( DirectionType::Choice::metronome );
                isSuccess &= directionType->getMetronome()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "octave-shift" )
            {
                directionType->setChoice( DirectionType::Choice::octaveShift );
                isSuccess &= directionType->getOctaveShift()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "harp-pedals" )
            {
                directionType->setChoice( DirectionType::Choice::harpPedals );
                isSuccess &= directionType->getHarpPedals()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "damp" )
            {
                directionType->setChoice( DirectionType::Choice::damp );
                isSuccess &= directionType->getDamp()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "damp-all" )
            {
                directionType->setChoice( DirectionType::Choice::dampAll );
                isSuccess &= directionType->getDampAll()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "eyeglasses" )
            {
                directionType->setChoice( DirectionType::Choice::eyeglasses );
                isSuccess &= directionType->getEyeglasses()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "string-mute" )
            {
                directionType->setChoice( DirectionType::Choice::stringMute );
                isSuccess &= directionType->getStringMute()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "scordatura" )
            {
                directionType->setChoice( DirectionType::Choice::scordatura );
                isSuccess &= directionType->getScordatura()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "image" )
            {
                directionType->setChoice( DirectionType::Choice::image );
                isSuccess &= directionType->getImage()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "principal-voice" )
            {
                directionType->setChoice( DirectionType::Choice::principalVoice );
                isSuccess &= directionType->getPrincipalVoice()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "accordion-registration" )
            {
                directionType->setChoice( DirectionType::Choice::accordionRegistration );
                isSuccess &= directionType->getAccordionRegistration()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "other-direction" )
            {
                directionType->setChoice( DirectionType::Choice::otherDirection );
                isSuccess &= directionType->getOtherDirection()->fromXElement( message, *subIter );
                return directionType;
            }
                
            std::string name = "rehearsal";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::rehearsal );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeRehearsal();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getRehearsalSet().size() == 1 )
                    {
                        directionType->addRehearsal( itemToAdd );
                        directionType->removeRehearsal( directionType->getRehearsalSet().cbegin() );
                    }
                    else
                    {
                        directionType->addRehearsal( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end rehearsal
            
            
            name = "segno";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::segno );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeSegno();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getSegnoSet().size() == 1 )
                    {
                        directionType->addSegno( itemToAdd );
                        directionType->removeSegno( directionType->getSegnoSet().cbegin() );
                    }
                    else
                    {
                        directionType->addSegno( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end segno
            
            
            name = "words";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::words );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeWords();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getWordsSet().size() == 1 )
                    {
                        directionType->addWords( itemToAdd );
                        directionType->removeWords( directionType->getWordsSet().cbegin() );
                    }
                    else
                    {
                        directionType->addWords( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end words
            
            
            name = "coda";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::coda );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeCoda();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getCodaSet().size() == 1 )
                    {
                        directionType->addCoda( itemToAdd );
                        directionType->removeCoda( directionType->getCodaSet().cbegin() );
                    }
                    else
                    {
                        directionType->addCoda( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end coda
            
            
            name = "dynamics";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::dynamics );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeDynamics();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getDynamicsSet().size() == 1 )
                    {
                        directionType->addDynamics( itemToAdd );
                        directionType->removeDynamics( directionType->getDynamicsSet().cbegin() );
                    }
                    else
                    {
                        directionType->addDynamics( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end dynamics
            
            
            name = "percussion";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::percussion );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makePercussion();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getPercussionSet().size() == 1 )
                    {
                        directionType->addPercussion( itemToAdd );
                        directionType->removePercussion( directionType->getPercussionSet().cbegin() );
                    }
                    else
                    {
                        directionType->addPercussion( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end percussion

            return directionType;
        }