Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / sound / sound.c
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 <log.h>
9 #include <sound.h>
10
11 void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
12                               uint freq, uint channels)
13 {
14         const unsigned short amplitude = 16000; /* between 1 and 32767 */
15         const int period = freq ? sample_rate / freq : 0;
16         const int half = period / 2;
17
18         if (!half) {
19                 memset(data, 0, size);
20                 return;
21         }
22
23         /* Make sure we don't overflow our buffer */
24         if (size % 2)
25                 size--;
26
27         while (size) {
28                 int i, j;
29
30                 for (i = 0; size && i < half; i++) {
31                         for (j = 0; size && j < channels; j++, size -= 2)
32                                 *data++ = amplitude;
33                 }
34                 for (i = 0; size && i < period - half; i++) {
35                         for (j = 0; size && j < channels; j++, size -= 2)
36                                 *data++ = -amplitude;
37                 }
38         }
39 }