2019-12-05 12:42 PM
#define F_SAMPLE 48000.0f
#define F_OUT 4000.0f
#define PI 3.14159f
for(uint16_t i = 0; i < sample_N; i++)
{
sin = sinf(i * 2 * PI * sample_dt);
dataI2S[i*2] = sin * 5000;
dataI2S[i*2 + 1] =sin * 5000;
}
HAL_I2S_Transmit_DMA(&hi2s3, (uint16_t *)dataI2S, sample_N*2);
#define F_SAMPLE 48000.0f
#define F_OUT 4000.0f
#define PI 3.14159f
int a = 1;
for(uint16_t i = 0; i < sample_N; i++)
{
sin = sinf(i * 2 * PI * sample_dt);
if(a%3) {
dataI2S[i*2] = sin * 5000;
dataI2S[i*2 + 1] =sin * 5000;
} else if (a%2) {
dataI2S[i*2] = sin * 3000;
dataI2S[i*2 + 1] =sin * 3000;
} else if (a%1) {
dataI2S[i*2] = sin * 7000;
dataI2S[i*2 + 1] =sin * 7000;
}
a++;
}
HAL_I2S_Transmit_DMA(&hi2s3, (uint16_t *)dataI2S, sample_N*2);
I am using the very above code to send sin wave to audio output and it perfectly creates sound. But I want to randomize the vawe and what I did is above. But it just generates the same sound. It does not produce sin*7000 or sin*3000. If I say only produce sin*3000 it does that. What could go wrong?
2019-12-05 12:59 PM
You are changing the amplitude, not the frequency.
2019-12-05 01:26 PM
As I understand,
HAL_I2S_Transmit_DMA(&hi2s3, (uint16_t *)dataI2S, sample_N3*2);
I need to change the sample_N here to change frequency but can I do that dynamically during audio generation?
2019-12-07 05:21 AM
To generate a single period of a sine with different resolutions, which at a constant sample rate translates as different frequencies, probably you want this:
sin = sinf(2.0f * PI * (float)i / (float)sample_N);
2019-12-07 05:31 AM
Genereted sound is still uniform and do not change its frequency playing. Sound only changes when I change
HAL_I2S_Transmit_DMA(&hi2s3, (uint16_t *)dataI2S, sample_N3*2);
sample_N3*2, here.