Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. short buffer[44100];
  2. float frequency = 440.0f;
  3. float sampling_ratio = 44100.0f;
  4. float amplitude = 0.5f;
  5. float t;
  6. for (int i = 0; i < 44100; i++)
  7. {
  8. float theta = ((float)i / sampling_ratio) * PI;
  9. buffer[i] = (short)(sin(theta * frequency) * 32767.0f * amplitude);
  10. }
  11.  
  12. void add_sine_wave(short* buffer, int buffer_length, float frequency, float sampling_ratio, float amplitude)
  13. {
  14. for (int i = 0; i < buffer_length; i++)
  15. {
  16. float theta = ((float)i / sampling_ratio) * M_PI;
  17. t = (sin(theta * frequency) * 32767.0f * amplitude);
  18. if(t>SHRT_MAX) t = SHRT_MAX; // overflow check
  19. if(t<SHRT_MIN) t = SHRT_MIN; // underflow check
  20. buffer[i] += (short) t;`
  21. }
  22. }
  23.  
  24. short buffer[44100];
  25. memset(buffer, 0, sizeof(buffer));
  26. // Create an A Major chord
  27. add_sine_wave(buffer, 44100, 440.0f, 44100.0f, 0.5f);
  28. add_sine_wave(buffer, 44100, 554.37f, 44100.0f, 0.5f);
  29. add_sine_wave(buffer, 44100, 659.26f, 44100.0f, 0.5f);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement