xref: /openbmc/u-boot/drivers/sound/sound.c (revision ae485b54)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012 Samsung Electronics
4  * R. Chandrasekar <rcsekar@samsung.com>
5  */
6 
7 #include <common.h>
8 #include <sound.h>
9 
10 void sound_create_square_wave(unsigned short *data, int size, uint32_t freq)
11 {
12 	const int sample = 48000;
13 	const unsigned short amplitude = 16000; /* between 1 and 32767 */
14 	const int period = freq ? sample / freq : 0;
15 	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;
28 			*data++ = amplitude;
29 		}
30 		for (i = 0; size && i < period - half; i++) {
31 			size -= 2;
32 			*data++ = -amplitude;
33 			*data++ = -amplitude;
34 		}
35 	}
36 }
37