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