Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Samsung Electronics |
| 4 | * R. Chandrasekar <rcsekar@samsung.com> |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 7 | #include <common.h> |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 8 | #include <sound.h> |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 9 | |
Simon Glass | ddad032 | 2018-11-15 19:56:13 -0700 | [diff] [blame] | 10 | void sound_create_square_wave(uint sample_rate, unsigned short *data, int size, |
| 11 | uint freq) |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 12 | { |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 13 | const unsigned short amplitude = 16000; /* between 1 and 32767 */ |
Simon Glass | ddad032 | 2018-11-15 19:56:13 -0700 | [diff] [blame] | 14 | const int period = freq ? sample_rate / freq : 0; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 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; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 28 | } |
| 29 | for (i = 0; size && i < period - half; i++) { |
| 30 | size -= 2; |
| 31 | *data++ = -amplitude; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | } |