<commit_before>
#include <stdio.h>
#include <stdlib.h>

#include "SDL/SDL.h"
#include "SDL/SDL_main.h"

#include "micromod.h"

/*
	Simple command line test player for micromod using SDL.
*/

#define SAMPLING_FREQ  48000  /* 48khz. */
#define OVERSAMPLE     2      /* 2x oversampling. */
#define NUM_CHANNELS   2      /* Stereo. */
#define BUFFER_SAMPLES 16384  /* 64k buffer. */

static SDL_sem *semaphore;
static long samples_remaining;
static short mix_buffer[ BUFFER_SAMPLES * NUM_CHANNELS * OVERSAMPLE ];
static long filt_l, filt_r;

/*
	2:1 downsampling with simple but effective anti-aliasing.
	Count is the number of stereo samples to process, and must be even.
	input may point to the same buffer as output.
*/
static void downsample( short *input, short *output, long count ) {
	long in_idx, out_idx, out_l, out_r;
	in_idx = out_idx = 0;
	while( out_idx < count ) {	
		out_l = filt_l + ( input[ in_idx++ ] >> 1 );
		out_r = filt_r + ( input[ in_idx++ ] >> 1 );
		filt_l = input[ in_idx++ ] >> 2;
		filt_r = input[ in_idx++ ] >> 2;
		output[ out_idx++ ] = out_l + filt_l;
		output[ out_idx++ ] = out_r + filt_r;
	}
}

static void audio_callback( void *udata, Uint8 *stream, int len ) {
	long count;
	/* Notify the main thread if song has finished. */	
	if( samples_remaining <= 0 ) SDL_SemPost( semaphore );
	
	/* Get audio from replay. */
	count = len * OVERSAMPLE / 4;
	if( samples_remaining < count ) count = samples_remaining;
	memset( mix_buffer, 0, BUFFER_SAMPLES * NUM_CHANNELS * OVERSAMPLE * sizeof( short ) );
	micromod_get_audio( mix_buffer, count );
	downsample( mix_buffer, ( short * ) stream, BUFFER_SAMPLES * OVERSAMPLE );
	
	samples_remaining -= count;
}

static void load_module( char *file_name ) {
	FILE *file;
	void *module;
	long length, read, error;

	file = fopen( file_name, "rb" );
	if( file == NULL ) {
		fprintf( stderr, "Unable to open file.\n" );
		exit( EXIT_FAILURE );
	}

	module = malloc( 1084 );
	if( module == NULL ) {<commit_msg>Fix downsampling in SDL player.<commit_after>
#include <stdio.h>
#include <stdlib.h>

#include "SDL/SDL.h"
#include "SDL/SDL_main.h"

#include "micromod.h"

/*
	Simple command line test player for micromod using SDL.
*/

#define SAMPLING_FREQ  48000  /* 48khz. */
#define OVERSAMPLE     2      /* 2x oversampling. */
#define NUM_CHANNELS   2      /* Stereo. */
#define BUFFER_SAMPLES 16384  /* 64k buffer. */

static SDL_sem *semaphore;
static long samples_remaining;
static short mix_buffer[ BUFFER_SAMPLES * NUM_CHANNELS * OVERSAMPLE ];
static long filt_l, filt_r;

/*
	2:1 downsampling with simple but effective anti-aliasing.
	Count is the number of stereo samples to process, and must be even.
	input may point to the same buffer as output.
*/
static void downsample( short *input, short *output, long count ) {
	long in_idx, out_idx, out_l, out_r;
	in_idx = out_idx = 0;
	while( out_idx < count ) {	
		out_l = filt_l + ( input[ in_idx++ ] >> 1 );
		out_r = filt_r + ( input[ in_idx++ ] >> 1 );
		filt_l = input[ in_idx++ ] >> 2;
		filt_r = input[ in_idx++ ] >> 2;
		output[ out_idx++ ] = out_l + filt_l;
		output[ out_idx++ ] = out_r + filt_r;
	}
}

static void audio_callback( void *udata, Uint8 *stream, int len ) {
	long count;
	/* Get audio from replay. */
	count = len * OVERSAMPLE / 4;
	if( samples_remaining < count ) {
		memset( stream, 0, len );
		count = samples_remaining;
	}
	memset( mix_buffer, 0, count * NUM_CHANNELS * sizeof( short ) );
	micromod_get_audio( mix_buffer, count );
	downsample( mix_buffer, ( short * ) stream, count );
	samples_remaining -= count;
	/* Notify the main thread if song has finished. */	
	if( samples_remaining <= 0 ) SDL_SemPost( semaphore );
}

static void load_module( char *file_name ) {
	FILE *file;
	void *module;
	long length, read, error;

	file = fopen( file_name, "rb" );
	if( file == NULL ) {
		fprintf( stderr, "Unable to open file.\n" );
		exit( EXIT_FAILURE );
	}

	module = malloc( 1084 );
	if( module == NULL ) {
{'fraction_non_alphanumeric': 0.1286401620663459, 'fraction_numerical': 0.01645986325652064, 'mean_word_length': 3.6037296037296036, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '3705527', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1456, 'n_words': 444}