blob: 041dfdccfebb3315ca04898b2aa056bf32540234 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * R. Chandrasekar <rcsekar@samsung.com>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00005 */
6
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00007#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00009#include <sound.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000010
Simon Glassddad0322018-11-15 19:56:13 -070011void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
Simon Glass51aa8602018-12-10 10:37:51 -070012 uint freq, uint channels)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000013{
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000014 const unsigned short amplitude = 16000; /* between 1 and 32767 */
Simon Glassddad0322018-11-15 19:56:13 -070015 const int period = freq ? sample_rate / freq : 0;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000016 const int half = period / 2;
17
18 assert(freq);
19
20 /* Make sure we don't overflow our buffer */
21 if (size % 2)
22 size--;
23
24 while (size) {
Simon Glass51aa8602018-12-10 10:37:51 -070025 int i, j;
26
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000027 for (i = 0; size && i < half; i++) {
Andrew Scullc40b01b2022-04-03 10:39:13 +000028 for (j = 0; size && j < channels; j++, size -= 2)
Simon Glass51aa8602018-12-10 10:37:51 -070029 *data++ = amplitude;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000030 }
31 for (i = 0; size && i < period - half; i++) {
Andrew Scullc40b01b2022-04-03 10:39:13 +000032 for (j = 0; size && j < channels; j++, size -= 2)
Simon Glass51aa8602018-12-10 10:37:51 -070033 *data++ = -amplitude;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000034 }
35 }
36}