Line data Source code
1 : /* 2 : * Copyright (C) 2004-2026 Savoir-faire Linux Inc. 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 : */ 17 : #pragma once 18 : 19 : #include <stdexcept> 20 : #include <string> 21 : #include <vector> 22 : #include "noncopyable.h" 23 : #include "tone.h" 24 : 25 : #define NUM_TONES 16 26 : 27 : /* 28 : * @file dtmfgenerator.h 29 : * @brief DMTF Generator Exception 30 : */ 31 : 32 : namespace jami { 33 : 34 : class DTMFException : public std::runtime_error 35 : { 36 : public: 37 0 : DTMFException(const std::string& str) 38 0 : : std::runtime_error(str) {}; 39 : }; 40 : 41 : /* 42 : * @file dtmfgenerator.h 43 : * @brief DTMF Tone Generator 44 : */ 45 : class DTMFGenerator 46 : { 47 : private: 48 : /** State of the DTMF generator */ 49 : struct DTMFState 50 : { 51 : unsigned int offset; /** Offset in the sample currently being played */ 52 : AVFrame* sample; /** Currently generated code */ 53 : }; 54 : 55 : /** State of the DTMF generator */ 56 : DTMFState state; 57 : 58 : /** Generated samples for each tone */ 59 : std::array<libjami::FrameBuffer, NUM_TONES> toneBuffers_; 60 : 61 : /** Sampling rate of generated dtmf */ 62 : unsigned sampleRate_; 63 : 64 : /** A tone object */ 65 : Tone tone_; 66 : 67 : public: 68 : /** 69 : * DTMF Generator contains frequency of each keys 70 : * and can build one DTMF. 71 : * @param sampleRate frequency of the sample (ex: 8000 hz) 72 : */ 73 : DTMFGenerator(unsigned int sampleRate, AVSampleFormat sampleFormat); 74 : 75 : ~DTMFGenerator(); 76 : 77 : NON_COPYABLE(DTMFGenerator); 78 : 79 : /* 80 : * Get n samples of the signal of code code 81 : * @param frame the output AVFrame to fill 82 : * @param code dtmf code to get sound 83 : */ 84 : void getSamples(AVFrame* frame, unsigned char code); 85 : 86 : /* 87 : * Get next n samples (continues where previous call to 88 : * genSample or genNextSamples stopped 89 : * @param buffer a AudioSample vector 90 : */ 91 : void getNextSamples(AVFrame* frame); 92 : 93 : private: 94 : /** 95 : * Fill tone buffer for a given index of the array of tones. 96 : * @param index of the tone in the array tones_ 97 : * @return AudioSample* The generated data 98 : */ 99 : libjami::FrameBuffer fillToneBuffer(unsigned index); 100 : }; 101 : 102 : } // namespace jami