1 /* $NetBSD: dtmf.c,v 1.4 2019/08/24 06:00:49 isaki Exp $ */
2
3 /*
4 * Copyright (c) 2010 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/endian.h>
30
31 #include <err.h>
32 #include <fcntl.h>
33 #include <math.h>
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include "dtmf.h"
41
42 #define PI2 (3.14159265358979323846f * 2)
43
44 static void
dtmf_create(int16_t * buf,unsigned int sample_rate,unsigned short sample_length,unsigned short channels,unsigned int chanmask,float freq1,float freq2)45 dtmf_create(int16_t *buf, unsigned int sample_rate,
46 unsigned short sample_length, unsigned short channels,
47 unsigned int chanmask, float freq1, float freq2)
48 {
49 int c;
50 unsigned i;
51 size_t sample_count = sample_rate * sample_length;
52
53 for (i = 0; i < sample_count; i++) {
54 for (c = 0; c < channels; c++) {
55 if ((chanmask & (1 << c)) == 0)
56 continue;
57 buf[c] = htole16(
58 (sin(i * PI2 * (freq1 / sample_rate)) +
59 sin(i * PI2 * (freq2 / sample_rate))) * 16383
60 );
61 }
62 buf += channels;
63 }
64 }
65
66 void
dtmf_new(int16_t ** buf,size_t * buflen,unsigned int sample_rate,unsigned short sample_length,unsigned short channels,unsigned int chanmask,float rate1,float rate2)67 dtmf_new(int16_t **buf, size_t *buflen, unsigned int sample_rate,
68 unsigned short sample_length, unsigned short channels,
69 unsigned int chanmask, float rate1, float rate2)
70 {
71 *buflen = sample_rate * sizeof(int16_t) * sample_length * channels;
72 *buf = calloc(1, *buflen);
73 if (*buf == NULL) {
74 warn("calloc");
75 return;
76 }
77
78 dtmf_create(*buf, sample_rate, sample_length, channels, chanmask,
79 rate1, rate2);
80 }
81