[5546] | 1 | //dtmf.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2012 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroardsp a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroardsp is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "libroardsp.h" |
---|
| 26 | |
---|
| 27 | ssize_t roar_dtmf_mus2samples(const int_least32_t t, const uint32_t rate) { |
---|
| 28 | int64_t ret = t; |
---|
| 29 | |
---|
| 30 | if ( t < 0 || rate == 0 ) { |
---|
| 31 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 32 | return -1; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | ret *= (int64_t)rate; |
---|
| 36 | ret /= (int64_t)1000000; |
---|
| 37 | |
---|
| 38 | return ret; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | int roar_dtmf_break(int16_t * samples, const size_t len, const uint32_t rate, const int options) { |
---|
| 42 | (void)rate, (void)options; |
---|
| 43 | |
---|
| 44 | ROAR_DBG("roar_dtmf_break(samples=%p, len=%llu, rate=%lu, options=%i) = ?", samples, (long long unsigned int)len, (long unsigned int)rate, options); |
---|
| 45 | |
---|
| 46 | if ( samples == NULL ) { |
---|
| 47 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 48 | return -1; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | memset(samples, 0, len*sizeof(int16_t)); |
---|
| 52 | |
---|
| 53 | return 0; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | static const struct tone { |
---|
| 57 | const uint16_t c; |
---|
| 58 | const float f0; |
---|
| 59 | const float f1; |
---|
| 60 | } _roardsp_tones[] = { |
---|
| 61 | {'1', 697, 1209}, |
---|
| 62 | {'2', 697, 1336}, |
---|
| 63 | {'3', 697, 1477}, |
---|
| 64 | {'A', 697, 1633}, |
---|
| 65 | |
---|
| 66 | {'4', 770, 1209}, |
---|
| 67 | {'5', 770, 1336}, |
---|
| 68 | {'6', 770, 1477}, |
---|
| 69 | {'B', 770, 1633}, |
---|
| 70 | |
---|
| 71 | {'7', 852, 1209}, |
---|
| 72 | {'8', 852, 1336}, |
---|
| 73 | {'9', 852, 1477}, |
---|
| 74 | {'C', 852, 1633}, |
---|
| 75 | |
---|
| 76 | {'*', 941, 1209}, |
---|
| 77 | {'0', 941, 1336}, |
---|
| 78 | {'#', 941, 1477}, |
---|
| 79 | {'D', 941, 1633}, |
---|
| 80 | |
---|
| 81 | {0, -1, -1} |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | static const struct tone * __lookup_tone(const int options, uint16_t c) { |
---|
| 85 | size_t i; |
---|
| 86 | |
---|
| 87 | (void)options; |
---|
| 88 | |
---|
| 89 | if ( c >= 'a' ) |
---|
| 90 | c -= 'a' - 'A'; |
---|
| 91 | |
---|
| 92 | for (i = 0; _roardsp_tones[i].c != 0; i++) { |
---|
| 93 | if ( _roardsp_tones[i].c == c ) { |
---|
| 94 | return &(_roardsp_tones[i]); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 99 | return NULL; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | int roar_dtmf_tone (int16_t * samples, const size_t len, const uint32_t rate, const int options, const uint16_t c) { |
---|
| 103 | const struct tone * ct = NULL; |
---|
| 104 | size_t i; |
---|
| 105 | float t; |
---|
| 106 | float t_inc = 1./rate; |
---|
| 107 | float fc0, fc1; |
---|
| 108 | |
---|
| 109 | ROAR_DBG("roar_dtmf_tone(samples=%p, len=%llu, rate=%lu, options=%i, c=%i) = ?", samples, (long long unsigned int)len, (long unsigned int)rate, options, (int)c); |
---|
| 110 | |
---|
| 111 | if ( samples == NULL ) { |
---|
| 112 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | ct = __lookup_tone(options, c); |
---|
| 117 | |
---|
| 118 | if ( ct == NULL ) { |
---|
| 119 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | fc0 = 2. * M_PI * ct->f0; |
---|
| 124 | fc1 = 2. * M_PI * ct->f1; |
---|
| 125 | |
---|
| 126 | // memset(samples, 0, len); |
---|
| 127 | |
---|
| 128 | for (i = 0, t = 0.; i < len; t += t_inc, i++) { |
---|
| 129 | samples[i] = (sinf(fc0*t) + sinf(fc1*t))*8192.0; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return 0; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | //ll |
---|