<commit_before>
#include "errno.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "signal.h"

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

#include "ibxm.h"

/*
	Simple command-line test player for ibxm-ac using SDL.
*/

#define SAMPLING_FREQ  48000  /* 48khz. */
#define REVERB_BUF_LEN 4800   /* 50ms. */
#define BUFFER_SAMPLES 16384  /* 64k buffer. */

static SDL_sem *semaphore;
static int samples_remaining;
static short reverb_buffer[ REVERB_BUF_LEN ];
static int mix_buffer[ SAMPLING_FREQ / 3 ];
static int mix_len, mix_idx, reverb_len, reverb_idx, interpolation, loop;

/* Simple stereo cross delay with feedback. */
static void reverb( short *buffer, int count ) {
	int buffer_idx, buffer_end;
	if( reverb_len > 2 ) {
		buffer_idx = 0;
		buffer_end = buffer_idx + ( count << 1 );
		while( buffer_idx < buffer_end ) {
			buffer[ buffer_idx ] = ( buffer[ buffer_idx ] * 3 + reverb_buffer[ reverb_idx + 1 ] ) >> 2;
			buffer[ buffer_idx + 1 ] = ( buffer[ buffer_idx + 1 ] * 3 + reverb_buffer[ reverb_idx ] ) >> 2;
			reverb_buffer[ reverb_idx ] = buffer[ buffer_idx ];
			reverb_buffer[ reverb_idx + 1 ] = buffer[ buffer_idx + 1 ];
			reverb_idx += 2;
			if( reverb_idx >= reverb_len ) {
				reverb_idx = 0;
			}
			buffer_idx += 2;
		}
	}
}

static void clip_audio( int *input, short *output, int count ) {
	int idx, end, sam;
	for( idx = 0, end = count << 1; idx < end; idx++ ) {
		sam = input[ idx ];
		if( sam < -32768 ) {
			sam = -32768;
		}
		if( sam > 32767 ) {
			sam = 32767;
		}
		output[ idx ] = sam;
	}
}

static void get_audio( struct replay *replay, short *buffer, int count ) {
	int buf_idx = 0, remain;
	while( buf_idx < count ) {
		if( mix_idx >= mix_len ) {
			mix_len = replay_get_audio( replay, mix_buffer );
			mix_idx = 0;
		}
		remain = mix_len - mix_idx;
		if( ( buf_idx + remain ) > count ) {
			remain = count - buf_idx;
		}
		clip_audio( &mix_buffer[ mix_idx << 1 ], &buffer[ buf_idx << 1 ], remain );
		mix_idx += remain;
		buf_idx += remain;
	}
}

static void audio_callback( void *udata, Uint8 *stream, int len ) {
	int count = len / 4;
	if( samples_remaining < count ) {
		/* Clear output.*/
		memset( stream, 0, len );
		count = samples_remaining;
	}
	if( count > 0 ) {
		/* Get audio from replay.*/
		get_audio( ( struct replay * ) udata, ( short * ) stream, count );
		reverb( ( short * ) stream, count );
		if( !loop ) {
			samples_remaining -= count;
		}
	} else {
		/* Notify the main thread to stop playback.*/
		SDL_SemPost( semaphore );
	}
}

static void termination_handler( int signum ) {
	/* Notify the main thread to stop playback. */
	SDL_SemPost( semaphore );
	fprintf( stderr, "\nTerminated!\n" );
}

static long read_file( char *file_name, void *buffer ) {
	long file_length = -1, bytes_read;
	FILE *input_file = fopen( file_name, "rb" );
	if( input_file != NULL ) {
		if( fseek( input_file, 0L, SEEK_END ) == 0 ) {
			file_length = ftell( input_file );
			if( file_length >= 0 && buffer ) {
				if( fseek( input_file, 0L, SEEK_SET ) == 0 ) {
					bytes_read = fread( buffer, 1, file_length, input_file ); 
					if( bytes_read != file_length ) {
						file_length = -1;
					}
				} else {
					file_length = -1;
				}
			}
		}
		fclose( input_file );
	}
	if( file_length < 0 ) {
		fputs( strerror( errno ), stderr );
		fputs( "\n", stderr );
	}
	return file_length;
}

static char* trim( char *string ) {
	int end = strlen( string );
	while( ( string[ end ] & 0xFF ) <= 32 ) {
		string[ end-- ] = 0;
	}
	return string;
}

static void print_module_info( struct module *module ) {
	int idx, end;
	for( idx = 0, end = module->num_instruments / 2; idx < end; idx++ ) {
		printf( "%03i - %-32s ", idx + 1, module->instruments[ idx ].name );
		printf( "%03i - %-32s\n", idx + end + 1, module->instruments[ idx + end ].name );
	}
	if( idx + end < module->num_instruments ) {
		printf( "%39s%03i - %-32s\n", "", idx + end + 1, module->instruments[ idx + end ].name );
	}
}

static int play_module( struct module *module ) {
	int result;
	SDL_AudioSpec audiospec;
	/* Initialise replay.*/
	struct replay *replay = new_replay( module, SAMPLING_FREQ, interpolation );
	if( replay ) {
		/* Calculate song length. */
		samples_remaining = replay_calculate_duration( replay );<commit_msg>Fix trim function and instrument name off-by-one.<commit_after>
#include "errno.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "signal.h"

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

#include "ibxm.h"

/*
	Simple command-line test player for ibxm-ac using SDL.
*/

#define SAMPLING_FREQ  48000  /* 48khz. */
#define REVERB_BUF_LEN 4800   /* 50ms. */
#define BUFFER_SAMPLES 16384  /* 64k buffer. */

static SDL_sem *semaphore;
static int samples_remaining;
static short reverb_buffer[ REVERB_BUF_LEN ];
static int mix_buffer[ SAMPLING_FREQ / 3 ];
static int mix_len, mix_idx, reverb_len, reverb_idx, interpolation, loop;

/* Simple stereo cross delay with feedback. */
static void reverb( short *buffer, int count ) {
	int buffer_idx, buffer_end;
	if( reverb_len > 2 ) {
		buffer_idx = 0;
		buffer_end = buffer_idx + ( count << 1 );
		while( buffer_idx < buffer_end ) {
			buffer[ buffer_idx ] = ( buffer[ buffer_idx ] * 3 + reverb_buffer[ reverb_idx + 1 ] ) >> 2;
			buffer[ buffer_idx + 1 ] = ( buffer[ buffer_idx + 1 ] * 3 + reverb_buffer[ reverb_idx ] ) >> 2;
			reverb_buffer[ reverb_idx ] = buffer[ buffer_idx ];
			reverb_buffer[ reverb_idx + 1 ] = buffer[ buffer_idx + 1 ];
			reverb_idx += 2;
			if( reverb_idx >= reverb_len ) {
				reverb_idx = 0;
			}
			buffer_idx += 2;
		}
	}
}

static void clip_audio( int *input, short *output, int count ) {
	int idx, end, sam;
	for( idx = 0, end = count << 1; idx < end; idx++ ) {
		sam = input[ idx ];
		if( sam < -32768 ) {
			sam = -32768;
		}
		if( sam > 32767 ) {
			sam = 32767;
		}
		output[ idx ] = sam;
	}
}

static void get_audio( struct replay *replay, short *buffer, int count ) {
	int buf_idx = 0, remain;
	while( buf_idx < count ) {
		if( mix_idx >= mix_len ) {
			mix_len = replay_get_audio( replay, mix_buffer );
			mix_idx = 0;
		}
		remain = mix_len - mix_idx;
		if( ( buf_idx + remain ) > count ) {
			remain = count - buf_idx;
		}
		clip_audio( &mix_buffer[ mix_idx << 1 ], &buffer[ buf_idx << 1 ], remain );
		mix_idx += remain;
		buf_idx += remain;
	}
}

static void audio_callback( void *udata, Uint8 *stream, int len ) {
	int count = len / 4;
	if( samples_remaining < count ) {
		/* Clear output.*/
		memset( stream, 0, len );
		count = samples_remaining;
	}
	if( count > 0 ) {
		/* Get audio from replay.*/
		get_audio( ( struct replay * ) udata, ( short * ) stream, count );
		reverb( ( short * ) stream, count );
		if( !loop ) {
			samples_remaining -= count;
		}
	} else {
		/* Notify the main thread to stop playback.*/
		SDL_SemPost( semaphore );
	}
}

static void termination_handler( int signum ) {
	/* Notify the main thread to stop playback. */
	SDL_SemPost( semaphore );
	fprintf( stderr, "\nTerminated!\n" );
}

static long read_file( char *file_name, void *buffer ) {
	long file_length = -1, bytes_read;
	FILE *input_file = fopen( file_name, "rb" );
	if( input_file != NULL ) {
		if( fseek( input_file, 0L, SEEK_END ) == 0 ) {
			file_length = ftell( input_file );
			if( file_length >= 0 && buffer ) {
				if( fseek( input_file, 0L, SEEK_SET ) == 0 ) {
					bytes_read = fread( buffer, 1, file_length, input_file ); 
					if( bytes_read != file_length ) {
						file_length = -1;
					}
				} else {
					file_length = -1;
				}
			}
		}
		fclose( input_file );
	}
	if( file_length < 0 ) {
		fputs( strerror( errno ), stderr );
		fputs( "\n", stderr );
	}
	return file_length;
}

static char* trim( char *string ) {
	int end = strlen( string ) - 1;
	while( end >= 0 && ( string[ end ] & 0xFF ) <= 32 ) {
		string[ end-- ] = 0;
	}
	return string;
}

static void print_module_info( struct module *module ) {
	int idx, end;
	for( idx = 0, end = module->num_instruments / 2; idx < end; idx++ ) {
		printf( "%03i - %-32s ", idx + 1, module->instruments[ idx + 1 ].name );
		printf( "%03i - %-32s\n", idx + end + 1, module->instruments[ idx + end + 1 ].name );
	}
	if( idx + end < module->num_instruments ) {
		printf( "%39s%03i - %-32s\n", "", idx + end + 1, module->instruments[ idx + end + 1 ].name );
	}
}

static int play_module( struct module *module ) {
	int result;
	SDL_AudioSpec audiospec;
	/* Initialise replay.*/
	struct replay *replay = new_replay( module, SAMPLING_FREQ, interpolation );
	if( replay ) {
		/* Calculate song length. */
		samples_remaining = replay_calculate_duration( replay );
{'fraction_non_alphanumeric': 0.14718260462941313, 'fraction_numerical': 0.023731587561374796, 'mean_word_length': 3.146873485215705, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 36, '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': '308677', 'n_tokens_mistral': 3651, 'n_tokens_neox': 3219, 'n_words': 908}