<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alsa/asoundlib.h>
#include "alsa.h"
#include "util.h"
#include "sampleconv.h"

struct alsa_enc_info {
	const char *name;
	snd_pcm_format_t fmt;
	int bytes, prec;
	void (*write_func)(sample_t *, char *, ssize_t);
	void (*read_func)(char *, sample_t *, ssize_t);
};

struct alsa_state {
	snd_pcm_t *dev;
	struct alsa_enc_info *enc_info;
	snd_pcm_sframes_t delay;
	char *buf;
	ssize_t buf_frames;
};

ssize_t alsa_read(struct codec *c, sample_t *buf, ssize_t frames)
{
	ssize_t n;
	struct alsa_state *state = (struct alsa_state *) c->data;

	try_again:
	n = snd_pcm_readi(state->dev, (char *) buf, frames);
	if (n < 0) {
		n = snd_pcm_recover(state->dev, n, 1);
		if (n < 0) {
			LOG(LL_ERROR, "dsp: alsa: snd_pcm_readi: read failed\n");
			return 0;
		}
		else
			goto try_again;
	}
	state->enc_info->read_func((char *) buf, buf, n * c->channels);
	return n;
}

ssize_t alsa_write(struct codec *c, sample_t *buf, ssize_t frames)
{
	ssize_t n, i = 0;
	struct alsa_state *state = (struct alsa_state *) c->data;

	while (i < frames) {
		n = (frames - i > state->buf_frames) ? state->buf_frames : frames - i;
		state->enc_info->write_func(&buf[i * c->channels], state->buf, n * c->channels);
		try_again:
		n = snd_pcm_writei(state->dev, state->buf, n);
		if (n < 0) {
			n = snd_pcm_recover(state->dev, n, 1);
			if (n < 0) {
				LOG(LL_ERROR, "dsp: alsa: snd_pcm_writei: write failed\n");
				return i;
			}
			else
				goto try_again;
		}
		i += n;
	}
	return i;
}

ssize_t alsa_seek(struct codec *c, ssize_t pos)
{
	return -1;
}

ssize_t alsa_delay(struct codec *c)
{
	struct alsa_state *state = (struct alsa_state *) c->data;
	if (snd_pcm_state(state->dev) == SND_PCM_STATE_PAUSED)
		return state->delay;
	snd_pcm_delay(state->dev, &state->delay);
	return state->delay;
}

void alsa_reset(struct codec *c)
{
	struct alsa_state *state = (struct alsa_state *) c->data;<commit_msg>alsa.c: Print warning on xrun.<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alsa/asoundlib.h>
#include "alsa.h"
#include "util.h"
#include "sampleconv.h"

struct alsa_enc_info {
	const char *name;
	snd_pcm_format_t fmt;
	int bytes, prec;
	void (*write_func)(sample_t *, char *, ssize_t);
	void (*read_func)(char *, sample_t *, ssize_t);
};

struct alsa_state {
	snd_pcm_t *dev;
	struct alsa_enc_info *enc_info;
	snd_pcm_sframes_t delay;
	char *buf;
	ssize_t buf_frames;
};

ssize_t alsa_read(struct codec *c, sample_t *buf, ssize_t frames)
{
	ssize_t n;
	struct alsa_state *state = (struct alsa_state *) c->data;

	try_again:
	n = snd_pcm_readi(state->dev, (char *) buf, frames);
	if (n < 0) {
		if (n == -EPIPE)
			LOG(LL_ERROR, "dsp: alsa: snd_pcm_readi: warning: overrun occurred\n");
		n = snd_pcm_recover(state->dev, n, 1);
		if (n < 0) {
			LOG(LL_ERROR, "dsp: alsa: snd_pcm_readi: error: read failed\n");
			return 0;
		}
		else
			goto try_again;
	}
	state->enc_info->read_func((char *) buf, buf, n * c->channels);
	return n;
}

ssize_t alsa_write(struct codec *c, sample_t *buf, ssize_t frames)
{
	ssize_t n, i = 0;
	struct alsa_state *state = (struct alsa_state *) c->data;

	while (i < frames) {
		n = (frames - i > state->buf_frames) ? state->buf_frames : frames - i;
		state->enc_info->write_func(&buf[i * c->channels], state->buf, n * c->channels);
		try_again:
		n = snd_pcm_writei(state->dev, state->buf, n);
		if (n < 0) {
			if (n == -EPIPE)
				LOG(LL_ERROR, "dsp: alsa: snd_pcm_readi: warning: underrun occurred\n");
			n = snd_pcm_recover(state->dev, n, 1);
			if (n < 0) {
				LOG(LL_ERROR, "dsp: alsa: snd_pcm_writei: error: write failed\n");
				return i;
			}
			else
				goto try_again;
		}
		i += n;
	}
	return i;
}

ssize_t alsa_seek(struct codec *c, ssize_t pos)
{
	return -1;
}

ssize_t alsa_delay(struct codec *c)
{
	struct alsa_state *state = (struct alsa_state *) c->data;
	if (snd_pcm_state(state->dev) == SND_PCM_STATE_PAUSED)
		return state->delay;
	snd_pcm_delay(state->dev, &state->delay);
	return state->delay;
}

void alsa_reset(struct codec *c)
{
	struct alsa_state *state = (struct alsa_state *) c->data;
{'fraction_non_alphanumeric': 0.15564671814671815, 'fraction_numerical': 0.004343629343629344, 'mean_word_length': 4.085889570552148, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 61, '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': '4561968', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1749, 'n_words': 415}