Fix MIDI Timestretch
* skip notes outside of region-range (source-start, region-length) * handle tempo-ramps properly, apply map to stretch-fraction * fix region properties after stretching (position needs to be re-set first, to set a midi-region's quarter-note position, which is used by the length calc)
This commit is contained in:
parent
fe880d9b57
commit
eca27d8218
@ -24,6 +24,8 @@
|
||||
#include "ardour/midi_region.h"
|
||||
#include "ardour/midi_source.h"
|
||||
#include "ardour/midi_stretch.h"
|
||||
#include "ardour/session.h"
|
||||
#include "ardour/tempo.h"
|
||||
#include "ardour/types.h"
|
||||
|
||||
#include "pbd/i18n.h"
|
||||
@ -53,11 +55,21 @@ MidiStretch::run (boost::shared_ptr<Region> r, Progress*)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* the name doesn't need to be super-precise, but allow for 2 fractional
|
||||
digits just to disambiguate close but not identical stretches.
|
||||
*/
|
||||
const TempoMap& tmap (session.tempo_map ());
|
||||
|
||||
snprintf (suffix, sizeof (suffix), "@%d", (int) floor (_request.time_fraction * 100.0f));
|
||||
/* Fraction is based on sample-time, while MIDI source
|
||||
* is always using music-time. This matters in case there is
|
||||
* a tempo-ramp. So we need to scale the ratio to music-time */
|
||||
const double mtfrac =
|
||||
tmap.framewalk_to_qn (region->position (), region->length () * _request.time_fraction).to_double ()
|
||||
/
|
||||
tmap.framewalk_to_qn (region->position (), region->length ()).to_double ();
|
||||
|
||||
/* the name doesn't need to be super-precise, but allow for 2 fractional
|
||||
* digits just to disambiguate close but not identical stretches.
|
||||
*/
|
||||
|
||||
snprintf (suffix, sizeof (suffix), "@%d", (int) floor (mtfrac * 100.0f));
|
||||
|
||||
string new_name = region->name();
|
||||
string::size_type at = new_name.find ('@');
|
||||
@ -95,12 +107,38 @@ MidiStretch::run (boost::shared_ptr<Region> r, Progress*)
|
||||
boost::shared_ptr<MidiModel> new_model = new_src->model();
|
||||
new_model->start_write();
|
||||
|
||||
const double r_start = tmap.quarter_notes_between_samples (region->position () - region->start (), region->position ());
|
||||
const double r_end = r_start + tmap.quarter_notes_between_samples (region->position (), region->position () + region->length ());
|
||||
|
||||
#ifdef DEBUG_MIDI_STRETCH
|
||||
printf ("stretch start: %f end: %f [* %f] * %f\n", r_start, r_end, _request.time_fraction, mtfrac);
|
||||
#endif
|
||||
|
||||
/* Note: pass true into force_discrete for the begin() iterator so that the model doesn't
|
||||
* do interpolation of controller data when we stretch.
|
||||
*/
|
||||
for (Evoral::Sequence<MidiModel::TimeType>::const_iterator i = old_model->begin (MidiModel::TimeType(), true);
|
||||
i != old_model->end(); ++i) {
|
||||
const MidiModel::TimeType new_time = i->time() * (double)_request.time_fraction;
|
||||
for (Evoral::Sequence<MidiModel::TimeType>::const_iterator i = old_model->begin (MidiModel::TimeType(), true); i != old_model->end(); ++i) {
|
||||
|
||||
if (i->time() < r_start) {
|
||||
#ifdef DEBUG_MIDI_STRETCH
|
||||
cout << "SKIP EARLY EVENT " << i->time() << "\n";
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
if (i->time() > r_end) {
|
||||
#ifdef DEBUG_MIDI_STRETCH
|
||||
cout << "SKIP LATE EVENT " << i->time() << "\n";
|
||||
continue;
|
||||
#else
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef DEBUG_MIDI_STRETCH
|
||||
cout << "STRETCH " << i->time() << " TO " << (i->time() - r_start) * mtfrac << "\n";
|
||||
#endif
|
||||
|
||||
const MidiModel::TimeType new_time = (i->time() - r_start) * mtfrac;
|
||||
|
||||
// FIXME: double copy
|
||||
Evoral::Event<MidiModel::TimeType> ev(*i, true);
|
||||
@ -108,13 +146,16 @@ MidiStretch::run (boost::shared_ptr<Region> r, Progress*)
|
||||
new_model->append(ev, Evoral::next_event_id());
|
||||
}
|
||||
|
||||
new_model->end_write (Evoral::Sequence<Temporal::Beats>::DeleteStuckNotes);
|
||||
new_model->end_write (Evoral::Sequence<Temporal::Beats>::ResolveStuckNotes);
|
||||
new_model->set_edited (true);
|
||||
|
||||
new_src->copy_interpolation_from (src);
|
||||
|
||||
const int ret = finish (region, nsrcs, new_name);
|
||||
|
||||
/* non-musical */
|
||||
results[0]->set_start(0);
|
||||
results[0]->set_position (r->position ()); // force updates the region's quarter-note position
|
||||
results[0]->set_length((samplecnt_t) floor (r->length() * _request.time_fraction), 0);
|
||||
|
||||
return ret;
|
||||
|
Loading…
Reference in New Issue
Block a user