Skip to content

Commit

Permalink
Xbox: Add log2f
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 committed Feb 21, 2022
1 parent eaca5c4 commit 6a59dc8
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ static int mixing_freq;
static sbyte midi_semitones_higher;
static float current_midi_tempo_modifier;

//https://github.com/gagern/gnulib/blob/master/lib/log2f.c

static float _frexpf (float x, int *expptr)
{
return (float) frexp ((double) x, expptr);
}

static float _log2f(float x) {
#define LOG2 0.693147180559945309417232121458176568075f
#define LOG2_INVERSE 1.44269504088896340735992468100189213743f
#define SQRT_HALF 0.707106781186547524400844362104849039284f

if (x == 0.0f)
return -HUGE_VALF;
if (x < 0.0f)
return 0.0f / 0.0f;

int e;
float y;

y = _frexpf(x, &e);
if (y < SQRT_HALF)
{
y = 2.0f * y;
e = e - 1;
}

return (float)e + logf(y) * LOG2_INVERSE;
}

// Tempo adjustments for specific songs:
// * PV scene, with 'Story 3 Jaffar enters':
// Speed must be exactly right, otherwise it will not line up with the flashing animation in the cutscene.
Expand Down Expand Up @@ -430,7 +460,7 @@ static void midi_note_on(midi_event_type* event) {
float octaves_from_A4 = ((int)event->channel.param1 - 69 - 12 + midi_semitones_higher) / 12.0f;
float frequency = powf(2.0f, octaves_from_A4) * 440.0f;
float f_number_float = frequency * (float)(1 << 20) / 49716.0f;
int block = (int)(log2f(f_number_float) - 9) & 7;
int block = (int)(_log2f(f_number_float) - 9) & 7;
int f = ((int)f_number_float >> block) & 1023;
word reg_offset = reg_single_offsets[voice];
// opl_write_reg_masked(0xB0 + reg_offset, 0, 0x20); // Turn note off first (should not be necessary)
Expand Down

0 comments on commit 6a59dc8

Please sign in to comment.