Skip to content

Commit

Permalink
prog
Browse files Browse the repository at this point in the history
  • Loading branch information
Yisheng Jiang committed Nov 24, 2023
1 parent ca31fcd commit c729a0e
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 104 deletions.
9 changes: 4 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@
</head>

<body class="sans-serif space-between" style="height:auto !important">
<div id="ch_ctrl_bar" style="position:absolute;right:0;top:20px;z-index: 10111110;"></div>
<header>
<i class="fa-regular fa-user"></i>
<select id="sf2select" aria-placeholder="dafds">
<option>Select SF2 File</option>
</select>
<span id="midilist"></span>
<span id="ctrls"> </span>

<span id="ctrls"> </span>
<input type="file" tabindex="0" value="send file" id="file-btn" accept=".mid" />
<div id="ch_ctrl_bar"></div>
</header>

<section> <span id="sequenceroot"></span>
</section>
<div id="channelContainer"></div>

<div id="analyze"></div>
<div id="debug"></div>
<footer class="">
<div id="stdout"></div>
<span id="sequenceroot"></span>
<datalist id="programs"></datalist>
<datalist id="drums"></datalist>
<div><a href="https://github.com/yishengjiang99/sf2rend">https://github.com/yishengjiang99/sf2rend</a></div>
Expand Down
106 changes: 66 additions & 40 deletions spin/src/spin.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,50 @@ unsigned char midi_cc_vals[nmidiChannels * 128] = {0};
float outputs[MAX_VOICE_CNT * RENDQ * 2];

float silence[440] = {.0f};
float calc_pitch_diff_log(zone_t* z, pcm_t* pcm, unsigned char key);
float calc_pitch_diff_log(zone_t *z, pcm_t *pcm, unsigned char key);
int output_arr_len = MAX_VOICE_CNT * RENDQ * 2;
float volEgOut[RENDQ];
float modEgOut[RENDQ];
float lfo1Out[RENDQ];
float lfo2Out[RENDQ];
#define effect_floor(v) v <= -12000 ? 0 : calcp2over1200(v)

void sp_wipe_output_tab() {
for (int i = 0; i < output_arr_len; i++) {
void sp_wipe_output_tab()
{
for (int i = 0; i < output_arr_len; i++)
{
outputs[i] = 0.0f;
}
}
spinner* spRef(int idx) { return &sps[idx]; }
pcm_t* pcmRef(int sampleId) { return &pcms[sampleId]; }
spinner* allocate_sp() {
spinner* x = &sps[sp_idx % MAX_VOICE_CNT];
spinner *spRef(int idx) { return &sps[idx]; }
pcm_t *pcmRef(int sampleId) { return &pcms[sampleId]; }
spinner *allocate_sp()
{
spinner *x = &sps[sp_idx % MAX_VOICE_CNT];
x->outputf = &outputs[sp_idx * RENDQ * 2];
sp_idx++;
return x;
}

spinner* newSpinner(int ch) {
spinner* x = allocate_sp();
spinner *newSpinner(int ch)
{
spinner *x = allocate_sp();
x->outputf = &outputs[ch * RENDQ * 2];
x->inputf = silence;
x->channelId = ch;
return x;
}
void trigger_release(spinner* x) {
void trigger_release(spinner *x)
{
_eg_release(&x->voleg);
_eg_release(&x->modeg);
if (x->zone->SampleModes > 0) {
if (x->zone->SampleModes > 0)
{
x->is_looping = 0;
}
}
void reset(spinner* x) {
void reset(spinner *x)
{
x->position = 0;
x->stride = .0f;
x->fract = 0.0f;
Expand All @@ -67,21 +74,23 @@ void reset(spinner* x) {
x->active_dynamics_flag = 0;
}

void set_midi_cc_val(int channel, int metric, int val) {
void set_midi_cc_val(int channel, int metric, int val)
{
midi_cc_vals[channel * 128 + metric] = (char)(val & 0x7f);
}

float trigger_attack(spinner* x, uint32_t key, uint32_t velocity) {
float trigger_attack(spinner *x, uint32_t key, uint32_t velocity)
{
#define ccval(eff) midi_cc_vals[x->channelId * 128 + eff]

x->velocity = (unsigned char)velocity;
x->position = 0;
x->fract = 0.0f;
x->voleg.stage = init;
x->key = (unsigned char)(key & 0x7f);
EG* eg = &x->voleg;
EG *eg = &x->voleg;
float scaleFactor = SAMPLE_RATE / (float)x->pcm->sampleRate;
zone_t* z = x->zone;
zone_t *z = x->zone;
eg->attack = (ccval(VCA_ATTACK_TIME) > 0)
? midi_p1200[ccval(VCA_ATTACK_TIME) | 0]
: z->VolEnvAttack * scaleFactor;
Expand All @@ -102,7 +111,8 @@ float trigger_attack(spinner* x, uint32_t key, uint32_t velocity) {

eg = &x->modeg;
eg->stage = init;
if (ccval(TML_BANK_SELECT_MSB) > 0) {
if (ccval(TML_BANK_SELECT_MSB) > 0)
{
x->is_looping = 0;
}

Expand Down Expand Up @@ -146,7 +156,8 @@ float trigger_attack(spinner* x, uint32_t key, uint32_t velocity) {

return x->stride;
};
void set_spinner_input(spinner* x, pcm_t* pcm) {
void set_spinner_input(spinner *x, pcm_t *pcm)
{
x->loopStart = pcm->loopstart;
x->loopEnd = pcm->loopend;
x->inputf = pcm->data;
Expand All @@ -155,15 +166,17 @@ void set_spinner_input(spinner* x, pcm_t* pcm) {
x->position = 0;
}

float calc_pitch_diff_log(zone_t* z, pcm_t* pcm, unsigned char key) {
float calc_pitch_diff_log(zone_t *z, pcm_t *pcm, unsigned char key)
{
short rt = z->OverrideRootKey > -1 ? z->OverrideRootKey : pcm->originalPitch;
float smpl_rate = rt * 100.f + z->CoarseTune * 100.f + z->FineTune;
float diff = key * 100.f - smpl_rate;
diff += ((pcm->sampleRate - SAMPLE_RATE) / 40.96f);
return diff;
}
void set_spinner_zone(spinner* x, zone_t* z) {
pcm_t* pcm = &pcms[z->SampleId];
void set_spinner_zone(spinner *x, zone_t *z)
{
pcm_t *pcm = &pcms[z->SampleId];
set_spinner_input(x, pcm);
x->zone = z;

Expand All @@ -177,16 +190,17 @@ void set_spinner_zone(spinner* x, zone_t* z) {
x->sampleLength -= z->EndAddrOfs - (z->EndAddrCoarseOfs << 15);
}

void _spinblock(spinner* x, int n, int blockOffset) {
void _spinblock(spinner *x, int n, int blockOffset)
{
#define ccval(eff) midi_cc_vals[x->channelId * 128 + eff]

double db, dbInc;
float stride = 1.0f;
float pdiff = x->pitch_dff_log;

int ch = x->channelId;
float* output_L = &x->outputf[blockOffset];
float* output_R = &x->outputf[RENDQ + blockOffset];
float *output_L = &x->outputf[blockOffset];
float *output_R = &x->outputf[RENDQ + blockOffset];
eg_roll(&x->modeg, n, modEgOut);
eg_roll(&x->voleg, n, volEgOut);
LFO_roll_out(&x->modlfo, n, lfo1Out);
Expand All @@ -202,7 +216,8 @@ void _spinblock(spinner* x, int n, int blockOffset) {
kRateCB += (float)x->zone->Attenuation;
kRateCB += midi_volume_log10(ccval(TML_VOLUME_MSB));
kRateCB += midi_volume_log10(midi_cc_vals[ch * 128 + TML_EXPRESSION_MSB]);
if (x->voleg.stage < decay) kRateCB += midi_volume_log10(x->velocity);
if (x->voleg.stage < decay)
kRateCB += midi_volume_log10(x->velocity);

double panLeft = panleftLUT[midi_cc_vals[ch * 128 + TML_PAN_MSB]];

Expand All @@ -220,31 +235,36 @@ void _spinblock(spinner* x, int n, int blockOffset) {
float Q = x->initialQ;
short initFc = x->initialFc;

for (int i = 0; i < n; i++) {
for (int i = 0; i < n; i++)
{
db = volEgOut[i] + lfo1_volume * lfo1Out[i];
pdiff += lfo1Out[i] * lfo1_pitch + modEgOut[i] * modeg_pitch +
lfo2Out[i] * lfo2_pitch;

stride = calcp2over1200(pdiff);

fract = fract + stride;
while (fract >= 1.0f) {
while (fract >= 1.0f)
{
position++;
fract -= 1.0f;
}
if (position >= x->loopEnd && isLooping > 0) position -= looplen;
if (position >= x->loopEnd && isLooping > 0)
position -= looplen;

outputf = lerp(x->inputf[position], x->inputf[position + 1], fract);
tfc = initFc + modeg_fc * modEgOut[i] + x->lfo1_fc * lfo1Out[i];

if (position >= nsamples) {
if (position >= nsamples)
{
position = 0;
outputf = 0.0;
x->voleg.stage = done;
}
outputf = applyCentible(outputf, (short)(db + kRateCB));

if (tfc > .5) {
if (tfc > .5)
{
fchertz = timecent2hertz(tfc) / SAMPLE_RATE;
new_lpf(&lpf, fchertz, Q);
outputf = calc_lpf(&lpf, outputf);
Expand All @@ -257,37 +277,43 @@ void _spinblock(spinner* x, int n, int blockOffset) {
x->stride = stride;
}

int spin(spinner* x, int n) {
int spin(spinner *x, int n)
{
_spinblock(x, 64, 0);

_spinblock(x, 64, 64);

if (x->voleg.egval < -1440.f) {
if (x->voleg.egval < -1440.f)
{
x->voleg.stage = done;
return 0;
}
if (x->voleg.stage == done) {
if (x->voleg.stage == done)
{
return 0;
}
return 1;
}

unsigned int sp_byte_len() { return sizeof(spinner); }
EG* get_vol_eg(spinner* x) { return &x->voleg; }
EG* get_mod_eg(spinner* x) { return &x->modeg; }
EG *get_vol_eg(spinner *x) { return &x->voleg; }
EG *get_mod_eg(spinner *x) { return &x->modeg; }

float* get_sp_output(spinner* x) { return x->outputf; }
int get_sp_channel_id(spinner* x) { return x->channelId; }
float *get_sp_output(spinner *x) { return x->outputf; }
int get_sp_channel_id(spinner *x) { return x->channelId; }

void gm_reset() {
for (int idx = 0; idx < nmidiChannels; idx++) {
void gm_reset()
{
for (int idx = 0; idx < nmidiChannels; idx++)
{
midi_cc_vals[idx * num_cc_list + TML_VOLUME_MSB] = 100;
midi_cc_vals[idx * num_cc_list + TML_PAN_MSB] = 64;
midi_cc_vals[idx * num_cc_list + TML_EXPRESSION_MSB] = 127;
if (idx == def_drum_c)
midi_cc_vals[idx * num_cc_list + TML_BANK_SELECT_MSB] = 128;
}
for (int i = 0; i < nchannels; i++) reset(&sps[i]);
for (int i = 0; i < nchannels; i++)
reset(&sps[i]);
}
// #include <math.h>
// #include <stdio.h>
Expand Down
Loading

0 comments on commit c729a0e

Please sign in to comment.