<commit_before>/*
 * Copyright (c) 2014, <NAME>
 * All rights reserved.
 *
 * This software is licensed under the terms and conditions of the
 * Simplified BSD License. See license.txt for details.
 *
 */
#include <inttypes.h>           // PRId64 and PRIu64
#include <portaudio.h>
#include <stdio.h>
#include <stdlib.h>

#include "audio_util.h"


#define SAMPLE_RATE 48000
#define CHANNELS    1
#define FRAME_SIZE  2 * CHANNELS        /* 2 bytes / sample */
#define BUFFER_LEN_SEC 0.1
#define BUFFER_SIZE (SAMPLE_RATE * FRAME_SIZE) * BUFFER_LEN_SEC


int audio_reader_cb(const void *input, void *output, unsigned long frame_cnt,
                    const PaStreamCallbackTimeInfo * timeInfo,
                    PaStreamCallbackFlags statusFlags, void *user_data)
{
    (void)output;
    (void)timeInfo;

    audio_t        *audio = (audio_t *) user_data;
    PaStreamCallbackResult result = paContinue;
    unsigned long   byte_cnt = frame_cnt * FRAME_SIZE;

    if (byte_cnt + ring_buffer_count(audio->rb) > ring_buffer_size(audio->rb))
        audio->overflows++;

    ring_buffer_write(audio->rb, (unsigned char *)input, byte_cnt);

    audio->frames_tot += frame_cnt;

    if (audio->frames_avg)
        audio->frames_avg = (audio->frames_avg + frame_cnt) / 2;
    else
        audio->frames_avg = frame_cnt;

    if (statusFlags)
        audio->status_errors++;

    return result;
}

audio_t        *audio_init(int index)
{
    audio_t        *audio;
    PaError         error;
    int             input_rate = SAMPLE_RATE;


    error = Pa_Initialize();  /** FIXME: make it quiet */
    if (error != paNoError)
    {
        fprintf(stderr, "Error initializing audio %d: %s", error,
                Pa_GetErrorText(error));
        return NULL;
    }

    audio = (audio_t *) malloc(sizeof(audio_t));
    if (!audio)
        return NULL;

    if (index < 0)
    {
        audio->input_param.device = Pa_GetDefaultInputDevice();
        fprintf(stderr, "Audio device not specified. Default is %d\n",
                audio->input_param.device);
    }
    else
    {
        audio->input_param.device = index;
    }

    /** FIXME: ring buffer assumes 1 channel */
    audio->input_param.channelCount =
        (audio->device_info->maxInputChannels == 1) ? 1 : 2;
    fprintf(stderr, "Number of channels: %d\n",
            audio->input_param.channelCount);

    audio->input_param.sampleFormat = paInt16;
    audio->input_param.hostApiSpecificStreamInfo = NULL;
    audio->input_param.suggestedLatency = 0.01f;        //audio->device_info->defaultLowInputLatency;

    audio->device_info = Pa_GetDeviceInfo(audio->input_param.device);
    fprintf(stderr, "Using audio device no. %d: %s\n",<commit_msg>Use higher audio latency for now.<commit_after>/*
 * Copyright (c) 2014, <NAME>
 * All rights reserved.
 *
 * This software is licensed under the terms and conditions of the
 * Simplified BSD License. See license.txt for details.
 *
 */
#include <inttypes.h>           // PRId64 and PRIu64
#include <portaudio.h>
#include <stdio.h>
#include <stdlib.h>

#include "audio_util.h"


#define SAMPLE_RATE 48000
#define CHANNELS    1
#define FRAME_SIZE  2 * CHANNELS        /* 2 bytes / sample */
#define BUFFER_LEN_SEC 0.1
#define BUFFER_SIZE (SAMPLE_RATE * FRAME_SIZE) * BUFFER_LEN_SEC


int audio_reader_cb(const void *input, void *output, unsigned long frame_cnt,
                    const PaStreamCallbackTimeInfo * timeInfo,
                    PaStreamCallbackFlags statusFlags, void *user_data)
{
    (void)output;
    (void)timeInfo;

    audio_t        *audio = (audio_t *) user_data;
    PaStreamCallbackResult result = paContinue;
    unsigned long   byte_cnt = frame_cnt * FRAME_SIZE;

    if (byte_cnt + ring_buffer_count(audio->rb) > ring_buffer_size(audio->rb))
        audio->overflows++;

    ring_buffer_write(audio->rb, (unsigned char *)input, byte_cnt);

    audio->frames_tot += frame_cnt;

    if (audio->frames_avg)
        audio->frames_avg = (audio->frames_avg + frame_cnt) / 2;
    else
        audio->frames_avg = frame_cnt;

    if (statusFlags)
        audio->status_errors++;

    return result;
}

audio_t        *audio_init(int index)
{
    audio_t        *audio;
    PaError         error;
    int             input_rate = SAMPLE_RATE;


    error = Pa_Initialize();  /** FIXME: make it quiet */
    if (error != paNoError)
    {
        fprintf(stderr, "Error initializing audio %d: %s", error,
                Pa_GetErrorText(error));
        return NULL;
    }

    audio = (audio_t *) malloc(sizeof(audio_t));
    if (!audio)
        return NULL;

    audio->status_errors = 0;
    audio->overflows = 0;
    audio->frames_avg = 0;
    audio->frames_tot = 0;

    if (index < 0)
    {
        audio->input_param.device = Pa_GetDefaultInputDevice();
        fprintf(stderr, "Audio device not specified. Default is %d\n",
                audio->input_param.device);
    }
    else
    {
        audio->input_param.device = index;
    }

    /** FIXME: ring buffer assumes 1 channel */
    audio->input_param.channelCount = CHANNELS;
    fprintf(stderr, "Number of channels: %d\n",
            audio->input_param.channelCount);

    audio->input_param.sampleFormat = paInt16;
    audio->input_param.hostApiSpecificStreamInfo = NULL;
    audio->input_param.suggestedLatency = 0.04f;        //audio->device_info->defaultLowInputLatency;

    audio->device_info = Pa_GetDeviceInfo(audio->input_param.device);
    fprintf(stderr, "Using audio device no. %d: %s\n",
{'fraction_non_alphanumeric': 0.1133964324717874, 'fraction_numerical': 0.010738987986894794, 'mean_word_length': 2.6978465679676984, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 65, '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': '3900086', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1719, 'n_words': 413}