<commit_before>/*
 *  soundpatty.cpp
 *
 *  Copyright (c) 2010 <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "soundpatty.h"
#include "input.h"

void SoundPatty::dump_out(const treshold_t args) { // STATIC
    printf ("%d;%.6f;%.6f\n", args.r, args.place, args.sec);
};


void SoundPatty::setAction(int action, char * cfg, void (*fn)(double)) {
    _action = action;
    read_captured_values(cfg);
    _callback = fn; // What to do when we catch a pattern
};


void SoundPatty::setAction(int action) {
    _action = action;
};


SoundPatty::SoundPatty(all_cfg_t all_cfg) { 
	cfg = all_cfg.first;
	volume = all_cfg.second;
    gSCounter = gMCounter = 0;
};


all_cfg_t SoundPatty::read_cfg (const char * filename) {
	map<string,double> cfg;
	vector<sVolumes> volume;
    ifstream file;
    file.open(filename);
    string line;
    int x;
    while (! file.eof() ) {
        getline(file, line);
        x = line.find(":");
        if (x == -1) break; // Last line, exit
        istringstream i(line.substr(x+2));
        double tmp; i >> tmp;
        cfg[line.substr(0,x)] = tmp;
    }

    sVolumes tmp;
    tmp.head = tmp.tail = tmp.max = tmp.min = tmp.proc = 0;
    volume.assign(cfg.size(), tmp); // Assign a bit more then nescesarry
    int max_index = 0; // Number of different tresholds
    for(map<string, double>::iterator C = cfg.begin(); C != cfg.end(); C++) {
        // Failed to use boost::regex :(
        if (C->first.find("treshold") == 0) {
            istringstream tmp(C->first.substr(8));
            int i; tmp >> i;
            max_index = max(max_index, i);
            if (C->first.find("_min") != string::npos) {
                volume[i].min = C->second;
            } else {
                volume[i].max = C->second;
            }
        }
    }
    volume.assign(volume.begin(), volume.begin()+max_index+1);
	return all_cfg_t(cfg, volume);
};


int SoundPatty::setInput(const int source_app, void * input_params) {
    if (0 <= source_app && source_app <= 2) {
        this->source_app = source_app;
    }
    switch(this->source_app) {
        case SRC_WAV:
            _input = new WavInput(this, input_params);
            break;
        case SRC_JACK_ONE:
            _input = new JackInput(this, input_params);
            break;
		case SRC_JACK_AUTO:
			_input = new JackInput(this, input_params);
			break;
    }
    return 0;
};

void SoundPatty::go() {
    string which_timeout (_action == ACTION_DUMP ? "sampletimeout" : "catchtimeout");
    buffer_t buf;

    while (_input->giveInput(&buf) != 0) { // Have pointer to data
        treshold_t ret;

        for (unsigned int i = 0; i < buf.nframes; gSCounter++, i++) {
            jack_default_audio_sample_t cur = buf.buf[i]<0?-buf.buf[i]:buf.buf[i];
            if (search_patterns(cur, &ret))
            {
                if (_action == ACTION_DUMP) {
                    SoundPatty::dump_out(ret);
                }
                if (_action == ACTION_CATCH) {
                    SoundPatty::do_checking(ret);
                }
            }
        }

        if ((double)gSCounter/_input->SAMPLE_RATE > cfg[which_timeout]) {
            return;
        }
    }
};


void SoundPatty::read_captured_values(const char * filename) {
        ifstream file;
        file.open(filename);
        string line;
        for (int i = 0; !file.eof(); i++) {
            getline(file, line);
            if (line.size() == 0) break;
            vector<string> numbers = explode(";", line);
            istringstream num(numbers[0]);
            istringstream place(numbers[1]);
            istringstream range(numbers[2]);

            double tmp2;
            pair<pair<int, Range>,valsitm_t > tmp;
            num >> tmp2; tmp.first.first = tmp2; // Index in volume
            range >> tmp2; tmp.first.second = Range(tmp2);
            place >> tmp.second.place; // Place in the stream
            tmp.second.c = i; // Counter in the stream
            vals.insert(tmp);<commit_msg>Add timeout message (need feedback)<commit_after>/*
 *  soundpatty.cpp
 *
 *  Copyright (c) 2010 <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "soundpatty.h"
#include "input.h"

void SoundPatty::dump_out(const treshold_t args) { // STATIC
    printf ("%d;%.6f;%.6f\n", args.r, args.place, args.sec);
};


void SoundPatty::setAction(int action, char * cfg, void (*fn)(double)) {
    _action = action;
    read_captured_values(cfg);
    _callback = fn; // What to do when we catch a pattern
};


void SoundPatty::setAction(int action) {
    _action = action;
};


SoundPatty::SoundPatty(all_cfg_t all_cfg) { 
	cfg = all_cfg.first;
	volume = all_cfg.second;
    gSCounter = gMCounter = 0;
};


all_cfg_t SoundPatty::read_cfg (const char * filename) {
	map<string,double> cfg;
	vector<sVolumes> volume;
    ifstream file;
    file.open(filename);
    string line;
    int x;
    while (! file.eof() ) {
        getline(file, line);
        x = line.find(":");
        if (x == -1) break; // Last line, exit
        istringstream i(line.substr(x+2));
        double tmp; i >> tmp;
        cfg[line.substr(0,x)] = tmp;
    }

    sVolumes tmp;
    tmp.head = tmp.tail = tmp.max = tmp.min = tmp.proc = 0;
    volume.assign(cfg.size(), tmp); // Assign a bit more then nescesarry
    int max_index = 0; // Number of different tresholds
    for(map<string, double>::iterator C = cfg.begin(); C != cfg.end(); C++) {
        // Failed to use boost::regex :(
        if (C->first.find("treshold") == 0) {
            istringstream tmp(C->first.substr(8));
            int i; tmp >> i;
            max_index = max(max_index, i);
            if (C->first.find("_min") != string::npos) {
                volume[i].min = C->second;
            } else {
                volume[i].max = C->second;
            }
        }
    }
    volume.assign(volume.begin(), volume.begin()+max_index+1);
	return all_cfg_t(cfg, volume);
};


int SoundPatty::setInput(const int source_app, void * input_params) {
    if (0 <= source_app && source_app <= 2) {
        this->source_app = source_app;
    }
    switch(this->source_app) {
        case SRC_WAV:
            _input = new WavInput(this, input_params);
            break;
        case SRC_JACK_ONE:
            _input = new JackInput(this, input_params);
            break;
		case SRC_JACK_AUTO:
			_input = new JackInput(this, input_params);
			break;
    }
    return 0;
};

void SoundPatty::go() {
    string which_timeout (_action == ACTION_DUMP ? "sampletimeout" : "catchtimeout");
    buffer_t buf;

    while (_input->giveInput(&buf) != 0) { // Have pointer to data
        treshold_t ret;

        for (unsigned int i = 0; i < buf.nframes; gSCounter++, i++) {
            jack_default_audio_sample_t cur = buf.buf[i]<0?-buf.buf[i]:buf.buf[i];
            if (search_patterns(cur, &ret))
            {
                if (_action == ACTION_DUMP) {
                    SoundPatty::dump_out(ret);
                }
                if (_action == ACTION_CATCH) {
                    SoundPatty::do_checking(ret);
                }
            }
        }

        if ((double)gSCounter/_input->SAMPLE_RATE > cfg[which_timeout]) {
            printf ("Timed out. Seconds passed: %.6f\n", (double)gSCounter/_input->SAMPLE_RATE);
            return;
        }
    }
};


void SoundPatty::read_captured_values(const char * filename) {
        ifstream file;
        file.open(filename);
        string line;
        for (int i = 0; !file.eof(); i++) {
            getline(file, line);
            if (line.size() == 0) break;
            vector<string> numbers = explode(";", line);
            istringstream num(numbers[0]);
            istringstream place(numbers[1]);
            istringstream range(numbers[2]);

            double tmp2;
            pair<pair<int, Range>,valsitm_t > tmp;
            num >> tmp2; tmp.first.first = tmp2; // Index in volume
            range >> tmp2; tmp.first.second = Range(tmp2);
            place >> tmp.second.place; // Place in the stream
            tmp.second.c = i; // Counter in the stream
            vals.insert(tmp);
{'fraction_non_alphanumeric': 0.13185364245687287, 'fraction_numerical': 0.00714207229974728, 'mean_word_length': 2.1992970123022846, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7411062', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2861, 'n_words': 758}