| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #include <math.h> |
| 7 |
|
| #include <stdlib.h> |
| 8 |
|
| #include <string.h> |
| 9 |
|
| |
| 10 |
|
| #include "tinyexpr.h" |
| 11 |
|
| #include "morpher_jit.h" |
| 12 |
|
| |
| 13 |
|
| #ifndef M_PI |
| 14 |
|
| #define M_PI 3.14159265358979323846 |
| 15 |
|
| #endif |
| 16 |
|
| |
| 17 |
|
| /* Predefined morpher functions */ |
| 18 |
1034 |
| static double morpher_sine(double t) { |
| 19 |
1034 |
| double s = sin(t * M_PI / 2.0); |
| 20 |
1034 |
| return s * s; |
| 21 |
|
| } |
| 22 |
|
| |
| 23 |
7 |
| static double morpher_linear(double t) { |
| 24 |
7 |
| return t; |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
19 |
| static double morpher_smoothstep(double t) { |
| 28 |
19 |
| return t * t * (3.0 - 2.0 * t); |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
0 |
| static double morpher_smootherstep(double t) { |
| 32 |
0 |
| return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
0 |
| static double morpher_cubic_in(double t) { |
| 36 |
0 |
| return t * t * t; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
0 |
| static double morpher_cubic_out(double t) { |
| 40 |
0 |
| double u = 1.0 - t; |
| 41 |
0 |
| return 1.0 - u * u * u; |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
0 |
| static double morpher_cubic_inout(double t) { |
| 45 |
0 |
▶ | if (t < 0.5) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 0.5) { |
0 |
0 |
0 |
|
| 46 |
0 |
| return 4.0 * t * t * t; |
| 47 |
|
| } else { |
| 48 |
0 |
| double u = 2.0 * t - 2.0; |
| 49 |
0 |
| return 0.5 * u * u * u + 1.0; |
| 50 |
|
| } |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
0 |
| static double morpher_exp_in(double t) { |
| 54 |
0 |
▶ | return t == 0.0 ? 0.0 : pow(2.0, 10.0 * (t - 1.0)); |
Branch: 1 branch
| Branch | True | False | Total |
| return t == 0.0 ? 0.0 : pow(2.0, 10.0 * (t - 1.0)); |
0 |
0 |
0 |
|
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
0 |
| static double morpher_exp_out(double t) { |
| 58 |
0 |
▶ | return t == 1.0 ? 1.0 : 1.0 - pow(2.0, -10.0 * t); |
Branch: 1 branch
| Branch | True | False | Total |
| return t == 1.0 ? 1.0 : 1.0 - pow(2.0, -10.0 * t); |
0 |
0 |
0 |
|
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
0 |
| static double morpher_tanh(double t) { |
| 62 |
0 |
| return tanh(2.0 * t) / tanh(2.0); |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
0 |
| static double morpher_welch(double t) { |
| 66 |
0 |
| return sin(t * M_PI / 2.0); |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
2 |
| static double morpher_quad_in(double t) { |
| 70 |
2 |
| return t * t; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
2 |
| static double morpher_quad_out(double t) { |
| 74 |
2 |
| return t * (2.0 - t); |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
2 |
| static double morpher_quad_inout(double t) { |
| 78 |
2 |
▶ | if (t < 0.5) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 0.5) |
1 |
1 |
2 |
|
| 79 |
1 |
| return 2.0 * t * t; |
| 80 |
1 |
| return -1.0 + (4.0 - 2.0 * t) * t; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
2 |
| static double morpher_circ_in(double t) { |
| 84 |
2 |
| double d = 1.0 - t * t; |
| 85 |
2 |
▶ | return (d <= 0.0) ? 1.0 : 1.0 - sqrt(d); |
Branch: 1 branch
| Branch | True | False | Total |
| return (d <= 0.0) ? 1.0 : 1.0 - sqrt(d); |
1 |
1 |
2 |
|
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
2 |
| static double morpher_circ_out(double t) { |
| 89 |
2 |
| double u = t - 1.0; |
| 90 |
2 |
| double d = 1.0 - u * u; |
| 91 |
2 |
▶ | return (d <= 0.0) ? 0.0 : sqrt(d); |
Branch: 1 branch
| Branch | True | False | Total |
| return (d <= 0.0) ? 0.0 : sqrt(d); |
1 |
1 |
2 |
|
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
2 |
| static double morpher_circ_inout(double t) { |
| 95 |
2 |
▶ | if (t < 0.5) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 0.5) { |
1 |
1 |
2 |
|
| 96 |
1 |
| double d = 1.0 - 4.0 * t * t; |
| 97 |
1 |
▶ | return (d <= 0.0) ? 0.5 : (1.0 - sqrt(d)) * 0.5; |
Branch: 1 branch
| Branch | True | False | Total |
| return (d <= 0.0) ? 0.5 : (1.0 - sqrt(d)) * 0.5; |
0 |
1 |
1 |
|
| 98 |
|
| } else { |
| 99 |
1 |
| double u = 2.0 * t - 2.0; |
| 100 |
1 |
| double d = 1.0 - u * u; |
| 101 |
1 |
▶ | return (d <= 0.0) ? 0.5 : (sqrt(d) + 1.0) * 0.5; |
Branch: 1 branch
| Branch | True | False | Total |
| return (d <= 0.0) ? 0.5 : (sqrt(d) + 1.0) * 0.5; |
0 |
1 |
1 |
|
| 102 |
|
| } |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| #define BACK_S 1.70158 |
| 106 |
|
| |
| 107 |
3 |
| static double morpher_back_in(double t) { |
| 108 |
3 |
| return t * t * ((BACK_S + 1.0) * t - BACK_S); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
2 |
| static double morpher_back_out(double t) { |
| 112 |
2 |
| double u = t - 1.0; |
| 113 |
2 |
| return u * u * ((BACK_S + 1.0) * u + BACK_S) + 1.0; |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
2 |
| static double morpher_back_inout(double t) { |
| 117 |
2 |
| double s2 = BACK_S * 1.525; |
| 118 |
2 |
▶ | if (t < 0.5) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 0.5) { |
1 |
1 |
2 |
|
| 119 |
1 |
| double u = 2.0 * t; |
| 120 |
1 |
| return (u * u * ((s2 + 1.0) * u - s2)) * 0.5; |
| 121 |
|
| } else { |
| 122 |
1 |
| double u = 2.0 * t - 2.0; |
| 123 |
1 |
| return (u * u * ((s2 + 1.0) * u + s2) + 2.0) * 0.5; |
| 124 |
|
| } |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
2 |
| static double morpher_elastic_in(double t) { |
| 128 |
2 |
▶ | if (t <= 0.0) return 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t <= 0.0) return 0.0; |
1 |
1 |
2 |
|
| 129 |
1 |
▶ | if (t >= 1.0) return 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t >= 1.0) return 1.0; |
1 |
0 |
1 |
|
| 130 |
0 |
| return -pow(2.0, 10.0 * t - 10.0) * sin((t * 10.0 - 10.75) * (2.0 * M_PI / 3.0)); |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
2 |
| static double morpher_elastic_out(double t) { |
| 134 |
2 |
▶ | if (t <= 0.0) return 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t <= 0.0) return 0.0; |
1 |
1 |
2 |
|
| 135 |
1 |
▶ | if (t >= 1.0) return 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t >= 1.0) return 1.0; |
1 |
0 |
1 |
|
| 136 |
0 |
| return pow(2.0, -10.0 * t) * sin((t * 10.0 - 0.75) * (2.0 * M_PI / 3.0)) + 1.0; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
2 |
| static double morpher_elastic_inout(double t) { |
| 140 |
2 |
| double c = (2.0 * M_PI) / 4.5; |
| 141 |
2 |
▶ | if (t <= 0.0) return 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t <= 0.0) return 0.0; |
1 |
1 |
2 |
|
| 142 |
1 |
▶ | if (t >= 1.0) return 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t >= 1.0) return 1.0; |
1 |
0 |
1 |
|
| 143 |
0 |
▶ | if (t < 0.5) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 0.5) |
0 |
0 |
0 |
|
| 144 |
0 |
| return -(pow(2.0, 20.0 * t - 10.0) * sin((20.0 * t - 11.125) * c)) * 0.5; |
| 145 |
0 |
| return (pow(2.0, -20.0 * t + 10.0) * sin((20.0 * t - 11.125) * c)) * 0.5 + 1.0; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
6 |
| static double bounce_out_raw(double t) { |
| 149 |
6 |
▶ | if (t < 1.0 / 2.75) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 1.0 / 2.75) |
2 |
4 |
6 |
|
| 150 |
2 |
| return 7.5625 * t * t; |
| 151 |
4 |
▶ | if (t < 2.0 / 2.75) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 2.0 / 2.75) { |
0 |
4 |
4 |
|
| 152 |
0 |
| t -= 1.5 / 2.75; |
| 153 |
0 |
| return 7.5625 * t * t + 0.75; |
| 154 |
|
| } |
| 155 |
4 |
▶ | if (t < 2.5 / 2.75) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 2.5 / 2.75) { |
0 |
4 |
4 |
|
| 156 |
0 |
| t -= 2.25 / 2.75; |
| 157 |
0 |
| return 7.5625 * t * t + 0.9375; |
| 158 |
|
| } |
| 159 |
4 |
| t -= 2.625 / 2.75; |
| 160 |
4 |
| return 7.5625 * t * t + 0.984375; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
2 |
| static double morpher_bounce_in(double t) { |
| 164 |
2 |
| return 1.0 - bounce_out_raw(1.0 - t); |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
2 |
| static double morpher_bounce_out(double t) { |
| 168 |
2 |
| return bounce_out_raw(t); |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
2 |
| static double morpher_bounce_inout(double t) { |
| 172 |
2 |
▶ | if (t < 0.5) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < 0.5) |
1 |
1 |
2 |
|
| 173 |
1 |
| return (1.0 - bounce_out_raw(1.0 - 2.0 * t)) * 0.5; |
| 174 |
1 |
| return (1.0 + bounce_out_raw(2.0 * t - 1.0)) * 0.5; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
|
| typedef double (*morpher_fn)(double); |
| 178 |
|
| |
| 179 |
|
| typedef struct { |
| 180 |
|
| const char *name; |
| 181 |
|
| morpher_fn fn; |
| 182 |
|
| } morpher_entry; |
| 183 |
|
| |
| 184 |
|
| static const morpher_entry predefined_morphers[] = { |
| 185 |
|
| {"back_in", morpher_back_in}, |
| 186 |
|
| {"back_inout", morpher_back_inout}, |
| 187 |
|
| {"back_out", morpher_back_out}, |
| 188 |
|
| {"bounce_in", morpher_bounce_in}, |
| 189 |
|
| {"bounce_inout", morpher_bounce_inout}, |
| 190 |
|
| {"bounce_out", morpher_bounce_out}, |
| 191 |
|
| {"circ_in", morpher_circ_in}, |
| 192 |
|
| {"circ_inout", morpher_circ_inout}, |
| 193 |
|
| {"circ_out", morpher_circ_out}, |
| 194 |
|
| {"cubic_in", morpher_cubic_in}, |
| 195 |
|
| {"cubic_inout", morpher_cubic_inout}, |
| 196 |
|
| {"cubic_out", morpher_cubic_out}, |
| 197 |
|
| {"elastic_in", morpher_elastic_in}, |
| 198 |
|
| {"elastic_inout", morpher_elastic_inout}, |
| 199 |
|
| {"elastic_out", morpher_elastic_out}, |
| 200 |
|
| {"exp_in", morpher_exp_in}, |
| 201 |
|
| {"exp_out", morpher_exp_out}, |
| 202 |
|
| {"linear", morpher_linear}, |
| 203 |
|
| {"quad_in", morpher_quad_in}, |
| 204 |
|
| {"quad_inout", morpher_quad_inout}, |
| 205 |
|
| {"quad_out", morpher_quad_out}, |
| 206 |
|
| {"sine", morpher_sine}, |
| 207 |
|
| {"smootherstep", morpher_smootherstep}, |
| 208 |
|
| {"smoothstep", morpher_smoothstep}, |
| 209 |
|
| {"tanh", morpher_tanh}, |
| 210 |
|
| {"welch", morpher_welch}, |
| 211 |
|
| {NULL, NULL} |
| 212 |
|
| }; |
| 213 |
|
| |
| 214 |
69 |
| static morpher_fn find_predefined_morpher(const char *name) { |
| 215 |
|
| const morpher_entry *e; |
| 216 |
1193 |
▶ | for (e = predefined_morphers; e->name; e++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (e = predefined_morphers; e->name; e++) { |
1178 |
15 |
1193 |
|
| 217 |
1178 |
▶ | if (strcmp(e->name, name) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(e->name, name) == 0) |
54 |
1124 |
1178 |
|
| 218 |
54 |
| return e->fn; |
| 219 |
|
| } |
| 220 |
15 |
| return NULL; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| typedef struct { |
| 224 |
|
| double *levels; /* N+1 level values */ |
| 225 |
|
| double *durations; /* N duration values */ |
| 226 |
|
| double *curves; /* N curve values */ |
| 227 |
|
| int segments; /* N (number of segments) */ |
| 228 |
|
| double total_duration; |
| 229 |
|
| |
| 230 |
|
| /* Flags */ |
| 231 |
|
| int is_morph; |
| 232 |
|
| int is_hold; |
| 233 |
|
| int is_fold_over; |
| 234 |
|
| int is_wrap_neg; |
| 235 |
|
| |
| 236 |
|
| /* Custom morpher: Perl callback, formula expression, predefined fn, or default */ |
| 237 |
|
| SV *morpher_cv; |
| 238 |
|
| te_expr *morpher_expr; |
| 239 |
|
| double morpher_expr_t; |
| 240 |
|
| char *morpher_formula; |
| 241 |
|
| morpher_fn morpher_fn; |
| 242 |
|
| jit_morpher *morpher_jit; |
| 243 |
|
| |
| 244 |
|
| /* Border levels for random generation */ |
| 245 |
|
| double border_start; |
| 246 |
|
| double border_end; |
| 247 |
|
| |
| 248 |
|
| /* Cached state for sequential access optimization */ |
| 249 |
|
| int current_segment; |
| 250 |
|
| int past_segment; |
| 251 |
|
| double passed_duration; |
| 252 |
|
| double level_diff; |
| 253 |
|
| int is_neg; |
| 254 |
|
| int is_asc; |
| 255 |
|
| } SegmentedEnvelope; |
| 256 |
|
| |
| 257 |
|
| /* Call Perl morpher callback */ |
| 258 |
52 |
| static double call_morpher(pTHX_ SV *morpher_cv, double t) { |
| 259 |
52 |
| dSP; |
| 260 |
|
| int count; |
| 261 |
|
| double result; |
| 262 |
|
| |
| 263 |
52 |
| ENTER; |
| 264 |
52 |
| SAVETMPS; |
| 265 |
|
| |
| 266 |
52 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
52 |
52 |
|
| 267 |
52 |
▶ | XPUSHs(sv_2mortal(newSVnv(t))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVnv(t))); |
0 |
52 |
52 |
|
| 268 |
52 |
| PUTBACK; |
| 269 |
|
| |
| 270 |
52 |
| count = call_sv(morpher_cv, G_SCALAR); |
| 271 |
|
| |
| 272 |
51 |
| SPAGAIN; |
| 273 |
|
| |
| 274 |
51 |
▶ | if (count != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 1) |
0 |
51 |
51 |
|
| 275 |
0 |
| croak("Morpher callback did not return a single value"); |
| 276 |
|
| |
| 277 |
51 |
| result = POPn; |
| 278 |
|
| |
| 279 |
51 |
| PUTBACK; |
| 280 |
51 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
51 |
0 |
51 |
|
| 281 |
51 |
| LEAVE; |
| 282 |
|
| |
| 283 |
51 |
| return result; |
| 284 |
|
| } |
| 285 |
|
| |
| 286 |
|
| /* Apply morphing to a value */ |
| 287 |
|
| /* Priority: morpher_cv > morpher_expr > morpher_fn (predefined or JIT) > default sine */ |
| 288 |
18785 |
| static double wrap_value(pTHX_ SegmentedEnvelope *env, double t) { |
| 289 |
18785 |
▶ | if (!env->is_morph) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!env->is_morph) |
17631 |
1154 |
18785 |
|
| 290 |
17631 |
| return t; |
| 291 |
1154 |
▶ | if (env->morpher_cv && SvOK(env->morpher_cv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (env->morpher_cv && SvOK(env->morpher_cv)) |
52 |
1102 |
1154 |
| if (env->morpher_cv && SvOK(env->morpher_cv)) |
52 |
0 |
52 |
|
| 292 |
52 |
| return call_morpher(aTHX_ env->morpher_cv, t); |
| 293 |
1102 |
▶ | if (env->morpher_expr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_expr) { |
0 |
1102 |
1102 |
|
| 294 |
0 |
| env->morpher_expr_t = t; |
| 295 |
0 |
| return te_eval(env->morpher_expr); |
| 296 |
|
| } |
| 297 |
1102 |
▶ | if (env->morpher_fn) |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_fn) |
68 |
1034 |
1102 |
|
| 298 |
68 |
| return env->morpher_fn(t); |
| 299 |
1034 |
| return morpher_sine(t); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
|
| /* Wrap position based on hold/fold/wrap settings */ |
| 303 |
18794 |
| static double wrap_pos(SegmentedEnvelope *env, double t) { |
| 304 |
18794 |
| double total = env->total_duration; |
| 305 |
|
| double at, ratio; |
| 306 |
|
| int fold_check; |
| 307 |
|
| |
| 308 |
18794 |
▶ | if (total <= 0.0 || isnan(t)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (total <= 0.0 || isnan(t)) |
18794 |
0 |
18794 |
| if (total <= 0.0 || isnan(t)) |
0 |
18794 |
18794 |
|
| 309 |
0 |
| return 0.0; |
| 310 |
|
| |
| 311 |
18794 |
▶ | if (env->is_hold) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->is_hold) { |
2344 |
16450 |
18794 |
|
| 312 |
2344 |
▶ | if (t > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t > 0) |
2318 |
26 |
2344 |
|
| 313 |
2318 |
▶ | return (t > total) ? total : t; |
Branch: 1 branch
| Branch | True | False | Total |
| return (t > total) ? total : t; |
24 |
2294 |
2318 |
|
| 314 |
26 |
| return 0; |
| 315 |
|
| } |
| 316 |
|
| |
| 317 |
16450 |
▶ | if (isinf(t)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (isinf(t)) |
0 |
16450 |
16450 |
|
| 318 |
0 |
| return 0.0; |
| 319 |
|
| |
| 320 |
16450 |
| at = fabs(t); |
| 321 |
16450 |
▶ | if (at > total) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (at > total) { |
12 |
16438 |
16450 |
|
| 322 |
12 |
| ratio = at / total; |
| 323 |
12 |
| fold_check = (int)fmod(floor(ratio), 2.0); |
| 324 |
12 |
▶ | if (t < 0 && env->is_wrap_neg) |
Branch: 2 branches
| Branch | True | False | Total |
| if (t < 0 && env->is_wrap_neg) |
5 |
7 |
12 |
| if (t < 0 && env->is_wrap_neg) |
3 |
2 |
5 |
|
| 325 |
3 |
| fold_check = !fold_check; |
| 326 |
|
| |
| 327 |
12 |
▶ | if (env->is_fold_over && fold_check) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (env->is_fold_over && fold_check) { |
10 |
2 |
12 |
| if (env->is_fold_over && fold_check) { |
5 |
5 |
10 |
|
| 328 |
5 |
| return (1.0 - (ratio - floor(ratio))) * total; |
| 329 |
|
| } else { |
| 330 |
7 |
| return (ratio - floor(ratio)) * total; |
| 331 |
|
| } |
| 332 |
|
| } |
| 333 |
16438 |
| return at; |
| 334 |
|
| } |
| 335 |
|
| |
| 336 |
|
| /* Update cached segment data */ |
| 337 |
296 |
| static void update_current_segment(SegmentedEnvelope *env, int i) { |
| 338 |
296 |
▶ | if (env->segments <= 0 || i < 0 || i >= env->segments) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (env->segments <= 0 || i < 0 || i >= env->segments) return; |
296 |
0 |
296 |
| if (env->segments <= 0 || i < 0 || i >= env->segments) return; |
296 |
0 |
296 |
| if (env->segments <= 0 || i < 0 || i >= env->segments) return; |
0 |
296 |
296 |
|
| 339 |
296 |
| env->current_segment = i; |
| 340 |
296 |
| env->level_diff = env->levels[i + 1] - env->levels[i]; |
| 341 |
296 |
| env->is_neg = (env->curves[i] < 0) ? 1 : 0; |
| 342 |
296 |
▶ | env->is_asc = (env->level_diff < 0 || env->is_neg) ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| env->is_asc = (env->level_diff < 0 || env->is_neg) ? -1 : 1; |
187 |
109 |
296 |
| env->is_asc = (env->level_diff < 0 || env->is_neg) ? -1 : 1; |
7 |
180 |
187 |
|
| 343 |
296 |
| env->past_segment = i; |
| 344 |
|
| } |
| 345 |
|
| |
| 346 |
|
| /* Main evaluation function */ |
| 347 |
18791 |
| static double envelope_at(pTHX_ SegmentedEnvelope *env, double t) { |
| 348 |
|
| double pd, d; |
| 349 |
|
| int i; |
| 350 |
|
| double val, curve_abs, ratio; |
| 351 |
|
| |
| 352 |
18791 |
▶ | if (env->segments <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->segments <= 0) |
0 |
18791 |
18791 |
|
| 353 |
0 |
▶ | return (env->levels) ? env->levels[0] : 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| return (env->levels) ? env->levels[0] : 0.0; |
0 |
0 |
0 |
|
| 354 |
|
| |
| 355 |
18791 |
▶ | if (isnan(t)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (isnan(t)) |
2 |
18789 |
18791 |
|
| 356 |
2 |
| return t; |
| 357 |
18789 |
▶ | if (isinf(t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isinf(t)) { |
3 |
18786 |
18789 |
|
| 358 |
3 |
▶ | if (env->is_hold) |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->is_hold) |
2 |
1 |
3 |
|
| 359 |
2 |
▶ | return (t > 0) ? env->levels[env->segments] : env->levels[0]; |
Branch: 1 branch
| Branch | True | False | Total |
| return (t > 0) ? env->levels[env->segments] : env->levels[0]; |
1 |
1 |
2 |
|
| 360 |
1 |
| return t - t; |
| 361 |
|
| } |
| 362 |
|
| |
| 363 |
18786 |
| t = wrap_pos(env, t); |
| 364 |
18786 |
| pd = env->passed_duration; |
| 365 |
18786 |
| i = env->current_segment; |
| 366 |
|
| |
| 367 |
|
| /* backward search */ |
| 368 |
18849 |
▶ | while (t < pd && i > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (t < pd && i > 0) { |
63 |
18786 |
18849 |
| while (t < pd && i > 0) { |
63 |
0 |
63 |
|
| 369 |
63 |
| pd -= env->durations[--i]; |
| 370 |
|
| } |
| 371 |
|
| |
| 372 |
|
| /* remove duration of passed segments */ |
| 373 |
18786 |
▶ | if (i == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == 0) |
8827 |
9959 |
18786 |
|
| 374 |
8827 |
| pd = 0; |
| 375 |
|
| else |
| 376 |
9959 |
| t -= pd; |
| 377 |
|
| |
| 378 |
|
| /* forward search - determine segment */ |
| 379 |
19149 |
▶ | while (i < env->segments) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < env->segments) { |
19149 |
0 |
19149 |
|
| 380 |
19149 |
| d = env->durations[i]; |
| 381 |
19149 |
▶ | if (t > d && i != env->segments - 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t > d && i != env->segments - 1) { |
368 |
18781 |
19149 |
| if (t > d && i != env->segments - 1) { |
363 |
5 |
368 |
|
| 382 |
363 |
| t -= d; |
| 383 |
363 |
| pd += d; |
| 384 |
363 |
| i++; |
| 385 |
|
| } else { |
| 386 |
18786 |
▶ | if (t > d) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t > d) |
5 |
18781 |
18786 |
|
| 387 |
5 |
| t = d; |
| 388 |
18786 |
▶ | if (i != env->past_segment) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i != env->past_segment) |
287 |
18499 |
18786 |
|
| 389 |
287 |
| update_current_segment(env, i); |
| 390 |
18786 |
| break; |
| 391 |
|
| } |
| 392 |
|
| } |
| 393 |
|
| |
| 394 |
|
| /* cache state for next call */ |
| 395 |
18786 |
▶ | if (pd != env->passed_duration) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pd != env->passed_duration) |
147 |
18639 |
18786 |
|
| 396 |
147 |
| env->passed_duration = pd; |
| 397 |
18786 |
▶ | if (i != env->current_segment) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i != env->current_segment) |
0 |
18786 |
18786 |
|
| 398 |
0 |
| env->current_segment = i; |
| 399 |
|
| |
| 400 |
|
| /* calculate result */ |
| 401 |
18786 |
| d = env->durations[i]; |
| 402 |
18786 |
▶ | if (d <= 0.0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (d <= 0.0) |
1 |
18785 |
18786 |
|
| 403 |
1 |
| return env->levels[i]; |
| 404 |
18785 |
▶ | ratio = (env->is_neg ? (d - t) : t) / d; |
Branch: 1 branch
| Branch | True | False | Total |
| ratio = (env->is_neg ? (d - t) : t) / d; |
2741 |
16044 |
18785 |
|
| 405 |
18785 |
| val = wrap_value(aTHX_ env, fabs(ratio)); |
| 406 |
18784 |
| curve_abs = fabs(env->curves[i]); |
| 407 |
18784 |
▶ | if (curve_abs != 1.0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (curve_abs != 1.0) |
3949 |
14835 |
18784 |
|
| 408 |
3949 |
▶ | val = (val < 0.0) ? -pow(-val, curve_abs) : pow(val, curve_abs); |
Branch: 1 branch
| Branch | True | False | Total |
| val = (val < 0.0) ? -pow(-val, curve_abs) : pow(val, curve_abs); |
1 |
3948 |
3949 |
|
| 409 |
18784 |
| val = val * env->is_asc + env->is_neg; |
| 410 |
18784 |
| val = fabs(val) * env->level_diff + env->levels[i]; |
| 411 |
|
| |
| 412 |
18784 |
| return val; |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| /* Normalize array sum to 1.0 */ |
| 416 |
6 |
| static void normalize_sum(double *arr, int len) { |
| 417 |
6 |
| double sum = 0; |
| 418 |
|
| int i; |
| 419 |
28 |
▶ | for (i = 0; i < len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) |
22 |
6 |
28 |
|
| 420 |
22 |
| sum += arr[i]; |
| 421 |
6 |
▶ | if (sum > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sum > 0) { |
6 |
0 |
6 |
|
| 422 |
28 |
▶ | for (i = 0; i < len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) |
22 |
6 |
28 |
|
| 423 |
22 |
| arr[i] /= sum; |
| 424 |
|
| } |
| 425 |
6 |
| } |
| 426 |
|
| |
| 427 |
|
| /* Calculate total duration */ |
| 428 |
195 |
| static double calc_total_duration(double *durations, int segments) { |
| 429 |
195 |
| double total = 0; |
| 430 |
|
| int i; |
| 431 |
963 |
▶ | for (i = 0; i < segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < segments; i++) |
768 |
195 |
963 |
|
| 432 |
768 |
| total += durations[i]; |
| 433 |
195 |
| return total; |
| 434 |
|
| } |
| 435 |
|
| |
| 436 |
|
| /* Allocate a new zeroed envelope */ |
| 437 |
27 |
| static SegmentedEnvelope *new_envelope(void) { |
| 438 |
|
| SegmentedEnvelope *env; |
| 439 |
27 |
| Newxz(env, 1, SegmentedEnvelope); |
| 440 |
27 |
| env->past_segment = -1; |
| 441 |
27 |
| return env; |
| 442 |
|
| } |
| 443 |
|
| |
| 444 |
|
| /* Bless an envelope pointer into a Perl object */ |
| 445 |
27 |
| static SV *bless_envelope(pTHX_ SegmentedEnvelope *env) { |
| 446 |
27 |
| SV *sv = newSViv(PTR2IV(env)); |
| 447 |
27 |
| sv = newRV_noinc(sv); |
| 448 |
27 |
| sv_bless(sv, gv_stashpv("Math::SegmentedEnvelope", GV_ADD)); |
| 449 |
27 |
| return sv; |
| 450 |
|
| } |
| 451 |
|
| |
| 452 |
|
| /* Free envelope memory */ |
| 453 |
242 |
| static void free_envelope(pTHX_ SegmentedEnvelope *env) { |
| 454 |
242 |
▶ | if (env) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (env) { |
239 |
3 |
242 |
|
| 455 |
239 |
▶ | if (env->levels) Safefree(env->levels); |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->levels) Safefree(env->levels); |
239 |
0 |
239 |
|
| 456 |
239 |
▶ | if (env->durations) Safefree(env->durations); |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->durations) Safefree(env->durations); |
239 |
0 |
239 |
|
| 457 |
239 |
▶ | if (env->curves) Safefree(env->curves); |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->curves) Safefree(env->curves); |
239 |
0 |
239 |
|
| 458 |
239 |
▶ | if (env->morpher_cv) SvREFCNT_dec(env->morpher_cv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_cv) SvREFCNT_dec(env->morpher_cv); |
2 |
237 |
239 |
|
| 459 |
239 |
| te_free(env->morpher_expr); |
| 460 |
239 |
| jit_morpher_free(env->morpher_jit); |
| 461 |
239 |
▶ | if (env->morpher_formula) Safefree(env->morpher_formula); |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_formula) Safefree(env->morpher_formula); |
5 |
234 |
239 |
|
| 462 |
239 |
| Safefree(env); |
| 463 |
|
| } |
| 464 |
242 |
| } |
| 465 |
|
| |
| 466 |
|
| /* Safe array fetching helpers */ |
| 467 |
2019 |
| static double av_fetch_nv(AV *av, int idx, double default_val) { |
| 468 |
2019 |
| SV **svp = av_fetch(av, idx, 0); |
| 469 |
2019 |
▶ | return (svp && *svp && SvOK(*svp)) ? SvNV(*svp) : default_val; |
Branch: 3 branches
| Branch | True | False | Total |
| return (svp && *svp && SvOK(*svp)) ? SvNV(*svp) : default_val; |
2014 |
5 |
2019 |
| return (svp && *svp && SvOK(*svp)) ? SvNV(*svp) : default_val; |
2014 |
0 |
2014 |
| return (svp && *svp && SvOK(*svp)) ? SvNV(*svp) : default_val; |
2014 |
0 |
2014 |
|
| 470 |
|
| } |
| 471 |
|
| |
| 472 |
534 |
| static AV *av_fetch_av(AV *av, int idx) { |
| 473 |
534 |
| SV **svp = av_fetch(av, idx, 0); |
| 474 |
534 |
▶ | if (!svp || !*svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!svp || !*svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV) |
534 |
0 |
534 |
| if (!svp || !*svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV) |
534 |
0 |
534 |
| if (!svp || !*svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV) |
531 |
3 |
534 |
| if (!svp || !*svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVAV) |
0 |
531 |
531 |
|
| 475 |
3 |
| return NULL; |
| 476 |
531 |
| return (AV*)SvRV(*svp); |
| 477 |
|
| } |
| 478 |
|
| |
| 479 |
|
| /* Safe object unwrapping */ |
| 480 |
1215 |
| static SegmentedEnvelope *get_envelope(pTHX_ SV *sv, const char *name) { |
| 481 |
|
| SegmentedEnvelope *env; |
| 482 |
1215 |
▶ | if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope")) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope")) |
1215 |
0 |
1215 |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope")) |
1211 |
4 |
1215 |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope")) |
1211 |
0 |
1211 |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope")) |
0 |
1211 |
1211 |
|
| 483 |
4 |
| croak("%s: not a Math::SegmentedEnvelope object", name); |
| 484 |
1211 |
| env = INT2PTR(SegmentedEnvelope*, SvIV(SvRV(sv))); |
| 485 |
1211 |
▶ | if (!env) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!env) |
1 |
1210 |
1211 |
|
| 486 |
1 |
| croak("%s: uninitialized or freed envelope object", name); |
| 487 |
1210 |
| return env; |
| 488 |
|
| } |
| 489 |
|
| |
| 490 |
585 |
| static SegmentedEnvelope *get_static_envelope(pTHX_ SV *sv, const char *name) { |
| 491 |
|
| SegmentedEnvelope *env; |
| 492 |
585 |
▶ | if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope::Static")) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope::Static")) |
585 |
0 |
585 |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope::Static")) |
585 |
0 |
585 |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope::Static")) |
585 |
0 |
585 |
| if (!sv || !SvROK(sv) || !sv_isobject(sv) || !sv_derived_from(sv, "Math::SegmentedEnvelope::Static")) |
0 |
585 |
585 |
|
| 493 |
0 |
| croak("%s: not a Math::SegmentedEnvelope::Static object", name); |
| 494 |
585 |
| env = INT2PTR(SegmentedEnvelope*, SvIV(SvRV(sv))); |
| 495 |
585 |
▶ | if (!env) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!env) |
0 |
585 |
585 |
|
| 496 |
0 |
| croak("%s: uninitialized or freed static envelope object", name); |
| 497 |
585 |
| return env; |
| 498 |
|
| } |
| 499 |
|
| |
| 500 |
|
| /* Compile a morpher formula string, binding to the given t variable */ |
| 501 |
0 |
| static te_expr *compile_morpher_formula(const char *formula, double *t_var) { |
| 502 |
|
| int err; |
| 503 |
0 |
| te_variable vars[] = {{"t", t_var, TE_VARIABLE, NULL}}; |
| 504 |
0 |
| te_expr *expr = te_compile(formula, vars, 1, &err); |
| 505 |
0 |
▶ | if (!expr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!expr) |
0 |
0 |
0 |
|
| 506 |
0 |
| croak("morpher_formula: parse error at position %d in '%s'", err, formula); |
| 507 |
0 |
| return expr; |
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
|
| /* Validate a morpher formula string (croaks on error, safe to call before allocation) */ |
| 511 |
37 |
| static void validate_morpher_formula(const char *formula) { |
| 512 |
37 |
| double dummy = 0; |
| 513 |
|
| int err; |
| 514 |
37 |
| te_variable vars[] = {{"t", &dummy, TE_VARIABLE, NULL}}; |
| 515 |
|
| te_expr *expr; |
| 516 |
|
| |
| 517 |
37 |
▶ | if (find_predefined_morpher(formula)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (find_predefined_morpher(formula)) |
27 |
10 |
37 |
|
| 518 |
27 |
| return; |
| 519 |
|
| |
| 520 |
10 |
| expr = te_compile(formula, vars, 1, &err); |
| 521 |
10 |
▶ | if (!expr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!expr) |
5 |
5 |
10 |
|
| 522 |
5 |
| croak("morpher_formula: parse error at position %d in '%s'", err, formula); |
| 523 |
5 |
| te_free(expr); |
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
|
| /* Set morpher formula on an envelope (shared logic for new/spline/accessor). |
| 527 |
|
| * Caller must validate formula first if allocation has already occurred. */ |
| 528 |
32 |
| static void set_morpher_formula(SegmentedEnvelope *env, const char *formula, STRLEN flen) { |
| 529 |
|
| morpher_fn pfn; |
| 530 |
|
| jit_morpher *jit; |
| 531 |
|
| |
| 532 |
32 |
| te_free(env->morpher_expr); env->morpher_expr = NULL; |
| 533 |
32 |
| jit_morpher_free(env->morpher_jit); env->morpher_jit = NULL; |
| 534 |
32 |
▶ | if (env->morpher_formula) { Safefree(env->morpher_formula); env->morpher_formula = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_formula) { Safefree(env->morpher_formula); env->morpher_formula = NULL; } |
0 |
32 |
32 |
|
| 535 |
32 |
| env->morpher_fn = NULL; |
| 536 |
32 |
| env->is_morph = 1; |
| 537 |
|
| |
| 538 |
32 |
| pfn = find_predefined_morpher(formula); |
| 539 |
32 |
▶ | if (pfn) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pfn) { |
27 |
5 |
32 |
|
| 540 |
27 |
| env->morpher_fn = pfn; |
| 541 |
27 |
| return; |
| 542 |
|
| } |
| 543 |
|
| |
| 544 |
5 |
| Newx(env->morpher_formula, flen + 1, char); |
| 545 |
5 |
| Copy(formula, env->morpher_formula, flen + 1, char); |
| 546 |
5 |
| jit = jit_morpher_compile(formula); |
| 547 |
5 |
▶ | if (jit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (jit) { |
5 |
0 |
5 |
|
| 548 |
5 |
| env->morpher_fn = jit->fn; |
| 549 |
5 |
| env->morpher_jit = jit; |
| 550 |
|
| } else { |
| 551 |
|
| /* compile_morpher_formula won't croak here because we pre-validated */ |
| 552 |
0 |
| env->morpher_expr = compile_morpher_formula(env->morpher_formula, &env->morpher_expr_t); |
| 553 |
|
| } |
| 554 |
|
| } |
| 555 |
|
| |
| 556 |
|
| /* Generate random envelope using Perl's RNG for srand() compatibility */ |
| 557 |
4 |
| static void generate_random_envelope(pTHX_ SegmentedEnvelope *env) { |
| 558 |
4 |
| int size = (int)(Drand01() * 5) + 3; |
| 559 |
|
| int i; |
| 560 |
|
| |
| 561 |
4 |
| env->segments = size; |
| 562 |
|
| |
| 563 |
4 |
| Newx(env->levels, size + 1, double); |
| 564 |
4 |
| Newx(env->durations, size, double); |
| 565 |
4 |
| Newx(env->curves, size, double); |
| 566 |
|
| |
| 567 |
4 |
| env->levels[0] = env->border_start; |
| 568 |
17 |
▶ | for (i = 1; i < size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < size; i++) |
13 |
4 |
17 |
|
| 569 |
13 |
| env->levels[i] = Drand01(); |
| 570 |
4 |
| env->levels[size] = env->border_end; |
| 571 |
|
| |
| 572 |
21 |
▶ | for (i = 0; i < size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) |
17 |
4 |
21 |
|
| 573 |
17 |
| env->durations[i] = Drand01() + 0.2; |
| 574 |
4 |
| normalize_sum(env->durations, size); |
| 575 |
|
| |
| 576 |
21 |
▶ | for (i = 0; i < size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) { |
17 |
4 |
21 |
|
| 577 |
17 |
| double c = Drand01() * 2 + 1; |
| 578 |
17 |
▶ | if (Drand01() > 0.5) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Drand01() > 0.5) |
10 |
7 |
17 |
|
| 579 |
10 |
| c = -c; |
| 580 |
17 |
| env->curves[i] = c; |
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
4 |
| env->total_duration = calc_total_duration(env->durations, env->segments); |
| 584 |
4 |
| } |
| 585 |
|
| |
| 586 |
|
| /* Copy envelope boundary/wrapping flags */ |
| 587 |
21 |
| static void copy_flags(SegmentedEnvelope *dst, const SegmentedEnvelope *src) { |
| 588 |
21 |
| dst->is_hold = src->is_hold; |
| 589 |
21 |
| dst->is_fold_over = src->is_fold_over; |
| 590 |
21 |
| dst->is_wrap_neg = src->is_wrap_neg; |
| 591 |
21 |
| } |
| 592 |
|
| |
| 593 |
|
| /* Initialize a SegmentedEnvelope as a deep copy of another (for static/table) */ |
| 594 |
31 |
| static void copy_envelope(pTHX_ SegmentedEnvelope *dst, SegmentedEnvelope *src) { |
| 595 |
|
| int i; |
| 596 |
|
| STRLEN flen; |
| 597 |
|
| |
| 598 |
31 |
| Newx(dst->levels, src->segments + 1, double); |
| 599 |
31 |
| Newx(dst->durations, src->segments, double); |
| 600 |
31 |
| Newx(dst->curves, src->segments, double); |
| 601 |
|
| |
| 602 |
125 |
▶ | for (i = 0; i <= src->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= src->segments; i++) |
94 |
31 |
125 |
|
| 603 |
94 |
| dst->levels[i] = src->levels[i]; |
| 604 |
94 |
▶ | for (i = 0; i < src->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < src->segments; i++) { |
63 |
31 |
94 |
|
| 605 |
63 |
| dst->durations[i] = src->durations[i]; |
| 606 |
63 |
| dst->curves[i] = src->curves[i]; |
| 607 |
|
| } |
| 608 |
|
| |
| 609 |
31 |
| dst->segments = src->segments; |
| 610 |
31 |
| dst->total_duration = src->total_duration; |
| 611 |
31 |
| dst->is_morph = src->is_morph; |
| 612 |
31 |
| dst->is_hold = src->is_hold; |
| 613 |
31 |
| dst->is_fold_over = src->is_fold_over; |
| 614 |
31 |
| dst->is_wrap_neg = src->is_wrap_neg; |
| 615 |
31 |
| dst->border_start = 0; |
| 616 |
31 |
| dst->border_end = 0; |
| 617 |
31 |
| dst->current_segment = 0; |
| 618 |
31 |
| dst->past_segment = -1; |
| 619 |
31 |
| dst->passed_duration = 0; |
| 620 |
31 |
| dst->level_diff = 0; |
| 621 |
31 |
| dst->is_neg = 0; |
| 622 |
31 |
| dst->is_asc = 0; |
| 623 |
|
| |
| 624 |
31 |
▶ | if (src->morpher_cv && SvOK(src->morpher_cv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (src->morpher_cv && SvOK(src->morpher_cv)) |
0 |
31 |
31 |
| if (src->morpher_cv && SvOK(src->morpher_cv)) |
0 |
0 |
0 |
|
| 625 |
0 |
| dst->morpher_cv = newSVsv(src->morpher_cv); |
| 626 |
|
| else |
| 627 |
31 |
| dst->morpher_cv = NULL; |
| 628 |
|
| |
| 629 |
31 |
| dst->morpher_expr_t = 0; |
| 630 |
31 |
| dst->morpher_fn = src->morpher_fn; |
| 631 |
31 |
▶ | if (src->morpher_formula) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->morpher_formula) { |
1 |
30 |
31 |
|
| 632 |
1 |
| flen = strlen(src->morpher_formula); |
| 633 |
1 |
| Newx(dst->morpher_formula, flen + 1, char); |
| 634 |
1 |
| Copy(src->morpher_formula, dst->morpher_formula, flen + 1, char); |
| 635 |
|
| /* Try JIT, fall back to tinyexpr */ |
| 636 |
|
| { |
| 637 |
1 |
| jit_morpher *jit = jit_morpher_compile(src->morpher_formula); |
| 638 |
1 |
▶ | if (jit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (jit) { |
1 |
0 |
1 |
|
| 639 |
1 |
| dst->morpher_fn = jit->fn; |
| 640 |
1 |
| dst->morpher_jit = jit; |
| 641 |
1 |
| dst->morpher_expr = NULL; |
| 642 |
|
| } else { |
| 643 |
0 |
| dst->morpher_fn = NULL; |
| 644 |
0 |
| dst->morpher_jit = NULL; |
| 645 |
0 |
| dst->morpher_expr = compile_morpher_formula(dst->morpher_formula, &dst->morpher_expr_t); |
| 646 |
|
| } |
| 647 |
|
| } |
| 648 |
|
| } else { |
| 649 |
30 |
| dst->morpher_jit = NULL; |
| 650 |
30 |
| dst->morpher_formula = NULL; |
| 651 |
30 |
| dst->morpher_expr = NULL; |
| 652 |
|
| } |
| 653 |
31 |
| } |
| 654 |
|
| |
| 655 |
|
| /* Initialize a stack-local SegmentedEnvelope borrowing arrays from parent (for table) */ |
| 656 |
29 |
| static void borrow_envelope(pTHX_ SegmentedEnvelope *dst, SegmentedEnvelope *src) { |
| 657 |
29 |
| dst->levels = src->levels; |
| 658 |
29 |
| dst->durations = src->durations; |
| 659 |
29 |
| dst->curves = src->curves; |
| 660 |
29 |
| dst->segments = src->segments; |
| 661 |
29 |
| dst->total_duration = src->total_duration; |
| 662 |
29 |
| dst->is_morph = src->is_morph; |
| 663 |
29 |
| dst->is_hold = src->is_hold; |
| 664 |
29 |
| dst->is_fold_over = src->is_fold_over; |
| 665 |
29 |
| dst->is_wrap_neg = src->is_wrap_neg; |
| 666 |
29 |
| dst->border_start = 0; |
| 667 |
29 |
| dst->border_end = 0; |
| 668 |
|
| |
| 669 |
29 |
| dst->morpher_cv = src->morpher_cv; |
| 670 |
29 |
| dst->morpher_fn = src->morpher_fn; |
| 671 |
29 |
| dst->morpher_jit = NULL; |
| 672 |
29 |
| dst->morpher_formula = NULL; |
| 673 |
29 |
| dst->morpher_expr_t = 0; |
| 674 |
29 |
▶ | if (src->morpher_formula && !src->morpher_jit) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (src->morpher_formula && !src->morpher_jit) { |
0 |
29 |
29 |
| if (src->morpher_formula && !src->morpher_jit) { |
0 |
0 |
0 |
|
| 675 |
0 |
| dst->morpher_expr = compile_morpher_formula(src->morpher_formula, &dst->morpher_expr_t); |
| 676 |
|
| } else { |
| 677 |
29 |
| dst->morpher_expr = NULL; |
| 678 |
|
| } |
| 679 |
|
| |
| 680 |
29 |
| dst->current_segment = 0; |
| 681 |
29 |
| dst->past_segment = -1; |
| 682 |
29 |
| dst->passed_duration = 0; |
| 683 |
29 |
| dst->level_diff = 0; |
| 684 |
29 |
| dst->is_neg = 0; |
| 685 |
29 |
| dst->is_asc = 0; |
| 686 |
29 |
| } |
| 687 |
|
| |
| 688 |
|
| MODULE = Math::SegmentedEnvelope PACKAGE = Math::SegmentedEnvelope |
| 689 |
|
| |
| 690 |
|
| PROTOTYPES: DISABLE |
| 691 |
|
| |
| 692 |
|
| SV * |
| 693 |
|
| new(class, ...) |
| 694 |
|
| SV *class |
| 695 |
|
| PREINIT: |
| 696 |
|
| SegmentedEnvelope *env; |
| 697 |
183 |
| SV *def_sv = NULL; |
| 698 |
|
| AV *def_av; |
| 699 |
183 |
| AV *levels_av = NULL, *durs_av = NULL, *curves_av = NULL; |
| 700 |
183 |
| int i, num_levels = 0, num_durs = 0, num_curves = 0; |
| 701 |
183 |
| int arg_start = 1; |
| 702 |
|
| HV *stash; |
| 703 |
|
| SV *self; |
| 704 |
|
| const char *classname; |
| 705 |
|
| CODE: |
| 706 |
|
| /* Parse arguments */ |
| 707 |
183 |
▶ | if (items == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 2) { |
70 |
113 |
183 |
|
| 708 |
70 |
| def_sv = ST(1); |
| 709 |
70 |
| arg_start = 2; |
| 710 |
113 |
▶ | } else if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (items > 2) { |
109 |
4 |
113 |
|
| 711 |
109 |
▶ | if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) { |
109 |
0 |
109 |
| if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) { |
109 |
0 |
109 |
|
| 712 |
109 |
| def_sv = ST(1); |
| 713 |
109 |
| arg_start = 2; |
| 714 |
0 |
▶ | } else if (SvPOK(ST(1))) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvPOK(ST(1))) { |
0 |
0 |
0 |
|
| 715 |
|
| STRLEN len; |
| 716 |
0 |
| const char *key = SvPV(ST(1), len); |
| 717 |
0 |
▶ | if (strEQ(key, "def") && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (strEQ(key, "def") && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) { |
0 |
0 |
0 |
| if (strEQ(key, "def") && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) { |
0 |
0 |
0 |
| if (strEQ(key, "def") && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 718 |
0 |
| def_sv = ST(2); |
| 719 |
0 |
| arg_start = 3; |
| 720 |
|
| } |
| 721 |
|
| } |
| 722 |
|
| } |
| 723 |
|
| |
| 724 |
|
| /* Check def in options if not found yet */ |
| 725 |
478 |
▶ | for (i = arg_start; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = arg_start; i < items; i += 2) { |
295 |
183 |
478 |
|
| 726 |
295 |
▶ | if (i + 1 < items && SvPOK(ST(i))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i))) { |
295 |
0 |
295 |
| if (i + 1 < items && SvPOK(ST(i))) { |
295 |
0 |
295 |
|
| 727 |
|
| STRLEN len; |
| 728 |
295 |
| const char *key = SvPV(ST(i), len); |
| 729 |
295 |
▶ | if (strEQ(key, "def")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key, "def")) { |
0 |
295 |
295 |
|
| 730 |
0 |
| def_sv = ST(i + 1); |
| 731 |
|
| } |
| 732 |
|
| } |
| 733 |
|
| } |
| 734 |
|
| |
| 735 |
|
| /* Validate def_sv before allocating env */ |
| 736 |
183 |
▶ | if (def_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (def_sv) { |
179 |
4 |
183 |
|
| 737 |
179 |
▶ | if (!SvROK(def_sv) || SvTYPE(SvRV(def_sv)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(def_sv) || SvTYPE(SvRV(def_sv)) != SVt_PVAV) |
178 |
1 |
179 |
| if (!SvROK(def_sv) || SvTYPE(SvRV(def_sv)) != SVt_PVAV) |
0 |
178 |
178 |
|
| 738 |
1 |
| croak("def must be an array reference"); |
| 739 |
178 |
| def_av = (AV*)SvRV(def_sv); |
| 740 |
178 |
▶ | if (av_len(def_av) != 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(def_av) != 2) |
0 |
178 |
178 |
|
| 741 |
0 |
| croak("def must have exactly 3 elements"); |
| 742 |
|
| |
| 743 |
178 |
| levels_av = av_fetch_av(def_av, 0); |
| 744 |
178 |
| durs_av = av_fetch_av(def_av, 1); |
| 745 |
178 |
| curves_av = av_fetch_av(def_av, 2); |
| 746 |
|
| |
| 747 |
178 |
▶ | if (!levels_av || !durs_av || !curves_av) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!levels_av || !durs_av || !curves_av) |
177 |
1 |
178 |
| if (!levels_av || !durs_av || !curves_av) |
177 |
0 |
177 |
| if (!levels_av || !durs_av || !curves_av) |
0 |
177 |
177 |
|
| 748 |
1 |
| croak("def elements must be array references"); |
| 749 |
|
| |
| 750 |
177 |
| num_levels = av_len(levels_av) + 1; |
| 751 |
177 |
| num_durs = av_len(durs_av) + 1; |
| 752 |
177 |
| num_curves = av_len(curves_av) + 1; |
| 753 |
|
| |
| 754 |
177 |
▶ | if (num_durs < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_durs < 1) |
1 |
176 |
177 |
|
| 755 |
1 |
| croak("envelope must have at least 1 segment"); |
| 756 |
|
| |
| 757 |
176 |
▶ | if (num_levels != num_durs + 1 || num_levels != num_curves + 1 || num_durs != num_curves) |
Branch: 3 branches
| Branch | True | False | Total |
| if (num_levels != num_durs + 1 || num_levels != num_curves + 1 || num_durs != num_curves) |
176 |
0 |
176 |
| if (num_levels != num_durs + 1 || num_levels != num_curves + 1 || num_durs != num_curves) |
176 |
0 |
176 |
| if (num_levels != num_durs + 1 || num_levels != num_curves + 1 || num_durs != num_curves) |
0 |
176 |
176 |
|
| 758 |
0 |
| croak("size mismatch in envelope definition"); |
| 759 |
|
| } |
| 760 |
|
| |
| 761 |
|
| /* Pre-validate morpher_formula before allocating (croak-safe) */ |
| 762 |
472 |
▶ | for (i = arg_start; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = arg_start; i < items; i += 2) { |
295 |
177 |
472 |
|
| 763 |
590 |
▶ | if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
295 |
0 |
295 |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
295 |
0 |
295 |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
32 |
263 |
295 |
|
| 764 |
32 |
▶ | if (SvOK(ST(i + 1))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(ST(i + 1))) |
32 |
0 |
32 |
|
| 765 |
64 |
| validate_morpher_formula(SvPV_nolen(ST(i + 1))); |
| 766 |
|
| } |
| 767 |
|
| } |
| 768 |
|
| |
| 769 |
|
| /* Allocate and zero-initialize */ |
| 770 |
177 |
| Newxz(env, 1, SegmentedEnvelope); |
| 771 |
177 |
| env->border_start = Drand01(); |
| 772 |
177 |
| env->border_end = Drand01(); |
| 773 |
177 |
| env->past_segment = -1; |
| 774 |
|
| |
| 775 |
|
| /* Parse remaining key-value pairs */ |
| 776 |
469 |
▶ | for (i = arg_start; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = arg_start; i < items; i += 2) { |
292 |
177 |
469 |
|
| 777 |
292 |
▶ | if (i + 1 < items && SvPOK(ST(i))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i))) { |
292 |
0 |
292 |
| if (i + 1 < items && SvPOK(ST(i))) { |
292 |
0 |
292 |
|
| 778 |
|
| STRLEN len; |
| 779 |
292 |
| const char *key = SvPV(ST(i), len); |
| 780 |
292 |
| SV *val = ST(i + 1); |
| 781 |
|
| |
| 782 |
292 |
▶ | if (strEQ(key, "is_morph")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key, "is_morph")) { |
70 |
222 |
292 |
|
| 783 |
70 |
| env->is_morph = SvTRUE(val) ? 1 : 0; |
| 784 |
222 |
▶ | } else if (strEQ(key, "is_hold")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "is_hold")) { |
60 |
162 |
222 |
|
| 785 |
60 |
| env->is_hold = SvTRUE(val) ? 1 : 0; |
| 786 |
162 |
▶ | } else if (strEQ(key, "is_fold_over")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "is_fold_over")) { |
62 |
100 |
162 |
|
| 787 |
62 |
| env->is_fold_over = SvTRUE(val) ? 1 : 0; |
| 788 |
100 |
▶ | } else if (strEQ(key, "is_wrap_neg")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "is_wrap_neg")) { |
59 |
41 |
100 |
|
| 789 |
59 |
| env->is_wrap_neg = SvTRUE(val) ? 1 : 0; |
| 790 |
41 |
▶ | } else if (strEQ(key, "morpher")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "morpher")) { |
2 |
39 |
41 |
|
| 791 |
2 |
▶ | if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { |
2 |
0 |
2 |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { |
2 |
0 |
2 |
|
| 792 |
2 |
| env->morpher_cv = newSVsv(val); |
| 793 |
|
| } |
| 794 |
39 |
▶ | } else if (strEQ(key, "morpher_formula")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "morpher_formula")) { |
29 |
10 |
39 |
|
| 795 |
29 |
▶ | if (SvOK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(val)) { |
29 |
0 |
29 |
|
| 796 |
|
| STRLEN flen; |
| 797 |
29 |
| const char *formula = SvPV(val, flen); |
| 798 |
29 |
| set_morpher_formula(env, formula, flen); |
| 799 |
|
| } |
| 800 |
10 |
▶ | } else if (strEQ(key, "border_level")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "border_level")) { |
1 |
9 |
10 |
|
| 801 |
1 |
▶ | if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
0 |
1 |
1 |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 802 |
0 |
| AV *bl = (AV*)SvRV(val); |
| 803 |
0 |
▶ | if (av_len(bl) >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(bl) >= 0) |
0 |
0 |
0 |
|
| 804 |
0 |
| env->border_start = av_fetch_nv(bl, 0, 0.0); |
| 805 |
0 |
▶ | if (av_len(bl) >= 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(bl) >= 1) |
0 |
0 |
0 |
|
| 806 |
0 |
| env->border_end = av_fetch_nv(bl, 1, env->border_start); |
| 807 |
|
| else |
| 808 |
0 |
| env->border_end = env->border_start; |
| 809 |
1 |
▶ | } else if (SvOK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvOK(val)) { |
1 |
0 |
1 |
|
| 810 |
1 |
| env->border_start = SvNV(val); |
| 811 |
1 |
| env->border_end = env->border_start; |
| 812 |
|
| } |
| 813 |
|
| } |
| 814 |
|
| } |
| 815 |
|
| } |
| 816 |
|
| |
| 817 |
|
| /* Initialize envelope data */ |
| 818 |
177 |
▶ | if (def_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (def_sv) { |
173 |
4 |
177 |
|
| 819 |
173 |
| env->segments = num_durs; |
| 820 |
173 |
| Newx(env->levels, num_levels, double); |
| 821 |
173 |
| Newx(env->durations, num_durs, double); |
| 822 |
173 |
| Newx(env->curves, num_curves, double); |
| 823 |
|
| |
| 824 |
942 |
▶ | for (i = 0; i < num_levels; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_levels; i++) |
769 |
173 |
942 |
|
| 825 |
769 |
| env->levels[i] = av_fetch_nv(levels_av, i, 0.0); |
| 826 |
769 |
▶ | for (i = 0; i < num_durs; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_durs; i++) |
596 |
173 |
769 |
|
| 827 |
596 |
| env->durations[i] = av_fetch_nv(durs_av, i, 0.0); |
| 828 |
769 |
▶ | for (i = 0; i < num_curves; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_curves; i++) |
596 |
173 |
769 |
|
| 829 |
596 |
| env->curves[i] = av_fetch_nv(curves_av, i, 1.0); |
| 830 |
|
| |
| 831 |
173 |
| env->total_duration = calc_total_duration(env->durations, env->segments); |
| 832 |
|
| } else { |
| 833 |
|
| /* Generate random envelope */ |
| 834 |
4 |
| generate_random_envelope(aTHX_ env); |
| 835 |
|
| } |
| 836 |
|
| |
| 837 |
|
| /* Create blessed reference */ |
| 838 |
177 |
▶ | classname = (sv_isobject(class)) ? HvNAME(SvSTASH(SvRV(class))) |
Branch: 4 branches
| Branch | True | False | Total |
| classname = (sv_isobject(class)) ? HvNAME(SvSTASH(SvRV(class))) |
0 |
0 |
0 |
| classname = (sv_isobject(class)) ? HvNAME(SvSTASH(SvRV(class))) |
0 |
0 |
0 |
| classname = (sv_isobject(class)) ? HvNAME(SvSTASH(SvRV(class))) |
0 |
0 |
0 |
| classname = (sv_isobject(class)) ? HvNAME(SvSTASH(SvRV(class))) |
0 |
0 |
0 |
|
| 839 |
354 |
▶ | : (SvPOK(class) ? SvPV_nolen(class) : "Math::SegmentedEnvelope"); |
Branch: 4 branches
| Branch | True | False | Total |
| : (SvPOK(class) ? SvPV_nolen(class) : "Math::SegmentedEnvelope"); |
0 |
177 |
177 |
| : (SvPOK(class) ? SvPV_nolen(class) : "Math::SegmentedEnvelope"); |
0 |
0 |
0 |
| : (SvPOK(class) ? SvPV_nolen(class) : "Math::SegmentedEnvelope"); |
0 |
0 |
0 |
| : (SvPOK(class) ? SvPV_nolen(class) : "Math::SegmentedEnvelope"); |
177 |
0 |
177 |
|
| 840 |
177 |
| self = newSViv(PTR2IV(env)); |
| 841 |
177 |
| self = newRV_noinc(self); |
| 842 |
177 |
| stash = gv_stashpv(classname, GV_ADD); |
| 843 |
177 |
| sv_bless(self, stash); |
| 844 |
|
| |
| 845 |
177 |
| RETVAL = self; |
| 846 |
|
| OUTPUT: |
| 847 |
|
| RETVAL |
| 848 |
|
| |
| 849 |
|
| void |
| 850 |
|
| DESTROY(self) |
| 851 |
|
| SV *self |
| 852 |
|
| PREINIT: |
| 853 |
|
| SegmentedEnvelope *env; |
| 854 |
|
| CODE: |
| 855 |
213 |
▶ | if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope")) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope")) { |
213 |
0 |
213 |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope")) { |
213 |
0 |
213 |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope")) { |
213 |
0 |
213 |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope")) { |
213 |
0 |
213 |
|
| 856 |
213 |
| env = INT2PTR(SegmentedEnvelope*, SvIV(SvRV(self))); |
| 857 |
213 |
| free_envelope(aTHX_ env); |
| 858 |
|
| } |
| 859 |
|
| |
| 860 |
|
| double |
| 861 |
|
| at(self, t) |
| 862 |
|
| SV *self |
| 863 |
|
| double t |
| 864 |
|
| PREINIT: |
| 865 |
|
| SegmentedEnvelope *env; |
| 866 |
|
| CODE: |
| 867 |
260 |
| env = get_envelope(aTHX_ self, "at"); |
| 868 |
259 |
| RETVAL = envelope_at(aTHX_ env, t); |
| 869 |
|
| OUTPUT: |
| 870 |
|
| RETVAL |
| 871 |
|
| |
| 872 |
|
| int |
| 873 |
|
| segment_at(self, t) |
| 874 |
|
| SV *self |
| 875 |
|
| double t |
| 876 |
|
| PREINIT: |
| 877 |
|
| SegmentedEnvelope *env; |
| 878 |
|
| double pd, d; |
| 879 |
|
| int i; |
| 880 |
|
| CODE: |
| 881 |
11 |
| env = get_envelope(aTHX_ self, "segment_at"); |
| 882 |
11 |
▶ | if (env->total_duration <= 0.0 || isnan(t)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (env->total_duration <= 0.0 || isnan(t)) { |
11 |
0 |
11 |
| if (env->total_duration <= 0.0 || isnan(t)) { |
1 |
10 |
11 |
|
| 883 |
1 |
| RETVAL = 0; |
| 884 |
10 |
▶ | } else if (isinf(t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (isinf(t)) { |
2 |
8 |
10 |
|
| 885 |
2 |
▶ | RETVAL = (t > 0 && env->segments > 0) ? (env->segments - 1) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (t > 0 && env->segments > 0) ? (env->segments - 1) : 0; |
2 |
0 |
2 |
| RETVAL = (t > 0 && env->segments > 0) ? (env->segments - 1) : 0; |
2 |
0 |
2 |
|
| 886 |
|
| } else { |
| 887 |
8 |
| t = wrap_pos(env, t); |
| 888 |
8 |
| pd = 0; |
| 889 |
16 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
16 |
0 |
16 |
|
| 890 |
16 |
| d = env->durations[i]; |
| 891 |
16 |
▶ | if (t <= pd + d || i == env->segments - 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t <= pd + d || i == env->segments - 1) { |
8 |
8 |
16 |
| if (t <= pd + d || i == env->segments - 1) { |
0 |
8 |
8 |
|
| 892 |
8 |
| RETVAL = i; |
| 893 |
8 |
| break; |
| 894 |
|
| } |
| 895 |
8 |
| pd += d; |
| 896 |
|
| } |
| 897 |
|
| } |
| 898 |
|
| OUTPUT: |
| 899 |
|
| RETVAL |
| 900 |
|
| |
| 901 |
|
| SV * |
| 902 |
|
| static(self) |
| 903 |
|
| SV *self |
| 904 |
|
| PREINIT: |
| 905 |
|
| SegmentedEnvelope *env; |
| 906 |
|
| SegmentedEnvelope *senv; |
| 907 |
|
| SV *closure; |
| 908 |
|
| CODE: |
| 909 |
29 |
| env = get_envelope(aTHX_ self, "static"); |
| 910 |
|
| |
| 911 |
29 |
| Newxz(senv, 1, SegmentedEnvelope); |
| 912 |
29 |
| copy_envelope(aTHX_ senv, env); |
| 913 |
|
| |
| 914 |
29 |
| closure = newSViv(PTR2IV(senv)); |
| 915 |
29 |
| RETVAL = sv_bless(newRV_noinc(closure), |
| 916 |
|
| gv_stashpv("Math::SegmentedEnvelope::Static", GV_ADD)); |
| 917 |
|
| OUTPUT: |
| 918 |
|
| RETVAL |
| 919 |
|
| |
| 920 |
|
| void |
| 921 |
|
| table(self, ...) |
| 922 |
|
| SV *self |
| 923 |
|
| PREINIT: |
| 924 |
|
| SegmentedEnvelope *env; |
| 925 |
|
| SegmentedEnvelope senv; |
| 926 |
25 |
| int size = 1024; |
| 927 |
25 |
| int loop = 1; |
| 928 |
25 |
| double from = 0; |
| 929 |
|
| double to; |
| 930 |
|
| int i; |
| 931 |
|
| double range, lp, p; |
| 932 |
|
| CODE: |
| 933 |
25 |
| env = get_envelope(aTHX_ self, "table"); |
| 934 |
|
| |
| 935 |
25 |
▶ | if (items > 1) size = SvIV(ST(1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) size = SvIV(ST(1)); |
25 |
0 |
25 |
|
| 936 |
25 |
▶ | if (items > 2) loop = SvIV(ST(2)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) loop = SvIV(ST(2)); |
2 |
23 |
25 |
|
| 937 |
25 |
▶ | if (items > 3) from = SvNV(ST(3)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 3) from = SvNV(ST(3)); |
1 |
24 |
25 |
|
| 938 |
25 |
▶ | to = (items > 4) ? SvNV(ST(4)) : env->total_duration; |
Branch: 1 branch
| Branch | True | False | Total |
| to = (items > 4) ? SvNV(ST(4)) : env->total_duration; |
1 |
24 |
25 |
|
| 939 |
|
| |
| 940 |
25 |
▶ | if (size <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (size <= 0) |
0 |
25 |
25 |
|
| 941 |
0 |
| croak("table size should be >= 1"); |
| 942 |
|
| |
| 943 |
25 |
| memset(&senv, 0, sizeof(senv)); |
| 944 |
25 |
| borrow_envelope(aTHX_ &senv, env); |
| 945 |
|
| |
| 946 |
25 |
| range = to - from; |
| 947 |
25 |
| lp = (double)loop / size; |
| 948 |
|
| |
| 949 |
26 |
▶ | if (senv.morpher_cv && SvOK(senv.morpher_cv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (senv.morpher_cv && SvOK(senv.morpher_cv)) { |
2 |
23 |
25 |
| if (senv.morpher_cv && SvOK(senv.morpher_cv)) { |
2 |
0 |
2 |
|
| 950 |
|
| /* Pre-compute to avoid stack corruption between PUSHs and call_sv */ |
| 951 |
2 |
| SV *buf_sv = sv_2mortal(newSVpvn("", 0)); |
| 952 |
|
| double *vals; |
| 953 |
2 |
▶ | SvGROW(buf_sv, size * sizeof(double)); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(buf_sv, size * sizeof(double)); |
2 |
0 |
2 |
| SvGROW(buf_sv, size * sizeof(double)); |
2 |
0 |
2 |
|
| 954 |
2 |
| vals = (double*)SvPVX(buf_sv); |
| 955 |
52 |
▶ | for (i = 0; i < size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) { |
51 |
1 |
52 |
|
| 956 |
51 |
| p = i * lp; |
| 957 |
51 |
| p = p - floor(p); |
| 958 |
51 |
| vals[i] = envelope_at(aTHX_ &senv, from + range * p); |
| 959 |
|
| } |
| 960 |
1 |
| SP -= items; |
| 961 |
1 |
▶ | EXTEND(SP, size); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, size); |
1 |
0 |
1 |
| EXTEND(SP, size); |
0 |
1 |
1 |
|
| 962 |
51 |
▶ | for (i = 0; i < size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) |
50 |
1 |
51 |
|
| 963 |
50 |
| PUSHs(sv_2mortal(newSVnv(vals[i]))); |
| 964 |
|
| } else { |
| 965 |
23 |
| SP -= items; |
| 966 |
23 |
▶ | EXTEND(SP, size); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, size); |
23 |
0 |
23 |
| EXTEND(SP, size); |
0 |
23 |
23 |
|
| 967 |
17851 |
▶ | for (i = 0; i < size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) { |
17828 |
23 |
17851 |
|
| 968 |
17828 |
| p = i * lp; |
| 969 |
17828 |
| p = p - floor(p); |
| 970 |
17828 |
| PUSHs(sv_2mortal(newSVnv(envelope_at(aTHX_ &senv, from + range * p)))); |
| 971 |
|
| } |
| 972 |
|
| } |
| 973 |
24 |
| te_free(senv.morpher_expr); |
| 974 |
24 |
| XSRETURN(size); |
| 975 |
|
| |
| 976 |
|
| void |
| 977 |
|
| clean(self) |
| 978 |
|
| SV *self |
| 979 |
|
| PREINIT: |
| 980 |
|
| SegmentedEnvelope *env; |
| 981 |
|
| CODE: |
| 982 |
1 |
| env = get_envelope(aTHX_ self, "clean"); |
| 983 |
1 |
| env->total_duration = calc_total_duration(env->durations, env->segments); |
| 984 |
1 |
| env->current_segment = 0; |
| 985 |
1 |
| env->past_segment = -1; |
| 986 |
1 |
| env->passed_duration = 0; |
| 987 |
|
| |
| 988 |
|
| double |
| 989 |
|
| duration(self) |
| 990 |
|
| SV *self |
| 991 |
|
| PREINIT: |
| 992 |
|
| SegmentedEnvelope *env; |
| 993 |
|
| CODE: |
| 994 |
203 |
| env = get_envelope(aTHX_ self, "duration"); |
| 995 |
201 |
▶ | RETVAL = env->total_duration; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = env->total_duration; |
113 |
88 |
201 |
|
| 996 |
|
| OUTPUT: |
| 997 |
|
| RETVAL |
| 998 |
|
| |
| 999 |
|
| int |
| 1000 |
|
| segments(self) |
| 1001 |
|
| SV *self |
| 1002 |
|
| PREINIT: |
| 1003 |
|
| SegmentedEnvelope *env; |
| 1004 |
|
| CODE: |
| 1005 |
107 |
| env = get_envelope(aTHX_ self, "segments"); |
| 1006 |
107 |
▶ | RETVAL = env->segments; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = env->segments; |
64 |
43 |
107 |
|
| 1007 |
|
| OUTPUT: |
| 1008 |
|
| RETVAL |
| 1009 |
|
| |
| 1010 |
|
| SV * |
| 1011 |
|
| level(self, idx, ...) |
| 1012 |
|
| SV *self |
| 1013 |
|
| int idx |
| 1014 |
|
| PREINIT: |
| 1015 |
|
| SegmentedEnvelope *env; |
| 1016 |
|
| int real_idx; |
| 1017 |
|
| CODE: |
| 1018 |
16 |
| env = get_envelope(aTHX_ self, "level"); |
| 1019 |
16 |
▶ | real_idx = (idx >= 0) ? idx : (env->segments + 1 + idx); |
Branch: 1 branch
| Branch | True | False | Total |
| real_idx = (idx >= 0) ? idx : (env->segments + 1 + idx); |
4 |
12 |
16 |
|
| 1020 |
|
| |
| 1021 |
16 |
▶ | if (real_idx < 0 || real_idx > env->segments) |
Branch: 2 branches
| Branch | True | False | Total |
| if (real_idx < 0 || real_idx > env->segments) |
16 |
0 |
16 |
| if (real_idx < 0 || real_idx > env->segments) |
0 |
16 |
16 |
|
| 1022 |
0 |
| croak("no such index '%d' in definition part '0'", idx); |
| 1023 |
|
| |
| 1024 |
16 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
5 |
11 |
16 |
|
| 1025 |
5 |
| env->levels[real_idx] = SvNV(ST(2)); |
| 1026 |
5 |
▶ | if (abs(env->current_segment - real_idx) <= 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (abs(env->current_segment - real_idx) <= 1) |
3 |
2 |
5 |
| if (abs(env->current_segment - real_idx) <= 1) |
3 |
0 |
3 |
|
| 1027 |
3 |
| update_current_segment(env, env->current_segment); |
| 1028 |
|
| } |
| 1029 |
|
| |
| 1030 |
16 |
| RETVAL = newSVnv(env->levels[real_idx]); |
| 1031 |
|
| OUTPUT: |
| 1032 |
|
| RETVAL |
| 1033 |
|
| |
| 1034 |
|
| void |
| 1035 |
|
| levels(self, ...) |
| 1036 |
|
| SV *self |
| 1037 |
|
| PREINIT: |
| 1038 |
|
| SegmentedEnvelope *env; |
| 1039 |
|
| int i; |
| 1040 |
|
| CODE: |
| 1041 |
5 |
| env = get_envelope(aTHX_ self, "levels"); |
| 1042 |
|
| |
| 1043 |
5 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
2 |
3 |
5 |
|
| 1044 |
2 |
▶ | if (items - 1 != env->segments + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items - 1 != env->segments + 1) |
0 |
2 |
2 |
|
| 1045 |
0 |
| croak("size mismatch against initial definition"); |
| 1046 |
9 |
▶ | for (i = 0; i <= env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= env->segments; i++) |
7 |
2 |
9 |
|
| 1047 |
7 |
| env->levels[i] = SvNV(ST(i + 1)); |
| 1048 |
2 |
| update_current_segment(env, env->current_segment); |
| 1049 |
|
| } |
| 1050 |
|
| |
| 1051 |
5 |
| SP -= items; |
| 1052 |
5 |
▶ | EXTEND(SP, env->segments + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, env->segments + 1); |
5 |
0 |
5 |
| EXTEND(SP, env->segments + 1); |
0 |
5 |
5 |
|
| 1053 |
22 |
▶ | for (i = 0; i <= env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= env->segments; i++) |
17 |
5 |
22 |
|
| 1054 |
17 |
| PUSHs(sv_2mortal(newSVnv(env->levels[i]))); |
| 1055 |
5 |
| XSRETURN(env->segments + 1); |
| 1056 |
|
| |
| 1057 |
|
| SV * |
| 1058 |
|
| dur(self, idx, ...) |
| 1059 |
|
| SV *self |
| 1060 |
|
| int idx |
| 1061 |
|
| PREINIT: |
| 1062 |
|
| SegmentedEnvelope *env; |
| 1063 |
|
| int real_idx; |
| 1064 |
|
| CODE: |
| 1065 |
8 |
| env = get_envelope(aTHX_ self, "dur"); |
| 1066 |
8 |
▶ | real_idx = (idx >= 0) ? idx : (env->segments + idx); |
Branch: 1 branch
| Branch | True | False | Total |
| real_idx = (idx >= 0) ? idx : (env->segments + idx); |
3 |
5 |
8 |
|
| 1067 |
|
| |
| 1068 |
8 |
▶ | if (real_idx < 0 || real_idx >= env->segments) |
Branch: 2 branches
| Branch | True | False | Total |
| if (real_idx < 0 || real_idx >= env->segments) |
8 |
0 |
8 |
| if (real_idx < 0 || real_idx >= env->segments) |
0 |
8 |
8 |
|
| 1069 |
0 |
| croak("no such index '%d' in definition part '1'", idx); |
| 1070 |
|
| |
| 1071 |
8 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
3 |
5 |
8 |
|
| 1072 |
3 |
| env->durations[real_idx] = SvNV(ST(2)); |
| 1073 |
3 |
| env->total_duration = calc_total_duration(env->durations, env->segments); |
| 1074 |
3 |
| env->current_segment = 0; |
| 1075 |
3 |
| env->past_segment = -1; |
| 1076 |
3 |
| env->passed_duration = 0; |
| 1077 |
|
| } |
| 1078 |
|
| |
| 1079 |
8 |
| RETVAL = newSVnv(env->durations[real_idx]); |
| 1080 |
|
| OUTPUT: |
| 1081 |
|
| RETVAL |
| 1082 |
|
| |
| 1083 |
|
| void |
| 1084 |
|
| durs(self, ...) |
| 1085 |
|
| SV *self |
| 1086 |
|
| PREINIT: |
| 1087 |
|
| SegmentedEnvelope *env; |
| 1088 |
|
| int i; |
| 1089 |
|
| CODE: |
| 1090 |
6 |
| env = get_envelope(aTHX_ self, "durs"); |
| 1091 |
|
| |
| 1092 |
6 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
2 |
4 |
6 |
|
| 1093 |
2 |
▶ | if (items - 1 != env->segments) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items - 1 != env->segments) |
0 |
2 |
2 |
|
| 1094 |
0 |
| croak("size mismatch against initial definition"); |
| 1095 |
7 |
▶ | for (i = 0; i < env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) |
5 |
2 |
7 |
|
| 1096 |
5 |
| env->durations[i] = SvNV(ST(i + 1)); |
| 1097 |
2 |
| env->total_duration = calc_total_duration(env->durations, env->segments); |
| 1098 |
2 |
| env->current_segment = 0; |
| 1099 |
2 |
| env->past_segment = -1; |
| 1100 |
2 |
| env->passed_duration = 0; |
| 1101 |
|
| } |
| 1102 |
|
| |
| 1103 |
6 |
| SP -= items; |
| 1104 |
6 |
▶ | EXTEND(SP, env->segments); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, env->segments); |
6 |
0 |
6 |
| EXTEND(SP, env->segments); |
0 |
6 |
6 |
|
| 1105 |
20 |
▶ | for (i = 0; i < env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) |
14 |
6 |
20 |
|
| 1106 |
14 |
| PUSHs(sv_2mortal(newSVnv(env->durations[i]))); |
| 1107 |
6 |
| XSRETURN(env->segments); |
| 1108 |
|
| |
| 1109 |
|
| SV * |
| 1110 |
|
| curve(self, idx, ...) |
| 1111 |
|
| SV *self |
| 1112 |
|
| int idx |
| 1113 |
|
| PREINIT: |
| 1114 |
|
| SegmentedEnvelope *env; |
| 1115 |
|
| int real_idx; |
| 1116 |
|
| CODE: |
| 1117 |
9 |
| env = get_envelope(aTHX_ self, "curve"); |
| 1118 |
9 |
▶ | real_idx = (idx >= 0) ? idx : (env->segments + idx); |
Branch: 1 branch
| Branch | True | False | Total |
| real_idx = (idx >= 0) ? idx : (env->segments + idx); |
3 |
6 |
9 |
|
| 1119 |
|
| |
| 1120 |
9 |
▶ | if (real_idx < 0 || real_idx >= env->segments) |
Branch: 2 branches
| Branch | True | False | Total |
| if (real_idx < 0 || real_idx >= env->segments) |
9 |
0 |
9 |
| if (real_idx < 0 || real_idx >= env->segments) |
0 |
9 |
9 |
|
| 1121 |
0 |
| croak("no such index '%d' in definition part '2'", idx); |
| 1122 |
|
| |
| 1123 |
9 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
3 |
6 |
9 |
|
| 1124 |
3 |
| env->curves[real_idx] = SvNV(ST(2)); |
| 1125 |
3 |
▶ | if (env->current_segment == real_idx) |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->current_segment == real_idx) |
2 |
1 |
3 |
|
| 1126 |
2 |
| update_current_segment(env, env->current_segment); |
| 1127 |
|
| } |
| 1128 |
|
| |
| 1129 |
9 |
| RETVAL = newSVnv(env->curves[real_idx]); |
| 1130 |
|
| OUTPUT: |
| 1131 |
|
| RETVAL |
| 1132 |
|
| |
| 1133 |
|
| void |
| 1134 |
|
| curves(self, ...) |
| 1135 |
|
| SV *self |
| 1136 |
|
| PREINIT: |
| 1137 |
|
| SegmentedEnvelope *env; |
| 1138 |
|
| int i; |
| 1139 |
|
| CODE: |
| 1140 |
5 |
| env = get_envelope(aTHX_ self, "curves"); |
| 1141 |
|
| |
| 1142 |
5 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
2 |
3 |
5 |
|
| 1143 |
2 |
▶ | if (items - 1 != env->segments) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items - 1 != env->segments) |
0 |
2 |
2 |
|
| 1144 |
0 |
| croak("size mismatch against initial definition"); |
| 1145 |
7 |
▶ | for (i = 0; i < env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) |
5 |
2 |
7 |
|
| 1146 |
5 |
| env->curves[i] = SvNV(ST(i + 1)); |
| 1147 |
2 |
| update_current_segment(env, env->current_segment); |
| 1148 |
|
| } |
| 1149 |
|
| |
| 1150 |
5 |
| SP -= items; |
| 1151 |
5 |
▶ | EXTEND(SP, env->segments); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, env->segments); |
5 |
0 |
5 |
| EXTEND(SP, env->segments); |
0 |
5 |
5 |
|
| 1152 |
17 |
▶ | for (i = 0; i < env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) |
12 |
5 |
17 |
|
| 1153 |
12 |
| PUSHs(sv_2mortal(newSVnv(env->curves[i]))); |
| 1154 |
5 |
| XSRETURN(env->segments); |
| 1155 |
|
| |
| 1156 |
|
| SV * |
| 1157 |
|
| is_morph(self, ...) |
| 1158 |
|
| SV *self |
| 1159 |
|
| PREINIT: |
| 1160 |
|
| SegmentedEnvelope *env; |
| 1161 |
|
| CODE: |
| 1162 |
63 |
| env = get_envelope(aTHX_ self, "is_morph"); |
| 1163 |
63 |
▶ | if (items > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) |
2 |
61 |
63 |
|
| 1164 |
2 |
| env->is_morph = SvTRUE(ST(1)) ? 1 : 0; |
| 1165 |
63 |
▶ | RETVAL = env->is_morph ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = env->is_morph ? &PL_sv_yes : &PL_sv_no; |
9 |
54 |
63 |
|
| 1166 |
|
| OUTPUT: |
| 1167 |
|
| RETVAL |
| 1168 |
|
| |
| 1169 |
|
| SV * |
| 1170 |
|
| is_hold(self, ...) |
| 1171 |
|
| SV *self |
| 1172 |
|
| PREINIT: |
| 1173 |
|
| SegmentedEnvelope *env; |
| 1174 |
|
| CODE: |
| 1175 |
83 |
| env = get_envelope(aTHX_ self, "is_hold"); |
| 1176 |
83 |
▶ | if (items > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) |
4 |
79 |
83 |
|
| 1177 |
4 |
| env->is_hold = SvTRUE(ST(1)) ? 1 : 0; |
| 1178 |
83 |
▶ | RETVAL = env->is_hold ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = env->is_hold ? &PL_sv_yes : &PL_sv_no; |
41 |
42 |
83 |
|
| 1179 |
|
| OUTPUT: |
| 1180 |
|
| RETVAL |
| 1181 |
|
| |
| 1182 |
|
| SV * |
| 1183 |
|
| is_fold_over(self, ...) |
| 1184 |
|
| SV *self |
| 1185 |
|
| PREINIT: |
| 1186 |
|
| SegmentedEnvelope *env; |
| 1187 |
|
| CODE: |
| 1188 |
79 |
| env = get_envelope(aTHX_ self, "is_fold_over"); |
| 1189 |
79 |
▶ | if (items > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) |
3 |
76 |
79 |
|
| 1190 |
3 |
| env->is_fold_over = SvTRUE(ST(1)) ? 1 : 0; |
| 1191 |
79 |
▶ | RETVAL = env->is_fold_over ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = env->is_fold_over ? &PL_sv_yes : &PL_sv_no; |
37 |
42 |
79 |
|
| 1192 |
|
| OUTPUT: |
| 1193 |
|
| RETVAL |
| 1194 |
|
| |
| 1195 |
|
| SV * |
| 1196 |
|
| is_wrap_neg(self, ...) |
| 1197 |
|
| SV *self |
| 1198 |
|
| PREINIT: |
| 1199 |
|
| SegmentedEnvelope *env; |
| 1200 |
|
| CODE: |
| 1201 |
78 |
| env = get_envelope(aTHX_ self, "is_wrap_neg"); |
| 1202 |
78 |
▶ | if (items > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) |
3 |
75 |
78 |
|
| 1203 |
3 |
| env->is_wrap_neg = SvTRUE(ST(1)) ? 1 : 0; |
| 1204 |
78 |
▶ | RETVAL = env->is_wrap_neg ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = env->is_wrap_neg ? &PL_sv_yes : &PL_sv_no; |
34 |
44 |
78 |
|
| 1205 |
|
| OUTPUT: |
| 1206 |
|
| RETVAL |
| 1207 |
|
| |
| 1208 |
|
| SV * |
| 1209 |
|
| morpher(self, ...) |
| 1210 |
|
| SV *self |
| 1211 |
|
| PREINIT: |
| 1212 |
|
| SegmentedEnvelope *env; |
| 1213 |
|
| CODE: |
| 1214 |
55 |
| env = get_envelope(aTHX_ self, "morpher"); |
| 1215 |
55 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
2 |
53 |
55 |
|
| 1216 |
2 |
▶ | if (env->morpher_cv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_cv) { |
1 |
1 |
2 |
|
| 1217 |
1 |
| SvREFCNT_dec(env->morpher_cv); |
| 1218 |
1 |
| env->morpher_cv = NULL; |
| 1219 |
|
| } |
| 1220 |
2 |
▶ | if (SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { |
1 |
1 |
2 |
| if (SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { |
1 |
0 |
1 |
| if (SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { |
1 |
0 |
1 |
|
| 1221 |
1 |
| env->morpher_cv = newSVsv(ST(1)); |
| 1222 |
|
| } |
| 1223 |
|
| } |
| 1224 |
55 |
▶ | if (env->morpher_cv && SvOK(env->morpher_cv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (env->morpher_cv && SvOK(env->morpher_cv)) |
2 |
53 |
55 |
| if (env->morpher_cv && SvOK(env->morpher_cv)) |
2 |
0 |
2 |
|
| 1225 |
2 |
| RETVAL = newSVsv(env->morpher_cv); |
| 1226 |
|
| else |
| 1227 |
53 |
| RETVAL = &PL_sv_undef; |
| 1228 |
|
| OUTPUT: |
| 1229 |
|
| RETVAL |
| 1230 |
|
| |
| 1231 |
|
| SV * |
| 1232 |
|
| morpher_formula(self, ...) |
| 1233 |
|
| SV *self |
| 1234 |
|
| PREINIT: |
| 1235 |
|
| SegmentedEnvelope *env; |
| 1236 |
|
| CODE: |
| 1237 |
63 |
| env = get_envelope(aTHX_ self, "morpher_formula"); |
| 1238 |
63 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
5 |
58 |
63 |
|
| 1239 |
5 |
▶ | if (!SvOK(ST(1))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(ST(1))) { |
1 |
4 |
5 |
|
| 1240 |
|
| /* undef = reset to default */ |
| 1241 |
1 |
| te_free(env->morpher_expr); env->morpher_expr = NULL; |
| 1242 |
1 |
| jit_morpher_free(env->morpher_jit); env->morpher_jit = NULL; |
| 1243 |
1 |
▶ | if (env->morpher_formula) { Safefree(env->morpher_formula); env->morpher_formula = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_formula) { Safefree(env->morpher_formula); env->morpher_formula = NULL; } |
1 |
0 |
1 |
|
| 1244 |
1 |
| env->morpher_fn = NULL; |
| 1245 |
|
| } else { |
| 1246 |
|
| STRLEN flen; |
| 1247 |
4 |
| const char *formula = SvPV(ST(1), flen); |
| 1248 |
4 |
| validate_morpher_formula(formula); |
| 1249 |
2 |
| set_morpher_formula(env, formula, flen); |
| 1250 |
|
| } |
| 1251 |
|
| } |
| 1252 |
61 |
▶ | if (env->morpher_formula) |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_formula) |
2 |
59 |
61 |
|
| 1253 |
2 |
| RETVAL = newSVpv(env->morpher_formula, 0); |
| 1254 |
59 |
▶ | else if (env->morpher_fn) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (env->morpher_fn) { |
6 |
53 |
59 |
|
| 1255 |
|
| /* Find the name of the predefined morpher */ |
| 1256 |
|
| const morpher_entry *e; |
| 1257 |
6 |
| RETVAL = &PL_sv_undef; |
| 1258 |
132 |
▶ | for (e = predefined_morphers; e->name; e++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (e = predefined_morphers; e->name; e++) { |
132 |
0 |
132 |
|
| 1259 |
132 |
▶ | if (e->fn == env->morpher_fn) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->fn == env->morpher_fn) { |
6 |
126 |
132 |
|
| 1260 |
6 |
| RETVAL = newSVpv(e->name, 0); |
| 1261 |
6 |
| break; |
| 1262 |
|
| } |
| 1263 |
|
| } |
| 1264 |
|
| } |
| 1265 |
|
| else |
| 1266 |
53 |
| RETVAL = &PL_sv_undef; |
| 1267 |
|
| OUTPUT: |
| 1268 |
|
| RETVAL |
| 1269 |
|
| |
| 1270 |
|
| void |
| 1271 |
|
| morpher_formulas(...) |
| 1272 |
|
| CODE: |
| 1273 |
|
| { |
| 1274 |
|
| const morpher_entry *e; |
| 1275 |
2 |
| int count = 0; |
| 1276 |
2 |
| SP -= items; |
| 1277 |
54 |
▶ | for (e = predefined_morphers; e->name; e++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (e = predefined_morphers; e->name; e++) |
52 |
2 |
54 |
|
| 1278 |
52 |
| count++; |
| 1279 |
2 |
▶ | EXTEND(SP, count); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, count); |
2 |
0 |
2 |
| EXTEND(SP, count); |
0 |
2 |
2 |
|
| 1280 |
54 |
▶ | for (e = predefined_morphers; e->name; e++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (e = predefined_morphers; e->name; e++) |
52 |
2 |
54 |
|
| 1281 |
52 |
| PUSHs(sv_2mortal(newSVpv(e->name, 0))); |
| 1282 |
2 |
| XSRETURN(count); |
| 1283 |
|
| } |
| 1284 |
|
| |
| 1285 |
|
| SV * |
| 1286 |
|
| morpher_jit_backend(self) |
| 1287 |
|
| SV *self |
| 1288 |
|
| PREINIT: |
| 1289 |
|
| SegmentedEnvelope *env; |
| 1290 |
|
| CODE: |
| 1291 |
1 |
| env = get_envelope(aTHX_ self, "morpher_jit_backend"); |
| 1292 |
1 |
▶ | if (env->morpher_jit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (env->morpher_jit) |
1 |
0 |
1 |
|
| 1293 |
1 |
| RETVAL = newSVpv(jit_backend_name(env->morpher_jit), 0); |
| 1294 |
0 |
▶ | else if (env->morpher_fn && !env->morpher_formula) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (env->morpher_fn && !env->morpher_formula) |
0 |
0 |
0 |
| else if (env->morpher_fn && !env->morpher_formula) |
0 |
0 |
0 |
|
| 1295 |
0 |
| RETVAL = newSVpv("builtin", 0); |
| 1296 |
|
| else |
| 1297 |
0 |
| RETVAL = newSVpv("none", 0); |
| 1298 |
|
| OUTPUT: |
| 1299 |
|
| RETVAL |
| 1300 |
|
| |
| 1301 |
|
| SV * |
| 1302 |
|
| border_level(self, ...) |
| 1303 |
|
| SV *self |
| 1304 |
|
| PREINIT: |
| 1305 |
|
| SegmentedEnvelope *env; |
| 1306 |
|
| AV *result; |
| 1307 |
|
| CODE: |
| 1308 |
6 |
| env = get_envelope(aTHX_ self, "border_level"); |
| 1309 |
6 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
3 |
3 |
6 |
|
| 1310 |
3 |
| SV *val = ST(1); |
| 1311 |
5 |
▶ | if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
2 |
1 |
3 |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
2 |
0 |
2 |
|
| 1312 |
2 |
| AV *bl = (AV*)SvRV(val); |
| 1313 |
2 |
▶ | if (av_len(bl) >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(bl) >= 0) |
2 |
0 |
2 |
|
| 1314 |
2 |
| env->border_start = av_fetch_nv(bl, 0, 0.0); |
| 1315 |
2 |
▶ | if (av_len(bl) >= 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(bl) >= 1) |
2 |
0 |
2 |
|
| 1316 |
2 |
| env->border_end = av_fetch_nv(bl, 1, env->border_start); |
| 1317 |
|
| else |
| 1318 |
0 |
| env->border_end = env->border_start; |
| 1319 |
1 |
▶ | } else if (SvOK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvOK(val)) { |
1 |
0 |
1 |
|
| 1320 |
1 |
| env->border_start = SvNV(val); |
| 1321 |
1 |
| env->border_end = env->border_start; |
| 1322 |
|
| } |
| 1323 |
|
| } |
| 1324 |
6 |
| result = newAV(); |
| 1325 |
6 |
| av_push(result, newSVnv(env->border_start)); |
| 1326 |
6 |
| av_push(result, newSVnv(env->border_end)); |
| 1327 |
6 |
| RETVAL = newRV_noinc((SV*)result); |
| 1328 |
|
| OUTPUT: |
| 1329 |
|
| RETVAL |
| 1330 |
|
| |
| 1331 |
|
| SV * |
| 1332 |
|
| normalize_duration(self) |
| 1333 |
|
| SV *self |
| 1334 |
|
| PREINIT: |
| 1335 |
|
| SegmentedEnvelope *env; |
| 1336 |
|
| CODE: |
| 1337 |
2 |
| env = get_envelope(aTHX_ self, "normalize_duration"); |
| 1338 |
2 |
| normalize_sum(env->durations, env->segments); |
| 1339 |
2 |
| env->total_duration = calc_total_duration(env->durations, env->segments); |
| 1340 |
2 |
| env->current_segment = 0; |
| 1341 |
2 |
| env->past_segment = -1; |
| 1342 |
2 |
| env->passed_duration = 0; |
| 1343 |
2 |
| RETVAL = newSVsv(self); |
| 1344 |
|
| OUTPUT: |
| 1345 |
|
| RETVAL |
| 1346 |
|
| |
| 1347 |
|
| SV * |
| 1348 |
|
| def(self) |
| 1349 |
|
| SV *self |
| 1350 |
|
| PREINIT: |
| 1351 |
|
| SegmentedEnvelope *env; |
| 1352 |
|
| AV *result, *levels_av, *durs_av, *curves_av; |
| 1353 |
|
| int i; |
| 1354 |
|
| CODE: |
| 1355 |
62 |
| env = get_envelope(aTHX_ self, "def"); |
| 1356 |
|
| |
| 1357 |
62 |
| levels_av = newAV(); |
| 1358 |
62 |
| durs_av = newAV(); |
| 1359 |
62 |
| curves_av = newAV(); |
| 1360 |
|
| |
| 1361 |
263 |
▶ | for (i = 0; i <= env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= env->segments; i++) |
201 |
62 |
263 |
|
| 1362 |
201 |
| av_push(levels_av, newSVnv(env->levels[i])); |
| 1363 |
201 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
139 |
62 |
201 |
|
| 1364 |
139 |
| av_push(durs_av, newSVnv(env->durations[i])); |
| 1365 |
139 |
| av_push(curves_av, newSVnv(env->curves[i])); |
| 1366 |
|
| } |
| 1367 |
|
| |
| 1368 |
62 |
| result = newAV(); |
| 1369 |
62 |
| av_push(result, newRV_noinc((SV*)levels_av)); |
| 1370 |
62 |
| av_push(result, newRV_noinc((SV*)durs_av)); |
| 1371 |
62 |
| av_push(result, newRV_noinc((SV*)curves_av)); |
| 1372 |
|
| |
| 1373 |
62 |
| RETVAL = newRV_noinc((SV*)result); |
| 1374 |
|
| OUTPUT: |
| 1375 |
|
| RETVAL |
| 1376 |
|
| |
| 1377 |
|
| SV * |
| 1378 |
|
| spline(...) |
| 1379 |
|
| PREINIT: |
| 1380 |
|
| AV *times_av, *values_av; |
| 1381 |
|
| int n, total_segs, res, span, i, idx, arg_start; |
| 1382 |
|
| double tension, tau; |
| 1383 |
|
| double *times, *vals; |
| 1384 |
|
| SegmentedEnvelope *env; |
| 1385 |
|
| SV *self; |
| 1386 |
|
| HV *stash; |
| 1387 |
|
| const char *classname; |
| 1388 |
|
| SV *times_ref, *values_ref; |
| 1389 |
|
| CODE: |
| 1390 |
|
| /* Detect class method vs instance method vs function call */ |
| 1391 |
8 |
▶ | if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
5 |
3 |
8 |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
4 |
1 |
5 |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
1 |
3 |
4 |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
1 |
0 |
1 |
|
| 1392 |
3 |
▶ | classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
Branch: 7 branches
| Branch | True | False | Total |
| classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
1 |
1 |
2 |
| classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
1 |
0 |
1 |
| classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
1 |
0 |
1 |
| classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
0 |
1 |
1 |
| classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
0 |
0 |
0 |
| classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
1 |
0 |
1 |
| classname = sv_isobject(ST(0)) ? HvNAME(SvSTASH(SvRV(ST(0)))) : SvPV_nolen(ST(0)); |
0 |
1 |
1 |
|
| 1393 |
2 |
| times_ref = ST(1); |
| 1394 |
2 |
| values_ref = ST(2); |
| 1395 |
2 |
| arg_start = 3; |
| 1396 |
6 |
▶ | } else if (items >= 2 && !sv_isobject(ST(0))) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (items >= 2 && !sv_isobject(ST(0))) { |
6 |
0 |
6 |
| } else if (items >= 2 && !sv_isobject(ST(0))) { |
6 |
0 |
6 |
|
| 1397 |
6 |
| classname = "Math::SegmentedEnvelope"; |
| 1398 |
6 |
| times_ref = ST(0); |
| 1399 |
6 |
| values_ref = ST(1); |
| 1400 |
6 |
| arg_start = 2; |
| 1401 |
|
| } else { |
| 1402 |
0 |
| croak("Usage: spline(\\@times, \\@values, %%opts)"); |
| 1403 |
|
| } |
| 1404 |
|
| |
| 1405 |
8 |
▶ | if (!SvROK(times_ref) || SvTYPE(SvRV(times_ref)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(times_ref) || SvTYPE(SvRV(times_ref)) != SVt_PVAV) |
8 |
0 |
8 |
| if (!SvROK(times_ref) || SvTYPE(SvRV(times_ref)) != SVt_PVAV) |
0 |
8 |
8 |
|
| 1406 |
0 |
| croak("spline: times must be an array reference"); |
| 1407 |
8 |
▶ | if (!SvROK(values_ref) || SvTYPE(SvRV(values_ref)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(values_ref) || SvTYPE(SvRV(values_ref)) != SVt_PVAV) |
8 |
0 |
8 |
| if (!SvROK(values_ref) || SvTYPE(SvRV(values_ref)) != SVt_PVAV) |
0 |
8 |
8 |
|
| 1408 |
0 |
| croak("spline: values must be an array reference"); |
| 1409 |
|
| |
| 1410 |
8 |
| times_av = (AV*)SvRV(times_ref); |
| 1411 |
8 |
| values_av = (AV*)SvRV(values_ref); |
| 1412 |
8 |
| n = av_len(times_av) + 1; |
| 1413 |
8 |
▶ | if (n < 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 2) |
1 |
7 |
8 |
|
| 1414 |
1 |
| croak("spline requires at least 2 points"); |
| 1415 |
7 |
▶ | if (n != av_len(values_av) + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n != av_len(values_av) + 1) |
1 |
6 |
7 |
|
| 1416 |
1 |
| croak("spline: times and values must have equal length"); |
| 1417 |
|
| |
| 1418 |
|
| /* Pre-validate morpher_formula before allocating */ |
| 1419 |
10 |
▶ | for (i = arg_start; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = arg_start; i < items; i += 2) { |
4 |
6 |
10 |
|
| 1420 |
8 |
▶ | if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
4 |
0 |
4 |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
4 |
0 |
4 |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "morpher_formula")) { |
1 |
3 |
4 |
|
| 1421 |
1 |
▶ | if (SvOK(ST(i + 1))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(ST(i + 1))) |
1 |
0 |
1 |
|
| 1422 |
2 |
| validate_morpher_formula(SvPV_nolen(ST(i + 1))); |
| 1423 |
|
| } |
| 1424 |
|
| } |
| 1425 |
|
| |
| 1426 |
|
| /* Parse optional key-value args */ |
| 1427 |
6 |
| res = 8; |
| 1428 |
6 |
| tension = 0.0; |
| 1429 |
10 |
▶ | for (i = arg_start; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = arg_start; i < items; i += 2) { |
4 |
6 |
10 |
|
| 1430 |
4 |
▶ | if (i + 1 < items && SvPOK(ST(i))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i))) { |
4 |
0 |
4 |
| if (i + 1 < items && SvPOK(ST(i))) { |
4 |
0 |
4 |
|
| 1431 |
4 |
| const char *key = SvPV_nolen(ST(i)); |
| 1432 |
4 |
▶ | if (strEQ(key, "resolution")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key, "resolution")) |
2 |
2 |
4 |
|
| 1433 |
2 |
| res = SvIV(ST(i + 1)); |
| 1434 |
2 |
▶ | else if (strEQ(key, "tension")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(key, "tension")) |
1 |
1 |
2 |
|
| 1435 |
1 |
| tension = SvNV(ST(i + 1)); |
| 1436 |
|
| } |
| 1437 |
|
| } |
| 1438 |
6 |
▶ | if (res < 1) res = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 1) res = 1; |
0 |
6 |
6 |
|
| 1439 |
6 |
▶ | if (tension < 0.0) tension = 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tension < 0.0) tension = 0.0; |
0 |
6 |
6 |
|
| 1440 |
6 |
▶ | if (tension > 1.0) tension = 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tension > 1.0) tension = 1.0; |
0 |
6 |
6 |
|
| 1441 |
6 |
| tau = (1.0 - tension) * 0.5; |
| 1442 |
|
| |
| 1443 |
|
| /* Read input arrays */ |
| 1444 |
6 |
| Newx(times, n, double); |
| 1445 |
6 |
| Newx(vals, n, double); |
| 1446 |
25 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
19 |
6 |
25 |
|
| 1447 |
19 |
| times[i] = av_fetch_nv(times_av, i, 0.0); |
| 1448 |
19 |
| vals[i] = av_fetch_nv(values_av, i, 0.0); |
| 1449 |
|
| } |
| 1450 |
|
| |
| 1451 |
|
| /* Allocate envelope: (n-1)*res segments, (n-1)*res+1 levels */ |
| 1452 |
6 |
| total_segs = (n - 1) * res; |
| 1453 |
6 |
| Newxz(env, 1, SegmentedEnvelope); |
| 1454 |
6 |
| env->past_segment = -1; |
| 1455 |
6 |
| env->segments = total_segs; |
| 1456 |
6 |
| Newx(env->levels, total_segs + 1, double); |
| 1457 |
6 |
| Newx(env->durations, total_segs, double); |
| 1458 |
6 |
| Newx(env->curves, total_segs, double); |
| 1459 |
|
| |
| 1460 |
6 |
| idx = 0; |
| 1461 |
19 |
▶ | for (span = 0; span < n - 1; span++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (span = 0; span < n - 1; span++) { |
13 |
6 |
19 |
|
| 1462 |
13 |
| double dt = times[span + 1] - times[span]; |
| 1463 |
13 |
▶ | double seg_dur = (dt > 0.0) ? dt / res : 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| double seg_dur = (dt > 0.0) ? dt / res : 0.0; |
13 |
0 |
13 |
|
| 1464 |
13 |
▶ | double p0 = vals[span > 0 ? span - 1 : span]; |
Branch: 1 branch
| Branch | True | False | Total |
| double p0 = vals[span > 0 ? span - 1 : span]; |
7 |
6 |
13 |
|
| 1465 |
13 |
| double p1 = vals[span]; |
| 1466 |
13 |
| double p2 = vals[span + 1]; |
| 1467 |
13 |
▶ | double p3 = vals[span + 2 < n ? span + 2 : span + 1]; |
Branch: 1 branch
| Branch | True | False | Total |
| double p3 = vals[span + 2 < n ? span + 2 : span + 1]; |
7 |
6 |
13 |
|
| 1468 |
|
| |
| 1469 |
154 |
▶ | for (i = 0; i <= res; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= res; i++) { |
141 |
13 |
154 |
|
| 1470 |
141 |
▶ | if (span > 0 && i == 0) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (span > 0 && i == 0) continue; |
83 |
58 |
141 |
| if (span > 0 && i == 0) continue; |
7 |
76 |
83 |
|
| 1471 |
|
| { |
| 1472 |
134 |
| double s = (double)i / res; |
| 1473 |
134 |
| double s2 = s * s; |
| 1474 |
134 |
| double s3 = s2 * s; |
| 1475 |
134 |
| double v = (-tau*s3 + 2*tau*s2 - tau*s) * p0 |
| 1476 |
134 |
| + ((2-tau)*s3 + (tau-3)*s2 + 1) * p1 |
| 1477 |
134 |
| + ((tau-2)*s3 + (3-2*tau)*s2 + tau*s) * p2 |
| 1478 |
134 |
| + (tau*s3 - tau*s2) * p3; |
| 1479 |
|
| |
| 1480 |
134 |
| env->levels[idx] = v; |
| 1481 |
134 |
▶ | if (idx > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx > 0) { |
128 |
6 |
134 |
|
| 1482 |
128 |
| env->durations[idx - 1] = seg_dur; |
| 1483 |
128 |
| env->curves[idx - 1] = 1.0; |
| 1484 |
|
| } |
| 1485 |
134 |
| idx++; |
| 1486 |
|
| } |
| 1487 |
|
| } |
| 1488 |
|
| } |
| 1489 |
|
| |
| 1490 |
6 |
| env->total_duration = calc_total_duration(env->durations, env->segments); |
| 1491 |
|
| |
| 1492 |
6 |
| Safefree(times); |
| 1493 |
6 |
| Safefree(vals); |
| 1494 |
|
| |
| 1495 |
|
| /* Parse remaining envelope options (is_morph, etc.) */ |
| 1496 |
10 |
▶ | for (i = arg_start; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = arg_start; i < items; i += 2) { |
4 |
6 |
10 |
|
| 1497 |
4 |
▶ | if (i + 1 < items && SvPOK(ST(i))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i))) { |
4 |
0 |
4 |
| if (i + 1 < items && SvPOK(ST(i))) { |
4 |
0 |
4 |
|
| 1498 |
4 |
| const char *key = SvPV_nolen(ST(i)); |
| 1499 |
4 |
| SV *val = ST(i + 1); |
| 1500 |
4 |
▶ | if (strEQ(key, "is_morph")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key, "is_morph")) |
0 |
4 |
4 |
|
| 1501 |
0 |
| env->is_morph = SvTRUE(val) ? 1 : 0; |
| 1502 |
4 |
▶ | else if (strEQ(key, "is_hold")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(key, "is_hold")) |
0 |
4 |
4 |
|
| 1503 |
0 |
| env->is_hold = SvTRUE(val) ? 1 : 0; |
| 1504 |
4 |
▶ | else if (strEQ(key, "is_fold_over")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(key, "is_fold_over")) |
0 |
4 |
4 |
|
| 1505 |
0 |
| env->is_fold_over = SvTRUE(val) ? 1 : 0; |
| 1506 |
4 |
▶ | else if (strEQ(key, "is_wrap_neg")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(key, "is_wrap_neg")) |
0 |
4 |
4 |
|
| 1507 |
0 |
| env->is_wrap_neg = SvTRUE(val) ? 1 : 0; |
| 1508 |
4 |
▶ | else if (strEQ(key, "morpher_formula")) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(key, "morpher_formula")) { |
1 |
3 |
4 |
|
| 1509 |
1 |
▶ | if (SvOK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(val)) { |
1 |
0 |
1 |
|
| 1510 |
|
| STRLEN flen; |
| 1511 |
1 |
| const char *formula = SvPV(val, flen); |
| 1512 |
1 |
| set_morpher_formula(env, formula, flen); |
| 1513 |
|
| } |
| 1514 |
3 |
▶ | } else if (strEQ(key, "morpher")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "morpher")) { |
0 |
3 |
3 |
|
| 1515 |
0 |
▶ | if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) |
0 |
0 |
0 |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) |
0 |
0 |
0 |
|
| 1516 |
0 |
| env->morpher_cv = newSVsv(val); |
| 1517 |
|
| } |
| 1518 |
|
| } |
| 1519 |
|
| } |
| 1520 |
|
| |
| 1521 |
|
| /* classname was set during arg parsing above */ |
| 1522 |
6 |
| self = newSViv(PTR2IV(env)); |
| 1523 |
6 |
| self = newRV_noinc(self); |
| 1524 |
6 |
| stash = gv_stashpv(classname, GV_ADD); |
| 1525 |
6 |
| sv_bless(self, stash); |
| 1526 |
6 |
| RETVAL = self; |
| 1527 |
|
| OUTPUT: |
| 1528 |
|
| RETVAL |
| 1529 |
|
| |
| 1530 |
|
| SV * |
| 1531 |
|
| quantize(self, steps) |
| 1532 |
|
| SV *self |
| 1533 |
|
| int steps |
| 1534 |
|
| PREINIT: |
| 1535 |
|
| SegmentedEnvelope *env, *nenv; |
| 1536 |
|
| int i; |
| 1537 |
|
| CODE: |
| 1538 |
2 |
| env = get_envelope(aTHX_ self, "quantize"); |
| 1539 |
2 |
▶ | if (steps < 2) steps = 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (steps < 2) steps = 2; |
0 |
2 |
2 |
|
| 1540 |
|
| |
| 1541 |
2 |
| nenv = new_envelope(); |
| 1542 |
2 |
| copy_flags(nenv, env); |
| 1543 |
2 |
| nenv->segments = env->segments; |
| 1544 |
|
| |
| 1545 |
2 |
| Newx(nenv->levels, env->segments + 1, double); |
| 1546 |
2 |
| Newx(nenv->durations, env->segments, double); |
| 1547 |
2 |
| Newx(nenv->curves, env->segments, double); |
| 1548 |
|
| |
| 1549 |
10 |
▶ | for (i = 0; i <= env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= env->segments; i++) |
8 |
2 |
10 |
|
| 1550 |
8 |
| nenv->levels[i] = floor(env->levels[i] * steps + 0.5) / steps; |
| 1551 |
8 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
6 |
2 |
8 |
|
| 1552 |
6 |
| nenv->durations[i] = env->durations[i]; |
| 1553 |
6 |
| nenv->curves[i] = env->curves[i]; |
| 1554 |
|
| } |
| 1555 |
2 |
| nenv->total_duration = env->total_duration; |
| 1556 |
|
| |
| 1557 |
2 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1558 |
|
| OUTPUT: |
| 1559 |
|
| RETVAL |
| 1560 |
|
| |
| 1561 |
|
| SV * |
| 1562 |
|
| trim(self, from_t, to_t, ...) |
| 1563 |
|
| SV *self |
| 1564 |
|
| double from_t |
| 1565 |
|
| double to_t |
| 1566 |
|
| PREINIT: |
| 1567 |
|
| SegmentedEnvelope *env, *nenv; |
| 1568 |
|
| SegmentedEnvelope senv; |
| 1569 |
|
| int i, segments; |
| 1570 |
|
| double dur, seg_dur; |
| 1571 |
|
| CODE: |
| 1572 |
3 |
| env = get_envelope(aTHX_ self, "trim"); |
| 1573 |
|
| |
| 1574 |
3 |
| dur = to_t - from_t; |
| 1575 |
3 |
▶ | if (dur <= 0.0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dur <= 0.0) |
1 |
2 |
3 |
|
| 1576 |
1 |
| croak("trim: to_t must be greater than from_t"); |
| 1577 |
|
| |
| 1578 |
2 |
| segments = 32; |
| 1579 |
3 |
▶ | for (i = 3; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 3; i < items; i += 2) { |
1 |
2 |
3 |
|
| 1580 |
2 |
▶ | if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "segments")) |
Branch: 3 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "segments")) |
1 |
0 |
1 |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "segments")) |
1 |
0 |
1 |
| if (i + 1 < items && SvPOK(ST(i)) && strEQ(SvPV_nolen(ST(i)), "segments")) |
1 |
0 |
1 |
|
| 1581 |
1 |
| segments = SvIV(ST(i + 1)); |
| 1582 |
|
| } |
| 1583 |
2 |
▶ | if (segments < 1) segments = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (segments < 1) segments = 1; |
0 |
2 |
2 |
|
| 1584 |
|
| |
| 1585 |
2 |
| seg_dur = dur / segments; |
| 1586 |
|
| |
| 1587 |
|
| /* Borrow parent for evaluation */ |
| 1588 |
2 |
| memset(&senv, 0, sizeof(senv)); |
| 1589 |
2 |
| borrow_envelope(aTHX_ &senv, env); |
| 1590 |
|
| |
| 1591 |
2 |
| nenv = new_envelope(); |
| 1592 |
2 |
| copy_flags(nenv, env); |
| 1593 |
2 |
| nenv->segments = segments; |
| 1594 |
2 |
| Newx(nenv->levels, segments + 1, double); |
| 1595 |
2 |
| Newx(nenv->durations, segments, double); |
| 1596 |
2 |
| Newx(nenv->curves, segments, double); |
| 1597 |
|
| |
| 1598 |
44 |
▶ | for (i = 0; i <= segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= segments; i++) |
42 |
2 |
44 |
|
| 1599 |
42 |
| nenv->levels[i] = envelope_at(aTHX_ &senv, from_t + i * seg_dur); |
| 1600 |
42 |
▶ | for (i = 0; i < segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < segments; i++) { |
40 |
2 |
42 |
|
| 1601 |
40 |
| nenv->durations[i] = seg_dur; |
| 1602 |
40 |
| nenv->curves[i] = 1.0; |
| 1603 |
|
| } |
| 1604 |
2 |
| nenv->total_duration = dur; |
| 1605 |
|
| |
| 1606 |
2 |
| te_free(senv.morpher_expr); |
| 1607 |
|
| |
| 1608 |
2 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1609 |
|
| OUTPUT: |
| 1610 |
|
| RETVAL |
| 1611 |
|
| |
| 1612 |
|
| SV * |
| 1613 |
|
| lerp(self, other_sv, mix) |
| 1614 |
|
| SV *self |
| 1615 |
|
| SV *other_sv |
| 1616 |
|
| double mix |
| 1617 |
|
| PREINIT: |
| 1618 |
|
| SegmentedEnvelope *a, *b, *nenv; |
| 1619 |
|
| int i; |
| 1620 |
|
| double imix; |
| 1621 |
|
| CODE: |
| 1622 |
7 |
| a = get_envelope(aTHX_ self, "lerp"); |
| 1623 |
7 |
| b = get_envelope(aTHX_ other_sv, "lerp"); |
| 1624 |
|
| |
| 1625 |
5 |
▶ | if (a->segments != b->segments) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->segments != b->segments) |
1 |
4 |
5 |
|
| 1626 |
1 |
| croak("lerp: envelopes must have the same number of segments (use blend for different sizes)"); |
| 1627 |
|
| |
| 1628 |
4 |
| imix = 1.0 - mix; |
| 1629 |
|
| |
| 1630 |
4 |
| nenv = new_envelope(); |
| 1631 |
4 |
| copy_flags(nenv, a); |
| 1632 |
4 |
| nenv->segments = a->segments; |
| 1633 |
|
| |
| 1634 |
4 |
| Newx(nenv->levels, a->segments + 1, double); |
| 1635 |
4 |
| Newx(nenv->durations, a->segments, double); |
| 1636 |
4 |
| Newx(nenv->curves, a->segments, double); |
| 1637 |
|
| |
| 1638 |
16 |
▶ | for (i = 0; i <= a->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= a->segments; i++) |
12 |
4 |
16 |
|
| 1639 |
12 |
| nenv->levels[i] = a->levels[i] * imix + b->levels[i] * mix; |
| 1640 |
12 |
▶ | for (i = 0; i < a->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->segments; i++) { |
8 |
4 |
12 |
|
| 1641 |
8 |
| nenv->durations[i] = a->durations[i] * imix + b->durations[i] * mix; |
| 1642 |
8 |
| nenv->curves[i] = a->curves[i] * imix + b->curves[i] * mix; |
| 1643 |
|
| } |
| 1644 |
4 |
| nenv->total_duration = calc_total_duration(nenv->durations, nenv->segments); |
| 1645 |
|
| |
| 1646 |
4 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1647 |
|
| OUTPUT: |
| 1648 |
|
| RETVAL |
| 1649 |
|
| |
| 1650 |
|
| SV * |
| 1651 |
|
| resample(self, num_segments) |
| 1652 |
|
| SV *self |
| 1653 |
|
| int num_segments |
| 1654 |
|
| PREINIT: |
| 1655 |
|
| SegmentedEnvelope *env, *nenv; |
| 1656 |
|
| SegmentedEnvelope senv; |
| 1657 |
|
| int i; |
| 1658 |
|
| double seg_dur; |
| 1659 |
|
| CODE: |
| 1660 |
2 |
| env = get_envelope(aTHX_ self, "resample"); |
| 1661 |
2 |
▶ | if (num_segments < 1) num_segments = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_segments < 1) num_segments = 1; |
0 |
2 |
2 |
|
| 1662 |
|
| |
| 1663 |
2 |
| memset(&senv, 0, sizeof(senv)); |
| 1664 |
2 |
| borrow_envelope(aTHX_ &senv, env); |
| 1665 |
|
| |
| 1666 |
2 |
| seg_dur = env->total_duration / num_segments; |
| 1667 |
|
| |
| 1668 |
2 |
| nenv = new_envelope(); |
| 1669 |
2 |
| copy_flags(nenv, env); |
| 1670 |
2 |
| nenv->segments = num_segments; |
| 1671 |
2 |
| Newx(nenv->levels, num_segments + 1, double); |
| 1672 |
2 |
| Newx(nenv->durations, num_segments, double); |
| 1673 |
2 |
| Newx(nenv->curves, num_segments, double); |
| 1674 |
|
| |
| 1675 |
28 |
▶ | for (i = 0; i <= num_segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= num_segments; i++) |
26 |
2 |
28 |
|
| 1676 |
26 |
| nenv->levels[i] = envelope_at(aTHX_ &senv, i * seg_dur); |
| 1677 |
26 |
▶ | for (i = 0; i < num_segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_segments; i++) { |
24 |
2 |
26 |
|
| 1678 |
24 |
| nenv->durations[i] = seg_dur; |
| 1679 |
24 |
| nenv->curves[i] = 1.0; |
| 1680 |
|
| } |
| 1681 |
2 |
| nenv->total_duration = env->total_duration; |
| 1682 |
|
| |
| 1683 |
2 |
| te_free(senv.morpher_expr); |
| 1684 |
|
| |
| 1685 |
2 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1686 |
|
| OUTPUT: |
| 1687 |
|
| RETVAL |
| 1688 |
|
| |
| 1689 |
|
| SV * |
| 1690 |
|
| clamp(self, lo, hi) |
| 1691 |
|
| SV *self |
| 1692 |
|
| double lo |
| 1693 |
|
| double hi |
| 1694 |
|
| PREINIT: |
| 1695 |
|
| SegmentedEnvelope *env, *nenv; |
| 1696 |
|
| int i; |
| 1697 |
|
| double v; |
| 1698 |
|
| CODE: |
| 1699 |
3 |
| env = get_envelope(aTHX_ self, "clamp"); |
| 1700 |
3 |
▶ | if (lo > hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo > hi) { |
1 |
2 |
3 |
|
| 1701 |
1 |
| double tmp = lo; |
| 1702 |
1 |
| lo = hi; |
| 1703 |
1 |
| hi = tmp; |
| 1704 |
|
| } |
| 1705 |
|
| |
| 1706 |
3 |
| nenv = new_envelope(); |
| 1707 |
3 |
| copy_flags(nenv, env); |
| 1708 |
3 |
| nenv->segments = env->segments; |
| 1709 |
3 |
| Newx(nenv->levels, env->segments + 1, double); |
| 1710 |
3 |
| Newx(nenv->durations, env->segments, double); |
| 1711 |
3 |
| Newx(nenv->curves, env->segments, double); |
| 1712 |
|
| |
| 1713 |
15 |
▶ | for (i = 0; i <= env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= env->segments; i++) { |
12 |
3 |
15 |
|
| 1714 |
12 |
| v = env->levels[i]; |
| 1715 |
12 |
▶ | if (v < lo) v = lo; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v < lo) v = lo; |
4 |
8 |
12 |
|
| 1716 |
12 |
▶ | if (v > hi) v = hi; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > hi) v = hi; |
3 |
9 |
12 |
|
| 1717 |
12 |
| nenv->levels[i] = v; |
| 1718 |
|
| } |
| 1719 |
12 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
9 |
3 |
12 |
|
| 1720 |
9 |
| nenv->durations[i] = env->durations[i]; |
| 1721 |
9 |
| nenv->curves[i] = env->curves[i]; |
| 1722 |
|
| } |
| 1723 |
3 |
| nenv->total_duration = env->total_duration; |
| 1724 |
|
| |
| 1725 |
3 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1726 |
|
| OUTPUT: |
| 1727 |
|
| RETVAL |
| 1728 |
|
| |
| 1729 |
|
| SV * |
| 1730 |
|
| smooth(self, ...) |
| 1731 |
|
| SV *self |
| 1732 |
|
| PREINIT: |
| 1733 |
|
| SegmentedEnvelope *env, *nenv; |
| 1734 |
|
| int i, passes, p; |
| 1735 |
|
| double *buf; |
| 1736 |
|
| CODE: |
| 1737 |
2 |
| env = get_envelope(aTHX_ self, "smooth"); |
| 1738 |
2 |
▶ | passes = (items > 1) ? SvIV(ST(1)) : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| passes = (items > 1) ? SvIV(ST(1)) : 1; |
2 |
0 |
2 |
|
| 1739 |
2 |
▶ | if (passes < 1) passes = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (passes < 1) passes = 1; |
0 |
2 |
2 |
|
| 1740 |
|
| |
| 1741 |
2 |
| nenv = new_envelope(); |
| 1742 |
2 |
| copy_flags(nenv, env); |
| 1743 |
2 |
| nenv->segments = env->segments; |
| 1744 |
2 |
| Newx(nenv->levels, env->segments + 1, double); |
| 1745 |
2 |
| Newx(nenv->durations, env->segments, double); |
| 1746 |
2 |
| Newx(nenv->curves, env->segments, double); |
| 1747 |
|
| |
| 1748 |
10 |
▶ | for (i = 0; i <= env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= env->segments; i++) |
8 |
2 |
10 |
|
| 1749 |
8 |
| nenv->levels[i] = env->levels[i]; |
| 1750 |
8 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
6 |
2 |
8 |
|
| 1751 |
6 |
| nenv->durations[i] = env->durations[i]; |
| 1752 |
6 |
| nenv->curves[i] = env->curves[i]; |
| 1753 |
|
| } |
| 1754 |
2 |
| nenv->total_duration = env->total_duration; |
| 1755 |
|
| |
| 1756 |
|
| /* Moving average smoothing: keep endpoints fixed */ |
| 1757 |
2 |
| Newx(buf, env->segments + 1, double); |
| 1758 |
7 |
▶ | for (p = 0; p < passes; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 0; p < passes; p++) { |
5 |
2 |
7 |
|
| 1759 |
5 |
| Copy(nenv->levels, buf, env->segments + 1, double); |
| 1760 |
16 |
▶ | for (i = 1; i < env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < env->segments; i++) |
11 |
5 |
16 |
|
| 1761 |
11 |
| nenv->levels[i] = (buf[i-1] + buf[i] + buf[i+1]) / 3.0; |
| 1762 |
|
| } |
| 1763 |
2 |
| Safefree(buf); |
| 1764 |
|
| |
| 1765 |
2 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1766 |
|
| OUTPUT: |
| 1767 |
|
| RETVAL |
| 1768 |
|
| |
| 1769 |
|
| SV * |
| 1770 |
|
| derivative(self) |
| 1771 |
|
| SV *self |
| 1772 |
|
| PREINIT: |
| 1773 |
|
| SegmentedEnvelope *env, *nenv; |
| 1774 |
|
| int i; |
| 1775 |
|
| CODE: |
| 1776 |
3 |
| env = get_envelope(aTHX_ self, "derivative"); |
| 1777 |
|
| |
| 1778 |
3 |
| nenv = new_envelope(); |
| 1779 |
3 |
| copy_flags(nenv, env); |
| 1780 |
3 |
| nenv->segments = env->segments; |
| 1781 |
3 |
| Newx(nenv->levels, env->segments + 1, double); |
| 1782 |
3 |
| Newx(nenv->durations, env->segments, double); |
| 1783 |
3 |
| Newx(nenv->curves, env->segments, double); |
| 1784 |
|
| |
| 1785 |
|
| /* Rate of change at each breakpoint (forward difference) */ |
| 1786 |
7 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
4 |
3 |
7 |
|
| 1787 |
4 |
| double dl = env->levels[i+1] - env->levels[i]; |
| 1788 |
4 |
| double dt = env->durations[i]; |
| 1789 |
4 |
▶ | nenv->levels[i] = (dt > 0) ? dl / dt : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| nenv->levels[i] = (dt > 0) ? dl / dt : 0; |
4 |
0 |
4 |
|
| 1790 |
|
| } |
| 1791 |
|
| /* Last level: same as previous (no forward segment) */ |
| 1792 |
6 |
| nenv->levels[env->segments] = (env->segments > 0) |
| 1793 |
3 |
▶ | ? nenv->levels[env->segments - 1] : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| ? nenv->levels[env->segments - 1] : 0; |
3 |
0 |
3 |
|
| 1794 |
|
| |
| 1795 |
7 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
4 |
3 |
7 |
|
| 1796 |
4 |
| nenv->durations[i] = env->durations[i]; |
| 1797 |
4 |
| nenv->curves[i] = 1.0; |
| 1798 |
|
| } |
| 1799 |
3 |
| nenv->total_duration = env->total_duration; |
| 1800 |
|
| |
| 1801 |
3 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1802 |
|
| OUTPUT: |
| 1803 |
|
| RETVAL |
| 1804 |
|
| |
| 1805 |
|
| SV * |
| 1806 |
|
| integrate(self) |
| 1807 |
|
| SV *self |
| 1808 |
|
| PREINIT: |
| 1809 |
|
| SegmentedEnvelope *env, *nenv; |
| 1810 |
|
| int i; |
| 1811 |
|
| double accum; |
| 1812 |
|
| CODE: |
| 1813 |
3 |
| env = get_envelope(aTHX_ self, "integrate"); |
| 1814 |
|
| |
| 1815 |
3 |
| nenv = new_envelope(); |
| 1816 |
3 |
| copy_flags(nenv, env); |
| 1817 |
3 |
| nenv->segments = env->segments; |
| 1818 |
3 |
| Newx(nenv->levels, env->segments + 1, double); |
| 1819 |
3 |
| Newx(nenv->durations, env->segments, double); |
| 1820 |
3 |
| Newx(nenv->curves, env->segments, double); |
| 1821 |
|
| |
| 1822 |
|
| /* Cumulative trapezoidal integration */ |
| 1823 |
3 |
| accum = 0; |
| 1824 |
3 |
| nenv->levels[0] = 0; |
| 1825 |
7 |
▶ | for (i = 0; i < env->segments; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) { |
4 |
3 |
7 |
|
| 1826 |
4 |
| accum += (env->levels[i] + env->levels[i+1]) * 0.5 * env->durations[i]; |
| 1827 |
4 |
| nenv->levels[i+1] = accum; |
| 1828 |
4 |
| nenv->durations[i] = env->durations[i]; |
| 1829 |
4 |
| nenv->curves[i] = 1.0; |
| 1830 |
|
| } |
| 1831 |
3 |
| nenv->total_duration = env->total_duration; |
| 1832 |
|
| |
| 1833 |
3 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1834 |
|
| OUTPUT: |
| 1835 |
|
| RETVAL |
| 1836 |
|
| |
| 1837 |
|
| SV * |
| 1838 |
|
| from_samples(...) |
| 1839 |
|
| PREINIT: |
| 1840 |
|
| AV *samples_av; |
| 1841 |
|
| SegmentedEnvelope *env; |
| 1842 |
|
| int n, i; |
| 1843 |
|
| double seg_dur, dur; |
| 1844 |
|
| SV *samples_ref; |
| 1845 |
6 |
| int arg_start = 2; |
| 1846 |
|
| CODE: |
| 1847 |
|
| /* Detect class method vs instance method vs function call */ |
| 1848 |
6 |
▶ | if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
6 |
0 |
6 |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
5 |
1 |
6 |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
5 |
0 |
5 |
| if (items >= 3 && (sv_isobject(ST(0)) || (SvPOK(ST(0)) && !SvROK(ST(0))))) { |
5 |
0 |
5 |
|
| 1849 |
6 |
| samples_ref = ST(1); |
| 1850 |
6 |
| dur = SvNV(ST(2)); |
| 1851 |
6 |
| arg_start = 3; |
| 1852 |
0 |
▶ | } else if (items >= 2 && !sv_isobject(ST(0))) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (items >= 2 && !sv_isobject(ST(0))) { |
0 |
0 |
0 |
| } else if (items >= 2 && !sv_isobject(ST(0))) { |
0 |
0 |
0 |
|
| 1853 |
0 |
| samples_ref = ST(0); |
| 1854 |
0 |
| dur = SvNV(ST(1)); |
| 1855 |
0 |
| arg_start = 2; |
| 1856 |
|
| } else { |
| 1857 |
0 |
| croak("Usage: from_samples(\\@values, $duration)"); |
| 1858 |
|
| } |
| 1859 |
|
| |
| 1860 |
6 |
▶ | if (!SvROK(samples_ref) || SvTYPE(SvRV(samples_ref)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(samples_ref) || SvTYPE(SvRV(samples_ref)) != SVt_PVAV) |
6 |
0 |
6 |
| if (!SvROK(samples_ref) || SvTYPE(SvRV(samples_ref)) != SVt_PVAV) |
0 |
6 |
6 |
|
| 1861 |
0 |
| croak("from_samples: samples must be an array reference"); |
| 1862 |
|
| |
| 1863 |
6 |
| samples_av = (AV*)SvRV(samples_ref); |
| 1864 |
6 |
| n = av_len(samples_av) + 1; |
| 1865 |
6 |
▶ | if (n < 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 2) |
1 |
5 |
6 |
|
| 1866 |
1 |
| croak("from_samples requires at least 2 samples"); |
| 1867 |
5 |
▶ | if (dur <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dur <= 0) |
1 |
4 |
5 |
|
| 1868 |
1 |
| croak("from_samples: duration must be positive"); |
| 1869 |
|
| |
| 1870 |
4 |
| seg_dur = dur / (n - 1); |
| 1871 |
|
| |
| 1872 |
4 |
| env = new_envelope(); |
| 1873 |
4 |
| env->segments = n - 1; |
| 1874 |
4 |
| Newx(env->levels, n, double); |
| 1875 |
4 |
| Newx(env->durations, n - 1, double); |
| 1876 |
4 |
| Newx(env->curves, n - 1, double); |
| 1877 |
|
| |
| 1878 |
20 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
16 |
4 |
20 |
|
| 1879 |
16 |
| env->levels[i] = av_fetch_nv(samples_av, i, 0.0); |
| 1880 |
16 |
▶ | for (i = 0; i < n - 1; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n - 1; i++) { |
12 |
4 |
16 |
|
| 1881 |
12 |
| env->durations[i] = seg_dur; |
| 1882 |
12 |
| env->curves[i] = 1.0; |
| 1883 |
|
| } |
| 1884 |
4 |
| env->total_duration = dur; |
| 1885 |
|
| |
| 1886 |
5 |
▶ | for (i = arg_start; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = arg_start; i < items; i += 2) { |
1 |
4 |
5 |
|
| 1887 |
1 |
▶ | if (i + 1 < items && SvPOK(ST(i))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i + 1 < items && SvPOK(ST(i))) { |
1 |
0 |
1 |
| if (i + 1 < items && SvPOK(ST(i))) { |
1 |
0 |
1 |
|
| 1888 |
1 |
| const char *key = SvPV_nolen(ST(i)); |
| 1889 |
1 |
| SV *val = ST(i + 1); |
| 1890 |
1 |
▶ | if (strEQ(key, "is_hold")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key, "is_hold")) |
1 |
0 |
1 |
|
| 1891 |
1 |
| env->is_hold = SvTRUE(val) ? 1 : 0; |
| 1892 |
0 |
▶ | else if (strEQ(key, "is_fold_over")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(key, "is_fold_over")) |
0 |
0 |
0 |
|
| 1893 |
0 |
| env->is_fold_over = SvTRUE(val) ? 1 : 0; |
| 1894 |
0 |
▶ | else if (strEQ(key, "is_wrap_neg")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(key, "is_wrap_neg")) |
0 |
0 |
0 |
|
| 1895 |
0 |
| env->is_wrap_neg = SvTRUE(val) ? 1 : 0; |
| 1896 |
|
| } |
| 1897 |
|
| } |
| 1898 |
|
| |
| 1899 |
4 |
| RETVAL = bless_envelope(aTHX_ env); |
| 1900 |
|
| OUTPUT: |
| 1901 |
|
| RETVAL |
| 1902 |
|
| |
| 1903 |
|
| SV * |
| 1904 |
|
| _raw_levels(self) |
| 1905 |
|
| SV *self |
| 1906 |
|
| PREINIT: |
| 1907 |
|
| SegmentedEnvelope *env; |
| 1908 |
|
| CODE: |
| 1909 |
1 |
| env = get_envelope(aTHX_ self, "_raw_levels"); |
| 1910 |
1 |
▶ | if (!env->levels || env->segments < 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!env->levels || env->segments < 1) |
1 |
0 |
1 |
| if (!env->levels || env->segments < 1) |
0 |
1 |
1 |
|
| 1911 |
0 |
| RETVAL = newSVpvn("", 0); |
| 1912 |
|
| else |
| 1913 |
1 |
| RETVAL = newSVpvn((const char *)env->levels, (env->segments + 1) * sizeof(double)); |
| 1914 |
|
| OUTPUT: |
| 1915 |
|
| RETVAL |
| 1916 |
|
| |
| 1917 |
|
| SV * |
| 1918 |
|
| _raw_table(self, size) |
| 1919 |
|
| SV *self |
| 1920 |
|
| int size |
| 1921 |
|
| PREINIT: |
| 1922 |
|
| SegmentedEnvelope *env; |
| 1923 |
|
| SegmentedEnvelope senv; |
| 1924 |
|
| double *buf; |
| 1925 |
|
| int i; |
| 1926 |
|
| double lp, p; |
| 1927 |
|
| CODE: |
| 1928 |
0 |
| env = get_envelope(aTHX_ self, "_raw_table"); |
| 1929 |
0 |
▶ | if (size < 1) size = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (size < 1) size = 1; |
0 |
0 |
0 |
|
| 1930 |
|
| |
| 1931 |
0 |
| memset(&senv, 0, sizeof(senv)); |
| 1932 |
0 |
| borrow_envelope(aTHX_ &senv, env); |
| 1933 |
|
| |
| 1934 |
0 |
| Newx(buf, size, double); |
| 1935 |
0 |
| lp = 1.0 / size; |
| 1936 |
0 |
▶ | for (i = 0; i < size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) { |
0 |
0 |
0 |
|
| 1937 |
0 |
| p = i * lp; |
| 1938 |
0 |
| buf[i] = envelope_at(aTHX_ &senv, p * env->total_duration); |
| 1939 |
|
| } |
| 1940 |
0 |
| te_free(senv.morpher_expr); |
| 1941 |
|
| |
| 1942 |
0 |
| RETVAL = newSVpvn((const char *)buf, size * sizeof(double)); |
| 1943 |
0 |
| Safefree(buf); |
| 1944 |
|
| OUTPUT: |
| 1945 |
|
| RETVAL |
| 1946 |
|
| |
| 1947 |
|
| SV * |
| 1948 |
|
| clone(self) |
| 1949 |
|
| SV *self |
| 1950 |
|
| PREINIT: |
| 1951 |
|
| SegmentedEnvelope *env, *nenv; |
| 1952 |
|
| CODE: |
| 1953 |
2 |
| env = get_envelope(aTHX_ self, "clone"); |
| 1954 |
2 |
| nenv = new_envelope(); |
| 1955 |
2 |
| copy_envelope(aTHX_ nenv, env); |
| 1956 |
2 |
| RETVAL = bless_envelope(aTHX_ nenv); |
| 1957 |
|
| OUTPUT: |
| 1958 |
|
| RETVAL |
| 1959 |
|
| |
| 1960 |
|
| double |
| 1961 |
|
| area(self) |
| 1962 |
|
| SV *self |
| 1963 |
|
| PREINIT: |
| 1964 |
|
| SegmentedEnvelope *env; |
| 1965 |
|
| int i; |
| 1966 |
3 |
▶ | double total = 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| double total = 0.0; |
3 |
0 |
3 |
|
| 1967 |
|
| CODE: |
| 1968 |
3 |
| env = get_envelope(aTHX_ self, "area"); |
| 1969 |
8 |
▶ | for (i = 0; i < env->segments; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < env->segments; i++) |
5 |
3 |
8 |
|
| 1970 |
5 |
| total += (env->levels[i] + env->levels[i+1]) * 0.5 * env->durations[i]; |
| 1971 |
3 |
▶ | RETVAL = total; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = total; |
0 |
3 |
3 |
|
| 1972 |
|
| OUTPUT: |
| 1973 |
|
| RETVAL |
| 1974 |
|
| |
| 1975 |
|
| MODULE = Math::SegmentedEnvelope PACKAGE = Math::SegmentedEnvelope::Static |
| 1976 |
|
| |
| 1977 |
|
| void |
| 1978 |
|
| DESTROY(self) |
| 1979 |
|
| SV *self |
| 1980 |
|
| PREINIT: |
| 1981 |
|
| SegmentedEnvelope *senv; |
| 1982 |
|
| CODE: |
| 1983 |
29 |
▶ | if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope::Static")) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope::Static")) { |
29 |
0 |
29 |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope::Static")) { |
29 |
0 |
29 |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope::Static")) { |
29 |
0 |
29 |
| if (self && SvROK(self) && sv_isobject(self) && sv_derived_from(self, "Math::SegmentedEnvelope::Static")) { |
29 |
0 |
29 |
|
| 1984 |
29 |
| senv = INT2PTR(SegmentedEnvelope*, SvIV(SvRV(self))); |
| 1985 |
29 |
| free_envelope(aTHX_ senv); |
| 1986 |
|
| } |
| 1987 |
|
| |
| 1988 |
|
| double |
| 1989 |
|
| call(self, t) |
| 1990 |
|
| SV *self |
| 1991 |
|
| double t |
| 1992 |
|
| PREINIT: |
| 1993 |
|
| SegmentedEnvelope *senv; |
| 1994 |
|
| CODE: |
| 1995 |
585 |
| senv = get_static_envelope(aTHX_ self, "call"); |
| 1996 |
585 |
| RETVAL = envelope_at(aTHX_ senv, t); |
| 1997 |
|
| OUTPUT: |
| 1998 |
|
| RETVAL |