sound.c 815 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Samsung Electronics
  4. * R. Chandrasekar <rcsekar@samsung.com>
  5. */
  6. #include <common.h>
  7. #include <log.h>
  8. #include <sound.h>
  9. void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
  10. uint freq, uint channels)
  11. {
  12. const unsigned short amplitude = 16000; /* between 1 and 32767 */
  13. const int period = freq ? sample_rate / freq : 0;
  14. const int half = period / 2;
  15. assert(freq);
  16. /* Make sure we don't overflow our buffer */
  17. if (size % 2)
  18. size--;
  19. while (size) {
  20. int i, j;
  21. for (i = 0; size && i < half; i++) {
  22. size -= 2;
  23. for (j = 0; j < channels; j++)
  24. *data++ = amplitude;
  25. }
  26. for (i = 0; size && i < period - half; i++) {
  27. size -= 2;
  28. for (j = 0; j < channels; j++)
  29. *data++ = -amplitude;
  30. }
  31. }
  32. }