xref: /openbmc/u-boot/drivers/sound/sound.c (revision 058d2316)
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  * R. Chandrasekar <rcsekar@samsung.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <sound.h>
10 
11 void sound_create_square_wave(unsigned short *data, int size, uint32_t freq)
12 {
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 }
38