void RealvecWidget::refreshFromControl() { Q_ASSERT(m_system); Q_ASSERT(!m_path.isEmpty()); MarControlPtr control = m_system->getControl( m_path.toStdString() ); if (control.isInvalid()) { qWarning() << "RealvecWidget: invalid control path:" << m_path; clearPlot(); return; } if (control->getType() != "mrs_realvec") { //qWarning() << "RealvecWidget: control type not 'mrs_realvec':" << m_control_path; clearPlot(); return; } const realvec & data = control->to<mrs_realvec>(); m_table->setData(data); m_data = data; if (m_plot) m_plot->setData(&m_data); }
void VampPlugin::applyParameters(MarSystem *system) { map<string, float>::const_iterator it; for (it = m_params.begin(); it != m_params.end(); ++it) { const string & id = it->first; float value = it->second; MarControlPtr control = system->control(id); if (control.isInvalid()) { cerr << "Invalid parameter id: " << id << endl; continue; } if (control->hasType<mrs_real>()) { control->setValue((mrs_real) value); } else if (control->hasType<mrs_natural>()) { control->setValue((mrs_natural) value); } else { cerr << "Invalid type for parameter id: " << id << endl; } } }
static int apply_controls( MarSystem *system, const vector<pair<string,string>> & controls ) { for( const auto & mapping : controls ) { const string & control_path = mapping.first; const string & control_value_text = mapping.second; MarControlPtr control = system->remoteControl(control_path); if (control.isInvalid()) { MRSERR("Can not set control - invalid path: " << control_path); return 1; } int err = set_control(control, control_value_text); if (err) { MRSERR("Can not set control - invalid value: " << control_path << " = " << control_value_text); return err; } } return 0; }
void RunnerThread::process_requests() { Event *event; while( m_shared->request_queue.pop(event) ) { //cout << "MarSystemThread: popped event: type=" << event->type << endl; switch (event->type) { case SetControl: { SetControlEvent* request = static_cast<SetControlEvent*>(event); //cout << "MarSystemThread: setting control: " << request->control->getName() << endl; set_control_value( request->control, request->value ); break; } case SetControlValue: { SetControlValueEvent* request = static_cast<SetControlValueEvent*>(event); //cout << "MarSystemThread: setting control value: " << request->path << endl; MarControlPtr control = m_system->getControl(request->path); if (control.isInvalid()) { MRSERR("Marsyas::Thread::System:: Can not set control - invalid path: " << request->path); } else set_control_value(control, request->value ); break; } case SetControls: { SetControlsEvent *request = static_cast<SetControlsEvent*>(event); for( const auto & mapping : request->control_values ) { const bool do_not_update = false; //cout << "MarSystemThread: setting staged control: " << mapping.first->getName() << endl; // FIXME: evil const_cast: MarControlPtr & settable_control = const_cast<MarControlPtr &>(mapping.first); set_control_value(settable_control, mapping.second, do_not_update); } m_system->update(); break; } default: MRSERR("Marsyas::Thread::System:: unsupported event type: " << event->type); } event->setProcessed(); } }
void MarControlGUI::updControl(MarControlPtr control) { //if this control is currently being edited by the user //do not update its widget if(editing_) return; //check control name and if it's the correct type if((control_->getType() != control->getType()) && (cname_ != control->getName())) return; //if no changes are needed, do nothing // if(!(*control != control_)) // return; //update internal MarControlPtr copy //control_ = control; //update corresponding widget string ctype = control_->getType(); if(ctype == "mrs_real") { realWidget_->setText(QString::number((double)control_->to<mrs_real>()));//does not trigger a signal! return; } if(ctype == "mrs_natural") { naturalWidget_->setText(QString::number((int)control_->to<mrs_natural>()));//does not trigger a signal! return; } if(ctype == "mrs_bool") { //we have to temporary disconnect the QCheckBox signal otherwise //we'll enter an infinite signal->slot loop... disconnect(boolWidget_, SIGNAL(toggled(bool)), this, SLOT(toMarControl(bool))); //[!] boolWidget_->setChecked(control_->to<mrs_bool>());//This does trigger a signal!! connect(boolWidget_, SIGNAL(toggled(bool)), this, SLOT(toMarControl(bool))); return; }
static void set_control_value( MarControlPtr & control, const any & value, bool update = true ) { // FIXME: real-time-safe setting of string and realvec! try { const T & typed_value = any_cast<T>( value ); control->setValue(typed_value, update); } catch ( bad_any_cast ) { return; }; }
void MarLpcWindow::ctrlChanged(MarControlPtr cname) { string name = cname->getName(); if (cname.isEqual(posPtr_)) { mrs_real fval = cname->to<mrs_natural>() / (512.0 * 40.0); if (posSlider_->isSliderDown() == false) { posSlider_->blockSignals(true); posSlider_->setValue((int)fval); posSlider_->blockSignals(false); } posControl_->updControl(cname); } /*if (cname.isEqual(frequencyPolePtr_)) { frequencyPoleSlider_->blockSignals(true); mrs_real fval = cname->to<mrs_real>(); int val = (int)(fval * 50.0); frequencyPoleSlider_->setValue(val); frequencyPoleSlider_->blockSignals(false); frequencyPoleControl_->updControl(cname); } if (cname.isEqual(amplitudePolePtr_)) { amplitudePoleSlider_->blockSignals(true); mrs_real fval = cname->to<mrs_real>(); int val = (int)(fval * 50.0); amplitudePoleSlider_->setValue(val); amplitudePoleSlider_->blockSignals(false); amplitudePoleControl_->updControl(cname); }*/ }
void process_message( MarSystem * root_system, const osc::ReceivedMessage& message ) { const char * path = message.AddressPattern(); if (path[0] == '/') ++path; // FIXME: Constructing std::string is not real-time-safe. MarControlPtr control = find_control(root_system, path); if (control.isInvalid()) { MRSWARN("OSC receiver: no control for path: " << path); return; } try { osc::ReceivedMessage::const_iterator it = message.ArgumentsBegin(); if (it == message.ArgumentsEnd()) throw std::runtime_error("OSC receiver: Message has no arguments."); char tag = it->TypeTag(); switch(tag) { case osc::TRUE_TYPE_TAG: case osc::FALSE_TYPE_TAG: control->setValue(it->AsBoolUnchecked()); break; case osc::INT32_TYPE_TAG: control->setValue(it->AsInt32Unchecked()); break; case osc::FLOAT_TYPE_TAG: control->setValue((mrs_real) it->AsFloatUnchecked()); break; case osc::DOUBLE_TYPE_TAG: control->setValue((mrs_real) it->AsDoubleUnchecked()); break; case osc::STRING_TYPE_TAG: control->setValue(it->AsStringUnchecked()); break; default: throw std::runtime_error("OSC receiver: Unsupported message argument type."); } } catch ( std::exception & e ) { MRSWARN("OSC receiver: error while parsing message: " << e.what()); } }
int set_control(MarControlPtr & control, const string & text) { string type = control->getType(); if (type == "mrs_bool") { if (text == "true") control->setValue(true); else if (text == "false") control->setValue(false); else return 1; } else if (type == "mrs_natural") { mrs_natural i; size_t count = 0; try { i = std::stol(text, &count); } catch (invalid_argument) {} if (count == text.size()) { control->setValue(i); } else return 1; } else if (type == "mrs_real") { mrs_real r; size_t count = 0; try { r = std::stod(text, &count); } catch (invalid_argument) {} if (count == text.size()) { control->setValue(r); } else return 1; } else if (type == "mrs_string") { control->setValue(text); } else return 1; return 0; }
static QVariant variantFromControl ( const MarControlPtr & control ) { QVariant value; std::string type = control->getType(); if (type == "mrs_real") value = QString::number( control->to<mrs_real>() ); else if (type == "mrs_natural") value = QString::number( control->to<mrs_natural>() ); else if (type == "mrs_bool") value = QVariant( control->to<mrs_bool>() ).convert(QVariant::String); else if (type == "mrs_string") value = QString::fromStdString(control->to<mrs_string>()); else if (type == "mrs_realvec") value = QString("[...]"); else value = QString("<unknown>"); return value; }
static any get_control_value( const MarControlPtr & control ) { std::string control_type = control->getType(); if(control_type == "mrs_bool") return any( control->to<bool>() ); else if(control_type == "mrs_real") return any( control->to<mrs_real>() ); else if(control_type == "mrs_natural") return any( control->to<mrs_natural>() ); else if(control_type == "mrs_string") return any( control->to<mrs_string>() ); else if(control_type == "mrs_realvec") return any( control->to<mrs_realvec>() ); else { MRSERR( "Marsyas::Thread::System:: Can not get control value - unsupported type: " << control_type.c_str() ); return any(); } }
static void set_control_value( MarControlPtr & control, const any & value, bool update = true ) { std::string control_type = control->getType(); if(control_type == "mrs_bool") set_control_value<bool>(control, value, update); else if(control_type == "mrs_real") set_control_value<mrs_real>(control, value, update); else if(control_type == "mrs_natural") set_control_value<mrs_natural>(control, value, update); else if(control_type == "mrs_string") set_control_value<mrs_string>(control, value, update); else if(control_type == "mrs_realvec") set_control_value<mrs_realvec>(control, value, update); else { MRSERR( "Marsyas::Thread::System:: Can not set control value - unsupported type: " << control_type.c_str() ); return; } }
void MarOscMultiGainWindow::ctrlChanged(MarControlPtr cname) { string name = cname->getName(); }
int main(int argc, char *argv[]) { if (argc < 3) { cerr << "Usage: <input file> <output file>" << endl; return 1; } char *input_filename = argv[1]; char *output_filename = argv[2]; detector::registerScripts(); ScriptTranslator translator; MarSystem *system = translator.translateRegistered("detector.mrs"); if (!system) { cerr << "Failure loading script!" << endl; return 1; } MarControlPtr input_control = system->control("input"); MarControlPtr output_control = system->control("output"); MarControlPtr done_control = system->control("done"); if ( input_control.isInvalid() || output_control.isInvalid() || done_control.isInvalid() ) { cerr << "Failure: Invalid script!" << endl; delete system; return 1; } input_control->setValue(string(input_filename)); //output_control->setValue(string("features.out")); mrs_real sample_rate = system->remoteControl("sndfile/osrate")->to<mrs_real>(); mrs_natural block_size = system->remoteControl("sndfile/onSamples")->to<mrs_natural>(); mrs_real block_duration = block_size / sample_rate; MarControlPtr output = system->getControl("mrs_realvec/processedData"); MarControlPtr confidence_ctl = system->remoteControl("onsets/confidence"); assert(!output.isInvalid()); assert(!confidence_ctl.isInvalid()); MarSystem *rms_sys = system->remoteSystem("rms"); assert(rms_sys); MarControlPtr rms_out = rms_sys->getControl("mrs_real/value"); assert(!rms_out.isInvalid()); std::vector<onset> onsets; int block = 0; const int block_offset = 5; while(!done_control->to<bool>()) { system->tick(); const realvec & data = output->to<realvec>(); assert(data.getSize() == 2); mrs_real confidence = confidence_ctl->to<mrs_real>(); if (!(data(0) > 0.0) || confidence < 10.0 / 100.0) { ++block; continue; } mrs_real centroid = data(1); double rms = rms_out->to<mrs_real>(); onset o; o.time = (block - block_offset + 0.5) * block_duration; if (centroid < 0.04) o.type = 0; else if (centroid < 0.3) o.type = 1; else o.type = 2; o.strength = rms; onsets.push_back(o); ++block; } string separator(","); ofstream out_file(output_filename); if (!out_file.is_open()) { cerr << "Failed to open output file for writing: " << output_filename << endl; return 1; } for (int i = 0; i < onsets.size(); ++i) { out_file << onsets[i].time << separator << onsets[i].type << separator << onsets[i].strength << endl; } cout << "Done." << endl; return 0; }
void MarControlDiagramDisplay::loadMarControl(MarControlPtr ctrl) { isLoaded_ = true; nodes.clear(); linkedControls_.clear(); linkedControls_ = ctrl->getLinks(); cout<<endl; string auxName1, auxName2; cout<<endl<<"--------------------------"; cout<<endl<<"linked controls list:"; for(int i=0; i<linkedControls_.size(); i++) { auxName1 = linkedControls_[i].first->getMarSystem()->getName() + "/" + linkedControls_[i].first->getName(); auxName2 = linkedControls_[i].second->getMarSystem()->getName() + "/" + linkedControls_[i].second->getName(); cout<<endl<<auxName1<<" -> "<<auxName2; } MarControlWidget* aux1; MarControlWidget* aux2; MarControlDiagramNode* aux3; MarControlDiagramNode* aux4; aux3 = NULL; bool highlight1; bool highlight2; for(int i=0; i<linkedControls_.size(); i++) { highlight1 = false; highlight2 = false; aux1 = msysw_->getTopMostParent()->ctrlMap_[linkedControls_[i].first]; aux2 = msysw_->getTopMostParent()->ctrlMap_[linkedControls_[i].second]; if(ctrl.isEqual(linkedControls_[i].first)) { highlight1 = true; } if(ctrl.isEqual(linkedControls_[i].second)) { highlight2 = true; } for(int j=0; j<nodes.size(); j++) { aux3 = nodeExists(nodes[j], aux2); } if(aux3 == NULL) { nodes.push_back(new MarControlDiagramNode(aux2, env_, NULL, highlight2)); if(aux1 != aux2) { nodes[nodes.size() - 1]->addLinker(new MarControlDiagramNode(aux1, env_, nodes[nodes.size() - 1], highlight1)); } } else { aux3->addLinker(new MarControlDiagramNode(aux1, env_, aux3, highlight1)); } } organizeDiagram(); debugger(); }
bool MarControl::linkTo(MarControlPtr ctrl, bool update) { if (ctrl.isInvalid()) { ostringstream oss; oss << "MarControl::linkTo() - Linking to an invalid control "; oss << "(" << ctrl->cname_ << " with " << cname_ << ")."; MRSWARN(oss.str()); return false; } //check if these controls are already linked //(i.e. they own the same MarControlValue) if(value_ == ctrl->value_) { return true;//already linked! :-) } if (ctrl->value_->type_ != value_->type_) { ostringstream oss; oss << "MarControl::linkTo() - Linking controls of different types "; oss << "(" << ctrl->cname_ << " with " << cname_ << ")."; MRSWARN(oss.str()); return false; } //unlink this control (but keeping all links to it) //before linking it again to the passed control this->unlinkFromTarget(); //store a pointer to the (soon to be old) MarControlValue object, //so we can later delete it from memory MarControlValue* oldvalue = value_; //and get a pointer to the new value MarControlValue* newvalue = ctrl->value_; //get all the links of our current MarControlValue so we can also //re-link them to the passed ctrl vector<pair<MarControl*, MarControl*> >::iterator lit; for(lit=oldvalue->links_.begin(); lit!=oldvalue->links_.end(); ++lit) { //make each linked control now point to the "passed" MarControlValue lit->first->value_ = newvalue; // check if this is the root link if(lit->first == lit->second) { //make it "link to" the passed control newvalue->links_.push_back(pair<MarControl*, MarControl*>(lit->first, ctrl())); } else //if not a root link, just copy the table entry unchanged into the new MarControlValue newvalue->links_.push_back(*lit); } //old MarControlValue can and should now be safely deleted from memory delete oldvalue; //check if it's needed to call update() if(update) value_->callMarSystemsUpdate();//newvalue->callMarSystemsUpdate(); return true; }