<commit_before>/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <alsa/asoundlib.h>
#include <getopt.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>

#include "cras_client.h"
#include "cras_types.h"
#include "cras_util.h"

#define PLAYBACK_CB_THRESHOLD (480)
#define PLAYBACK_BUFFER_SIZE (4800)

static const size_t MAX_IODEVS = 10; /* Max devices to print out. */
static const size_t MAX_ATTACHED_CLIENTS = 10; /* Max clients to print out. */

static uint8_t *file_buf;
static size_t file_buf_size;
static size_t file_buf_read_offset;
static struct timespec last_latency;
static int show_latency;
static int keep_looping = 1;
static int exit_after_done_playing = 1;
static size_t duration_frames;
static int full_frames;
uint32_t min_cb_level = PLAYBACK_CB_THRESHOLD;

struct cras_audio_format *aud_format;

static void check_stream_terminate(size_t frames)
{
	if (duration_frames) {
		if (duration_frames <= frames)
			keep_looping = 0;
		else
			duration_frames -= frames;
	}
}

/* Run from callback thread. */
static int got_samples(struct cras_client *client, cras_stream_id_t stream_id,
		       uint8_t *samples, size_t frames,
		       const struct timespec *sample_time, void *arg)
{
	int *fd = (int *)arg;
	int ret;
	int write_size;

	check_stream_terminate(frames);

	cras_client_calc_capture_latency(sample_time, &last_latency);

	write_size = frames * cras_client_format_bytes_per_frame(aud_format);
	ret = write(*fd, samples, write_size);
	if (ret != write_size)
		printf("Error writing file\n");
	return frames;
}

/* Run from callback thread. */
static int put_samples(struct cras_client *client, cras_stream_id_t stream_id,
		       uint8_t *samples, size_t frames,
		       const struct timespec *sample_time, void *arg)
{
	size_t this_size;
	snd_pcm_uframes_t avail;
	uint32_t frame_bytes = cras_client_format_bytes_per_frame(aud_format);

	if (file_buf_read_offset >= file_buf_size) {
		if (exit_after_done_playing)
			keep_looping = 0;
		return EOF;
	}

	check_stream_terminate(frames);

	if (frames < min_cb_level)
		printf("req for only %zu - %d min\n", frames, min_cb_level);
	avail = frames * frame_bytes;

	this_size = file_buf_size - file_buf_read_offset;
	if (this_size > avail)
		this_size = avail;

	if (full_frames && this_size > min_cb_level * frame_bytes)
		this_size = min_cb_level * frame_bytes;

	cras_client_calc_playback_latency(sample_time, &last_latency);

	memcpy(samples, file_buf + file_buf_read_offset, this_size);
	file_buf_read_offset += this_size;

	return this_size / frame_bytes;
}

static int stream_error(struct cras_client *client,
			cras_stream_id_t stream_id,
			int err,
			void *arg)
{
	printf("Stream error %d\n", err);
	keep_looping = 0;
	return 0;
}

static void print_last_latency()
{
	printf("%u.%09u\n", (unsigned)last_latency.tv_sec,
	       (unsigned)last_latency.tv_nsec);
}

static void print_dev_info(const struct cras_iodev_info *devs, int num_devs)
{
	unsigned i;

	printf("\tID\tPriority\tName\n");
	for (i = 0; i < num_devs; i++, devs++)
		printf("\t%zu\t%zu\t\t%s\n",
		       devs->idx,
		       devs->priority,
		       devs->name);
}

static void print_device_lists(struct cras_client *client)
{
	struct cras_iodev_info devs[MAX_IODEVS];<commit_msg>CRAS: test_client - Print plugged state of iodevs.<commit_after>/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <alsa/asoundlib.h>
#include <getopt.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>

#include "cras_client.h"
#include "cras_types.h"
#include "cras_util.h"

#define PLAYBACK_CB_THRESHOLD (480)
#define PLAYBACK_BUFFER_SIZE (4800)

static const size_t MAX_IODEVS = 10; /* Max devices to print out. */
static const size_t MAX_ATTACHED_CLIENTS = 10; /* Max clients to print out. */

static uint8_t *file_buf;
static size_t file_buf_size;
static size_t file_buf_read_offset;
static struct timespec last_latency;
static int show_latency;
static int keep_looping = 1;
static int exit_after_done_playing = 1;
static size_t duration_frames;
static int full_frames;
uint32_t min_cb_level = PLAYBACK_CB_THRESHOLD;

struct cras_audio_format *aud_format;

static void check_stream_terminate(size_t frames)
{
	if (duration_frames) {
		if (duration_frames <= frames)
			keep_looping = 0;
		else
			duration_frames -= frames;
	}
}

/* Run from callback thread. */
static int got_samples(struct cras_client *client, cras_stream_id_t stream_id,
		       uint8_t *samples, size_t frames,
		       const struct timespec *sample_time, void *arg)
{
	int *fd = (int *)arg;
	int ret;
	int write_size;

	check_stream_terminate(frames);

	cras_client_calc_capture_latency(sample_time, &last_latency);

	write_size = frames * cras_client_format_bytes_per_frame(aud_format);
	ret = write(*fd, samples, write_size);
	if (ret != write_size)
		printf("Error writing file\n");
	return frames;
}

/* Run from callback thread. */
static int put_samples(struct cras_client *client, cras_stream_id_t stream_id,
		       uint8_t *samples, size_t frames,
		       const struct timespec *sample_time, void *arg)
{
	size_t this_size;
	snd_pcm_uframes_t avail;
	uint32_t frame_bytes = cras_client_format_bytes_per_frame(aud_format);

	if (file_buf_read_offset >= file_buf_size) {
		if (exit_after_done_playing)
			keep_looping = 0;
		return EOF;
	}

	check_stream_terminate(frames);

	if (frames < min_cb_level)
		printf("req for only %zu - %d min\n", frames, min_cb_level);
	avail = frames * frame_bytes;

	this_size = file_buf_size - file_buf_read_offset;
	if (this_size > avail)
		this_size = avail;

	if (full_frames && this_size > min_cb_level * frame_bytes)
		this_size = min_cb_level * frame_bytes;

	cras_client_calc_playback_latency(sample_time, &last_latency);

	memcpy(samples, file_buf + file_buf_read_offset, this_size);
	file_buf_read_offset += this_size;

	return this_size / frame_bytes;
}

static int stream_error(struct cras_client *client,
			cras_stream_id_t stream_id,
			int err,
			void *arg)
{
	printf("Stream error %d\n", err);
	keep_looping = 0;
	return 0;
}

static void print_last_latency()
{
	printf("%u.%09u\n", (unsigned)last_latency.tv_sec,
	       (unsigned)last_latency.tv_nsec);
}

static void print_dev_info(const struct cras_iodev_info *devs, int num_devs)
{
	unsigned i;

	printf("\tID\tPriority\tPlugged\t\tName\n");
	for (i = 0; i < num_devs; i++)
		printf("\t%zu\t%zu\t\t%s\t\t%s\n",
		       devs[i].idx,
		       devs[i].priority,
		       devs[i].plugged ? "yes" : "no",
		       devs[i].name);
}

static void print_device_lists(struct cras_client *client)
{
	struct cras_iodev_info devs[MAX_IODEVS];
{'fraction_non_alphanumeric': 0.10416967248593277, 'fraction_numerical': 0.008945318135911123, 'mean_word_length': 4.728925619834711, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, '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': '648248', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2573, 'n_words': 621}