Skip to content

Commit 03167dd

Browse files
Ensure float operations in Sound Expression/Synth & Compass cal. (#454)
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. Related PR, which includes files in codal-core to ensure float operations: - lancaster-university/codal-core#176
1 parent 5787f3e commit 03167dd

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

source/MicroBitCompassCalibrator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ CompassCalibration MicroBitCompassCalibrator::spherify(Sample3D centre, Sample3D
168168

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

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

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

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

203-
scaleX = 1.0 + scale * (weightX / wmag);
204-
scaleY = 1.0 + scale * (weightY / wmag);
205-
scaleZ = 1.0 + scale * (weightZ / wmag);
203+
scaleX = 1.0f + scale * (weightX / wmag);
204+
scaleY = 1.0f + scale * (weightY / wmag);
205+
scaleZ = 1.0f + scale * (weightZ / wmag);
206206

207207
result.scale.x = (int)(1024 * scaleX);
208208
result.scale.y = (int)(1024 * scaleY);

source/SoundExpressions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ bool SoundExpressions::parseSoundExpression(const char *soundChars, SoundEffect
276276
}
277277

278278
// Volume envelope
279-
float effectVolumeFloat = (float) CLAMP(0, effectVolume, 1023) / 1023.0;
280-
float endVolumeFloat = (float) CLAMP(0, endVolume, 1023) / 1023.0;
279+
float effectVolumeFloat = (float) CLAMP(0, effectVolume, 1023) / 1023.0f;
280+
float endVolumeFloat = (float) CLAMP(0, endVolume, 1023) / 1023.0f;
281281
fx->volume = volumeScaleFactor * effectVolumeFloat;
282282
fx->effects[1].effect = SoundSynthesizerEffects::volumeRampEffect;
283283
fx->effects[1].steps = 36;

source/SoundSynthesizerEffects.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ void SoundSynthesizerEffects::linearInterpolation(SoundEmojiSynthesizer *synth,
102102
void SoundSynthesizerEffects::logarithmicInterpolation(SoundEmojiSynthesizer *synth, ToneEffect *context)
103103
{
104104
// Original frequency gen here, for reference. -John
105-
//synth->frequency = synth->effect->frequency+(log10(context->step)*(context->parameter[0]-synth->effect->frequency)/1.95);
105+
//synth->frequency = synth->effect->frequency+(log10f(context->step)*(context->parameter[0]-synth->effect->frequency)/1.95);
106106

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

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

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

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

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

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

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

155155
// Argeppio functions

0 commit comments

Comments
 (0)