void externalTransition( const vle::devs::ExternalEventList& events, const vle::devs::Time& time) { vle::devs::ExternalEventList::const_iterator it = events.begin(); std::cout.precision(12); std::cout << time << " - [" << getModelName() << "] externalTransition: " << mPhase << std::endl; while (it != events.end()) { if ((*it)->onPort("transport")) { Transport* transport = new Transport( vle::value::toMapValue( (*it)->getAttributeValue("transport"))); std::cout << time << " - [" << getModelName() << "] DECISION TRANSPORT: " << transport->toString() << " => " << mPhase << std::endl; transport->arrived(time); mTransports.push_back(transport); } else if ((*it)->onPort("loaded")) { TransportID transportID = (*it)->getIntegerAttributeValue("id"); std::cout << time << " - [" << getModelName() << "] DECISION LOADED: transport -> " << transportID << std::endl; removeWaitingTransport(transportID); mPhase = SEND_DEPART; } ++it; } updateSigma(time); }
void sync(Transport tr){ //std::cout << tr.toString() << std::endl; transport_string = tr.toString() ; if(playing){ Transport diff = tr - last_transport; t = diff.toSec(); ros::Duration elapsed = ros::Time::now() - last_update_time; sync_offset = t - elapsed.toSec(); offset_string = std::to_string(sync_offset); //upon reaching a new bar, obtain note info for the next bar if(tr.position.bar != last_transport.position.bar || tr.tempo != last_transport.tempo){ double cur_t = (ros::Time::now() - playback_start_time).toSec(); //information about the beats std::vector<terpsichore::pair> beats; //binary bars //if(tr.timeSignature.beatsPerBar % 2 == 0){ for(int i = 1; i <= tr.timeSignature.beatsPerBar; i++){ double beats_ahead = (1 * tr.timeSignature.beatsPerBar) + (i - 1); double beat_time = ((double)(beats_ahead + (tr.position.unit/tr.position.resolution))/tr.tempo) * 60.0 + cur_t; terpsichore::pair b; std::vector<double> d; b.t = beat_time; if(i % 2 == 0){ //weak beat d.push_back(0.5); }else{ //stronk beat d.push_back(1.0); } b.data = d; beats.push_back(b); } //} bardata.beats = beats; std::vector<terpsichore::pair> events; //grab the first clip in the map std::map<int, Clip*>::iterator it = listener->clips.begin(); if(it != listener->clips.end()){ Clip* c = it->second; //std::cout << "now in " << tr.position.toString() << std::endl; for(std::multimap<Position, Note>::iterator it = c->notes.begin(); it != c->notes.end(); it++){ Position p = it->first; if(p.bar == (tr.position.bar + 1) || (tr.position.bar) % c->length.bar + 1 == p.bar){ Note n = it->second; terpsichore::pair d; d.t = (1.0 * tr.timeSignature.beatsPerBar + p.toFloat(tr.timeSignature))/tr.tempo * 60.0 + cur_t; double scaled_pitch = (double)(n.pitch)/(double)(108); double scaled_vel = double(n.velocity)/(double)(127); std::vector<double> info; info.push_back(scaled_pitch); info.push_back((double)n.duration); info.push_back(scaled_vel); d.data = info; events.push_back(d); //std::cout << "added note in " << p.toString() << std::endl; }else{ //std::cout << "not adding the note in " << p.toString() << std::endl; continue; } } bardata.events = events; } data_pub.publish(bardata); } } last_update_time = ros::Time::now(); last_transport = tr; }