cancel
Showing results for 
Search instead for 
Did you mean: 

Frequency change on audio

FKrlr.2155
Associate II

#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?

4 REPLIES 4

You are changing the amplitude, not the frequency.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
FKrlr.2155
Associate II

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?

Piranha
Chief II

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);

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.