Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics |
| 3 | * R. Chandrasekar <rcsekar@samsung.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 8 | #include <common.h> |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 9 | #include <sound.h> |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 10 | |
Simon Glass | 59c021e | 2014-02-27 13:26:20 -0700 | [diff] [blame] | 11 | void sound_create_square_wave(unsigned short *data, int size, uint32_t freq) |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 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 | } |