<commit_before>/*
 * noiseprof - Noise Profiling Effect. 
 *
 * Written by <NAME> (<EMAIL>)
 *
 * Copyright 1999 <NAME> and others
 * This file is part of SoX.

 * SoX is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#include "noisered.h"

#include <assert.h>
#include <string.h>
#include <errno.h>

typedef struct chandata {
    float *sum;
    int   *profilecount;

    float *window;
} chandata_t;

typedef struct profdata {
    char* output_filename;
    FILE* output_file;

    chandata_t *chandata;
    st_size_t bufdata;
} * profdata_t;

/*
 * Get the filename, if any. We don't open it until st_noiseprof_start.
 */
int st_noiseprof_getopts(eff_t effp, int n, char **argv) 
{
    profdata_t data = (profdata_t) effp->priv;

    if (n == 1) {
        data->output_filename = argv[0];
    } else if (n > 1) {
        st_fail("Usage: noiseprof [filename]");
        return (ST_EOF);
    }

    return (ST_SUCCESS);
}

/*
 * Prepare processing.
 * Do all initializations.
 */
int st_noiseprof_start(eff_t effp)
{
    profdata_t data = (profdata_t) effp->priv;
    int channels = effp->ininfo.channels;
    int i;
   
    if (data->output_filename != NULL) {
        data->output_file = fopen(data->output_filename, "w");
        if (data->output_file == NULL) {
            st_fail("Couldn't open output file %s: %s",
                    data->output_filename, strerror(errno));            
        }
    } else {
        /* FIXME: We should detect output to stdout, and redirect to stderr. */
        data->output_file = stderr;
    }

    data->chandata = (chandata_t*)calloc(channels, sizeof(*(data->chandata)));
    for (i = 0; i < channels; i ++) {
        data->chandata[i].sum          =
            (float*)calloc(FREQCOUNT, sizeof(float));
        data->chandata[i].profilecount =
            (int*)calloc(FREQCOUNT, sizeof(int));
        data->chandata[i].window       =
            (float*)calloc(WINDOWSIZE, sizeof(float));
    }
    data->bufdata = 0;
    return (ST_SUCCESS);
}

/* Collect statistics from the complete window on channel chan. */
static void collect_data(profdata_t data, chandata_t* chan) {
    float *out = (float*)calloc(FREQCOUNT, sizeof(float));

    int i;

    PowerSpectrum(WINDOWSIZE, chan->window, out);

    for (i = 0; i < FREQCOUNT; i ++) {
        float value = log(out[i]);
        if (finite(value)) {
            chan->sum[i] += value;
            chan->profilecount[i] ++;
        }
    }

    free(out);
}<commit_msg>Avoid using non-ANSI library routine.<commit_after>/*
 * noiseprof - Noise Profiling Effect. 
 *
 * Written by <NAME> (<EMAIL>)
 *
 * Copyright 1999 <NAME> and others
 * This file is part of SoX.

 * SoX is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#include "noisered.h"

#include <assert.h>
#include <string.h>
#include <errno.h>

typedef struct chandata {
    float *sum;
    int   *profilecount;

    float *window;
} chandata_t;

typedef struct profdata {
    char* output_filename;
    FILE* output_file;

    chandata_t *chandata;
    st_size_t bufdata;
} * profdata_t;

/*
 * Get the filename, if any. We don't open it until st_noiseprof_start.
 */
int st_noiseprof_getopts(eff_t effp, int n, char **argv) 
{
    profdata_t data = (profdata_t) effp->priv;

    if (n == 1) {
        data->output_filename = argv[0];
    } else if (n > 1) {
        st_fail("Usage: noiseprof [filename]");
        return (ST_EOF);
    }

    return (ST_SUCCESS);
}

/*
 * Prepare processing.
 * Do all initializations.
 */
int st_noiseprof_start(eff_t effp)
{
    profdata_t data = (profdata_t) effp->priv;
    int channels = effp->ininfo.channels;
    int i;
   
    if (data->output_filename != NULL) {
        data->output_file = fopen(data->output_filename, "w");
        if (data->output_file == NULL) {
            st_fail("Couldn't open output file %s: %s",
                    data->output_filename, strerror(errno));            
        }
    } else {
        /* FIXME: We should detect output to stdout, and redirect to stderr. */
        data->output_file = stderr;
    }

    data->chandata = (chandata_t*)calloc(channels, sizeof(*(data->chandata)));
    for (i = 0; i < channels; i ++) {
        data->chandata[i].sum          =
            (float*)calloc(FREQCOUNT, sizeof(float));
        data->chandata[i].profilecount =
            (int*)calloc(FREQCOUNT, sizeof(int));
        data->chandata[i].window       =
            (float*)calloc(WINDOWSIZE, sizeof(float));
    }
    data->bufdata = 0;
    return (ST_SUCCESS);
}

/* Collect statistics from the complete window on channel chan. */
static void collect_data(profdata_t data, chandata_t* chan) {
    float *out = (float*)calloc(FREQCOUNT, sizeof(float));

    int i;

    PowerSpectrum(WINDOWSIZE, chan->window, out);

    for (i = 0; i < FREQCOUNT; i ++) {
        if (out[i] > 0) {
            float value = log(out[i]);
            chan->sum[i] += value;
            chan->profilecount[i] ++;
        }
    }

    free(out);
}
{'fraction_non_alphanumeric': 0.13205438629167443, 'fraction_numerical': 0.00428385174147886, 'mean_word_length': 2.549239920687376, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6669618', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1798, 'n_words': 481}