Skip to content

Commit

Permalink
Ensure float operations in Sound Expression/Synth & Compass cal.
Browse files Browse the repository at this point in the history
Avoid implicit int promotion to doubles when the final results are
stored in floats, and use float versions of math function when
the results are stored in floats as well.
  • Loading branch information
microbit-carlos committed Dec 18, 2024
1 parent 9892f01 commit e67bb09
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions source/MicroBitCompassCalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ CompassCalibration MicroBitCompassCalibrator::spherify(Sample3D centre, Sample3D

for (int i = 0; i < samples; i++)
{
int d = sqrt((float)centre.dSquared(data[i]));
int d = sqrtf((float)centre.dSquared(data[i]));

if (d > radius)
radius = d;
Expand All @@ -179,7 +179,7 @@ CompassCalibration MicroBitCompassCalibrator::spherify(Sample3D centre, Sample3D
for (int i = 0; i < samples; i++)
{
// Calculate the distance from this point to the centre of the sphere
float d = sqrt(centre.dSquared(data[i]));
float d = sqrtf(centre.dSquared(data[i]));

// Now determine a scalar multiplier that, when applied to the vector to the centre,
// will place this point on the surface of the sphere.
Expand All @@ -198,11 +198,11 @@ CompassCalibration MicroBitCompassCalibrator::spherify(Sample3D centre, Sample3D
weightZ += s * fabsf(dz / d);
}

float wmag = sqrt((weightX * weightX) + (weightY * weightY) + (weightZ * weightZ));
float wmag = sqrtf((weightX * weightX) + (weightY * weightY) + (weightZ * weightZ));

scaleX = 1.0 + scale * (weightX / wmag);
scaleY = 1.0 + scale * (weightY / wmag);
scaleZ = 1.0 + scale * (weightZ / wmag);
scaleX = 1.0f + scale * (weightX / wmag);
scaleY = 1.0f + scale * (weightY / wmag);
scaleZ = 1.0f + scale * (weightZ / wmag);

result.scale.x = (int)(1024 * scaleX);
result.scale.y = (int)(1024 * scaleY);
Expand Down
4 changes: 2 additions & 2 deletions source/SoundExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ bool SoundExpressions::parseSoundExpression(const char *soundChars, SoundEffect
}

// Volume envelope
float effectVolumeFloat = (float) CLAMP(0, effectVolume, 1023) / 1023.0;
float endVolumeFloat = (float) CLAMP(0, endVolume, 1023) / 1023.0;
float effectVolumeFloat = (float) CLAMP(0, effectVolume, 1023) / 1023.0f;
float endVolumeFloat = (float) CLAMP(0, endVolume, 1023) / 1023.0f;
fx->volume = volumeScaleFactor * effectVolumeFloat;
fx->effects[1].effect = SoundSynthesizerEffects::volumeRampEffect;
fx->effects[1].steps = 36;
Expand Down
18 changes: 9 additions & 9 deletions source/SoundSynthesizerEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ void SoundSynthesizerEffects::linearInterpolation(SoundEmojiSynthesizer *synth,
void SoundSynthesizerEffects::logarithmicInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context)
{
// Original frequency gen here, for reference. -John
//synth->frequency = synth->effect->frequency+(log10(context->step)*(context->parameter[0]-synth->effect->frequency)/1.95);
//synth->frequency = synth->effect->frequency+(log10f(context->step)*(context->parameter[0]-synth->effect->frequency)/1.95);

synth->frequency = synth->effect->frequency+(log10(
synth->frequency = synth->effect->frequency+(log10f(
( context->step==0 ? 1 : context->step ) // This is a hack, to prevent step==0 from jumping this to extreme values. -John
)*(context->parameter[0]-synth->effect->frequency)/1.95);
)*(context->parameter[0]-synth->effect->frequency)/1.95f);

// This is a bit of a hack, but will protect the synth for now until the math here can be fixed properly. -John
if( synth->frequency < 0 )
Expand All @@ -117,39 +117,39 @@ void SoundSynthesizerEffects::logarithmicInterpolation(SoundEmojiSynthesizer *sy
// parameter[0]: end frequency
void SoundSynthesizerEffects::curveInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context)
{
synth->frequency = (sin(context->step*3.12159f/180.0f)*(context->parameter[0]-synth->effect->frequency)+synth->effect->frequency);
synth->frequency = (sinf(context->step*3.12159f/180.0f)*(context->parameter[0]-synth->effect->frequency)+synth->effect->frequency);
}

// Cosine interpolate function
// parameter[0]: end frequency
void SoundSynthesizerEffects::slowVibratoInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context){
synth->frequency = sin(context->step/10)*context->parameter[0]+synth->effect->frequency;
synth->frequency = sinf(context->step/10)*context->parameter[0]+synth->effect->frequency;
}

//warble function
// parameter[0]: end frequency
void SoundSynthesizerEffects::warbleInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context)
{
synth->frequency = (sin(context->step)*(context->parameter[0]-synth->effect->frequency)+synth->effect->frequency);
synth->frequency = (sinf(context->step)*(context->parameter[0]-synth->effect->frequency)+synth->effect->frequency);
}

// Vibrato function
// parameter[0]: end frequency
void SoundSynthesizerEffects::vibratoInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context){
synth->frequency = synth->effect->frequency + sin(context->step)*context->parameter[0];
synth->frequency = synth->effect->frequency + sinf(context->step)*context->parameter[0];
}

// Exponential rising function
// parameter[0]: end frequency
void SoundSynthesizerEffects::exponentialRisingInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context)
{
synth->frequency = synth->effect->frequency + sin(0.01745329f*context->step)*context->parameter[0];
synth->frequency = synth->effect->frequency + sinf(0.01745329f*context->step)*context->parameter[0];
}

// Exponential falling function
void SoundSynthesizerEffects::exponentialFallingInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context)
{
synth->frequency = synth->effect->frequency + cos(0.01745329f*context->step)*context->parameter[0];
synth->frequency = synth->effect->frequency + cosf(0.01745329f*context->step)*context->parameter[0];
}

// Argeppio functions
Expand Down
2 changes: 1 addition & 1 deletion target-locked.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"generate_hex": true,
"libraries": [
{
"branch": "c43b89936ed72c022f2649f5540a9a74ef48fd04",
"branch": "b395ae364d00597c10b90e0b45aaad0ff15c8495",
"name": "codal-core",
"type": "git",
"url": "https://github.com/lancaster-university/codal-core"
Expand Down

0 comments on commit e67bb09

Please sign in to comment.