blob: 9dda2dba822aa8bc0a4aaf504130f1bfaddfc530 [file] [log] [blame]
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00001/*
2 * Copyright (C) 2012 Samsung Electronics
3 * R. Chandrasekar <rcsekar@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00006 */
7
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00008#include <common.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00009#include <sound.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000010
Simon Glass59c021e2014-02-27 13:26:20 -070011void sound_create_square_wave(unsigned short *data, int size, uint32_t freq)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000012{
13 const int sample = 48000;
14 const unsigned short amplitude = 16000; /* between 1 and 32767 */
15 const int period = freq ? sample / freq : 0;
16 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) {
25 int i;
26 for (i = 0; size && i < half; i++) {
27 size -= 2;
28 *data++ = amplitude;
29 *data++ = amplitude;
30 }
31 for (i = 0; size && i < period - half; i++) {
32 size -= 2;
33 *data++ = -amplitude;
34 *data++ = -amplitude;
35 }
36 }
37}