sound.c 862 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. if (!half) {
  16. memset(data, 0, size);
  17. return;
  18. }
  19. /* Make sure we don't overflow our buffer */
  20. if (size % 2)
  21. size--;
  22. while (size) {
  23. int i, j;
  24. for (i = 0; size && i < half; i++) {
  25. for (j = 0; size && j < channels; j++, size -= 2)
  26. *data++ = amplitude;
  27. }
  28. for (i = 0; size && i < period - half; i++) {
  29. for (j = 0; size && j < channels; j++, size -= 2)
  30. *data++ = -amplitude;
  31. }
  32. }
  33. }