<commit_before>/* RTcmix  - Copyright (C) 2000  The RTcmix Development Team
   See ``AUTHORS'' for a list of contributors. See ``LICENSE'' for
   the license to this software and for a DISCLAIMER OF ALL WARRANTIES.
*/
/* Command line score player that lets us specify total duration of run,
	and up to 32 scores to play. Each starts after a configurable delay time.
	This is to test certain conditions that happen when RTcmix receives multiple
	scores during a run, as if they are part of one large score. There are likely
	to be cases where the scores interfere with each other. It is also a way to
	automate throwing a lot of stuff at the RTcmix embed lib to make if fail.

	<NAME>, 8/1/17
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <portaudio.h>

#define EMBEDDEDAUDIO
#include <RTcmix_API.h>

#define MAX_SCORES 32
#define SAMPLE_RATE 44100.0
#define FRAMES_PER_BUF 512
#define MIN_FRAMES_PER_BUF 64	// I think this may be right for PortAudio
#define INPUT_CHANS 0
#define OUTPUT_CHANS 2
#define BUS_COUNT 32

float totalDuration = 10.0;
float scoreDelayTime = 5.0;	//FIXME: good default? Can derive from sco?
PaStream *stream = NULL;
int verbose = 0;
int printJobOutput = 0;
float rescaleFactor = 1.0;
float sampleRate = SAMPLE_RATE;
int framesPerBuffer = FRAMES_PER_BUF;
int numOutChannels = OUTPUT_CHANS;
int numInternalBuses = BUS_COUNT;
char *progname = NULL;
const char *scoreNames[MAX_SCORES];
char *scores[MAX_SCORES];
int numScores = 0;

#define USAGE_MSG                                                   " \n\
usage: %s [options] score1 [score2 ... scoreN]                        \n\
                                                                      \n\
  option   description                               default          \n\
  --------------------------------------------------------------------\n\
  -a NUM   amplitude multiplier                      1.0              \n\
  -b NUM   number of RTcmix internal buses           32               \n\
  -B NUM   frames per buffer                         512              \n\
  -c NUM   number of output channels                 2                \n\
  -d NUM   total play duration                       10.0 (sec)       \n\
  -h       show this help text                                        \n\
  -j       print RTcmix job output                                    \n\
  -r NUM   sampling rate                             44100.0          \n\
  -v       print settings before playing                              \n\
  -y NUM   delay time between scores                 5.0 (sec)        \n\
                                                                      \n\
  You must leave a space between a - flag and its arg. -d3 won't work.\n\
  This program won't work for any scores that take live audio input or\n\
  invoke MIDI, OSC, or the mouse or display apps. There is no checking\n\
  for these dependencies.                                             \n\
  The amplitude multiplier is applied after RTcmix generates samples, \n\
  so it does not prevent clipping.                                    \n\
                                                                      \n\
"

/* this function called by high-priority system audio thread
	(CoreAudio on macOS)
*/
static int paCallback(const void *input,
							void *output,
							unsigned long frames,
							const PaStreamCallbackTimeInfo *timeInfo,
							PaStreamCallbackFlags statusFlags,
							void *userData)
{
	RTcmix_runAudio((void *)input, output, frames);
	if (rescaleFactor != 1.0) {
		float *buf = (float *) output;
		unsigned long len = frames * numOutChannels;
		for (unsigned long i = 0; i < len; i++)
			*buf++ *= rescaleFactor;
	}
	return paContinue;
}

int initAudio()
{
	PaError err = Pa_Initialize();
	if (err != paNoError)
		goto error;

	err = Pa_OpenDefaultStream(&stream,
										INPUT_CHANS,
										numOutChannels,
										paFloat32,
										sampleRate,
										framesPerBuffer,
										paCallback,
										NULL);
	if (err != paNoError)
		goto error;

	err = Pa_StartStream(stream);
	if (err != paNoError)
		goto error;

	return 0;
error:
	fprintf(stderr, "PortAudio init failed (%s)\n", Pa_GetErrorText(err));
	return -1;
}

void rtcmixPrintCallback(const char *printBuffer, void *inContext)
{
	(void) inContext;
	const char *p = printBuffer;
	char str[1024];
	int len = strlen(p);
	while (len > 0) {
		strncpy(str, p, 1024);
		str[len-1] = 0;
		printf("%s\n", str);
		p += (len + 1);
		len = strlen(p);
	}
}<commit_msg>correct string handling<commit_after>/* RTcmix  - Copyright (C) 2000  The RTcmix Development Team
   See ``AUTHORS'' for a list of contributors. See ``LICENSE'' for
   the license to this software and for a DISCLAIMER OF ALL WARRANTIES.
*/
/* Command line score player that lets us specify total duration of run,
	and up to 32 scores to play. Each starts after a configurable delay time.
	This is to test certain conditions that happen when RTcmix receives multiple
	scores during a run, as if they are part of one large score. There are likely
	to be cases where the scores interfere with each other. It is also a way to
	automate throwing a lot of stuff at the RTcmix embed lib to make if fail.

	<NAME>, 8/1/17
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <portaudio.h>

#define EMBEDDEDAUDIO
#include <RTcmix_API.h>

#define MAX_SCORES 32
#define SAMPLE_RATE 44100.0
#define FRAMES_PER_BUF 512
#define MIN_FRAMES_PER_BUF 64	// I think this may be right for PortAudio
#define INPUT_CHANS 0
#define OUTPUT_CHANS 2
#define BUS_COUNT 32

float totalDuration = 10.0;
float scoreDelayTime = 5.0;	//FIXME: good default? Can derive from sco?
PaStream *stream = NULL;
int verbose = 0;
int printJobOutput = 0;
float rescaleFactor = 1.0;
float sampleRate = SAMPLE_RATE;
int framesPerBuffer = FRAMES_PER_BUF;
int numOutChannels = OUTPUT_CHANS;
int numInternalBuses = BUS_COUNT;
char *progname = NULL;
const char *scoreNames[MAX_SCORES];
char *scores[MAX_SCORES];
int numScores = 0;

#define USAGE_MSG                                                   " \n\
usage: %s [options] score1 [score2 ... scoreN]                        \n\
                                                                      \n\
  option   description                               default          \n\
  --------------------------------------------------------------------\n\
  -a NUM   amplitude multiplier                      1.0              \n\
  -b NUM   number of RTcmix internal buses           32               \n\
  -B NUM   frames per buffer                         512              \n\
  -c NUM   number of output channels                 2                \n\
  -d NUM   total play duration                       10.0 (sec)       \n\
  -h       show this help text                                        \n\
  -j       print RTcmix job output                                    \n\
  -r NUM   sampling rate                             44100.0          \n\
  -v       print settings before playing                              \n\
  -y NUM   delay time between scores                 5.0 (sec)        \n\
                                                                      \n\
  You must leave a space between a - flag and its arg. -d3 won't work.\n\
  This program won't work for any scores that take live audio input or\n\
  invoke MIDI, OSC, or the mouse or display apps. There is no checking\n\
  for these dependencies.                                             \n\
  The amplitude multiplier is applied after RTcmix generates samples, \n\
  so it does not prevent clipping.                                    \n\
                                                                      \n\
"

/* this function called by high-priority system audio thread
	(CoreAudio on macOS)
*/
static int paCallback(const void *input,
							void *output,
							unsigned long frames,
							const PaStreamCallbackTimeInfo *timeInfo,
							PaStreamCallbackFlags statusFlags,
							void *userData)
{
	RTcmix_runAudio((void *)input, output, frames);
	if (rescaleFactor != 1.0) {
		float *buf = (float *) output;
		unsigned long len = frames * numOutChannels;
		for (unsigned long i = 0; i < len; i++)
			*buf++ *= rescaleFactor;
	}
	return paContinue;
}

int initAudio()
{
	PaError err = Pa_Initialize();
	if (err != paNoError)
		goto error;

	err = Pa_OpenDefaultStream(&stream,
										INPUT_CHANS,
										numOutChannels,
										paFloat32,
										sampleRate,
										framesPerBuffer,
										paCallback,
										NULL);
	if (err != paNoError)
		goto error;

	err = Pa_StartStream(stream);
	if (err != paNoError)
		goto error;

	return 0;
error:
	fprintf(stderr, "PortAudio init failed (%s)\n", Pa_GetErrorText(err));
	return -1;
}

void rtcmixPrintCallback(const char *printBuffer, void *inContext)
{
	(void) inContext;
	const char *p = printBuffer;
	char str[1024];
	int len = strlen(p);
	while (len > 0) {
		strncpy(str, p, 1024);
		str[len] = 0;
		printf("%s", str);
		p += (len + 1);
		len = strlen(p);
	}
}
{'fraction_non_alphanumeric': 0.09692206941715782, 'fraction_numerical': 0.01691770355817507, 'mean_word_length': 1.7181845149807178, 'pattern_counts': {'":': 0, '<': 23, '<?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': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5601774', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2581, 'n_words': 988}