blob: c0fc50c99dac5e6c7ec051a9b762415a0d3c6670 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * R. Chandrasekar <rcsekar@samsung.com>
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +00005 */
6
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +00007#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +00009#include <sound.h>
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000010
Simon Glass7d92b062018-11-15 19:56:13 -070011void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
Simon Glassf9871772018-12-10 10:37:51 -070012 uint freq, uint channels)
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000013{
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000014 const unsigned short amplitude = 16000; /* between 1 and 32767 */
Simon Glass7d92b062018-11-15 19:56:13 -070015 const int period = freq ? sample_rate / freq : 0;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000016 const int half = period / 2;
17
Heinrich Schuchardtd0e87772022-12-04 14:00:05 +010018 if (!half) {
19 memset(data, 0, size);
20 return;
21 }
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000022
23 /* Make sure we don't overflow our buffer */
24 if (size % 2)
25 size--;
26
27 while (size) {
Simon Glassf9871772018-12-10 10:37:51 -070028 int i, j;
29
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000030 for (i = 0; size && i < half; i++) {
Andrew Scull49209da2022-04-03 10:39:13 +000031 for (j = 0; size && j < channels; j++, size -= 2)
Simon Glassf9871772018-12-10 10:37:51 -070032 *data++ = amplitude;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000033 }
34 for (i = 0; size && i < period - half; i++) {
Andrew Scull49209da2022-04-03 10:39:13 +000035 for (j = 0; size && j < channels; j++, size -= 2)
Simon Glassf9871772018-12-10 10:37:51 -070036 *data++ = -amplitude;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000037 }
38 }
39}