feat(transport): set project tempo by tapping a pulse (phase 97)
This commit is contained in:
parent
7ad85b6144
commit
b7a4a1acd4
9 changed files with 92 additions and 5 deletions
23
src/control/tempo.c
Normal file
23
src/control/tempo.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "xtc/xtc_tempo.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
void xtc_tap_tempo_reset(xtc_tap_tempo *s){if(s)memset(s,0,sizeof(*s));}
|
||||
xtc_result xtc_tap_tempo_push(xtc_tap_tempo *s,double seconds,unsigned int *out)
|
||||
{
|
||||
xtc_tap_tempo copy;double interval,sum=0;unsigned int i,tempo;
|
||||
if(!s || !out || !isfinite(seconds) || seconds<0 || s->started>1 || s->count>4 || s->head>=4 ||
|
||||
(s->count<4 && s->head!=s->count) || (!s->started && s->count) ||
|
||||
(s->started && (!isfinite(s->last) || s->last<0)))return XTC_ERR_INVALID_ARGUMENT;
|
||||
for(i=0;i<s->count;++i)if(!isfinite(s->intervals[i]) || s->intervals[i]<.2 || s->intervals[i]>3)return XTC_ERR_INVALID_ARGUMENT;
|
||||
if(!s->started || seconds-s->last>3) {
|
||||
xtc_tap_tempo_reset(s);s->started=1;s->last=seconds;return XTC_ERR_NOT_FOUND;
|
||||
}
|
||||
interval=seconds-s->last;
|
||||
if(interval<.2)return XTC_ERR_INVALID_ARGUMENT;
|
||||
copy=*s;copy.last=seconds;copy.intervals[copy.head]=interval;copy.head=(copy.head+1)%4;
|
||||
if(copy.count<4)++copy.count;
|
||||
for(i=0;i<copy.count;++i)sum+=copy.intervals[i];
|
||||
tempo=(unsigned int)lround(60000.0*copy.count/sum);
|
||||
if(tempo<20000 || tempo>300000)return XTC_ERR_INVALID_ARGUMENT;
|
||||
*s=copy;*out=tempo;return XTC_OK;
|
||||
}
|
||||
|
|
@ -36,6 +36,7 @@
|
|||
#include <cmath>
|
||||
#include <cerrno>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
|
||||
namespace {
|
||||
class PanelWindow : public Fl_Double_Window {
|
||||
|
|
@ -266,6 +267,9 @@ MainWindow::MainWindow(bool prompt_unsaved,const char *configuration)
|
|||
auto *fit = new Fl_Button(790, 82, 100, 28, "Fit song");
|
||||
fit->tooltip("Choose the largest supported zoom that fits the song (Ctrl+0)");
|
||||
fit->callback([](Fl_Widget *, void *c) { static_cast<MainWindow *>(c)->fit_song(); }, this);
|
||||
auto *tap=new Fl_Button(905,82,80,28,"Tap");
|
||||
tap->tooltip("Tap tempo: averages up to four intervals, 20–300 BPM; pauses over three seconds restart.");
|
||||
tap->callback([](Fl_Widget *,void *c){auto *w=static_cast<MainWindow *>(c);double now=std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();auto r=w->tap_tempo(now);if(r!=XTC_OK && r!=XTC_ERR_NOT_FOUND && r!=XTC_ERR_INVALID_ARGUMENT)w->report_error("Tap tempo unchanged",r);},this);
|
||||
loop_button_ = new Fl_Check_Button(1000, 82, 100, 28, "Loop");
|
||||
loop_button_->callback([](Fl_Widget *widget,void *c){auto *w=static_cast<MainWindow *>(c);
|
||||
xtc_result r=w->set_loop(static_cast<uint64_t>(w->settings_.loop_start),static_cast<uint64_t>(w->settings_.loop_end),static_cast<Fl_Check_Button *>(widget)->value()!=0);
|
||||
|
|
@ -1409,6 +1413,7 @@ xtc_result MainWindow::install_project(xtc_project *candidate, bool reset_histor
|
|||
history_.swap(new_history);
|
||||
for(auto &snapshot:mix_snapshots_)snapshot.reset();
|
||||
if(track_inserts_)track_inserts_->clear_session();
|
||||
xtc_tap_tempo_reset(&tap_state_);
|
||||
}
|
||||
graph_widget_->reset_layout();
|
||||
graph_widget_->set_graph(xtc_project_graph(candidate));
|
||||
|
|
@ -2750,3 +2755,12 @@ xtc_result MainWindow::open_track_inserts(uint64_t track)
|
|||
}
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
xtc_result MainWindow::tap_tempo(double seconds)
|
||||
{
|
||||
if(recorder_)return XTC_ERR_INVALID_STATE;
|
||||
auto state=tap_state_;unsigned int tempo=0;auto r=xtc_tap_tempo_push(&state,seconds,&tempo);
|
||||
if(r==XTC_ERR_NOT_FOUND){tap_state_=state;return r;}
|
||||
if(r!=XTC_OK)return r;
|
||||
r=set_tempo(tempo);if(r==XTC_OK)tap_state_=state;return r;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include "xtc/xtc_backend.h"
|
||||
#include "xtc/xtc_mixer.h"
|
||||
#include "xtc/xtc_plugin.h"
|
||||
#include "xtc/xtc_tempo.h"
|
||||
#include "xtc/xtc_control.h"
|
||||
#include "xtc/xtc_settings.h"
|
||||
|
||||
|
|
@ -42,6 +43,7 @@ class MainWindow : public Fl_Double_Window {
|
|||
* the current document. No engine/model changes happen in audio callbacks. */
|
||||
xtc_result create_track(bool midi);
|
||||
xtc_result set_tempo(unsigned int milli_bpm);
|
||||
xtc_result tap_tempo(double seconds);
|
||||
xtc_result collect_project(const char *directory);
|
||||
xtc_result navigate_marker(int direction);
|
||||
xtc_result loop_section();
|
||||
|
|
@ -224,6 +226,7 @@ class MainWindow : public Fl_Double_Window {
|
|||
|
||||
private:
|
||||
friend class TrackInsertsPanel;
|
||||
xtc_tap_tempo tap_state_{};
|
||||
TrackInsertsPanel *track_inserts_=nullptr;
|
||||
using MixSnapshot=std::unique_ptr<xtc_mix_snapshot,decltype(&xtc_mix_snapshot_destroy)>;
|
||||
MixSnapshot mix_snapshots_[2]{{nullptr,xtc_mix_snapshot_destroy},{nullptr,xtc_mix_snapshot_destroy}};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue