blob: 4f0ad0d8f0d706f4bd0881e878341f37a388f414 [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>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00008#include <sound.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00009
Simon Glassddad0322018-11-15 19:56:13 -070010void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
11 uint freq)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000012{
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000013 const unsigned short amplitude = 16000; /* between 1 and 32767 */
Simon Glassddad0322018-11-15 19:56:13 -070014 const int period = freq ? sample_rate / freq : 0;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000015 const int half = period / 2;
16
17 assert(freq);
18
19 /* Make sure we don't overflow our buffer */
20 if (size % 2)
21 size--;
22
23 while (size) {
24 int i;
25 for (i = 0; size && i < half; i++) {
26 size -= 2;
27 *data++ = amplitude;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000028 }
29 for (i = 0; size && i < period - half; i++) {
30 size -= 2;
31 *data++ = -amplitude;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000032 }
33 }
34}