Skip to content

Commit

Permalink
Improve bpm changes on Jack connection
Browse files Browse the repository at this point in the history
  • Loading branch information
gvnnz committed Jun 16, 2018
1 parent 24c3738 commit aecc943
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/core/const.h
Expand Up @@ -93,7 +93,9 @@

/* -- MIN/MAX values -------------------------------------------------------- */
#define G_MIN_BPM 20.0f
#define G_MIN_BPM_STR "20.0"
#define G_MAX_BPM 999.0f
#define G_MAX_BPM_STR "999.0"
#define G_MAX_BEATS 32
#define G_MAX_BARS 32
#define G_MAX_QUANTIZE 8
Expand Down
3 changes: 3 additions & 0 deletions src/core/kernelAudio.cpp
Expand Up @@ -439,6 +439,9 @@ bool hasAPI(int API)
}


int getAPI() { return api; }


/* -------------------------------------------------------------------------- */


Expand Down
1 change: 1 addition & 0 deletions src/core/kernelAudio.h
Expand Up @@ -78,6 +78,7 @@ int getDeviceByName(const char* name);
int getDefaultOut();
int getDefaultIn();
bool hasAPI(int API);
int getAPI();

#ifdef __linux__

Expand Down
89 changes: 57 additions & 32 deletions src/glue/main.cpp
Expand Up @@ -42,6 +42,7 @@
#include "../core/kernelMidi.h"
#include "../core/kernelAudio.h"
#include "../core/conf.h"
#include "../core/const.h"
#ifdef WITH_VST
#include "../core/pluginHost.h"
#endif
Expand All @@ -51,57 +52,81 @@
extern gdMainWindow *G_MainWin;


using std::string;
using namespace giada::m;


void glue_setBpm(const char *v1, const char *v2)
namespace
{
void setBpm_(float f, string s)
{
if (f < G_MIN_BPM) {
f = G_MIN_BPM;
s = G_MIN_BPM_STR;
}
else
if (f > G_MAX_BPM) {
f = G_MAX_BPM;
s = G_MAX_BPM_STR;
}

float vPre = clock::getBpm();
clock::setBpm(f);
recorder::updateBpm(vPre, f, clock::getQuanto());
mixer::allocVirtualInput(clock::getFramesInLoop());

gu_refreshActionEditor();
G_MainWin->mainTimer->setBpm(s.c_str());

gu_log("[glue::setBpm_] Bpm changed to %s (real=%f)\n", s.c_str(), clock::getBpm());
}
} // {anonymous}


/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */


void glue_setBpm(const char* v1, const char* v2)
{
/* Never change this stuff while recording audio */

if (mixer::recording)
return;

char bpmS[6];
float bpmF = atof(v1) + (atof(v2)/10);
if (bpmF < 20.0f) {
bpmF = 20.0f;
sprintf(bpmS, "20.0");
}
else
sprintf(bpmS, "%s.%s", v1, !strcmp(v2, "") ? "0" : v2);

/* a value such as atof("120.1") will never be 120.1 but 120.0999999,
* because of the rounding error. So we pass the real "wrong" value to
* G_Mixer and we show the nice looking (but fake) one to the GUI. */
/* A value such as atof("120.1") will never be 120.1 but 120.0999999, because
of the rounding error. So we pass the real "wrong" value to mixer and we show
the nice looking (but fake) one to the GUI.
On Linux, let Jack handle the bpm change if its on. */

float oldBpmF = clock::getBpm();
clock::setBpm(bpmF);
recorder::updateBpm(oldBpmF, bpmF, clock::getQuanto());
mixer::allocVirtualInput(clock::getFramesInLoop());
float f = atof(v1) + (atof(v2)/10);
string s = string(v1) + "." + string(v2);

#ifdef __linux__
kernelAudio::jackSetBpm(clock::getBpm());
#ifdef G_OS_LINUX
if (kernelAudio::getAPI() == G_SYS_API_JACK)
kernelAudio::jackSetBpm(f);
else
#endif

gu_refreshActionEditor();
G_MainWin->mainTimer->setBpm(bpmS);

gu_log("[glue] Bpm changed to %s (real=%f)\n", bpmS, clock::getBpm());
setBpm_(f, s);
}


/* -------------------------------------------------------------------------- */


void glue_setBpm(float v)
void glue_setBpm(float f)
{
if (v < G_MIN_BPM || v > G_MAX_BPM)
v = G_DEFAULT_BPM;
double fIpart;
double fPpart = modf(v, &fIpart);
int iIpart = fIpart;
int iPpart = ceilf(fPpart);
glue_setBpm(gu_iToString(iIpart).c_str(), gu_iToString(iPpart).c_str());
/* Never change this stuff while recording audio */

if (mixer::recording)
return;

float intpart;
float fracpart = std::round(std::modf(f, &intpart) * 10);
string s = std::to_string((int) intpart) + "." + std::to_string((int)fracpart);

setBpm_(f, s);
}


Expand Down
14 changes: 11 additions & 3 deletions src/glue/main.h
Expand Up @@ -36,8 +36,16 @@
#define G_GLUE_MAIN_H


/* glue_setBpm (1)
Sets bpm value from string to float. */

void glue_setBpm(const char* v1, const char* v2);

/* glue_setBpm (2)
Sets bpm value. Usually called from the Jack callback or non-UI components. */

void glue_setBpm(float v);

void glue_setBeats(int beats, int bars, bool expand);
void glue_quantize(int val);
void glue_setOutVol(float v, bool gui=true);
Expand All @@ -46,13 +54,13 @@ void glue_clearAllSamples();
void glue_clearAllActions();

/* resetToInitState
* reset Giada to init state. If resetGui also refresh all widgets. If
* createColumns also build initial empty columns. */
Resets Giada to init state. If resetGui also refresh all widgets. If
createColumns also build initial empty columns. */

void glue_resetToInitState(bool resetGui=true, bool createColumns=true);

/* beatsDivide/Multiply
* shrinks or enlarges the number of beats by 2. */
Shrinks or enlarges the number of beats by 2. */

void glue_beatsMultiply();
void glue_beatsDivide();
Expand Down

0 comments on commit aecc943

Please sign in to comment.