<commit_before>// ------------------------------------------------------------------------
// audioio-rtnull.cpp: Null audio object with realtime behaviour
// Copyright (C) 1999,2002 <NAME> (<EMAIL>)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#include <cmath>
#include <iostream>
#include <string>
#include <time.h>

#include <kvutils/dbc.h>
#include <kvutils/kvutils.h>

#include "audioio-device.h"
#include "audioio-rtnull.h"

#include "eca-error.h"
#include "eca-debug.h"

using std::cerr;
using std::endl;

/** 
 * Definitions from glibc's sys/time.h
 */

#ifndef timerclear
#define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
#endif

#ifndef timeradd
#define	timeradd(a, b, result)						      \
  do {									      \
    (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;			      \
    (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;			      \
    if ((result)->tv_usec >= 1000000)					      \
      {									      \
	++(result)->tv_sec;						      \
	(result)->tv_usec -= 1000000;					      \
      }									      \
  } while (0)
#endif

#ifndef timercmp
#define	timercmp(a, b, CMP) 						      \
  (((a)->tv_sec == (b)->tv_sec) ? 					      \
   ((a)->tv_usec CMP (b)->tv_usec) : 					      \
   ((a)->tv_sec CMP (b)->tv_sec))
#endif

#ifndef timersub
#define	timersub(a, b, result) \
do { \
	(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
	(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
	if ((result)->tv_usec < 0) { \
		--(result)->tv_sec; \
		(result)->tv_usec += 1000000; \
	} \
} while (0)
#endif

/**
 * Function definitions
 */

REALTIME_NULL::REALTIME_NULL(const std::string& name)
{
}

REALTIME_NULL::~REALTIME_NULL(void)
{ 
  if (is_open() == true && is_running()) stop();

  if (is_open() == true) {
    close();
  }
}

void REALTIME_NULL::open(void) throw (AUDIO_IO::SETUP_ERROR &)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) open");

  double t = static_cast<double>(buffersize()) / samples_per_second();

  buffer_length_rep.tv_sec = static_cast<time_t>(floor(t));
  buffer_length_rep.tv_usec = static_cast<long>((t - buffer_length_rep.tv_sec) * 1000000.0);

  total_buffers_rep = 2;
  if (max_buffers() == true) { 
    total_buffers_rep = 3;
  }
  total_buffer_length_rep.tv_sec = total_buffers_rep * buffer_length_rep.tv_sec;
  total_buffer_length_rep.tv_usec = total_buffers_rep * buffer_length_rep.tv_usec;

  AUDIO_IO_DEVICE::open();
}

void REALTIME_NULL::close(void)
{
  AUDIO_IO_DEVICE::close();
}

void REALTIME_NULL::prepare(void)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) prepare");

  timerclear(&data_processed_rep);

  AUDIO_IO_DEVICE::prepare();
}

void REALTIME_NULL::start(void)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) start");

  gettimeofday(&start_time_rep, NULL);

  AUDIO_IO_DEVICE::start();
}

void REALTIME_NULL::stop(void)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) stop");

  AUDIO_IO_DEVICE::stop();
}

/**
 * Calculates 'time_since_start_rep'.
 */
void REALTIME_NULL::calculate_device_position(void)
{
  struct timeval now;
  gettimeofday(&now, NULL);

  timersub(&now, &start_time_rep, &time_since_start_rep);
}

/**
 * Calculates 'avail_data_rep'.
 */
void REALTIME_NULL::calculate_available_data(void)
{ 
  if (io_mode() == io_read) {
    /* capture: device is always ahead */<commit_msg>Minor fixes; added missing const.<commit_after>// ------------------------------------------------------------------------
// audioio-rtnull.cpp: Null audio object with realtime behaviour
// Copyright (C) 1999,2002 <NAME> (<EMAIL>)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#include <cmath>
#include <iostream>
#include <string>
#include <time.h>

#include <kvutils/dbc.h>
#include <kvutils/kvutils.h>

#include "audioio-device.h"
#include "audioio-rtnull.h"

#include "eca-error.h"
#include "eca-debug.h"

using std::cerr;
using std::endl;

/** 
 * Definitions from glibc's sys/time.h
 */

#ifndef timerclear
#define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
#endif

#ifndef timeradd
#define	timeradd(a, b, result)						      \
  do {									      \
    (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;			      \
    (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;			      \
    if ((result)->tv_usec >= 1000000)					      \
      {									      \
	++(result)->tv_sec;						      \
	(result)->tv_usec -= 1000000;					      \
      }									      \
  } while (0)
#endif

#ifndef timercmp
#define	timercmp(a, b, CMP) 						      \
  (((a)->tv_sec == (b)->tv_sec) ? 					      \
   ((a)->tv_usec CMP (b)->tv_usec) : 					      \
   ((a)->tv_sec CMP (b)->tv_sec))
#endif

#ifndef timersub
#define	timersub(a, b, result) \
do { \
	(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
	(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
	if ((result)->tv_usec < 0) { \
		--(result)->tv_sec; \
		(result)->tv_usec += 1000000; \
	} \
} while (0)
#endif

/**
 * Function definitions
 */

REALTIME_NULL::REALTIME_NULL(const std::string& name)
{
}

REALTIME_NULL::~REALTIME_NULL(void)
{ 
  if (is_open() == true && is_running()) stop();

  if (is_open() == true) {
    close();
  }
}

void REALTIME_NULL::open(void) throw (AUDIO_IO::SETUP_ERROR &)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) open");

  double t = static_cast<double>(buffersize()) / samples_per_second();

  buffer_length_rep.tv_sec = static_cast<time_t>(floor(t));
  buffer_length_rep.tv_usec = static_cast<long>((t - buffer_length_rep.tv_sec) * 1000000.0);

  total_buffers_rep = 2;
  if (max_buffers() == true) { 
    total_buffers_rep = 3;
  }
  total_buffer_length_rep.tv_sec = total_buffers_rep * buffer_length_rep.tv_sec;
  total_buffer_length_rep.tv_usec = total_buffers_rep * buffer_length_rep.tv_usec;

  AUDIO_IO_DEVICE::open();
}

void REALTIME_NULL::close(void)
{
  AUDIO_IO_DEVICE::close();
}

void REALTIME_NULL::prepare(void)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) prepare");

  timerclear(&data_processed_rep);

  AUDIO_IO_DEVICE::prepare();
}

void REALTIME_NULL::start(void)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) start");

  gettimeofday(&start_time_rep, NULL);

  AUDIO_IO_DEVICE::start();
}

void REALTIME_NULL::stop(void)
{
  ecadebug->msg(ECA_DEBUG::user_objects, "(audioio-rtnull) stop");

  AUDIO_IO_DEVICE::stop();
}

/**
 * Calculates 'time_since_start_rep'.
 */
void REALTIME_NULL::calculate_device_position(void)
{
  struct timeval now;
  gettimeofday(&now, NULL);

  timersub(&now, &start_time_rep, &time_since_start_rep);
}

/**
 * Calculates 'avail_data_rep'.
 */
void REALTIME_NULL::calculate_available_data(void) const
{ 
  if (io_mode() == io_read) {
    /* capture: device is always ahead */
{'fraction_non_alphanumeric': 0.18568287319814317, 'fraction_numerical': 0.014170535059858295, 'mean_word_length': 3.04695996045477, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 87, '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': '219986', 'n_tokens_mistral': 3181, 'n_tokens_neox': 2913, 'n_words': 635}