I try such code:
static float shift = 0.0;
double amplitude = 1000000 * pow(10, 1 / 400.0);
for (int i = 0; i < nSampleSize / nBlockAlign; i ++)
{
// Sound :)
Buffer [i] = amplitude * sin((shift + i)) / 100;
}
shift = shift + amplitude * nSampleSize / nBlockAlign ;
return (char *)Buffer;
It generally give correct results but way 2 loud. How to make it like 95% less loud?
sin() will return a value between -1 and 1, so when multiplied with a number x the result will vary between -x and x.
If x is bigger or smaller than the numeric bounds of the array type it will cause artifacts.
Buffer is a char array it seems, which is not really common for an audiobuffer.
Usually they are ints or doubles. When its an array of ints the amplitude should be between INT_MIN and INT_MAX. When it is an array of doubles the amplitude will usually vary between -1 and 1.
In your case I would try and use CHAR_MAX? (EDIT: and loose the “/100”)
But I’m guessing a char array is not expected by the calling function, because there are max 256 discrete possible values for the amplitude, which will result in poor audio quality. This term is called bit depth.
Also have a look at sinusoids