Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- short buffer[44100];
- float frequency = 440.0f;
- float sampling_ratio = 44100.0f;
- float amplitude = 0.5f;
- float t;
- for (int i = 0; i < 44100; i++)
- {
- float theta = ((float)i / sampling_ratio) * PI;
- buffer[i] = (short)(sin(theta * frequency) * 32767.0f * amplitude);
- }
- void add_sine_wave(short* buffer, int buffer_length, float frequency, float sampling_ratio, float amplitude)
- {
- for (int i = 0; i < buffer_length; i++)
- {
- float theta = ((float)i / sampling_ratio) * M_PI;
- t = (sin(theta * frequency) * 32767.0f * amplitude);
- if(t>SHRT_MAX) t = SHRT_MAX; // overflow check
- if(t<SHRT_MIN) t = SHRT_MIN; // underflow check
- buffer[i] += (short) t;`
- }
- }
- short buffer[44100];
- memset(buffer, 0, sizeof(buffer));
- // Create an A Major chord
- add_sine_wave(buffer, 44100, 440.0f, 44100.0f, 0.5f);
- add_sine_wave(buffer, 44100, 554.37f, 44100.0f, 0.5f);
- add_sine_wave(buffer, 44100, 659.26f, 44100.0f, 0.5f);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement