<commit_before>/*
 * Copyright © 2013 Mozilla Foundation
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */
#undef NDEBUG
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "cubeb/cubeb.h"
#include "cubeb-internal.h"

#define NELEMS(x) ((int) (sizeof(x) / sizeof(x[0])))

cubeb_log_level g_log_level;
cubeb_log_callback g_log_callback;

struct cubeb {
  struct cubeb_ops * ops;
};

struct cubeb_stream {
  struct cubeb * context;
};

#if defined(USE_PULSE)
int pulse_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_JACK)
int jack_init (cubeb ** context, char const * context_name);
#endif
#if defined(USE_ALSA)
int alsa_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_AUDIOUNIT)
int audiounit_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_WINMM)
int winmm_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_WASAPI)
int wasapi_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_SNDIO)
int sndio_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_OPENSL)
int opensl_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_AUDIOTRACK)
int audiotrack_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_KAI)
int kai_init(cubeb ** context, char const * context_name);
#endif

static int
validate_stream_params(cubeb_stream_params * input_stream_params,
                       cubeb_stream_params * output_stream_params)
{
  XASSERT(input_stream_params || output_stream_params);
  if (output_stream_params) {
    if (output_stream_params->rate < 1000 || output_stream_params->rate > 192000 ||
        output_stream_params->channels < 1 || output_stream_params->channels > 8) {
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  }
  if (input_stream_params) {
    if (input_stream_params->rate < 1000 || input_stream_params->rate > 192000 ||
        input_stream_params->channels < 1 || input_stream_params->channels > 8) {
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  }
  // Rate and sample format must be the same for input and output, if using a
  // duplex stream
  if (input_stream_params && output_stream_params) {
    if (input_stream_params->rate != output_stream_params->rate  ||
        input_stream_params->format != output_stream_params->format) {
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  }

  cubeb_stream_params * params = input_stream_params ?
                                 input_stream_params : output_stream_params;

  switch (params->format) {
  case CUBEB_SAMPLE_S16LE:
  case CUBEB_SAMPLE_S16BE:
  case CUBEB_SAMPLE_FLOAT32LE:
  case CUBEB_SAMPLE_FLOAT32BE:
    return CUBEB_OK;
  }

  return CUBEB_ERROR_INVALID_FORMAT;
}

static int
validate_latency(int latency)
{
  if (latency < 1 || latency > 96000) {
    return CUBEB_ERROR_INVALID_PARAMETER;
  }
  return CUBEB_OK;
}

int
cubeb_init(cubeb ** context, char const * context_name)
{
  int (* init[])(cubeb **, char const *) = {
#if defined(USE_JACK)
    jack_init,
#endif
#if defined(USE_PULSE)
    pulse_init,
#endif
#if defined(USE_ALSA)
    alsa_init,
#endif
#if defined(USE_AUDIOUNIT)
    audiounit_init,
#endif
#if defined(USE_WASAPI)
    wasapi_init,
#endif
#if defined(USE_WINMM)
    winmm_init,
#endif
#if defined(USE_SNDIO)
    sndio_init,
#endif
#if defined(USE_OPENSL)
    opensl_init,
#endif
#if defined(USE_AUDIOTRACK)
    audiotrack_init,<commit_msg>Move jack_init below pulse_init in cubeb.c.<commit_after>/*
 * Copyright © 2013 Mozilla Foundation
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */
#undef NDEBUG
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "cubeb/cubeb.h"
#include "cubeb-internal.h"

#define NELEMS(x) ((int) (sizeof(x) / sizeof(x[0])))

cubeb_log_level g_log_level;
cubeb_log_callback g_log_callback;

struct cubeb {
  struct cubeb_ops * ops;
};

struct cubeb_stream {
  struct cubeb * context;
};

#if defined(USE_PULSE)
int pulse_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_JACK)
int jack_init (cubeb ** context, char const * context_name);
#endif
#if defined(USE_ALSA)
int alsa_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_AUDIOUNIT)
int audiounit_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_WINMM)
int winmm_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_WASAPI)
int wasapi_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_SNDIO)
int sndio_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_OPENSL)
int opensl_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_AUDIOTRACK)
int audiotrack_init(cubeb ** context, char const * context_name);
#endif
#if defined(USE_KAI)
int kai_init(cubeb ** context, char const * context_name);
#endif

static int
validate_stream_params(cubeb_stream_params * input_stream_params,
                       cubeb_stream_params * output_stream_params)
{
  XASSERT(input_stream_params || output_stream_params);
  if (output_stream_params) {
    if (output_stream_params->rate < 1000 || output_stream_params->rate > 192000 ||
        output_stream_params->channels < 1 || output_stream_params->channels > 8) {
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  }
  if (input_stream_params) {
    if (input_stream_params->rate < 1000 || input_stream_params->rate > 192000 ||
        input_stream_params->channels < 1 || input_stream_params->channels > 8) {
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  }
  // Rate and sample format must be the same for input and output, if using a
  // duplex stream
  if (input_stream_params && output_stream_params) {
    if (input_stream_params->rate != output_stream_params->rate  ||
        input_stream_params->format != output_stream_params->format) {
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  }

  cubeb_stream_params * params = input_stream_params ?
                                 input_stream_params : output_stream_params;

  switch (params->format) {
  case CUBEB_SAMPLE_S16LE:
  case CUBEB_SAMPLE_S16BE:
  case CUBEB_SAMPLE_FLOAT32LE:
  case CUBEB_SAMPLE_FLOAT32BE:
    return CUBEB_OK;
  }

  return CUBEB_ERROR_INVALID_FORMAT;
}

static int
validate_latency(int latency)
{
  if (latency < 1 || latency > 96000) {
    return CUBEB_ERROR_INVALID_PARAMETER;
  }
  return CUBEB_OK;
}

int
cubeb_init(cubeb ** context, char const * context_name)
{
  int (* init[])(cubeb **, char const *) = {
#if defined(USE_PULSE)
    pulse_init,
#endif
#if defined(USE_JACK)
    jack_init,
#endif
#if defined(USE_ALSA)
    alsa_init,
#endif
#if defined(USE_AUDIOUNIT)
    audiounit_init,
#endif
#if defined(USE_WASAPI)
    wasapi_init,
#endif
#if defined(USE_WINMM)
    winmm_init,
#endif
#if defined(USE_SNDIO)
    sndio_init,
#endif
#if defined(USE_OPENSL)
    opensl_init,
#endif
#if defined(USE_AUDIOTRACK)
    audiotrack_init,
{'fraction_non_alphanumeric': 0.1041018387553041, 'fraction_numerical': 0.012164073550212163, 'mean_word_length': 4.567716535433071, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 45, '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': '7074994', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2528, 'n_words': 568}