<commit_before>
#include <string.h>

#include "mapper_internal.h"
#include "types_internal.h"
#include <mapper/mapper.h>
#include "operations.h"
#include "expression.h"

const char* mapper_clipping_type_strings[] =
{
    "none",        /* CT_NONE */
    "mute",        /* CT_MUTE */
    "clamp",       /* CT_CLAMP */
    "fold",        /* CT_FOLD */
    "wrap",        /* CT_WRAP */
};

const char* mapper_scaling_type_strings[] =
{
    "bypass",      /* SC_BYPASS */
    "linear",      /* SC_LINEAR */
    "expression",  /* SC_EXPRESSION */
    "calibrate",   /* SC_CALIBRATE */
    "mute",        /* SC_MUTE */
};

int mapper_mapping_perform(mapper_mapping mapping,
                            mapper_signal_value_t *from_value,
                            mapper_signal_value_t *to_value)
{
    int p, changed = 0;
    float v;
    error err = NO_ERR;


    p = mapping->history_pos;
    mapping->history_input[p] = from_value->f;
    v = mapping->history_input[p];
    
    if (mapping->props.muted) {
        return 0;
    }

    if (mapping->props.scaling == SC_BYPASS /*|| mapping->type==LINEAR */ ) {
        /*for (i=0; i < mapping->order_input; i++)
           v = mapping->history_input[(p+i)%5] * mapping->coef_input[i];

           for (i=0; i < mapping->order_output; i++)
           v = mapping->history_output[(p+i)%5] * mapping->coef_output[i]; */

        /*v=mapping->history_input[p]; */
        mapping->history_output[p] = v;

        --p;
        if (p < 0)
            p = MAX_HISTORY_ORDER;
    }


    else if (mapping->props.scaling == SC_EXPRESSION
             || mapping->props.scaling == SC_LINEAR) {
        v = EvalTree(mapping->expr_tree, mapping->history_input,
                     mapping->history_output, p, &err);
        mapping->history_output[p] = v;

        --p;
        if (p < 0)
            p = MAX_HISTORY_ORDER - 1;
    }

    else if (mapping->props.scaling == SC_CALIBRATE) {
        /* If calibration mode has just taken effect, first data
         * sample sets source min and max */
        if (mapping->calibrating) {
            mapping->props.range.src_min = from_value->f;
            mapping->props.range.src_max = from_value->f;
            mapping->props.range.known |=
                MAPPING_RANGE_SRC_MIN | MAPPING_RANGE_SRC_MAX;
            mapping->calibrating = 0;
            changed = 1;
        } else {
            if (from_value->f < mapping->props.range.src_min) {
                mapping->props.range.src_min = from_value->f;
                mapping->props.range.known |= MAPPING_RANGE_SRC_MIN;
                changed = 1;
            }
            if (from_value->f > mapping->props.range.src_max) {
                mapping->props.range.src_max = from_value->f;
                mapping->props.range.known |= MAPPING_RANGE_SRC_MAX;
                changed = 1;
            }
        }

        if (changed) {
            /* Need to arrange to send an admin bus message stating
             * new ranges and expression.  The expression has to be
             * modified to fit the range. */
            if (mapping->props.range.src_min == mapping->props.range.src_max) {
                free(mapping->props.expression);
                mapping->props.expression = (char*) malloc(100 * sizeof(char));
                snprintf(mapping->props.expression, 100, "y=%f",
                         mapping->props.range.src_min);
            } else if (mapping->props.range.known == MAPPING_RANGE_KNOWN
                       && mapping->props.range.src_min == mapping->props.range.dest_min
                       && mapping->props.range.src_max ==
                       mapping->props.range.dest_max)
                snprintf(mapping->props.expression, 100, "y=x");

            else if (mapping->props.range.known == MAPPING_RANGE_KNOWN) {
                float scale = (mapping->props.range.dest_min
                               - mapping->props.range.dest_max)
                    / (mapping->props.range.src_min - mapping->props.range.src_max);
                float offset =
                    (mapping->props.range.dest_max * mapping->props.range.src_min
                     - mapping->props.range.dest_min * mapping->props.range.src_max)
                    / (mapping->props.range.src_min - mapping->props.range.src_max);

                free(mapping->props.expression);
                mapping->props.expression = (char*) malloc(256 * sizeof(char));
                snprintf(mapping->props.expression, 256, "y=x*%g+%g",
                         scale, offset);
            }

            DeleteTree(mapping->expr_tree);
            Tree *T = NewTree();
            int success_tree = get_expr_Tree(T, mapping->props.expression);

            if (!success_tree)
                return;

            mapping->expr_tree = T;
        }

        v = EvalTree(mapping->expr_tree, mapping->history_input,
                     mapping->history_output, p, &err);<commit_msg>libmapper: fix a return value from mapper_mapping_perform().<commit_after>
#include <string.h>

#include "mapper_internal.h"
#include "types_internal.h"
#include <mapper/mapper.h>
#include "operations.h"
#include "expression.h"

const char* mapper_clipping_type_strings[] =
{
    "none",        /* CT_NONE */
    "mute",        /* CT_MUTE */
    "clamp",       /* CT_CLAMP */
    "fold",        /* CT_FOLD */
    "wrap",        /* CT_WRAP */
};

const char* mapper_scaling_type_strings[] =
{
    "bypass",      /* SC_BYPASS */
    "linear",      /* SC_LINEAR */
    "expression",  /* SC_EXPRESSION */
    "calibrate",   /* SC_CALIBRATE */
    "mute",        /* SC_MUTE */
};

int mapper_mapping_perform(mapper_mapping mapping,
                            mapper_signal_value_t *from_value,
                            mapper_signal_value_t *to_value)
{
    int p, changed = 0;
    float v;
    error err = NO_ERR;


    p = mapping->history_pos;
    mapping->history_input[p] = from_value->f;
    v = mapping->history_input[p];
    
    if (mapping->props.muted) {
        return 0;
    }

    if (mapping->props.scaling == SC_BYPASS /*|| mapping->type==LINEAR */ ) {
        /*for (i=0; i < mapping->order_input; i++)
           v = mapping->history_input[(p+i)%5] * mapping->coef_input[i];

           for (i=0; i < mapping->order_output; i++)
           v = mapping->history_output[(p+i)%5] * mapping->coef_output[i]; */

        /*v=mapping->history_input[p]; */
        mapping->history_output[p] = v;

        --p;
        if (p < 0)
            p = MAX_HISTORY_ORDER;
    }


    else if (mapping->props.scaling == SC_EXPRESSION
             || mapping->props.scaling == SC_LINEAR) {
        v = EvalTree(mapping->expr_tree, mapping->history_input,
                     mapping->history_output, p, &err);
        mapping->history_output[p] = v;

        --p;
        if (p < 0)
            p = MAX_HISTORY_ORDER - 1;
    }

    else if (mapping->props.scaling == SC_CALIBRATE) {
        /* If calibration mode has just taken effect, first data
         * sample sets source min and max */
        if (mapping->calibrating) {
            mapping->props.range.src_min = from_value->f;
            mapping->props.range.src_max = from_value->f;
            mapping->props.range.known |=
                MAPPING_RANGE_SRC_MIN | MAPPING_RANGE_SRC_MAX;
            mapping->calibrating = 0;
            changed = 1;
        } else {
            if (from_value->f < mapping->props.range.src_min) {
                mapping->props.range.src_min = from_value->f;
                mapping->props.range.known |= MAPPING_RANGE_SRC_MIN;
                changed = 1;
            }
            if (from_value->f > mapping->props.range.src_max) {
                mapping->props.range.src_max = from_value->f;
                mapping->props.range.known |= MAPPING_RANGE_SRC_MAX;
                changed = 1;
            }
        }

        if (changed) {
            /* Need to arrange to send an admin bus message stating
             * new ranges and expression.  The expression has to be
             * modified to fit the range. */
            if (mapping->props.range.src_min == mapping->props.range.src_max) {
                free(mapping->props.expression);
                mapping->props.expression = (char*) malloc(100 * sizeof(char));
                snprintf(mapping->props.expression, 100, "y=%f",
                         mapping->props.range.src_min);
            } else if (mapping->props.range.known == MAPPING_RANGE_KNOWN
                       && mapping->props.range.src_min == mapping->props.range.dest_min
                       && mapping->props.range.src_max ==
                       mapping->props.range.dest_max)
                snprintf(mapping->props.expression, 100, "y=x");

            else if (mapping->props.range.known == MAPPING_RANGE_KNOWN) {
                float scale = (mapping->props.range.dest_min
                               - mapping->props.range.dest_max)
                    / (mapping->props.range.src_min - mapping->props.range.src_max);
                float offset =
                    (mapping->props.range.dest_max * mapping->props.range.src_min
                     - mapping->props.range.dest_min * mapping->props.range.src_max)
                    / (mapping->props.range.src_min - mapping->props.range.src_max);

                free(mapping->props.expression);
                mapping->props.expression = (char*) malloc(256 * sizeof(char));
                snprintf(mapping->props.expression, 256, "y=x*%g+%g",
                         scale, offset);
            }

            DeleteTree(mapping->expr_tree);
            Tree *T = NewTree();
            int success_tree = get_expr_Tree(T, mapping->props.expression);

            if (!success_tree)
                return 0;

            mapping->expr_tree = T;
        }

        v = EvalTree(mapping->expr_tree, mapping->history_input,
                     mapping->history_output, p, &err);
{'fraction_non_alphanumeric': 0.13197867417764814, 'fraction_numerical': 0.005733829594608188, 'mean_word_length': 1.729069448256931, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3894844', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2910, 'n_words': 530}