Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Yan Morin <yan.morin@savoirfairelinux.com> 5 : * Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com> 6 : * 7 : * Portions (c) 2003 iptel.org 8 : * 9 : * This program is free software; you can redistribute it and/or modify 10 : * it under the terms of the GNU General Public License as published by 11 : * the Free Software Foundation; either version 3 of the License, or 12 : * (at your option) any later version. 13 : * 14 : * This program 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 program; if not, write to the Free Software 21 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 : */ 23 : 24 : #pragma once 25 : 26 : #include <stdexcept> 27 : #include <string> 28 : #include <vector> 29 : #include "noncopyable.h" 30 : #include "tone.h" 31 : 32 : #define NUM_TONES 16 33 : 34 : /* 35 : * @file dtmfgenerator.h 36 : * @brief DMTF Generator Exception 37 : */ 38 : 39 : namespace jami { 40 : 41 : class DTMFException : public std::runtime_error 42 : { 43 : public: 44 0 : DTMFException(const std::string& str) 45 0 : : std::runtime_error(str) {}; 46 : }; 47 : 48 : /* 49 : * @file dtmfgenerator.h 50 : * @brief DTMF Tone Generator 51 : */ 52 : class DTMFGenerator 53 : { 54 : private: 55 : /** Struct to handle a DTMF */ 56 : struct DTMFTone 57 : { 58 : unsigned char code; /** Code of the tone */ 59 : unsigned lower; /** Lower frequency */ 60 : unsigned higher; /** Higher frequency */ 61 : }; 62 : 63 : /** State of the DTMF generator */ 64 : struct DTMFState 65 : { 66 : unsigned int offset; /** Offset in the sample currently being played */ 67 : AVFrame* sample; /** Currently generated code */ 68 : }; 69 : 70 : /** State of the DTMF generator */ 71 : DTMFState state; 72 : 73 : /** The different kind of tones */ 74 : static const DTMFTone tones_[NUM_TONES]; 75 : 76 : /** Generated samples for each tone */ 77 : std::array<libjami::FrameBuffer, NUM_TONES> toneBuffers_; 78 : 79 : /** Sampling rate of generated dtmf */ 80 : unsigned sampleRate_; 81 : 82 : /** A tone object */ 83 : Tone tone_; 84 : 85 : public: 86 : /** 87 : * DTMF Generator contains frequency of each keys 88 : * and can build one DTMF. 89 : * @param sampleRate frequency of the sample (ex: 8000 hz) 90 : */ 91 : DTMFGenerator(unsigned int sampleRate, AVSampleFormat sampleFormat); 92 : 93 : ~DTMFGenerator(); 94 : 95 : NON_COPYABLE(DTMFGenerator); 96 : 97 : /* 98 : * Get n samples of the signal of code code 99 : * @param frame the output AVFrame to fill 100 : * @param code dtmf code to get sound 101 : */ 102 : void getSamples(AVFrame* frame, unsigned char code); 103 : 104 : /* 105 : * Get next n samples (continues where previous call to 106 : * genSample or genNextSamples stopped 107 : * @param buffer a AudioSample vector 108 : */ 109 : void getNextSamples(AVFrame* frame); 110 : 111 : private: 112 : /** 113 : * Fill tone buffer for a given index of the array of tones. 114 : * @param index of the tone in the array tones_ 115 : * @return AudioSample* The generated data 116 : */ 117 : libjami::FrameBuffer fillToneBuffer(int index); 118 : }; 119 : 120 : } // namespace jami