Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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 : /** Struct to handle a DTMF */ 49 : struct DTMFTone 50 : { 51 : unsigned char code; /** Code of the tone */ 52 : unsigned lower; /** Lower frequency */ 53 : unsigned higher; /** Higher frequency */ 54 : }; 55 : 56 : /** State of the DTMF generator */ 57 : struct DTMFState 58 : { 59 : unsigned int offset; /** Offset in the sample currently being played */ 60 : AVFrame* sample; /** Currently generated code */ 61 : }; 62 : 63 : /** State of the DTMF generator */ 64 : DTMFState state; 65 : 66 : /** The different kind of tones */ 67 : static const DTMFTone tones_[NUM_TONES]; 68 : 69 : /** Generated samples for each tone */ 70 : std::array<libjami::FrameBuffer, NUM_TONES> toneBuffers_; 71 : 72 : /** Sampling rate of generated dtmf */ 73 : unsigned sampleRate_; 74 : 75 : /** A tone object */ 76 : Tone tone_; 77 : 78 : public: 79 : /** 80 : * DTMF Generator contains frequency of each keys 81 : * and can build one DTMF. 82 : * @param sampleRate frequency of the sample (ex: 8000 hz) 83 : */ 84 : DTMFGenerator(unsigned int sampleRate, AVSampleFormat sampleFormat); 85 : 86 : ~DTMFGenerator(); 87 : 88 : NON_COPYABLE(DTMFGenerator); 89 : 90 : /* 91 : * Get n samples of the signal of code code 92 : * @param frame the output AVFrame to fill 93 : * @param code dtmf code to get sound 94 : */ 95 : void getSamples(AVFrame* frame, unsigned char code); 96 : 97 : /* 98 : * Get next n samples (continues where previous call to 99 : * genSample or genNextSamples stopped 100 : * @param buffer a AudioSample vector 101 : */ 102 : void getNextSamples(AVFrame* frame); 103 : 104 : private: 105 : /** 106 : * Fill tone buffer for a given index of the array of tones. 107 : * @param index of the tone in the array tones_ 108 : * @return AudioSample* The generated data 109 : */ 110 : libjami::FrameBuffer fillToneBuffer(int index); 111 : }; 112 : 113 : } // namespace jami