<commit_before>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>

#include "../../ckvm.h"
#include "ugen.h"

typedef struct _SndIn {
	AVFormatContext *pFormatCtx;
	AVCodecContext *pCodecCtx;
	AVPacket decodingPacket;
	AVFrame *frame;
	SwrContext *resampler;
	int audioStreamIndex; /* which stream is audio */

	float *buffer;
	int samplesLeft;
	float nextSampleIndex; /* can advance at fractional rates */

	int eof, closed;
} SndIn;

static int sndin_handle_packet(SndIn *sndin); /* returns number of samples in frame */
static void sndin_handle_frame(SndIn *sndin);

/* returns 0 on failure */
static
int
sndin_open(SndIn *sndin, const char *filename, int out_sample_rate)
{
	AVCodec *pCodec;

	sndin->pFormatCtx = NULL;
	sndin->pCodecCtx = NULL;
	sndin->frame = NULL;
	sndin->decodingPacket.size = 0;
	sndin->buffer = NULL;

	sndin->samplesLeft = sndin->nextSampleIndex = 0;
	sndin->eof = 0;
	sndin->closed = 0;

	/* Register all formats and codecs */
	av_register_all();

	/* open audio file */
	if(avformat_open_input(&sndin->pFormatCtx, filename, NULL, NULL) != 0)
		return 0;

	/* get stream information */
	if (avformat_find_stream_info(sndin->pFormatCtx, NULL) < 0) {
		avformat_close_input(&sndin->pFormatCtx);
		return 0; /* Couldn't find stream information */
	}

	/* identify best audio stream */
	sndin->audioStreamIndex = av_find_best_stream(sndin->pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &pCodec, 0);
	if (sndin->audioStreamIndex < 0) {
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	/* allocate audio buffer */
	sndin->frame = avcodec_alloc_frame();
	if (!sndin->frame) {
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	/* Get the codec context for the audio stream */
	AVStream *audioStream = sndin->pFormatCtx->streams[sndin->audioStreamIndex];
	sndin->pCodecCtx = audioStream->codec;
	sndin->pCodecCtx->codec = pCodec;

	if (avcodec_open2(sndin->pCodecCtx, sndin->pCodecCtx->codec, NULL) != 0) {
		av_free(sndin->frame);
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	/* initialize a converter from the input sample format to float samples */
	sndin->resampler = swr_alloc();
	av_opt_set_int(sndin->resampler, "in_channel_layout", sndin->pCodecCtx->channel_layout, 0);
	av_opt_set_int(sndin->resampler, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);
	av_opt_set_int(sndin->resampler, "in_sample_rate", sndin->pCodecCtx->sample_rate, 0);
	av_opt_set_int(sndin->resampler, "out_sample_rate", out_sample_rate, 0);
	av_opt_set_sample_fmt(sndin->resampler, "in_sample_fmt", sndin->pCodecCtx->sample_fmt, 0);
	av_opt_set_sample_fmt(sndin->resampler, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
	if(swr_init(sndin->resampler) != 0) {
		avcodec_close(sndin->pCodecCtx);
		av_free(sndin->frame);
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	return 1;<commit_msg>use default channel layout when one is not provided<commit_after>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>

#include "../../ckvm.h"
#include "ugen.h"

typedef struct _SndIn {
	AVFormatContext *pFormatCtx;
	AVCodecContext *pCodecCtx;
	AVPacket decodingPacket;
	AVFrame *frame;
	SwrContext *resampler;
	int audioStreamIndex; /* which stream is audio */

	float *buffer;
	int samplesLeft;
	float nextSampleIndex; /* can advance at fractional rates */

	int eof, closed;
} SndIn;

static int sndin_handle_packet(SndIn *sndin); /* returns number of samples in frame */
static void sndin_handle_frame(SndIn *sndin);

/* returns 0 on failure */
static
int
sndin_open(SndIn *sndin, const char *filename, int out_sample_rate)
{
	AVCodec *pCodec;

	sndin->pFormatCtx = NULL;
	sndin->pCodecCtx = NULL;
	sndin->frame = NULL;
	sndin->decodingPacket.size = 0;
	sndin->buffer = NULL;

	sndin->samplesLeft = sndin->nextSampleIndex = 0;
	sndin->eof = 0;
	sndin->closed = 0;

	/* Register all formats and codecs */
	av_register_all();

	/* open audio file */
	if(avformat_open_input(&sndin->pFormatCtx, filename, NULL, NULL) != 0)
		return 0;

	/* get stream information */
	if (avformat_find_stream_info(sndin->pFormatCtx, NULL) < 0) {
		avformat_close_input(&sndin->pFormatCtx);
		return 0; /* Couldn't find stream information */
	}

	/* identify best audio stream */
	sndin->audioStreamIndex = av_find_best_stream(sndin->pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &pCodec, 0);
	if (sndin->audioStreamIndex < 0) {
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	/* allocate audio buffer */
	sndin->frame = avcodec_alloc_frame();
	if (!sndin->frame) {
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	/* Get the codec context for the audio stream */
	AVStream *audioStream = sndin->pFormatCtx->streams[sndin->audioStreamIndex];
	sndin->pCodecCtx = audioStream->codec;
	sndin->pCodecCtx->codec = pCodec;

	if (avcodec_open2(sndin->pCodecCtx, sndin->pCodecCtx->codec, NULL) != 0) {
		av_free(sndin->frame);
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	/* initialize a converter from the input sample format to float samples */
	sndin->resampler = swr_alloc();
	av_opt_set_int(sndin->resampler, "in_channel_layout", sndin->pCodecCtx->channel_layout ? sndin->pCodecCtx->channel_layout : av_get_default_channel_layout(sndin->pCodecCtx->channels), 0);
	av_opt_set_int(sndin->resampler, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);
	av_opt_set_int(sndin->resampler, "in_sample_rate", sndin->pCodecCtx->sample_rate, 0);
	av_opt_set_int(sndin->resampler, "out_sample_rate", out_sample_rate, 0);
	av_opt_set_sample_fmt(sndin->resampler, "in_sample_fmt", sndin->pCodecCtx->sample_fmt, 0);
	av_opt_set_sample_fmt(sndin->resampler, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
	if(swr_init(sndin->resampler) != 0) {
		avcodec_close(sndin->pCodecCtx);
		av_free(sndin->frame);
		avformat_close_input(&sndin->pFormatCtx);
		return 0;
	}

	return 1;
{'fraction_non_alphanumeric': 0.13713532223504202, 'fraction_numerical': 0.008900609856601285, 'mean_word_length': 5.314255983350677, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 117, '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': '6499707', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2343, 'n_words': 461}