Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Samsung Electronics |
| 4 | * R. Chandrasekar <rcsekar@samsung.com> |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 9 | #include <sound.h> |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 10 | |
Simon Glass | 7d92b06 | 2018-11-15 19:56:13 -0700 | [diff] [blame] | 11 | void sound_create_square_wave(uint sample_rate, unsigned short *data, int size, |
Simon Glass | f987177 | 2018-12-10 10:37:51 -0700 | [diff] [blame] | 12 | uint freq, uint channels) |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 13 | { |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 14 | const unsigned short amplitude = 16000; /* between 1 and 32767 */ |
Simon Glass | 7d92b06 | 2018-11-15 19:56:13 -0700 | [diff] [blame] | 15 | const int period = freq ? sample_rate / freq : 0; |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 16 | const int half = period / 2; |
| 17 | |
Heinrich Schuchardt | d0e8777 | 2022-12-04 14:00:05 +0100 | [diff] [blame] | 18 | if (!half) { |
| 19 | memset(data, 0, size); |
| 20 | return; |
| 21 | } |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 22 | |
| 23 | /* Make sure we don't overflow our buffer */ |
| 24 | if (size % 2) |
| 25 | size--; |
| 26 | |
| 27 | while (size) { |
Simon Glass | f987177 | 2018-12-10 10:37:51 -0700 | [diff] [blame] | 28 | int i, j; |
| 29 | |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 30 | for (i = 0; size && i < half; i++) { |
Andrew Scull | 49209da | 2022-04-03 10:39:13 +0000 | [diff] [blame] | 31 | for (j = 0; size && j < channels; j++, size -= 2) |
Simon Glass | f987177 | 2018-12-10 10:37:51 -0700 | [diff] [blame] | 32 | *data++ = amplitude; |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 33 | } |
| 34 | for (i = 0; size && i < period - half; i++) { |
Andrew Scull | 49209da | 2022-04-03 10:39:13 +0000 | [diff] [blame] | 35 | for (j = 0; size && j < channels; j++, size -= 2) |
Simon Glass | f987177 | 2018-12-10 10:37:51 -0700 | [diff] [blame] | 36 | *data++ = -amplitude; |
Rajeshwari Shinde | 511ed5f | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | } |