<commit_before>/*
 * Copyright © 2012 <NAME>
 * Copyright © 2013 <NAME>
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */

#define _BSD_SOURCE
#define _POSIX_SOURCE
#include <algorithm>
#include <limits>
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <poll.h>
#include <unistd.h>
#include <stdlib.h>
#include "cubeb/cubeb.h"
#include "cubeb-internal.h"

#include "cubeb-speex-resampler.h"

#include <jack/jack.h>
#include <jack/ringbuffer.h>

static const int MAX_STREAMS = 16;
static const int MAX_CHANNELS  = 8;
static const int FIFO_SIZE = 4096 * sizeof(float);
static const bool AUTO_CONNECT_JACK_PORTS = true;

static void
s16ne_to_float(float *dst, const int16_t *src, size_t n)
{
  for(size_t i=0;i < n;i++)
    *(dst++) = *(src++) / 32767.0f;
}

typedef enum {
  STATE_INACTIVE,
  STATE_STARTING,
  STATE_STARTED,
  STATE_RUNNING,
  STATE_DRAINING,
  STATE_STOPPING,
  STATE_STOPPED,
  STATE_DRAINED,
} play_state;

static bool
is_running(play_state state)
{
  return state == STATE_STARTING
    || state == STATE_STARTED
    || state == STATE_DRAINING
    || state == STATE_RUNNING;
}

struct AutoLock
{
  AutoLock(pthread_mutex_t& m) : mutex(m)
  {
    pthread_mutex_lock(&mutex);
  }

  ~AutoLock()
  {
    pthread_mutex_unlock(&mutex);
  }

private:
  pthread_mutex_t& mutex;
};

extern "C"
{
/*static*/ int jack_init (cubeb ** context, char const * context_name);
}
static char const * cbjack_get_backend_id(cubeb * context);
static int cbjack_get_max_channel_count(cubeb * ctx, uint32_t * max_channels);
static void cbjack_destroy(cubeb * context);
static int cbjack_stream_init(cubeb * context, cubeb_stream ** stream, char const * stream_name,
                  cubeb_stream_params stream_params, unsigned int latency,
                  cubeb_data_callback data_callback,
                  cubeb_state_callback state_callback,
                  void * user_ptr);
static void cbjack_stream_destroy(cubeb_stream * stream);
static int cbjack_stream_start(cubeb_stream * stream);
static int cbjack_stream_stop(cubeb_stream * stream);
static int cbjack_stream_get_position(cubeb_stream * stream, uint64_t * position);
static int cbjack_stream_get_latency(cubeb_stream * stream, uint32_t * latency);

static struct cubeb_ops const cbjack_ops = {
  .init = jack_init,
  .get_backend_id = cbjack_get_backend_id,
  .get_max_channel_count = cbjack_get_max_channel_count,
  .destroy = cbjack_destroy,
  .stream_init = cbjack_stream_init,
  .stream_destroy = cbjack_stream_destroy,
  .stream_start = cbjack_stream_start,
  .stream_stop = cbjack_stream_stop,
  .stream_get_position = cbjack_stream_get_position,
  .stream_get_latency = cbjack_stream_get_latency
};

struct cubeb_stream {
  cubeb *context;

  bool in_use; /**< Set to false iff the stream is free */
  bool ports_ready; /**< Set to true iff the JACK ports are ready */

  cubeb_data_callback data_callback;
  cubeb_state_callback state_callback;
  void *user_ptr;
  cubeb_stream_params params;

  SpeexResamplerState *resampler;

  play_state state;
  uint64_t position;
  char stream_name[256];
  jack_port_t *output_ports[MAX_CHANNELS];
  jack_ringbuffer_t *ringbuffer[MAX_CHANNELS];
};<commit_msg>jack: trivial compile fix (untested)<commit_after>/*
 * Copyright © 2012 <NAME>
 * Copyright © 2013 <NAME>
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */

#define _BSD_SOURCE
#define _POSIX_SOURCE
#include <algorithm>
#include <limits>
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <poll.h>
#include <unistd.h>
#include <stdlib.h>
#include "cubeb/cubeb.h"
#include "cubeb-internal.h"

#include "cubeb-speex-resampler.h"

#include <jack/jack.h>
#include <jack/ringbuffer.h>

static const int MAX_STREAMS = 16;
static const int MAX_CHANNELS  = 8;
static const int FIFO_SIZE = 4096 * sizeof(float);
static const bool AUTO_CONNECT_JACK_PORTS = true;

static void
s16ne_to_float(float *dst, const int16_t *src, size_t n)
{
  for(size_t i=0;i < n;i++)
    *(dst++) = *(src++) / 32767.0f;
}

typedef enum {
  STATE_INACTIVE,
  STATE_STARTING,
  STATE_STARTED,
  STATE_RUNNING,
  STATE_DRAINING,
  STATE_STOPPING,
  STATE_STOPPED,
  STATE_DRAINED,
} play_state;

static bool
is_running(play_state state)
{
  return state == STATE_STARTING
    || state == STATE_STARTED
    || state == STATE_DRAINING
    || state == STATE_RUNNING;
}

struct AutoLock
{
  AutoLock(pthread_mutex_t& m) : mutex(m)
  {
    pthread_mutex_lock(&mutex);
  }

  ~AutoLock()
  {
    pthread_mutex_unlock(&mutex);
  }

private:
  pthread_mutex_t& mutex;
};

extern "C"
{
/*static*/ int jack_init (cubeb ** context, char const * context_name);
}
static char const * cbjack_get_backend_id(cubeb * context);
static int cbjack_get_max_channel_count(cubeb * ctx, uint32_t * max_channels);
static void cbjack_destroy(cubeb * context);
static int cbjack_stream_init(cubeb * context, cubeb_stream ** stream, char const * stream_name,
                  cubeb_stream_params stream_params, unsigned int latency,
                  cubeb_data_callback data_callback,
                  cubeb_state_callback state_callback,
                  void * user_ptr);
static void cbjack_stream_destroy(cubeb_stream * stream);
static int cbjack_stream_start(cubeb_stream * stream);
static int cbjack_stream_stop(cubeb_stream * stream);
static int cbjack_stream_get_position(cubeb_stream * stream, uint64_t * position);
static int cbjack_stream_get_latency(cubeb_stream * stream, uint32_t * latency);

static struct cubeb_ops const cbjack_ops = {
  .init = jack_init,
  .get_backend_id = cbjack_get_backend_id,
  .get_max_channel_count = cbjack_get_max_channel_count,
  .get_min_latency = NULL,
  .get_preferred_sample_rate = NULL,
  .destroy = cbjack_destroy,
  .stream_init = cbjack_stream_init,
  .stream_destroy = cbjack_stream_destroy,
  .stream_start = cbjack_stream_start,
  .stream_stop = cbjack_stream_stop,
  .stream_get_position = cbjack_stream_get_position,
  .stream_get_latency = cbjack_stream_get_latency,
  .stream_set_volume = NULL,
  .stream_set_panning = NULL,
  .stream_get_current_device = NULL,
  .stream_device_destroy = NULL,
  .stream_register_device_changed_callback = NULL
};

struct cubeb_stream {
  cubeb *context;

  bool in_use; /**< Set to false iff the stream is free */
  bool ports_ready; /**< Set to true iff the JACK ports are ready */

  cubeb_data_callback data_callback;
  cubeb_state_callback state_callback;
  void *user_ptr;
  cubeb_stream_params params;

  SpeexResamplerState *resampler;

  play_state state;
  uint64_t position;
  char stream_name[256];
  jack_port_t *output_ports[MAX_CHANNELS];
  jack_ringbuffer_t *ringbuffer[MAX_CHANNELS];
};
{'fraction_non_alphanumeric': 0.09923332851150007, 'fraction_numerical': 0.010704469839432953, 'mean_word_length': 4.653311529026983, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 33, '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': '2312962', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2473, 'n_words': 560}