Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SCALE0 op #311

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- **FIX**: fix MIDI IN ops channel number being off by 1
- **FIX**: improve `TR.P` accuracy
- **FIX**: fix `KILL` not stopping TR pulses in progress
- **NEW**: new op: `SCALE0` / `SCL0`

## v4.0.0

Expand Down
5 changes: 5 additions & 0 deletions docs/ops/maths.toml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ prototype = "SCALE a b x y i"
aliases = ["SCL"]
short = "scale `i` from range `a` to `b` to range `x` to `y`, i.e. `i * (y - x) / (b - a)`"

[SCALE0]
prototype = "SCALE a b i"
aliases = ["SCL0"]
short = "scale `i` from range `0` to `a` to range `0` to `b`"

[ER]
prototype = "ER f l i"
short = "Euclidean rhythm, `f` is fill (`1-32`), `l` is length (`1-32`) and `i` is step (any value), returns `0` or `1`"
Expand Down
1 change: 1 addition & 0 deletions docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- **FIX**: fix MIDI IN ops channel number being off by 1
- **FIX**: improve `TR.P` accuracy
- **FIX**: fix `KILL` not stopping TR pulses in progress
- **NEW**: new op: `SCALE0` / `SCL0`

## v4.0.0

Expand Down
4 changes: 3 additions & 1 deletion module/help_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ const char* help4[HELP4_LENGTH] = { "4/17 DATA AND TABLES",
" S = STEP (0-15)"
};

#define HELP5_LENGTH 128
#define HELP5_LENGTH 130
const char* help5[HELP5_LENGTH] = { "5/17 OPERATORS",
" ",
"RAND A|RANDOM 0 - A",
Expand Down Expand Up @@ -341,6 +341,8 @@ const char* help5[HELP5_LENGTH] = { "5/17 OPERATORS",
"WRAP A B C|WRAP A AROUND B-C",
"SCALE A B X Y I",
" SCALE I FROM A..B TO X..Y",
"SCALE0 A B I",
" SCALE I FROM 0..A TO 0..B",
"QT A B|QUANTIZE A TO B*X",
"QT.S X R S|QUANTIZE TO SCALE",
" X = INPUT *",
Expand Down
2 changes: 2 additions & 0 deletions src/match_token.rl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@
"JI" => { MATCH_OP(E_OP_JI); };
"SCALE" => { MATCH_OP(E_OP_SCALE); };
"SCL" => { MATCH_OP(E_OP_SCL); };
"SCALE0" => { MATCH_OP(E_OP_SCALE0); };
"SCL0" => { MATCH_OP(E_OP_SCL0); };
"N" => { MATCH_OP(E_OP_N); };
"VN" => { MATCH_OP(E_OP_VN); };
"HZ" => { MATCH_OP(E_OP_HZ); };
Expand Down
26 changes: 25 additions & 1 deletion src/ops/maths.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ static void op_JI_get(const void *data, scene_state_t *ss, exec_state_t *es,
command_state_t *cs);
static void op_SCALE_get(const void *data, scene_state_t *ss, exec_state_t *es,
command_state_t *cs);
static void op_SCALE0_get(const void *data, scene_state_t *ss, exec_state_t *es,
command_state_t *cs);
static void op_N_get(const void *data, scene_state_t *ss, exec_state_t *es,
command_state_t *cs);
static void op_VN_get(const void *data, scene_state_t *ss, exec_state_t *es,
Expand Down Expand Up @@ -233,6 +235,8 @@ const tele_op_t op_OR4 = MAKE_GET_OP(OR4 , op_OR4_get , 4, true);
const tele_op_t op_JI = MAKE_GET_OP(JI , op_JI_get , 2, true);
const tele_op_t op_SCALE = MAKE_GET_OP(SCALE , op_SCALE_get , 5, true);
const tele_op_t op_SCL = MAKE_GET_OP(SCL , op_SCALE_get , 5, true);
const tele_op_t op_SCALE0 = MAKE_GET_OP(SCALE0 , op_SCALE0_get , 3, true);
const tele_op_t op_SCL0 = MAKE_GET_OP(SCL0 , op_SCALE0_get , 3, true);
const tele_op_t op_N = MAKE_GET_OP(N , op_N_get , 1, true);
const tele_op_t op_VN = MAKE_GET_OP(VN , op_VN_get , 1, true);
const tele_op_t op_HZ = MAKE_GET_OP(HZ , op_HZ_get , 1, true);
Expand Down Expand Up @@ -970,6 +974,26 @@ static void op_SCALE_get(const void *NOTUSED(data), scene_state_t *NOTUSED(ss),
cs_push(cs, result + x);
}

static void op_SCALE0_get(const void *NOTUSED(data), scene_state_t *NOTUSED(ss),
exec_state_t *NOTUSED(es), command_state_t *cs) {
int32_t b, y, i;
int32_t a = 0;
b = cs_pop(cs);
int32_t x = 0;
y = cs_pop(cs);
i = cs_pop(cs);

if ((b - a) == 0) {
cs_push(cs, 0);
return;
}

int32_t result = (i - a) * (y - x) * 2 / (b - a);
result = result / 2 + (result & 1); // rounding

cs_push(cs, result + x);
}

static void op_N_get(const void *NOTUSED(data), scene_state_t *NOTUSED(ss),
exec_state_t *NOTUSED(es), command_state_t *cs) {
int16_t a = cs_pop(cs);
Expand Down Expand Up @@ -1256,7 +1280,7 @@ static void op_BPM_get(const void *NOTUSED(data), scene_state_t *NOTUSED(ss),
if (a < 2) a = 2;
if (a > 1000) a = 1000;
ret = ((((uint32_t)(1 << 31)) / ((a << 20) / 60)) * 1000) >> 10;
ret = ret / 2 + (ret & 1); // rounding
ret = ret / 2 + (ret & 1); // rounding
cs_push(cs, (int16_t)ret);
}

Expand Down
2 changes: 2 additions & 0 deletions src/ops/maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extern const tele_op_t op_OR4;
extern const tele_op_t op_JI;
extern const tele_op_t op_SCALE;
extern const tele_op_t op_SCL;
extern const tele_op_t op_SCALE0;
extern const tele_op_t op_SCL0;
extern const tele_op_t op_N;
extern const tele_op_t op_VN;
extern const tele_op_t op_HZ;
Expand Down
20 changes: 10 additions & 10 deletions src/ops/op.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ const tele_op_t *tele_ops[E_OP__LENGTH] = {
&op_AVG, &op_EQ, &op_NE, &op_LT, &op_GT, &op_LTE, &op_GTE, &op_INR,
&op_OUTR, &op_INRI, &op_OUTRI, &op_NZ, &op_EZ, &op_RSH, &op_LSH, &op_LROT,
&op_RROT, &op_EXP, &op_ABS, &op_SGN, &op_AND, &op_OR, &op_AND3, &op_OR3,
&op_AND4, &op_OR4, &op_JI, &op_SCALE, &op_SCL, &op_N, &op_VN, &op_HZ,
&op_N_S, &op_N_C, &op_N_CS, &op_N_B, &op_N_BX, &op_V, &op_VV, &op_ER,
&op_NR, &op_DR_T, &op_DR_P, &op_DR_V, &op_BPM, &op_BIT_OR, &op_BIT_AND,
&op_BIT_NOT, &op_BIT_XOR, &op_BSET, &op_BGET, &op_BCLR, &op_BTOG, &op_BREV,
&op_XOR, &op_CHAOS, &op_CHAOS_R, &op_CHAOS_ALG, &op_SYM_PLUS, &op_SYM_DASH,
&op_SYM_STAR, &op_SYM_FORWARD_SLASH, &op_SYM_PERCENTAGE, &op_SYM_EQUAL_x2,
&op_SYM_EXCLAMATION_EQUAL, &op_SYM_LEFT_ANGLED, &op_SYM_RIGHT_ANGLED,
&op_SYM_LEFT_ANGLED_EQUAL, &op_SYM_RIGHT_ANGLED_EQUAL,
&op_SYM_RIGHT_ANGLED_LEFT_ANGLED, &op_SYM_LEFT_ANGLED_RIGHT_ANGLED,
&op_SYM_RIGHT_ANGLED_EQUAL_LEFT_ANGLED,
&op_AND4, &op_OR4, &op_JI, &op_SCALE, &op_SCL, &op_SCALE0, &op_SCL0, &op_N,
&op_VN, &op_HZ, &op_N_S, &op_N_C, &op_N_CS, &op_N_B, &op_N_BX, &op_V,
&op_VV, &op_ER, &op_NR, &op_DR_T, &op_DR_P, &op_DR_V, &op_BPM, &op_BIT_OR,
&op_BIT_AND, &op_BIT_NOT, &op_BIT_XOR, &op_BSET, &op_BGET, &op_BCLR,
&op_BTOG, &op_BREV, &op_XOR, &op_CHAOS, &op_CHAOS_R, &op_CHAOS_ALG,
&op_SYM_PLUS, &op_SYM_DASH, &op_SYM_STAR, &op_SYM_FORWARD_SLASH,
&op_SYM_PERCENTAGE, &op_SYM_EQUAL_x2, &op_SYM_EXCLAMATION_EQUAL,
&op_SYM_LEFT_ANGLED, &op_SYM_RIGHT_ANGLED, &op_SYM_LEFT_ANGLED_EQUAL,
&op_SYM_RIGHT_ANGLED_EQUAL, &op_SYM_RIGHT_ANGLED_LEFT_ANGLED,
&op_SYM_LEFT_ANGLED_RIGHT_ANGLED, &op_SYM_RIGHT_ANGLED_EQUAL_LEFT_ANGLED,
&op_SYM_LEFT_ANGLED_EQUAL_RIGHT_ANGLED, &op_SYM_EXCLAMATION,
&op_SYM_LEFT_ANGLED_x2, &op_SYM_RIGHT_ANGLED_x2, &op_SYM_LEFT_ANGLED_x3,
&op_SYM_RIGHT_ANGLED_x3, &op_SYM_AMPERSAND_x2, &op_SYM_PIPE_x2,
Expand Down
2 changes: 2 additions & 0 deletions src/ops/op_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ typedef enum {
E_OP_JI,
E_OP_SCALE,
E_OP_SCL,
E_OP_SCALE0,
E_OP_SCL0,
E_OP_N,
E_OP_VN,
E_OP_HZ,
Expand Down