int InstrumentEditor::findFreeDrumkitComponentId( int startingPoint ) { bool bFoundFreeSlot = true; std::vector<DrumkitComponent*>* pDrumkitComponentList = Hydrogen::get_instance()->getSong()->get_components(); for (std::vector<DrumkitComponent*>::iterator it = pDrumkitComponentList->begin() ; it != pDrumkitComponentList->end(); ++it) { DrumkitComponent* pDrumkitComponent = *it; if( pDrumkitComponent->get_id() == startingPoint ) { bFoundFreeSlot = false; break; } } if(bFoundFreeSlot) return startingPoint; else return findFreeDrumkitComponentId( startingPoint + 1 ); }
// Returns 0 on success, passes the TinyXml error code otherwise. int SongWriter::writeSong( Song *song, const QString& filename ) { INFOLOG( "Saving song " + filename ); int rv = 0; // return value // FIXME: has the file write-permssion? // FIXME: verificare che il file non sia gia' esistente // FIXME: effettuare copia di backup per il file gia' esistente QDomDocument doc; QDomProcessingInstruction header = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\""); doc.appendChild( header ); QDomNode songNode = doc.createElement( "song" ); LocalFileMng::writeXmlString( songNode, "version", QString( get_version().c_str() ) ); LocalFileMng::writeXmlString( songNode, "bpm", QString("%1").arg( song->__bpm ) ); LocalFileMng::writeXmlString( songNode, "volume", QString("%1").arg( song->get_volume() ) ); LocalFileMng::writeXmlString( songNode, "metronomeVolume", QString("%1").arg( song->get_metronome_volume() ) ); LocalFileMng::writeXmlString( songNode, "name", song->__name ); LocalFileMng::writeXmlString( songNode, "author", song->__author ); LocalFileMng::writeXmlString( songNode, "notes", song->get_notes() ); LocalFileMng::writeXmlString( songNode, "license", song->get_license() ); LocalFileMng::writeXmlBool( songNode, "loopEnabled", song->is_loop_enabled() ); LocalFileMng::writeXmlBool( songNode, "patternModeMode", Preferences::get_instance()->patternModePlaysSelected()); LocalFileMng::writeXmlString( songNode, "playbackTrackFilename", QString("%1").arg( song->get_playback_track_filename() ) ); LocalFileMng::writeXmlBool( songNode, "playbackTrackEnabled", song->get_playback_track_enabled() ); LocalFileMng::writeXmlString( songNode, "playbackTrackVolume", QString("%1").arg( song->get_playback_track_volume() ) ); if ( song->get_mode() == Song::SONG_MODE ) { LocalFileMng::writeXmlString( songNode, "mode", QString( "song" ) ); } else { LocalFileMng::writeXmlString( songNode, "mode", QString( "pattern" ) ); } LocalFileMng::writeXmlString( songNode, "humanize_time", QString("%1").arg( song->get_humanize_time_value() ) ); LocalFileMng::writeXmlString( songNode, "humanize_velocity", QString("%1").arg( song->get_humanize_velocity_value() ) ); LocalFileMng::writeXmlString( songNode, "swing_factor", QString("%1").arg( song->get_swing_factor() ) ); // component List QDomNode componentListNode = doc.createElement( "componentList" ); for (std::vector<DrumkitComponent*>::iterator it = song->get_components()->begin() ; it != song->get_components()->end(); ++it) { DrumkitComponent* pCompo = *it; QDomNode componentNode = doc.createElement( "drumkitComponent" ); LocalFileMng::writeXmlString( componentNode, "id", QString("%1").arg( pCompo->get_id() ) ); LocalFileMng::writeXmlString( componentNode, "name", pCompo->get_name() ); LocalFileMng::writeXmlString( componentNode, "volume", QString("%1").arg( pCompo->get_volume() ) ); componentListNode.appendChild( componentNode ); } songNode.appendChild( componentListNode ); // instrument list QDomNode instrumentListNode = doc.createElement( "instrumentList" ); unsigned nInstrument = song->get_instrument_list()->size(); // INSTRUMENT NODE for ( unsigned i = 0; i < nInstrument; i++ ) { Instrument *instr = song->get_instrument_list()->get( i ); assert( instr ); QDomNode instrumentNode = doc.createElement( "instrument" ); LocalFileMng::writeXmlString( instrumentNode, "id", QString("%1").arg( instr->get_id() ) ); LocalFileMng::writeXmlString( instrumentNode, "name", instr->get_name() ); LocalFileMng::writeXmlString( instrumentNode, "drumkit", instr->get_drumkit_name() ); LocalFileMng::writeXmlString( instrumentNode, "volume", QString("%1").arg( instr->get_volume() ) ); LocalFileMng::writeXmlBool( instrumentNode, "isMuted", instr->is_muted() ); LocalFileMng::writeXmlString( instrumentNode, "pan_L", QString("%1").arg( instr->get_pan_l() ) ); LocalFileMng::writeXmlString( instrumentNode, "pan_R", QString("%1").arg( instr->get_pan_r() ) ); LocalFileMng::writeXmlString( instrumentNode, "gain", QString("%1").arg( instr->get_gain() ) ); LocalFileMng::writeXmlBool( instrumentNode, "applyVelocity", instr->get_apply_velocity() ); LocalFileMng::writeXmlBool( instrumentNode, "filterActive", instr->is_filter_active() ); LocalFileMng::writeXmlString( instrumentNode, "filterCutoff", QString("%1").arg( instr->get_filter_cutoff() ) ); LocalFileMng::writeXmlString( instrumentNode, "filterResonance", QString("%1").arg( instr->get_filter_resonance() ) ); LocalFileMng::writeXmlString( instrumentNode, "FX1Level", QString("%1").arg( instr->get_fx_level( 0 ) ) ); LocalFileMng::writeXmlString( instrumentNode, "FX2Level", QString("%1").arg( instr->get_fx_level( 1 ) ) ); LocalFileMng::writeXmlString( instrumentNode, "FX3Level", QString("%1").arg( instr->get_fx_level( 2 ) ) ); LocalFileMng::writeXmlString( instrumentNode, "FX4Level", QString("%1").arg( instr->get_fx_level( 3 ) ) ); assert( instr->get_adsr() ); LocalFileMng::writeXmlString( instrumentNode, "Attack", QString("%1").arg( instr->get_adsr()->get_attack() ) ); LocalFileMng::writeXmlString( instrumentNode, "Decay", QString("%1").arg( instr->get_adsr()->get_decay() ) ); LocalFileMng::writeXmlString( instrumentNode, "Sustain", QString("%1").arg( instr->get_adsr()->get_sustain() ) ); LocalFileMng::writeXmlString( instrumentNode, "Release", QString("%1").arg( instr->get_adsr()->get_release() ) ); LocalFileMng::writeXmlString( instrumentNode, "randomPitchFactor", QString("%1").arg( instr->get_random_pitch_factor() ) ); LocalFileMng::writeXmlString( instrumentNode, "muteGroup", QString("%1").arg( instr->get_mute_group() ) ); LocalFileMng::writeXmlBool( instrumentNode, "isStopNote", instr->is_stop_notes() ); switch ( instr->sample_selection_alg() ) { case Instrument::VELOCITY: LocalFileMng::writeXmlString( instrumentNode, "sampleSelectionAlgo", "VELOCITY" ); break; case Instrument::RANDOM: LocalFileMng::writeXmlString( instrumentNode, "sampleSelectionAlgo", "RANDOM" ); break; case Instrument::ROUND_ROBIN: LocalFileMng::writeXmlString( instrumentNode, "sampleSelectionAlgo", "ROUND_ROBIN" ); break; } LocalFileMng::writeXmlString( instrumentNode, "midiOutChannel", QString("%1").arg( instr->get_midi_out_channel() ) ); LocalFileMng::writeXmlString( instrumentNode, "midiOutNote", QString("%1").arg( instr->get_midi_out_note() ) ); LocalFileMng::writeXmlString( instrumentNode, "isHihat", QString("%1").arg( instr->get_hihat_grp() ) ); LocalFileMng::writeXmlString( instrumentNode, "lower_cc", QString("%1").arg( instr->get_lower_cc() ) ); LocalFileMng::writeXmlString( instrumentNode, "higher_cc", QString("%1").arg( instr->get_higher_cc() ) ); for (std::vector<InstrumentComponent*>::iterator it = instr->get_components()->begin() ; it != instr->get_components()->end(); ++it) { InstrumentComponent* pComponent = *it; QDomNode componentNode = doc.createElement( "instrumentComponent" ); LocalFileMng::writeXmlString( componentNode, "component_id", QString("%1").arg( pComponent->get_drumkit_componentID() ) ); LocalFileMng::writeXmlString( componentNode, "gain", QString("%1").arg( pComponent->get_gain() ) ); for ( unsigned nLayer = 0; nLayer < InstrumentComponent::getMaxLayers(); nLayer++ ) { InstrumentLayer *pLayer = pComponent->get_layer( nLayer ); if ( pLayer == NULL ) continue; Sample *pSample = pLayer->get_sample(); if ( pSample == NULL ) continue; bool sIsModified = pSample->get_is_modified(); Sample::Loops lo = pSample->get_loops(); Sample::Rubberband ro = pSample->get_rubberband(); QString sMode = pSample->get_loop_mode_string(); QDomNode layerNode = doc.createElement( "layer" ); LocalFileMng::writeXmlString( layerNode, "filename", Filesystem::prepare_sample_path( pSample->get_filepath() ) ); LocalFileMng::writeXmlBool( layerNode, "ismodified", sIsModified); LocalFileMng::writeXmlString( layerNode, "smode", pSample->get_loop_mode_string() ); LocalFileMng::writeXmlString( layerNode, "startframe", QString("%1").arg( lo.start_frame ) ); LocalFileMng::writeXmlString( layerNode, "loopframe", QString("%1").arg( lo.loop_frame ) ); LocalFileMng::writeXmlString( layerNode, "loops", QString("%1").arg( lo.count ) ); LocalFileMng::writeXmlString( layerNode, "endframe", QString("%1").arg( lo.end_frame ) ); LocalFileMng::writeXmlString( layerNode, "userubber", QString("%1").arg( ro.use ) ); LocalFileMng::writeXmlString( layerNode, "rubberdivider", QString("%1").arg( ro.divider ) ); LocalFileMng::writeXmlString( layerNode, "rubberCsettings", QString("%1").arg( ro.c_settings ) ); LocalFileMng::writeXmlString( layerNode, "rubberPitch", QString("%1").arg( ro.pitch ) ); LocalFileMng::writeXmlString( layerNode, "min", QString("%1").arg( pLayer->get_start_velocity() ) ); LocalFileMng::writeXmlString( layerNode, "max", QString("%1").arg( pLayer->get_end_velocity() ) ); LocalFileMng::writeXmlString( layerNode, "gain", QString("%1").arg( pLayer->get_gain() ) ); LocalFileMng::writeXmlString( layerNode, "pitch", QString("%1").arg( pLayer->get_pitch() ) ); Sample::VelocityEnvelope* velocity = pSample->get_velocity_envelope(); for (int y = 0; y < velocity->size(); y++){ QDomNode volumeNode = doc.createElement( "volume" ); LocalFileMng::writeXmlString( volumeNode, "volume-position", QString("%1").arg( velocity->at(y).frame ) ); LocalFileMng::writeXmlString( volumeNode, "volume-value", QString("%1").arg( velocity->at(y).value ) ); layerNode.appendChild( volumeNode ); } Sample::PanEnvelope* pan = pSample->get_pan_envelope(); for (int y = 0; y < pan->size(); y++){ QDomNode panNode = doc.createElement( "pan" ); LocalFileMng::writeXmlString( panNode, "pan-position", QString("%1").arg( pan->at(y).frame ) ); LocalFileMng::writeXmlString( panNode, "pan-value", QString("%1").arg( pan->at(y).value ) ); layerNode.appendChild( panNode ); } componentNode.appendChild( layerNode ); } instrumentNode.appendChild( componentNode ); } instrumentListNode.appendChild( instrumentNode ); } songNode.appendChild( instrumentListNode ); // pattern list QDomNode patternListNode = doc.createElement( "patternList" ); unsigned nPatterns = song->get_pattern_list()->size(); for ( unsigned i = 0; i < nPatterns; i++ ) { Pattern *pat = song->get_pattern_list()->get( i ); // pattern QDomNode patternNode = doc.createElement( "pattern" ); LocalFileMng::writeXmlString( patternNode, "name", pat->get_name() ); LocalFileMng::writeXmlString( patternNode, "category", pat->get_category() ); LocalFileMng::writeXmlString( patternNode, "size", QString("%1").arg( pat->get_length() ) ); LocalFileMng::writeXmlString( patternNode, "info", pat->get_info() ); QDomNode noteListNode = doc.createElement( "noteList" ); const Pattern::notes_t* notes = pat->get_notes(); FOREACH_NOTE_CST_IT_BEGIN_END(notes,it) { Note *pNote = it->second; assert( pNote ); QDomNode noteNode = doc.createElement( "note" ); LocalFileMng::writeXmlString( noteNode, "position", QString("%1").arg( pNote->get_position() ) ); LocalFileMng::writeXmlString( noteNode, "leadlag", QString("%1").arg( pNote->get_lead_lag() ) ); LocalFileMng::writeXmlString( noteNode, "velocity", QString("%1").arg( pNote->get_velocity() ) ); LocalFileMng::writeXmlString( noteNode, "pan_L", QString("%1").arg( pNote->get_pan_l() ) ); LocalFileMng::writeXmlString( noteNode, "pan_R", QString("%1").arg( pNote->get_pan_r() ) ); LocalFileMng::writeXmlString( noteNode, "pitch", QString("%1").arg( pNote->get_pitch() ) ); LocalFileMng::writeXmlString( noteNode, "probability", QString("%1").arg( pNote->get_probability() ) ); LocalFileMng::writeXmlString( noteNode, "key", pNote->key_to_string() ); LocalFileMng::writeXmlString( noteNode, "length", QString("%1").arg( pNote->get_length() ) ); LocalFileMng::writeXmlString( noteNode, "instrument", QString("%1").arg( pNote->get_instrument()->get_id() ) ); QString noteoff = "false"; if ( pNote->get_note_off() ) noteoff = "true"; LocalFileMng::writeXmlString( noteNode, "note_off", noteoff ); noteListNode.appendChild( noteNode ); } patternNode.appendChild( noteListNode ); patternListNode.appendChild( patternNode ); }
void InstrumentEditor::compoChangeAddDelete(QAction* pAction) { QString sSelectedAction = pAction->text(); Hydrogen * pEngine = Hydrogen::get_instance(); if( sSelectedAction.compare("add") == 0 ) { if ( m_pInstrument ) { bool bIsOkPressed; QString sNewName = QInputDialog::getText( this, "Hydrogen", trUtf8( "Component name" ), QLineEdit::Normal, "New Component", &bIsOkPressed ); if ( bIsOkPressed ) { DrumkitComponent* pDrumkitComponent = new DrumkitComponent( findFreeDrumkitComponentId(), sNewName ); pEngine->getSong()->get_components()->push_back( pDrumkitComponent ); //InstrumentComponent* instrument_component = new InstrumentComponent( dm_component->get_id() ); //instrument_component->set_gain( 1.0f ); //m_pInstrument->get_components()->push_back( instrument_component ); m_nSelectedComponent = pDrumkitComponent->get_id(); m_pLayerPreview->set_selected_component( pDrumkitComponent->get_id() ); selectedInstrumentChangedEvent(); // this will force an update... EventQueue::get_instance()->push_event( EVENT_SELECTED_INSTRUMENT_CHANGED, -1 ); #ifdef H2CORE_HAVE_JACK pEngine->renameJackPorts(pEngine->getSong()); #endif } else { // user entered nothing or pressed Cancel } } } else if( sSelectedAction.compare("delete") == 0 ) { std::vector<DrumkitComponent*>* pDrumkitComponents = pEngine->getSong()->get_components(); if(pDrumkitComponents->size() == 1){ return; } DrumkitComponent* pDrumkitComponent = pEngine->getSong()->get_component( m_nSelectedComponent ); InstrumentList* pInstruments = pEngine->getSong()->get_instrument_list(); for ( int n = ( int )pInstruments->size() - 1; n >= 0; n-- ) { Instrument* pInstrument = pInstruments->get( n ); for( int o = 0 ; o < pInstrument->get_components()->size() ; o++ ) { InstrumentComponent* pInstrumentComponent = pInstrument->get_components()->at( o ); if( pInstrumentComponent->get_drumkit_componentID() == pDrumkitComponent->get_id() ) { for( int nLayer = 0; nLayer < MAX_LAYERS; nLayer++ ) { InstrumentLayer* pLayer = pInstrumentComponent->get_layer( nLayer ); if( pLayer ) delete pLayer; } pInstrument->get_components()->erase( pInstrument->get_components()->begin() + o );; break; } } } for ( int n = 0 ; n < pDrumkitComponents->size() ; n++ ) { DrumkitComponent* pTmpDrumkitComponent = pDrumkitComponents->at( n ); if( pTmpDrumkitComponent->get_id() == pDrumkitComponent->get_id() ) { pDrumkitComponents->erase( pDrumkitComponents->begin() + n ); break; } } m_nSelectedComponent = pDrumkitComponents->front()->get_id(); selectedInstrumentChangedEvent(); // this will force an update... EventQueue::get_instance()->push_event( EVENT_SELECTED_INSTRUMENT_CHANGED, -1 ); } else if( sSelectedAction.compare("rename") == 0 ) { labelCompoClicked( NULL ); } else { m_nSelectedComponent = -1; std::vector<DrumkitComponent*>* pDrumkitComponents = pEngine->getSong()->get_components(); for (std::vector<DrumkitComponent*>::iterator it = pDrumkitComponents->begin() ; it != pDrumkitComponents->end(); ++it) { DrumkitComponent* pDrumkitComponent = *it; if( pDrumkitComponent->get_name().compare( sSelectedAction ) == 0) { m_nSelectedComponent = pDrumkitComponent->get_id(); m_pCompoNameLbl->setText( pDrumkitComponent->get_name() ); break; } } if( m_pInstrument && !m_pInstrument->get_component(m_nSelectedComponent)) { INFOLOG("Component needs to be added"); InstrumentComponent* pInstrComponent = new InstrumentComponent( m_nSelectedComponent ); pInstrComponent->set_gain( 1.0f ); m_pInstrument->get_components()->push_back( pInstrComponent ); #ifdef H2CORE_HAVE_JACK pEngine->renameJackPorts(pEngine->getSong()); #endif } m_pLayerPreview->set_selected_component(m_nSelectedComponent); selectedInstrumentChangedEvent(); // this will force an update... EventQueue::get_instance()->push_event( EVENT_SELECTED_INSTRUMENT_CHANGED, -1 ); } }