This commit is contained in:
libretroadmin 2023-02-23 13:29:37 +01:00
parent d2873c52bf
commit 3b634bdc8c
33 changed files with 638 additions and 484 deletions

View file

@ -923,7 +923,7 @@ audio_mixer_voice_t* audio_mixer_play(audio_mixer_sound_t* sound,
}
else
{
if(i < AUDIO_MIXER_MAX_VOICES)
if (i < AUDIO_MIXER_MAX_VOICES)
{
audio_mixer_release(voice);
AUDIO_MIXER_UNLOCK(voice);

View file

@ -33,13 +33,12 @@
struct chorus_data
{
float old[2][CHORUS_MAX_DELAY];
unsigned old_ptr;
float delay;
float depth;
float input_rate;
float mix_dry;
float mix_wet;
unsigned old_ptr;
unsigned lfo_ptr;
unsigned lfo_period;
};
@ -66,37 +65,37 @@ static void chorus_process(void *data, struct dspfilter_output *output,
unsigned delay_int;
float delay_frac, l_a, l_b, r_a, r_b;
float chorus_l, chorus_r;
float in[2] = { out[0], out[1] };
float delay = ch->delay + ch->depth * sin((2.0 * M_PI * ch->lfo_ptr++) / ch->lfo_period);
float in[2] = { out[0], out[1] };
float delay = ch->delay + ch->depth * sin((2.0 * M_PI * ch->lfo_ptr++) / ch->lfo_period);
delay *= ch->input_rate;
delay *= ch->input_rate;
if (ch->lfo_ptr >= ch->lfo_period)
ch->lfo_ptr = 0;
ch->lfo_ptr = 0;
delay_int = (unsigned)delay;
delay_int = (unsigned)delay;
if (delay_int >= CHORUS_MAX_DELAY - 1)
delay_int = CHORUS_MAX_DELAY - 2;
delay_int = CHORUS_MAX_DELAY - 2;
delay_frac = delay - delay_int;
delay_frac = delay - delay_int;
ch->old[0][ch->old_ptr] = in[0];
ch->old[1][ch->old_ptr] = in[1];
l_a = ch->old[0][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
l_b = ch->old[0][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
r_a = ch->old[1][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
r_b = ch->old[1][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
l_a = ch->old[0][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
l_b = ch->old[0][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
r_a = ch->old[1][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
r_b = ch->old[1][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
/* Lerp introduces aliasing of the chorus component,
* but doing full polyphase here is probably overkill. */
chorus_l = l_a * (1.0f - delay_frac) + l_b * delay_frac;
chorus_r = r_a * (1.0f - delay_frac) + r_b * delay_frac;
chorus_l = l_a * (1.0f - delay_frac) + l_b * delay_frac;
chorus_r = r_a * (1.0f - delay_frac) + r_b * delay_frac;
out[0] = ch->mix_dry * in[0] + ch->mix_wet * chorus_l;
out[1] = ch->mix_dry * in[1] + ch->mix_wet * chorus_r;
out[0] = ch->mix_dry * in[0] + ch->mix_wet * chorus_l;
out[1] = ch->mix_dry * in[1] + ch->mix_wet * chorus_r;
ch->old_ptr = (ch->old_ptr + 1) & CHORUS_DELAY_MASK;
ch->old_ptr = (ch->old_ptr + 1) & CHORUS_DELAY_MASK;
}
}
@ -113,24 +112,24 @@ static void *chorus_init(const struct dspfilter_info *info,
config->get_float(userdata, "lfo_freq", &lfo_freq, 0.5f);
config->get_float(userdata, "drywet", &drywet, 0.8f);
delay /= 1000.0f;
depth /= 1000.0f;
delay /= 1000.0f;
depth /= 1000.0f;
if (depth > delay)
depth = delay;
depth = delay;
if (drywet < 0.0f)
drywet = 0.0f;
drywet = 0.0f;
else if (drywet > 1.0f)
drywet = 1.0f;
drywet = 1.0f;
ch->mix_dry = 1.0f - 0.5f * drywet;
ch->mix_wet = 0.5f * drywet;
ch->mix_dry = 1.0f - 0.5f * drywet;
ch->mix_wet = 0.5f * drywet;
ch->delay = delay;
ch->depth = depth;
ch->lfo_period = (1.0f / lfo_freq) * info->input_rate;
ch->input_rate = info->input_rate;
ch->delay = delay;
ch->depth = depth;
ch->lfo_period = (1.0f / lfo_freq) * info->input_rate;
ch->input_rate = info->input_rate;
if (!ch->lfo_period)
ch->lfo_period = 1;
return ch;
@ -151,10 +150,6 @@ static const struct dspfilter_implementation chorus_plug = {
#endif
const struct dspfilter_implementation *
dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &chorus_plug;
}
dspfilter_get_implementation(dspfilter_simd_mask_t mask) { return &chorus_plug; }
#undef dspfilter_get_implementation

View file

@ -42,7 +42,7 @@ static void delta_process(void *data, struct dspfilter_output *output,
const struct dspfilter_input *input)
{
unsigned i, c;
struct delta_data *d = (struct delta_data*)data;
struct delta_data *d = (struct delta_data*)data;
float *out = output->samples;
output->samples = input->samples;
output->frames = input->frames;
@ -51,9 +51,9 @@ static void delta_process(void *data, struct dspfilter_output *output,
{
for (c = 0; c < 2; c++)
{
float current = *out;
*out++ = current + (current - d->old[c]) * d->intensity;
d->old[c] = current;
float current = *out;
*out++ = current + (current - d->old[c]) * d->intensity;
d->old[c] = current;
}
}
}
@ -83,7 +83,6 @@ static const struct dspfilter_implementation delta_plug = {
const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &delta_plug;
}

View file

@ -75,11 +75,11 @@ static void echo_process(void *data, struct dspfilter_output *output,
echo_right += echo->channels[c].buffer[(echo->channels[c].ptr << 1) + 1];
}
echo_left *= echo->amp;
echo_right *= echo->amp;
echo_left *= echo->amp;
echo_right *= echo->amp;
left = out[0] + echo_left;
right = out[1] + echo_right;
left = out[0] + echo_left;
right = out[1] + echo_right;
for (c = 0; c < echo->num_channels; c++)
{
@ -121,28 +121,25 @@ static void *echo_init(const struct dspfilter_info *info,
&num_feedback, default_feedback, 1);
config->get_float(userdata, "amp", &echo->amp, 0.2f);
channels = num_feedback = num_delay = MIN(num_delay, num_feedback);
channels = num_feedback = num_delay = MIN(num_delay, num_feedback);
echo_channels = (struct echo_channel*)calloc(channels,
sizeof(*echo_channels));
if (!echo_channels)
if (!(echo_channels = (struct echo_channel*)calloc(channels,
sizeof(*echo_channels))))
goto error;
echo->channels = echo_channels;
echo->num_channels = channels;
echo->channels = echo_channels;
echo->num_channels = channels;
for (i = 0; i < channels; i++)
{
unsigned frames = (unsigned)(delay[i] * info->input_rate / 1000.0f + 0.5f);
unsigned frames = (unsigned)(delay[i] * info->input_rate / 1000.0f + 0.5f);
if (!frames)
goto error;
echo->channels[i].buffer = (float*)calloc(frames, 2 * sizeof(float));
if (!echo->channels[i].buffer)
if (!(echo->channels[i].buffer = (float*)calloc(frames, 2 * sizeof(float))))
goto error;
echo->channels[i].frames = frames;
echo->channels[i].frames = frames;
echo->channels[i].feedback = feedback[i];
}
@ -173,7 +170,6 @@ static const struct dspfilter_implementation echo_plug = {
const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &echo_plug;
}

View file

@ -34,12 +34,11 @@
struct eq_data
{
fft_t *fft;
float buffer[8 * 1024];
float *save;
float *block;
fft_complex_t *filter;
fft_complex_t *fftblock;
float buffer[8 * 1024];
unsigned block_size;
unsigned block_ptr;
};
@ -88,11 +87,11 @@ static void eq_process(void *data, struct dspfilter_output *output,
memcpy(eq->block + eq->block_ptr * 2, in, write_avail * 2 * sizeof(float));
in += write_avail * 2;
input_frames -= write_avail;
in += write_avail * 2;
input_frames -= write_avail;
eq->block_ptr += write_avail;
// Convolve a new block.
/* Convolve a new block. */
if (eq->block_ptr == eq->block_size)
{
unsigned i, c;
@ -105,16 +104,16 @@ static void eq_process(void *data, struct dspfilter_output *output,
fft_process_inverse(eq->fft, out + c, eq->fftblock, 2);
}
// Overlap add method, so add in saved block now.
/* Overlap add method, so add in saved block now. */
for (i = 0; i < 2 * eq->block_size; i++)
out[i] += eq->save[i];
out[i] += eq->save[i];
// Save block for later.
/* Save block for later. */
memcpy(eq->save, out + 2 * eq->block_size, 2 * eq->block_size * sizeof(float));
out += eq->block_size * 2;
out += eq->block_size * 2;
output->frames += eq->block_size;
eq->block_ptr = 0;
eq->block_ptr = 0;
}
}
}
@ -184,8 +183,8 @@ static void generate_response(fft_complex_t *response,
lerp = (freq - start_freq) / (end_freq - start_freq);
gain = (1.0f - lerp) * start_gain + lerp * end_gain;
response[i].real = gain;
response[i].imag = 0.0f;
response[i].real = gain;
response[i].imag = 0.0f;
response[2 * samples - i].real = gain;
response[2 * samples - i].imag = 0.0f;
}
@ -196,10 +195,9 @@ static void create_filter(struct eq_data *eq, unsigned size_log2,
{
int i;
int half_block_size = eq->block_size >> 1;
double window_mod = 1.0 / kaiser_window_function(0.0, beta);
fft_t *fft = fft_new(size_log2);
float *time_filter = (float*)calloc(eq->block_size * 2 + 1, sizeof(*time_filter));
double window_mod = 1.0 / kaiser_window_function(0.0, beta);
fft_t *fft = fft_new(size_log2);
float *time_filter = (float*)calloc(eq->block_size * 2 + 1, sizeof(*time_filter));
if (!fft || !time_filter)
goto end;
@ -228,8 +226,8 @@ static void create_filter(struct eq_data *eq, unsigned size_log2,
for (i = 0; i < (int)eq->block_size; i++)
{
/* Kaiser window. */
double phase = (double)i / eq->block_size;
phase = 2.0 * (phase - 0.5);
double phase = (double)i / eq->block_size;
phase = 2.0 * (phase - 0.5);
time_filter[i] *= window_mod * kaiser_window_function(phase, beta);
}
@ -261,15 +259,15 @@ end:
static void *eq_init(const struct dspfilter_info *info,
const struct dspfilter_config *config, void *userdata)
{
float *frequencies, *gain;
unsigned num_freq, num_gain, i, size;
int size_log2;
float beta;
struct eq_gain *gains = NULL;
char *filter_path = NULL;
float *frequencies, *gain;
unsigned num_freq, num_gain, i, size;
struct eq_gain *gains = NULL;
char *filter_path = NULL;
const float default_freq[] = { 0.0f, info->input_rate };
const float default_gain[] = { 0.0f, 0.0f };
struct eq_data *eq = (struct eq_data*)calloc(1, sizeof(*eq));
struct eq_data *eq = (struct eq_data*)calloc(1, sizeof(*eq));
if (!eq)
return NULL;
@ -289,8 +287,7 @@ static void *eq_init(const struct dspfilter_info *info,
num_gain = num_freq = MIN(num_gain, num_freq);
gains = (struct eq_gain*)calloc(num_gain, sizeof(*gains));
if (!gains)
if (!(gains = (struct eq_gain*)calloc(num_gain, sizeof(*gains))))
goto error;
for (i = 0; i < num_gain; i++)
@ -303,15 +300,15 @@ static void *eq_init(const struct dspfilter_info *info,
eq->block_size = size;
eq->save = (float*)calloc( size, 2 * sizeof(*eq->save));
eq->block = (float*)calloc(2 * size, 2 * sizeof(*eq->block));
eq->fftblock = (fft_complex_t*)calloc(2 * size, sizeof(*eq->fftblock));
eq->filter = (fft_complex_t*)calloc(2 * size, sizeof(*eq->filter));
eq->save = (float*)calloc( size, 2 * sizeof(*eq->save));
eq->block = (float*)calloc(2 * size, 2 * sizeof(*eq->block));
eq->fftblock = (fft_complex_t*)calloc(2 * size, sizeof(*eq->fftblock));
eq->filter = (fft_complex_t*)calloc(2 * size, sizeof(*eq->filter));
/* Use an FFT which is twice the block size with zero-padding
* to make circular convolution => proper convolution.
*/
eq->fft = fft_new(size_log2 + 1);
eq->fft = fft_new(size_log2 + 1);
if (!eq->fft || !eq->fftblock || !eq->save || !eq->block || !eq->filter)
goto error;
@ -345,7 +342,6 @@ static const struct dspfilter_implementation eq_plug = {
const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &eq_plug;
}

View file

@ -364,7 +364,6 @@ static const struct dspfilter_implementation iir_plug = {
const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &iir_plug;
}

View file

@ -104,7 +104,6 @@ static const struct dspfilter_implementation panning = {
const struct dspfilter_implementation *
dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &panning;
}

View file

@ -27,8 +27,8 @@
#include <retro_miscellaneous.h>
#include <libretro_dspfilter.h>
#define phaserlfoshape 4.0
#define phaserlfoskipsamples 20
#define PHASER_LFO_SHAPE 4.0
#define PHASER_LFO_SKIP_SAMPLES 20
struct phaser_data
{
@ -71,10 +71,10 @@ static void phaser_process(void *data, struct dspfilter_output *output,
for (c = 0; c < 2; c++)
m[c] = in[c] + ph->fbout[c] * ph->fb * 0.01f;
if ((ph->skipcount++ % phaserlfoskipsamples) == 0)
if ((ph->skipcount++ % PHASER_LFO_SKIP_SAMPLES) == 0)
{
ph->gain = 0.5 * (1.0 + cos(ph->skipcount * ph->lfoskip + ph->phase));
ph->gain = (exp(ph->gain * phaserlfoshape) - 1.0) / (exp(phaserlfoshape) - 1);
ph->gain = (exp(ph->gain * PHASER_LFO_SHAPE) - 1.0) / (exp(PHASER_LFO_SHAPE) - 1);
ph->gain = 1.0 - ph->gain * ph->depth;
}
@ -138,7 +138,6 @@ static const struct dspfilter_implementation phaser_plug = {
const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &phaser_plug;
}

View file

@ -127,7 +127,7 @@ static float revmodel_process(struct revmodel *rev, float in)
static void revmodel_update(struct revmodel *rev)
{
int i;
rev->wet1 = rev->wet * (rev->width / 2.0f + 0.5f);
rev->wet1 = rev->wet * (rev->width / 2.0f + 0.5f);
if (rev->mode >= freezemode)
{
@ -138,15 +138,15 @@ static void revmodel_update(struct revmodel *rev)
else
{
rev->roomsize1 = rev->roomsize;
rev->damp1 = rev->damp;
rev->gain = fixedgain;
rev->damp1 = rev->damp;
rev->gain = fixedgain;
}
for (i = 0; i < NUMCOMBS; i++)
{
rev->combL[i].feedback = rev->roomsize1;
rev->combL[i].damp1 = rev->damp1;
rev->combL[i].damp2 = 1.0f - rev->damp1;
rev->combL[i].damp1 = rev->damp1;
rev->combL[i].damp2 = 1.0f - rev->damp1;
}
}
@ -186,7 +186,7 @@ static void revmodel_setmode(struct revmodel *rev, float value)
revmodel_update(rev);
}
static void revmodel_init(struct revmodel *rev,int srate, bool right)
static void revmodel_init(struct revmodel *rev, int srate, bool right)
{
unsigned c;
static const int comb_lengths[8] = { 1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 };
@ -196,19 +196,19 @@ static void revmodel_init(struct revmodel *rev,int srate, bool right)
for (c = 0; c < NUMCOMBS; ++c)
{
rev->bufcomb[c] = malloc(r*comb_lengths[c]+stereosep*sizeof(float));
rev->combL[c].buffer = rev->bufcomb[c];
memset(rev->combL[c].buffer,0,r*comb_lengths[c]+stereosep*sizeof(float));
rev->combL[c].bufsize=r*comb_lengths[c]+stereosep;
rev->bufcomb[c] = malloc(r * comb_lengths[c] + stereosep * sizeof(float));
rev->combL[c].buffer = rev->bufcomb[c];
memset(rev->combL[c].buffer, 0, r * comb_lengths[c] + stereosep * sizeof(float));
rev->combL[c].bufsize = r * comb_lengths[c] + stereosep;
}
for (c = 0; c < NUMALLPASSES; ++c)
{
rev->bufallpass[c] = malloc(r*allpass_lengths[c]+stereosep*sizeof(float));
rev->allpassL[c].buffer = rev->bufallpass[c];
memset(rev->allpassL[c].buffer,0,r*allpass_lengths[c]+stereosep*sizeof(float));
rev->allpassL[c].bufsize=r*allpass_lengths[c]+stereosep;
rev->allpassL[c].feedback = 0.5f;
rev->bufallpass[c] = malloc(r * allpass_lengths[c] + stereosep * sizeof(float));
rev->allpassL[c].buffer = rev->bufallpass[c];
memset(rev->allpassL[c].buffer, 0, r * allpass_lengths[c] + stereosep * sizeof(float));
rev->allpassL[c].bufsize = r * allpass_lengths[c] + stereosep;
rev->allpassL[c].feedback = 0.5f;
}
revmodel_setwet(rev, initialwet);
@ -226,8 +226,8 @@ struct reverb_data
static void reverb_free(void *data)
{
struct reverb_data *rev = (struct reverb_data*)data;
unsigned i;
struct reverb_data *rev = (struct reverb_data*)data;
for (i = 0; i < NUMCOMBS; i++)
{

View file

@ -32,11 +32,11 @@
struct tremolo_core
{
float freq;
float depth;
float* wavetable;
int index;
int maxindex;
float *wavetable;
float freq;
float depth;
int index;
int maxindex;
};
struct tremolo
@ -54,24 +54,25 @@ static void tremolo_free(void *data)
static void tremolocore_init(struct tremolo_core *core,float depth,int samplerate,float freq)
{
const double offset = 1. - depth / 2.;
unsigned i;
double env;
core->index = 0;
core->maxindex = samplerate/freq;
core->wavetable = malloc(core->maxindex*sizeof(float));
memset(core->wavetable, 0, core->maxindex * sizeof(float));
for (i = 0; i < core->maxindex; i++) {
env = freq * i / samplerate;
env = sin((M_PI*2) * fmod(env + 0.25, 1.0));
core->wavetable[i] = env * (1 - fabs(offset)) + offset;
}
double env;
unsigned i;
const double offset = 1. - depth / 2.;
core->index = 0;
core->maxindex = samplerate / freq;
core->wavetable = malloc(core->maxindex * sizeof(float));
memset(core->wavetable, 0, core->maxindex * sizeof(float));
for (i = 0; i < core->maxindex; i++)
{
env = freq * i / samplerate;
env = sin((M_PI*2) * fmod(env + 0.25, 1.0));
core->wavetable[i] = env * (1 - fabs(offset)) + offset;
}
}
float tremolocore_core(struct tremolo_core *core,float in)
{
core->index = core->index % core->maxindex;
return in * core->wavetable[core->index++];
core->index = core->index % core->maxindex;
return in * core->wavetable[core->index++];
}
static void tremolo_process(void *data, struct dspfilter_output *output,
@ -81,16 +82,15 @@ static void tremolo_process(void *data, struct dspfilter_output *output,
float *out;
struct tremolo *tre = (struct tremolo*)data;
output->samples = input->samples;
output->frames = input->frames;
out = output->samples;
output->samples = input->samples;
output->frames = input->frames;
out = output->samples;
for (i = 0; i < input->frames; i++, out += 2)
{
float in[2] = { out[0], out[1] };
out[0] = tremolocore_core(&tre->left, in[0]);
out[1] = tremolocore_core(&tre->right, in[1]);
float in[2] = { out[0], out[1] };
out[0] = tremolocore_core(&tre->left, in[0]);
out[1] = tremolocore_core(&tre->right, in[1]);
}
}
@ -98,7 +98,7 @@ static void *tremolo_init(const struct dspfilter_info *info,
const struct dspfilter_config *config, void *userdata)
{
float freq, depth;
struct tremolo *tre = (struct tremolo*)calloc(1, sizeof(*tre));
struct tremolo *tre = (struct tremolo*)calloc(1, sizeof(*tre));
if (!tre)
return NULL;
@ -125,7 +125,6 @@ static const struct dspfilter_implementation tremolo_plug = {
const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &tremolo_plug;
}

View file

@ -30,31 +30,30 @@
#define sqr(a) ((a) * (a))
const float BASE_DELAY_SEC = 0.002; // 2 ms
const float VIBRATO_FREQUENCY_DEFAULT_HZ = 2;
const float VIBRATO_FREQUENCY_MAX_HZ = 14;
const float VIBRATO_DEPTH_DEFAULT_PERCENT = 50;
const int add_delay = 3;
static const float BASE_DELAY_SEC = 0.002; /* 2 ms */
static const float VIBRATO_FREQUENCY_DEFAULT_HZ = 2;
static const float VIBRATO_FREQUENCY_MAX_HZ = 14;
static const float VIBRATO_DEPTH_DEFAULT_PERCENT = 50;
static const int add_delay = 3;
float hermite_interp(float x, float *y)
static float hermite_interp(float x, float *y)
{
float c0, c1, c2, c3;
c0 = y[1];
c1 = (1.0 / 2.0)*(y[2] - y[0]);
c2 = (y[0] - (5.0 / 2.0)*y[1]) + (2.0*y[2] - (1.0 / 2.0)*y[3]);
c3 = (1.0 / 2.0)*(y[3] - y[0]) + (3.0 / 2.0)*(y[1] - y[2]);
return ((c3*x + c2)*x + c1)*x + c0;
float c0 = y[1];
float c1 = (1.0f / 2.0f) * (y[2] - y[0]);
float c2 = (y[0] - (5.0f / 2.0f) * y[1]) + (2.0f * y[2] - (1.0f / 2.0f) * y[3]);
float c3 = (1.0f / 2.0f) * (y[3] - y[0]) + (3.0f / 2.0f) * (y[1] - y[2]);
return ((c3 * x + c2) * x + c1) * x + c0;
}
struct vibrato_core
{
float freq;
float samplerate;
int phase;
float depth;
float* buffer;
int writeindex;
int size;
float* buffer;
float freq;
float samplerate;
float depth;
int phase;
int writeindex;
int size;
};
struct vibrato
@ -72,60 +71,62 @@ static void vibrato_free(void *data)
static void vibratocore_init(struct vibrato_core *core,float depth,int samplerate,float freq)
{
core->size = BASE_DELAY_SEC * samplerate * 2;
core->buffer = malloc((core->size + add_delay)*sizeof(float));
memset(core->buffer, 0, (core->size + add_delay) * sizeof(float));
core->size = BASE_DELAY_SEC * samplerate * 2;
core->buffer = malloc((core->size + add_delay) * sizeof(float));
memset(core->buffer, 0, (core->size + add_delay) * sizeof(float));
core->samplerate = samplerate;
core->freq = freq;
core->depth = depth;
core->phase = 0;
core->freq = freq;
core->depth = depth;
core->phase = 0;
core->writeindex = 0;
}
float vibratocore_core(struct vibrato_core *core,float in)
{
float M = core->freq / core->samplerate;
int maxphase = core->samplerate / core->freq;
float lfo = sin(M * 2. * M_PI * core->phase++);
core->phase = core->phase % maxphase;
lfo = (lfo + 1) * 1.; // transform from [-1; 1] to [0; 1]
int maxdelay = BASE_DELAY_SEC * core->samplerate;
float delay = lfo * core->depth * maxdelay;
delay += add_delay;
float readindex = core->writeindex - 1 - delay;
while (readindex < 0)readindex += core->size;
while (readindex >= core->size)readindex -= core->size;
int ipart = (int)readindex; // integer part of the delay
float fpart = readindex - ipart; // fractional part of the delay
float value = hermite_interp(fpart, &(core->buffer[ipart]));
core->buffer[core->writeindex] = in;
if (core->writeindex < add_delay){
core->buffer[core->size + core->writeindex] = in;
}
core->writeindex++;
if (core->writeindex == core->size) {
core->writeindex = 0;
}
return value;
int ipart;
float delay, readindex, fpart, value;
float M = core->freq / core->samplerate;
int maxphase = core->samplerate / core->freq;
float lfo = sin(M * 2. * M_PI * core->phase++);
int maxdelay = BASE_DELAY_SEC * core->samplerate;
core->phase = core->phase % maxphase;
lfo = (lfo + 1) * 1.; // transform from [-1; 1] to [0; 1]
delay = lfo * core->depth * maxdelay;
delay += add_delay;
readindex = core->writeindex - 1 - delay;
while (readindex < 0)
readindex += core->size;
while (readindex >= core->size)
readindex -= core->size;
ipart = (int)readindex; /* Integer part of the delay */
fpart = readindex - ipart; /* fractional part of the delay */
value = hermite_interp(fpart, &(core->buffer[ipart]));
core->buffer[core->writeindex] = in;
if (core->writeindex < add_delay)
core->buffer[core->size + core->writeindex] = in;
core->writeindex++;
if (core->writeindex == core->size)
core->writeindex = 0;
return value;
}
static void vibrato_process(void *data, struct dspfilter_output *output,
static void vibrato_process(void *data,
struct dspfilter_output *output,
const struct dspfilter_input *input)
{
unsigned i;
float *out;
struct vibrato *vib = (struct vibrato*)data;
output->samples = input->samples;
output->frames = input->frames;
out = output->samples;
output->samples = input->samples;
output->frames = input->frames;
out = output->samples;
for (i = 0; i < input->frames; i++, out += 2)
{
float in[2] = { out[0], out[1] };
out[0] = vibratocore_core(&vib->left, in[0]);
out[1] = vibratocore_core(&vib->right, in[1]);
out[0] = vibratocore_core(&vib->left, in[0]);
out[1] = vibratocore_core(&vib->right, in[1]);
}
}
@ -133,7 +134,7 @@ static void *vibrato_init(const struct dspfilter_info *info,
const struct dspfilter_config *config, void *userdata)
{
float freq, depth;
struct vibrato *vib = (struct vibrato*)calloc(1, sizeof(*vib));
struct vibrato *vib = (struct vibrato*)calloc(1, sizeof(*vib));
if (!vib)
return NULL;
@ -160,7 +161,6 @@ static const struct dspfilter_implementation vibrato_plug = {
const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
{
(void)mask;
return &vibrato_plug;
}

View file

@ -68,39 +68,39 @@ static void wahwah_process(void *data, struct dspfilter_output *output,
if ((wah->skipcount++ % WAHWAH_LFO_SKIP_SAMPLES) == 0)
{
float omega, sn, cs, alpha;
float frequency = (1.0 + cos(wah->skipcount * wah->lfoskip + wah->phase)) / 2.0;
float frequency = (1.0f + cos(wah->skipcount * wah->lfoskip + wah->phase)) / 2.0f;
frequency = frequency * wah->depth * (1.0 - wah->freqofs) + wah->freqofs;
frequency = exp((frequency - 1.0) * 6.0);
frequency = frequency * wah->depth * (1.0f - wah->freqofs) + wah->freqofs;
frequency = exp((frequency - 1.0f) * 6.0f);
omega = M_PI * frequency;
sn = sin(omega);
cs = cos(omega);
alpha = sn / (2.0 * wah->res);
omega = M_PI * frequency;
sn = sin(omega);
cs = cos(omega);
alpha = sn / (2.0f * wah->res);
wah->b0 = (1.0 - cs) / 2.0;
wah->b1 = 1.0 - cs;
wah->b2 = (1.0 - cs) / 2.0;
wah->a0 = 1.0 + alpha;
wah->a1 = -2.0 * cs;
wah->a2 = 1.0 - alpha;
wah->b0 = (1.0f - cs) / 2.0f;
wah->b1 = 1.0f - cs;
wah->b2 = (1.0f - cs) / 2.0f;
wah->a0 = 1.0f + alpha;
wah->a1 = -2.0f * cs;
wah->a2 = 1.0f - alpha;
}
out_l = (wah->b0 * in[0] + wah->b1 * wah->l.xn1 + wah->b2 * wah->l.xn2 - wah->a1 * wah->l.yn1 - wah->a2 * wah->l.yn2) / wah->a0;
out_r = (wah->b0 * in[1] + wah->b1 * wah->r.xn1 + wah->b2 * wah->r.xn2 - wah->a1 * wah->r.yn1 - wah->a2 * wah->r.yn2) / wah->a0;
out_l = (wah->b0 * in[0] + wah->b1 * wah->l.xn1 + wah->b2 * wah->l.xn2 - wah->a1 * wah->l.yn1 - wah->a2 * wah->l.yn2) / wah->a0;
out_r = (wah->b0 * in[1] + wah->b1 * wah->r.xn1 + wah->b2 * wah->r.xn2 - wah->a1 * wah->r.yn1 - wah->a2 * wah->r.yn2) / wah->a0;
wah->l.xn2 = wah->l.xn1;
wah->l.xn1 = in[0];
wah->l.yn2 = wah->l.yn1;
wah->l.yn1 = out_l;
wah->l.xn2 = wah->l.xn1;
wah->l.xn1 = in[0];
wah->l.yn2 = wah->l.yn1;
wah->l.yn1 = out_l;
wah->r.xn2 = wah->r.xn1;
wah->r.xn1 = in[1];
wah->r.yn2 = wah->r.yn1;
wah->r.yn1 = out_r;
wah->r.xn2 = wah->r.xn1;
wah->r.xn1 = in[1];
wah->r.yn2 = wah->r.yn1;
wah->r.yn1 = out_r;
out[0] = out_l;
out[1] = out_r;
out[0] = out_l;
out[1] = out_r;
}
}
@ -117,8 +117,8 @@ static void *wahwah_init(const struct dspfilter_info *info,
config->get_float(userdata, "depth", &wah->depth, 0.7f);
config->get_float(userdata, "resonance", &wah->res, 2.5f);
wah->lfoskip = wah->freq * 2.0 * M_PI / info->input_rate;
wah->phase = wah->startphase * M_PI / 180.0;
wah->lfoskip = wah->freq * 2.0f * M_PI / info->input_rate;
wah->phase = wah->startphase * M_PI / 180.0f;
return wah;
}

View file

@ -68,24 +68,17 @@ const static unsigned char unb64[]={
*/
char* base64(const void* binaryData, int len, int *flen)
{
const unsigned char* bin = (const unsigned char*) binaryData;
char* res;
int rc = 0; /* result counter */
int byteNo; /* I need this after the loop */
int modulusLen = len % 3 ;
const unsigned char* bin = (const unsigned char*) binaryData;
int rc = 0; /* result counter */
int modulusLen = len % 3 ;
/* 2 gives 1 and 1 gives 2, but 0 gives 0. */
int pad = ((modulusLen&1)<<1) + ((modulusLen&2)>>1);
int pad = ((modulusLen&1)<<1) + ((modulusLen&2)>>1);
*flen = 4*(len + pad)/3;
res = (char*) malloc(*flen + 1); /* and one for the null */
if (!res)
{
/* ERROR: base64 could not allocate enough memory. */
*flen = 4*(len + pad)/3;
if (!(res = (char*) malloc(*flen + 1))) /* and one for the NULL */
return 0;
}
for (byteNo=0; byteNo <= len-3; byteNo+=3)
{
@ -120,34 +113,29 @@ char* base64(const void* binaryData, int len, int *flen)
unsigned char* unbase64(const char* ascii, int len, int *flen)
{
const unsigned char *safeAsciiPtr = (const unsigned char*) ascii;
unsigned char *bin;
int cb = 0;
int charNo;
unsigned char *bin;
const unsigned char *safeAsciiPtr = (const unsigned char*) ascii;
int cb = 0;
int pad = 0;
if (len < 2) { /* 2 accesses below would be OOB. */
if (len < 2) /* 2 accesses below would be OOB (Out Of Bounds). */
{
/* catch empty string, return NULL as result. */
/* ERROR: You passed an invalid base64 string (too short).
* You get NULL back. */
*flen = 0;
return 0;
}
if(safeAsciiPtr[len-1]=='=')
if (safeAsciiPtr[len-1]=='=')
++pad;
if(safeAsciiPtr[len-2]=='=')
if (safeAsciiPtr[len-2]=='=')
++pad;
*flen = 3*len/4 - pad;
bin = (unsigned char*)malloc(*flen);
if (!bin)
{
/* ERROR: unbase64 could not allocate enough memory. */
if (!(bin = (unsigned char*)malloc(*flen)))
return 0;
}
for (charNo=0; charNo <= len-4-pad; charNo+=4)
{

View file

@ -165,12 +165,12 @@ static void dump_content(RFILE *file, const void *frame,
{
/* BGR24 byte order input matches output. Can directly copy, but... need to make sure we pad it. */
uint32_t zeros = 0;
int pad = (int)(line_size-pitch);
int padding = (int)(line_size-pitch);
for (j = 0; j < height; j++, u.u8 += pitch)
{
filestream_write(file, u.u8, pitch);
if(pad != 0)
filestream_write(file, &zeros, pad);
if (padding != 0)
filestream_write(file, &zeros, padding);
}
}
break;
@ -184,8 +184,7 @@ static void dump_content(RFILE *file, const void *frame,
}
/* allocate line buffer, and initialize the final four bytes to zero, for deterministic padding */
line = (uint8_t*)malloc(line_size);
if (!line)
if (!(line = (uint8_t*)malloc(line_size)))
return;
*(uint32_t*)(line + line_size - 4) = 0;

View file

@ -118,7 +118,7 @@ rxml_document_t *rxml_load_document(const char *path)
error:
free(memory_buffer);
if(file)
if (file)
filestream_close(file);
return NULL;
}

View file

@ -210,6 +210,208 @@ extern int audioAddData(uint32_t portNum, float *data,
#endif
/*============================================================
INPUT KEYBOARD PROTOTYPES
============================================================ */
#ifdef __PSL1GHT__
#include <io/kb.h>
#define CELL_KB_RMODE_INPUTCHAR KB_RMODE_INPUTCHAR
#define CELL_KB_CODETYPE_RAW KB_CODETYPE_RAW
#define cellKbData KbData
#define cellKbInfo KbInfo
#define cellKbSetCodeType ioKbSetCodeType
#define cellKbSetReadMode ioKbSetReadMode
#define cellKbInit ioKbInit
#define cellKbGetInfo ioKbGetInfo
#define cellKbRead ioKbRead
#else
#include <cell/keyboard.h>
#define KB_RMODE_INPUTCHAR CELL_KB_RMODE_INPUTCHAR
#define KB_CODETYPE_RAW CELL_KB_CODETYPE_RAW
#define KbInfo cellKbInfo
#define ioKbSetCodeType cellKbSetCodeType
#define ioKbSetReadMode cellKbSetReadMode
#define ioKbInit cellKbInit
#define ioKbGetInfo cellKbGetInfo
#define ioKbRead cellKbRead
/* Keyboard RAWDAT Key code (can't be converted to ASCII codes) */
#define KB_RAWKEY_NO_EVENT 0x00
#define KB_RAWKEY_E_ROLLOVER 0x01
#define KB_RAWKEY_E_POSTFAIL 0x02
#define KB_RAWKEY_E_UNDEF 0x03
#define KB_RAWKEY_ESCAPE 0x29
#define KB_RAWKEY_106_KANJI 0x35 /* The half-width/full width Kanji key code */
#define KB_RAWKEY_CAPS_LOCK 0x39
#define KB_RAWKEY_F1 0x3a
#define KB_RAWKEY_F2 0x3b
#define KB_RAWKEY_F3 0x3c
#define KB_RAWKEY_F4 0x3d
#define KB_RAWKEY_F5 0x3e
#define KB_RAWKEY_F6 0x3f
#define KB_RAWKEY_F7 0x40
#define KB_RAWKEY_F8 0x41
#define KB_RAWKEY_F9 0x42
#define KB_RAWKEY_F10 0x43
#define KB_RAWKEY_F11 0x44
#define KB_RAWKEY_F12 0x45
#define KB_RAWKEY_PRINTSCREEN 0x46
#define KB_RAWKEY_SCROLL_LOCK 0x47
#define KB_RAWKEY_PAUSE 0x48
#define KB_RAWKEY_INSERT 0x49
#define KB_RAWKEY_HOME 0x4a
#define KB_RAWKEY_PAGE_UP 0x4b
#define KB_RAWKEY_DELETE 0x4c
#define KB_RAWKEY_END 0x4d
#define KB_RAWKEY_PAGE_DOWN 0x4e
#define KB_RAWKEY_RIGHT_ARROW 0x4f
#define KB_RAWKEY_LEFT_ARROW 0x50
#define KB_RAWKEY_DOWN_ARROW 0x51
#define KB_RAWKEY_UP_ARROW 0x52
#define KB_RAWKEY_NUM_LOCK 0x53
#define KB_RAWKEY_APPLICATION 0x65 /* Application key code */
#define KB_RAWKEY_KANA 0x88 /* Katakana/Hiragana/Romaji key code */
#define KB_RAWKEY_HENKAN 0x8a /* Conversion key code */
#define KB_RAWKEY_MUHENKAN 0x8b /* No Conversion key code */
/* Keyboard RAW Key Code definition */
#define KB_RAWKEY_A 0x04
#define KB_RAWKEY_B 0x05
#define KB_RAWKEY_C 0x06
#define KB_RAWKEY_D 0x07
#define KB_RAWKEY_E 0x08
#define KB_RAWKEY_F 0x09
#define KB_RAWKEY_G 0x0A
#define KB_RAWKEY_H 0x0B
#define KB_RAWKEY_I 0x0C
#define KB_RAWKEY_J 0x0D
#define KB_RAWKEY_K 0x0E
#define KB_RAWKEY_L 0x0F
#define KB_RAWKEY_M 0x10
#define KB_RAWKEY_N 0x11
#define KB_RAWKEY_O 0x12
#define KB_RAWKEY_P 0x13
#define KB_RAWKEY_Q 0x14
#define KB_RAWKEY_R 0x15
#define KB_RAWKEY_S 0x16
#define KB_RAWKEY_T 0x17
#define KB_RAWKEY_U 0x18
#define KB_RAWKEY_V 0x19
#define KB_RAWKEY_W 0x1A
#define KB_RAWKEY_X 0x1B
#define KB_RAWKEY_Y 0x1C
#define KB_RAWKEY_Z 0x1D
#define KB_RAWKEY_1 0x1E
#define KB_RAWKEY_2 0x1F
#define KB_RAWKEY_3 0x20
#define KB_RAWKEY_4 0x21
#define KB_RAWKEY_5 0x22
#define KB_RAWKEY_6 0x23
#define KB_RAWKEY_7 0x24
#define KB_RAWKEY_8 0x25
#define KB_RAWKEY_9 0x26
#define KB_RAWKEY_0 0x27
#define KB_RAWKEY_ENTER 0x28
#define KB_RAWKEY_ESC 0x29
#define KB_RAWKEY_BS 0x2A
#define KB_RAWKEY_TAB 0x2B
#define KB_RAWKEY_SPACE 0x2C
#define KB_RAWKEY_MINUS 0x2D
#define KB_RAWKEY_EQUAL_101 0x2E /* = and + */
#define KB_RAWKEY_ACCENT_CIRCONFLEX_106 0x2E /* ^ and ~ */
#define KB_RAWKEY_LEFT_BRACKET_101 0x2F /* [ */
#define KB_RAWKEY_ATMARK_106 0x2F /* @ */
#define KB_RAWKEY_RIGHT_BRACKET_101 0x30 /* ] */
#define KB_RAWKEY_LEFT_BRACKET_106 0x30 /* [ */
#define KB_RAWKEY_BACKSLASH_101 0x31 /* \ and | */
#define KB_RAWKEY_RIGHT_BRACKET_106 0x32 /* ] */
#define KB_RAWKEY_SEMICOLON 0x33 /* ; */
#define KB_RAWKEY_QUOTATION_101 0x34 /* ' and " */
#define KB_RAWKEY_COLON_106 0x34 /* : and * */
#define KB_RAWKEY_COMMA 0x36
#define KB_RAWKEY_PERIOD 0x37
#define KB_RAWKEY_SLASH 0x38
#define KB_RAWKEY_CAPS_LOCK 0x39
#define KB_RAWKEY_KPAD_NUMLOCK 0x53
#define KB_RAWKEY_KPAD_SLASH 0x54
#define KB_RAWKEY_KPAD_ASTERISK 0x55
#define KB_RAWKEY_KPAD_MINUS 0x56
#define KB_RAWKEY_KPAD_PLUS 0x57
#define KB_RAWKEY_KPAD_ENTER 0x58
#define KB_RAWKEY_KPAD_1 0x59
#define KB_RAWKEY_KPAD_2 0x5A
#define KB_RAWKEY_KPAD_3 0x5B
#define KB_RAWKEY_KPAD_4 0x5C
#define KB_RAWKEY_KPAD_5 0x5D
#define KB_RAWKEY_KPAD_6 0x5E
#define KB_RAWKEY_KPAD_7 0x5F
#define KB_RAWKEY_KPAD_8 0x60
#define KB_RAWKEY_KPAD_9 0x61
#define KB_RAWKEY_KPAD_0 0x62
#define KB_RAWKEY_KPAD_PERIOD 0x63
#define KB_RAWKEY_BACKSLASH_106 0x87
#define KB_RAWKEY_YEN_106 0x89
#define KB_CODETYPE_RAW CELL_KB_CODETYPE_RAW
/*! \brief Keyboard Led State. */
typedef struct KbLed
{
union
{
u32 leds;
struct
{
u32 reserved : 27; /*!< \brief Reserved MSB */
u32 kana : 1; /*!< \brief LED Kana 0:OFF 1:ON Bit4 */
u32 compose : 1; /*!< \brief LED Compose 0:OFF 1:ON Bit3 */
u32 scroll_lock : 1; /*!< \brief LED Scroll Lock 0:OFF 1:ON Bit2 */
u32 caps_lock : 1; /*!< \brief LED Caps Lock 0:OFF 1:ON Bit1 */
u32 num_lock : 1; /*!< \brief LED Num Lock 0:OFF 1:ON Bit0 LSB */
}_KbLedS;
}_KbLedU;
} KbLed;
/*! \brief Keyboard Modifier Key State. */
typedef struct KbMkey
{
union
{
u32 mkeys;
struct
{
u32 reserved : 24; /*!< \brief Reserved MSB */
u32 r_win : 1; /*!< \brief Modifier Key Right WIN 0:OFF 1:ON Bit7 */
u32 r_alt : 1; /*!< \brief Modifier Key Right ALT 0:OFF 1:ON Bit6 */
u32 r_shift : 1; /*!< \brief Modifier Key Right SHIFT 0:OFF 1:ON Bit5 */
u32 r_ctrl : 1; /*!< \brief Modifier Key Right CTRL 0:OFF 1:ON Bit4 */
u32 l_win : 1; /*!< \brief Modifier Key Left WIN 0:OFF 1:ON Bit3 */
u32 l_alt : 1; /*!< \brief Modifier Key Left ALT 0:OFF 1:ON Bit2 */
u32 l_shift : 1; /*!< \brief Modifier Key Left SHIFT 0:OFF 1:ON Bit1 */
u32 l_ctrl : 1; /*!< \brief Modifier Key Left CTRL 0:OFF 1:ON Bit0 LSB */
/* For Macintosh Keyboard ALT & WIN correspond respectively to OPTION & APPLE keys */
}_KbMkeyS;
}_KbMkeyU;
} KbMkey;
/*! \brief Keyboard input data data structure. */
typedef struct KbData
{
KbLed led; /*!< \brief Keyboard Led State */
KbMkey mkey; /*!< \brief Keyboard Modifier Key State */
s32 nb_keycode; /*!< \brief Number of key codes (0 equal no data) */
u16 keycode[MAX_KEYCODES]; /*!< \brief Keycode values */
} KbData;
#endif
/*============================================================
OSK PROTOTYPES
============================================================ */

View file

@ -75,22 +75,22 @@ static INLINE int16_t tofloat16(float f)
e = ((i >> 23) & 0x000000ff) - (127 - 15);
m = i & 0x007fffff;
if(e <= 0)
if (e <= 0)
{
if(e < -10)
if (e < -10)
return (int16_t)(s);
m = (m | 0x00800000) >> (1 - e);
if(m & 0x00001000)
if (m & 0x00001000)
m += 0x00002000;
return (int16_t)(s | (m >> 13));
}
if(e == 0xff - (127 - 15))
if (e == 0xff - (127 - 15))
{
if(m == 0)
if (m == 0)
return (int16_t)(s | 0x7c00);
m >>= 13;
@ -98,11 +98,11 @@ static INLINE int16_t tofloat16(float f)
return (int16_t)(s | 0x7c00 | m | (m == 0));
}
if(m & 0x00001000)
if (m & 0x00001000)
{
m += 0x00002000;
if(m & 0x00800000)
if (m & 0x00800000)
{
m = 0;
e += 1;

View file

@ -66,19 +66,20 @@ static void crash(void)
cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
size = (size + 1023) & ~1023;
uint64_t *ptr = NULL;
cothread_t handle = 0;
size = (size + 1023) & ~1023;
#if HAVE_POSIX_MEMALIGN >= 1
if (posix_memalign(&handle, 1024, size + 512) < 0)
return 0;
#else
handle = memalign(1024, size + 512);
handle = memalign(1024, size + 512);
#endif
if (!handle)
return handle;
uint64_t *ptr = (uint64_t*)handle;
ptr = (uint64_t*)handle;
/* Non-volatiles. */
ptr[0] = 0; /* x8 */
ptr[1] = 0; /* x9 */

View file

@ -20,7 +20,7 @@ extern "C" {
static thread_local long long co_active_buffer[64];
static thread_local cothread_t co_active_handle = 0;
#ifndef CO_USE_INLINE_ASM
static void (*co_swap)(cothread_t, cothread_t) = 0;
static void (*co_swap)(cothread_t, cothread_t) = 0;
#endif
#ifdef _WIN32
@ -137,9 +137,8 @@ cothread_t co_active(void)
cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
cothread_t handle;
#ifndef CO_USE_INLINE_ASM
if(!co_swap)
if (!co_swap)
{
co_init();
co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
@ -152,15 +151,15 @@ cothread_t co_create(unsigned int size, void (*entrypoint)(void))
size &= ~15; /* align stack to 16-byte boundary */
#ifdef __GENODE__
if((handle = (cothread_t)genode_alloc_secondary_stack(size)))
if ((handle = (cothread_t)genode_alloc_secondary_stack(size)))
{
long long *p = (long long*)((char*)handle); /* OS returns top of stack */
*--p = (long long)crash; /* crash if entrypoint returns */
*--p = (long long)entrypoint; /* start of function */
*(long long*)handle = (long long)p; /* stack pointer */
long long *p = (long long*)((char*)handle); /* OS returns top of stack */
*--p = (long long)crash; /* crash if entrypoint returns */
*--p = (long long)entrypoint; /* start of function */
*(long long*)handle = (long long)p; /* stack pointer */
}
#else
if((handle = (cothread_t)malloc(size)))
if ((handle = (cothread_t)malloc(size)))
{
long long *p = (long long*)((char*)handle + size); /* seek to top of stack */
*--p = (long long)crash; /* crash if entrypoint returns */

View file

@ -58,8 +58,9 @@ void co_switch_arm(cothread_t handle, cothread_t current);
cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
size = (size + 1023) & ~1023;
uint32_t *ptr = NULL;
cothread_t handle = 0;
size = (size + 1023) & ~1023;
#if defined(__APPLE__) || HAVE_POSIX_MEMALIGN >= 1
if (posix_memalign(&handle, 1024, size + 256) < 0)
return 0;
@ -70,7 +71,7 @@ cothread_t co_create(unsigned int size, void (*entrypoint)(void))
if (!handle)
return handle;
uint32_t *ptr = (uint32_t*)handle;
ptr = (uint32_t*)handle;
/* Non-volatiles. */
ptr[0] = 0; /* r4 */
ptr[1] = 0; /* r5 */

View file

@ -24,7 +24,7 @@ static void __stdcall co_thunk(void *coentry)
cothread_t co_active(void)
{
if(!co_active_)
if (!co_active_)
{
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
ConvertThreadToFiberEx(0, FIBER_FLAG_FLOAT_SWITCH);
@ -38,7 +38,7 @@ cothread_t co_active(void)
cothread_t co_create(unsigned int heapsize, void (*coentry)(void))
{
if(!co_active_)
if (!co_active_)
{
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
ConvertThreadToFiberEx(0, FIBER_FLAG_FLOAT_SWITCH);

View file

@ -13,13 +13,15 @@
extern "C"
void *genode_alloc_secondary_stack(unsigned long stack_size)
{
try {
return Genode::Thread::myself()->alloc_secondary_stack("libco", stack_size); }
catch (...) {
try
{
return Genode::Thread::myself()->alloc_secondary_stack("libco", stack_size);
}
catch (...)
{
Genode::error("libco: failed to allocate ", stack_size, " byte secondary stack");
return nullptr;
}
}
extern "C"

View file

@ -16,8 +16,8 @@ floating-point and AltiVec save/restore */
#define LIBCO_MPROTECT (__unix__ && !LIBCO_PPC_ASM)
#if LIBCO_MPROTECT
#include <unistd.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/mman.h>
#endif
/* State format (offsets in 32-bit words)
@ -58,8 +58,8 @@ or are directly to function */
#endif
/* Swap code is in ppc.S */
void co_swap_asm( cothread_t, cothread_t );
#define CO_SWAP_ASM( x, y ) co_swap_asm( x, y )
void co_swap_asm(cothread_t, cothread_t);
#define CO_SWAP_ASM(x, y) co_swap_asm(x, y)
#else
@ -272,122 +272,118 @@ static const uint32_t libco_ppc_code [] = {
#if LIBCO_PPCDESC
/* Function call goes through indirect descriptor */
#define CO_SWAP_ASM( x, y ) \
((void (*)( cothread_t, cothread_t )) (uintptr_t) x)( x, y )
#define CO_SWAP_ASM(x, y) \
((void (*)(cothread_t, cothread_t)) (uintptr_t) x)(x, y)
#else
/* Function call goes directly to code */
#define CO_SWAP_ASM( x, y ) \
((void (*)( cothread_t, cothread_t )) (uintptr_t) libco_ppc_code)( x, y )
#define CO_SWAP_ASM(x, y) \
((void (*)(cothread_t, cothread_t)) (uintptr_t) libco_ppc_code)(x, y)
#endif
#endif
static uint32_t* co_create_( unsigned size, uintptr_t entry )
static uint32_t* co_create_( unsigned size, uintptr_t entry)
{
uint32_t* t = (uint32_t*) malloc( size );
uint32_t *t = (uint32_t*)malloc(size);
(void) entry;
#if LIBCO_PPCDESC
if (t)
{
/* Copy entry's descriptor */
memcpy(t, (void*)entry, sizeof(void*) * 3);
#if LIBCO_PPCDESC
if ( t )
{
/* Copy entry's descriptor */
memcpy( t, (void*) entry, sizeof (void*) * 3 );
/* Set function pointer to swap routine */
#ifdef LIBCO_PPC_ASM
*(const void**) t = *(void**) &co_swap_asm;
#else
*(const void**) t = libco_ppc_code;
#endif
}
/* Set function pointer to swap routine */
#ifdef LIBCO_PPC_ASM
*(const void**) t = *(void**) &co_swap_asm;
#else
*(const void**) t = libco_ppc_code;
#endif
}
#endif
return t;
}
cothread_t co_create( unsigned int size, void (*entry_)( void ) )
cothread_t co_create(unsigned int size, void (*entry_)(void))
{
uintptr_t entry = (uintptr_t) entry_;
uint32_t* t = NULL;
uint32_t *t = NULL;
/* Be sure main thread was successfully allocated */
if ( co_active() )
if (co_active())
{
size += state_size + above_stack + stack_align;
t = co_create_( size, entry );
t = co_create_(size, entry);
}
if ( t )
{
uintptr_t sp;
int shift;
if (t)
{
uintptr_t sp;
#if LIBCO_PPC64
int shift = 16;
#else
int shift = 0;
#endif
/* Save current registers into new thread, so that any special ones will
have proper values when thread is begun */
CO_SWAP_ASM(t, t);
/* Save current registers into new thread, so that any special ones will
have proper values when thread is begun */
CO_SWAP_ASM( t, t );
#if LIBCO_PPCDESC
/* Get real address */
entry = (uintptr_t) *(void**)entry;
#endif
#if LIBCO_PPCDESC
/* Get real address */
entry = (uintptr_t) *(void**) entry;
#endif
/* Put stack near end of block, and align */
sp = (uintptr_t) t + size - above_stack;
sp -= sp % stack_align;
/* Put stack near end of block, and align */
sp = (uintptr_t) t + size - above_stack;
sp -= sp % stack_align;
/* On PPC32, we save and restore GPRs as 32 bits. For PPC64, we
save and restore them as 64 bits, regardless of the size the ABI
uses. So, we manually write pointers at the proper size. We always
save and restore at the same address, and since PPC is big-endian,
we must put the low byte first on PPC32. */
/* On PPC32, we save and restore GPRs as 32 bits. For PPC64, we
save and restore them as 64 bits, regardless of the size the ABI
uses. So, we manually write pointers at the proper size. We always
save and restore at the same address, and since PPC is big-endian,
we must put the low byte first on PPC32. */
/* If uintptr_t is 32 bits, >>32 is undefined behavior, so we do two shifts
and don't have to care how many bits uintptr_t is. */
/* If uintptr_t is 32 bits, >>32 is undefined behavior, so we do two shifts
and don't have to care how many bits uintptr_t is. */
#if LIBCO_PPC64
shift = 16;
#else
shift = 0;
#endif
/* Set up so entry will be called on next swap */
t [8] = (uint32_t) (entry >> shift >> shift);
t [9] = (uint32_t) entry;
/* Set up so entry will be called on next swap */
t [8] = (uint32_t) (entry >> shift >> shift);
t [9] = (uint32_t) entry;
t [10] = (uint32_t) (sp >> shift >> shift);
t [11] = (uint32_t) sp;
}
t [10] = (uint32_t) (sp >> shift >> shift);
t [11] = (uint32_t) sp;
}
return t;
}
void co_delete( cothread_t t )
void co_delete(cothread_t t)
{
free(t);
}
static void co_init_( void )
static void co_init_(void)
{
#if LIBCO_MPROTECT
/* TODO: pre- and post-pad PPC code so that this doesn't make other
data executable and writable */
long page_size = sysconf( _SC_PAGESIZE );
if ( page_size > 0 )
long page_size = sysconf(_SC_PAGESIZE);
if (page_size > 0)
{
uintptr_t align = page_size;
uintptr_t begin = (uintptr_t) libco_ppc_code;
uintptr_t end = begin + sizeof libco_ppc_code;
/* Align beginning and end */
end += align - 1;
end -= end % align;
begin -= begin % align;
end += align - 1;
end -= end % align;
begin -= begin % align;
mprotect( (void*) begin, end - begin, PROT_READ | PROT_WRITE | PROT_EXEC );
mprotect((void*)begin, end - begin, PROT_READ | PROT_WRITE | PROT_EXEC);
}
#endif
co_active_handle = co_create_( state_size, (uintptr_t) &co_switch );
co_active_handle = co_create_(state_size, (uintptr_t) &co_switch);
}
cothread_t co_active(void)
@ -400,8 +396,7 @@ cothread_t co_active(void)
void co_switch(cothread_t t)
{
cothread_t old = co_active_handle;
cothread_t old = co_active_handle;
co_active_handle = t;
CO_SWAP_ASM( t, old );
CO_SWAP_ASM(t, old);
}

View file

@ -19,48 +19,41 @@ cothread_t co_active()
cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
/* Similar scenario as with active_thread_id except there will only be one active_thread_id while there could be many
* new threads each with their own handle, so we create them on the heap instead and delete them manually when they're
* no longer needed in co_delete().
*/
cothread_t handle = malloc(sizeof(cothread_t));
ee_thread_t thread;
/* Similar scenario as with active_thread_id except there will only be one active_thread_id while there could be many
* new threads each with their own handle, so we create them on the heap instead and delete them manually when they're
* no longer needed in co_delete().
*/
ee_thread_t thread;
int32_t new_thread_id;
cothread_t handle = malloc(sizeof(cothread_t));
void *threadStack = (void *)malloc(size);
// u8 threadStack[size/8] __attribute__ ((aligned(16)));
void *threadStack = (void *)malloc(size);
if (!threadStack)
return -1;
if ( threadStack== NULL)
{
printf("libco: ERROR: creating threadStack\n");
return(-1);
}
thread.stack_size = size;
thread.gp_reg = &_gp;
thread.func = (void *)entrypoint;
thread.stack = threadStack;
thread.option = 0;
thread.initial_priority = 1;
thread.stack_size = size;
thread.gp_reg = &_gp;
thread.func = (void *)entrypoint;
thread.stack = threadStack;
thread.option = 0;
thread.initial_priority = 1;
int32_t new_thread_id = CreateThread(&thread);
if (new_thread_id < 0)
printf("libco: ERROR: creating thread\n");
StartThread(new_thread_id, NULL);
*(uint32_t *)handle = new_thread_id;
return handle;
new_thread_id = CreateThread(&thread);
StartThread(new_thread_id, NULL);
*(uint32_t *)handle = new_thread_id;
return handle;
}
void co_delete(cothread_t handle)
{
TerminateThread(*(uint32_t *)handle);
DeleteThread(*(uint32_t *)handle);
free(handle);
TerminateThread(*(uint32_t *)handle);
DeleteThread(*(uint32_t *)handle);
free(handle);
}
void co_switch(cothread_t handle)
{
WakeupThread(*(uint32_t *)handle);
/* Sleep the currently active thread so the new thread can start */
SleepThread();
WakeupThread(*(uint32_t *)handle);
/* Sleep the currently active thread so the new thread can start */
SleepThread();
}

View file

@ -6,9 +6,9 @@
typedef void (*entrypoint_t)(void);
cothread_t co_active()
cothread_t co_active(void)
{
return (void *) sceKernelGetThreadId();
return (void *)sceKernelGetThreadId();
}
static int thread_wrap(unsigned int argc, void *argp)

View file

@ -45,24 +45,21 @@ extern "C" {
if (block < 0)
return;
/* get base address */
ret = sceKernelGetMemBlockBase(block, &base);
if (ret < 0)
/* Get base address */
if ((ret = sceKernelGetMemBlockBase(block, &base)) < 0)
return;
/* set domain to be writable by user */
ret = sceKernelOpenVMDomain();
if (ret < 0)
/* Set domain to be writable by user */
if ((ret = sceKernelOpenVMDomain()) < 0)
return;
memcpy(base, co_swap_function, sizeof co_swap_function);
/* set domain back to read-only */
ret = sceKernelCloseVMDomain();
if (ret < 0)
/* Set domain back to read-only */
if ((ret = sceKernelCloseVMDomain()) < 0)
return;
/* flush icache */
/* Flush icache */
ret = sceKernelSyncVMDomain(block, base,
MB_ALIGN(FOUR_KB_ALIGN(sizeof co_swap_function)));
if (ret < 0)
@ -73,7 +70,8 @@ extern "C" {
cothread_t co_active(void)
{
if (!co_active_handle) co_active_handle = &co_active_buffer;
if (!co_active_handle)
co_active_handle = &co_active_buffer;
return co_active_handle;
}
@ -82,15 +80,16 @@ extern "C" {
unsigned long* handle = 0;
if (!co_swap)
co_init();
if (!co_active_handle) co_active_handle = &co_active_buffer;
size += 256;
size &= ~15;
if (!co_active_handle)
co_active_handle = &co_active_buffer;
size += 256;
size &= ~15;
if ((handle = (unsigned long*)malloc(size)))
{
unsigned long* p = (unsigned long*)((unsigned char*)handle + size);
handle[8] = (unsigned long)p;
handle[9] = (unsigned long)entrypoint;
unsigned long *p = (unsigned long*)((unsigned char*)handle + size);
handle[8] = (unsigned long)p;
handle[9] = (unsigned long)entrypoint;
}
return handle;

View file

@ -15,18 +15,17 @@ extern "C" {
static thread_local cothread_t co_active_ = 0;
typedef struct SceFiber {
typedef struct SceFiber
{
char reserved[128];
} SceFiber __attribute__( ( aligned ( 8 ) ) ) ;
int32_t _sceFiberInitializeImpl(SceFiber* fiber, char* name, void* entry, uint32_t argOnInitialize, void* addrContext, int32_t sizeContext, void* params);
/* Forward declarations */
int32_t _sceFiberInitializeImpl(SceFiber *fiber, char *name, void *entry, uint32_t argOnInitialize,
void* addrContext, int32_t sizeContext, void* params);
int32_t sceFiberFinalize(SceFiber* fiber);
int32_t sceFiberRun(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
int32_t sceFiberSwitch(SceFiber* fiber, uint32_t argOnRunTo, uint32_t* argOnRun);
int32_t sceFiberReturnToThread(uint32_t argOnReturn, uint32_t* argOnRun);
static void co_thunk(uint32_t argOnInitialize, uint32_t argOnRun)
@ -36,9 +35,9 @@ static void co_thunk(uint32_t argOnInitialize, uint32_t argOnRun)
cothread_t co_active(void)
{
if(!co_active_)
if (!co_active_)
{
sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
co_active_ = (cothread_t)1;
}
return co_active_;
@ -46,48 +45,45 @@ cothread_t co_active(void)
cothread_t co_create(unsigned int heapsize, void (*coentry)(void))
{
SceFiber* tailFiber = malloc(sizeof(SceFiber));
char * m_contextBuffer = malloc(sizeof(char)*heapsize);
if(!co_active_)
int ret;
SceFiber* tail_fiber = malloc(sizeof(SceFiber));
char * m_ctxbuf = malloc(sizeof(char)*heapsize);
if (!co_active_)
{
sceSysmoduleLoadModule(SCE_SYSMODULE_FIBER);
co_active_ = (cothread_t)1;
}
//_sceFiberInitializeImpl
int ret = _sceFiberInitializeImpl(tailFiber, "tailFiber", co_thunk, (uint32_t)coentry, (void*) m_contextBuffer, heapsize, NULL);
if(ret==0){
return (cothread_t)tailFiber;
}else{
return (cothread_t)ret;
co_active_ = (cothread_t)1;
}
/* _sceFiberInitializeImpl */
if ((ret = _sceFiberInitializeImpl(
tail_fiber, "tailFiber", co_thunk,
(uint32_t)coentry, (void*)m_ctxbuf, heapsize, NULL)) == 0)
return (cothread_t)tail_fiber;
return (cothread_t)ret;
}
void co_delete(cothread_t cothread)
{
if(cothread == (cothread_t)1){
return;
}
sceFiberFinalize((SceFiber*)cothread);
if (cothread != (cothread_t)1)
sceFiberFinalize((SceFiber*)cothread);
}
void co_switch(cothread_t cothread)
{
uint32_t argOnReturn = 0;
if(cothread == (cothread_t)1){
co_active_ = cothread;
sceFiberReturnToThread(0, NULL);
}else{
SceFiber* theFiber = (SceFiber*)cothread;
if(co_active_ == (cothread_t)1){
co_active_ = cothread;
sceFiberRun(theFiber, 0, &argOnReturn);
}else{
co_active_ = cothread;
sceFiberSwitch(theFiber, 0, &argOnReturn);
}
uint32_t argOnReturn = 0;
if (cothread == (cothread_t)1)
{
co_active_ = cothread;
sceFiberReturnToThread(0, NULL);
}
else
{
SceFiber* theFiber = (SceFiber*)cothread;
co_active_ = cothread;
if (co_active_ == (cothread_t)1)
sceFiberRun(theFiber, 0, &argOnReturn);
else
sceFiberSwitch(theFiber, 0, &argOnReturn);
}
}

View file

@ -33,7 +33,7 @@ static thread_local cothread_struct *creating, *co_running = 0;
static void springboard(int ignored)
{
if(sigsetjmp(creating->context, 0))
if (sigsetjmp(creating->context, 0))
co_running->coentry();
}
@ -46,42 +46,40 @@ cothread_t co_active(void)
cothread_t co_create(unsigned int size, void (*coentry)(void))
{
if(!co_running)
cothread_struct *thread;
if (!co_running)
co_running = &co_primary;
cothread_struct *thread = (cothread_struct*)malloc(sizeof(cothread_struct));
if(thread)
if ((thread = (cothread_struct*)malloc(sizeof(cothread_struct))))
{
stack_t stack;
stack_t old_stack;
struct sigaction handler = {{0}};
struct sigaction old_handler = {{0}};
thread->coentry = thread->stack = 0;
stack.ss_flags = 0;
stack.ss_size = size;
thread->stack = stack.ss_sp = malloc(size);
stack.ss_flags = 0;
stack.ss_size = size;
thread->stack = stack.ss_sp = malloc(size);
if(stack.ss_sp && !sigaltstack(&stack, &old_stack))
if (stack.ss_sp && !sigaltstack(&stack, &old_stack))
{
handler.sa_handler = springboard;
handler.sa_flags = SA_ONSTACK;
struct sigaction old_handler = {{0}};
struct sigaction handler = {{0}};
handler.sa_handler = springboard;
handler.sa_flags = SA_ONSTACK;
sigemptyset(&handler.sa_mask);
creating = thread;
creating = thread;
if(!sigaction(SIGUSR1, &handler, &old_handler))
if (!sigaction(SIGUSR1, &handler, &old_handler))
{
if(!raise(SIGUSR1))
thread->coentry = coentry;
if (!raise(SIGUSR1))
thread->coentry = coentry;
sigaltstack(&old_stack, 0);
sigaction(SIGUSR1, &old_handler, 0);
}
}
if(thread->coentry != coentry)
if (thread->coentry != coentry)
{
co_delete(thread);
thread = 0;
@ -95,7 +93,7 @@ void co_delete(cothread_t cothread)
{
if (cothread)
{
if(((cothread_struct*)cothread)->stack)
if (((cothread_struct*)cothread)->stack)
free(((cothread_struct*)cothread)->stack);
free(cothread);
}

View file

@ -37,13 +37,13 @@ cothread_t co_active(void)
cothread_t co_create(unsigned int heapsize, void (*coentry)(void))
{
ucontext_t *thread;
if (!co_running)
co_running = &co_primary;
ucontext_t *thread = (ucontext_t*)malloc(sizeof(ucontext_t));
if(thread)
if ((thread = (ucontext_t*)malloc(sizeof(ucontext_t))))
{
if((!getcontext(thread) && !(thread->uc_stack.ss_sp = 0)) && (thread->uc_stack.ss_sp = malloc(heapsize)))
if ((!getcontext(thread) && !(thread->uc_stack.ss_sp = 0)) && (thread->uc_stack.ss_sp = malloc(heapsize)))
{
thread->uc_link = co_running;
thread->uc_stack.ss_size = heapsize;
@ -63,7 +63,7 @@ void co_delete(cothread_t cothread)
if (!cothread)
return;
if(((ucontext_t*)cothread)->uc_stack.ss_sp)
if (((ucontext_t*)cothread)->uc_stack.ss_sp)
free(((ucontext_t*)cothread)->uc_stack.ss_sp);
free(cothread);
}
@ -71,8 +71,7 @@ void co_delete(cothread_t cothread)
void co_switch(cothread_t cothread)
{
ucontext_t *old_thread = co_running;
co_running = (ucontext_t*)cothread;
co_running = (ucontext_t*)cothread;
swapcontext(old_thread, co_running);
}

View file

@ -70,7 +70,7 @@ static void crash(void)
cothread_t co_active(void)
{
if(!co_active_handle)
if (!co_active_handle)
co_active_handle = &co_active_buffer;
return co_active_handle;
}
@ -78,24 +78,24 @@ cothread_t co_active(void)
cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
cothread_t handle;
if(!co_swap)
if (!co_swap)
{
co_init();
co_swap = (void (fastcall*)(cothread_t, cothread_t))co_swap_function;
}
if(!co_active_handle)
if (!co_active_handle)
co_active_handle = &co_active_buffer;
size += 256; /* allocate additional space for storage */
size &= ~15; /* align stack to 16-byte boundary */
if((handle = (cothread_t)malloc(size)))
if ((handle = (cothread_t)malloc(size)))
{
long *p = (long*)((char*)handle + size); /* seek to top of stack */
*--p = (long)crash; /* crash if entrypoint returns */
*--p = (long)entrypoint; /* start of function */
*(long*)handle = (long)p; /* stack pointer */
long *p = (long*)((char*)handle + size); /* seek to top of stack */
*--p = (long)crash; /* crash if entrypoint returns */
*--p = (long)entrypoint; /* start of function */
*(long*)handle = (long)p; /* stack pointer */
}
return handle;

View file

@ -189,7 +189,7 @@ struct hostent *gethostbyname(const char *name)
return NULL;
rid = sceNetResolverCreate("resolver", NULL, 0);
if(rid < 0)
if (rid < 0)
return NULL;
if (sceNetResolverStartNtoa(rid, name, &addr, 0, 0, 0) < 0)

View file

@ -242,7 +242,7 @@ static INLINE int pthread_detach(pthread_t thread)
static INLINE int pthread_join(pthread_t thread, void **retval)
{
/*retval is ignored*/
if(threadJoin((Thread)thread, INT64_MAX))
if (threadJoin((Thread)thread, INT64_MAX))
return -1;
threadFree((Thread)thread);

View file

@ -96,7 +96,7 @@ static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
#ifdef VITA
*mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
if(*mutex<0)
if (*mutex<0)
return *mutex;
return 0;
#else
@ -220,13 +220,13 @@ static INLINE int pthread_cond_init(pthread_cond_t *cond,
pthread_mutex_init(&cond->mutex,NULL);
if(cond->mutex<0)
if (cond->mutex<0)
return cond->mutex;
snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) cond);
cond->sema = sceKernelCreateSema(name_buffer, 0, 0, 1, 0);
if(cond->sema < 0)
if (cond->sema < 0)
{
pthread_mutex_destroy(&cond->mutex);
return cond->sema;
@ -269,7 +269,7 @@ static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
{
#ifdef VITA
int ret = sceKernelDeleteSema(cond->sema);
if(ret < 0)
if (ret < 0)
return ret;
return sceKernelDeleteMutex(cond->mutex);