blob: 43c5f0e485c1ad7185880b5fb2c00615cc66354f [file] [log] [blame]
/*
* Copyright (C) 2012 Samsung Electronics
* R. Chandrasekar <rcsekar@samsung.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <common.h>
#include <sound.h>
void sound_create_square_wave(unsigned short *data, int size, uint32_t freq)
{
const int sample = 48000;
const unsigned short amplitude = 16000; /* between 1 and 32767 */
const int period = freq ? sample / freq : 0;
const int half = period / 2;
assert(freq);
/* Make sure we don't overflow our buffer */
if (size % 2)
size--;
while (size) {
int i;
for (i = 0; size && i < half; i++) {
size -= 2;
*data++ = amplitude;
*data++ = amplitude;
}
for (i = 0; size && i < period - half; i++) {
size -= 2;
*data++ = -amplitude;
*data++ = -amplitude;
}
}
}