Line data Source code
1 : /*
2 : * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 : *
4 : * Inspired by tonegenerator of
5 : * Laurielle Lea <laurielle.lea@savoirfairelinux.com> (2004)
6 : *
7 : * This program is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation, either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 : */
20 : #include "tonelist.h"
21 :
22 : #include <ciso646> // fix windows compiler bug
23 :
24 : namespace jami {
25 :
26 : TelephoneTone::CountryId
27 32 : TelephoneTone::getCountryId(const std::string& countryName)
28 : {
29 32 : if (countryName == "North America")
30 32 : return CountryId::ZID_NORTH_AMERICA;
31 0 : else if (countryName == "France")
32 0 : return CountryId::ZID_FRANCE;
33 0 : else if (countryName == "Australia")
34 0 : return CountryId::ZID_AUSTRALIA;
35 0 : else if (countryName == "United Kingdom")
36 0 : return CountryId::ZID_UNITED_KINGDOM;
37 0 : else if (countryName == "Spain")
38 0 : return CountryId::ZID_SPAIN;
39 0 : else if (countryName == "Italy")
40 0 : return CountryId::ZID_ITALY;
41 0 : else if (countryName == "Japan")
42 0 : return CountryId::ZID_JAPAN;
43 : else
44 0 : return CountryId::ZID_NORTH_AMERICA; // default
45 : }
46 :
47 32 : TelephoneTone::TelephoneTone(const std::string& countryName, unsigned int sampleRate, AVSampleFormat sampleFormat)
48 32 : : countryId_(getCountryId(countryName))
49 32 : , currentTone_(Tone::ToneId::TONE_NULL)
50 : {
51 32 : buildTones(sampleRate, sampleFormat);
52 32 : }
53 :
54 : void
55 616 : TelephoneTone::setCurrentTone(Tone::ToneId toneId)
56 : {
57 616 : if (toneId != Tone::ToneId::TONE_NULL && currentTone_ != toneId)
58 64 : tones_[(size_t) toneId]->reset();
59 :
60 616 : currentTone_ = toneId;
61 616 : }
62 :
63 : void
64 55 : TelephoneTone::setSampleRate(unsigned int sampleRate, AVSampleFormat sampleFormat)
65 : {
66 55 : buildTones(sampleRate, sampleFormat);
67 55 : }
68 :
69 : std::shared_ptr<Tone>
70 0 : TelephoneTone::getCurrentTone()
71 : {
72 0 : if (currentTone_ < Tone::ToneId::DIALTONE or currentTone_ >= Tone::ToneId::TONE_NULL)
73 0 : return nullptr;
74 :
75 0 : return tones_[(size_t) currentTone_];
76 : }
77 :
78 : void
79 87 : TelephoneTone::buildTones(unsigned int sampleRate, AVSampleFormat sampleFormat)
80 : {
81 87 : const char* toneZone[(size_t) TelephoneTone::CountryId::ZID_COUNTRIES][(size_t) Tone::ToneId::TONE_NULL]
82 : = {{
83 : // ZID_NORTH_AMERICA
84 : "350+440", // Tone::TONE_DIALTONE
85 : "480+620/500,0/500", // Tone::TONE_BUSY
86 : "440+480/2000,0/4000", // Tone::TONE_RINGTONE
87 : "480+620/250,0/250", // Tone::TONE_CONGESTION
88 : },
89 : {
90 : // ZID_FRANCE
91 : "440",
92 : "440/500,0/500",
93 : "440/1500,0/3500",
94 : "440/250,0/250",
95 : },
96 : {
97 : // ZID_AUSTRALIA
98 : "413+438",
99 : "425/375,0/375",
100 : "413+438/400,0/200,413+438/400,0/2000",
101 : "425/375,0/375,420/375,8/375",
102 : },
103 : {
104 : // ZID_UNITED_KINGDOM
105 : "350+440",
106 : "400/375,0/375",
107 : "400+450/400,0/200,400+450/400,0/2000",
108 : "400/400,0/350,400/225,0/525",
109 : },
110 : {
111 : // ZID_SPAIN
112 : "425",
113 : "425/200,0/200",
114 : "425/1500,0/3000",
115 : "425/200,0/200,425/200,0/200,425/200,0/600",
116 : },
117 : {
118 : // ZID_ITALY
119 : "425/600,0/1000,425/200,0/200",
120 : "425/500,0/500",
121 : "425/1000,0/4000",
122 : "425/200,0/200",
123 : },
124 : {
125 : // ZID_JAPAN
126 : "400",
127 : "400/500,0/500",
128 : "400+15/1000,0/2000",
129 : "400/500,0/500",
130 : }};
131 87 : tones_[(size_t) Tone::ToneId::DIALTONE]
132 174 : = std::make_shared<Tone>(toneZone[(size_t) countryId_][(size_t) Tone::ToneId::DIALTONE],
133 : sampleRate,
134 87 : sampleFormat);
135 87 : tones_[(size_t) Tone::ToneId::BUSY]
136 174 : = std::make_shared<Tone>(toneZone[(size_t) countryId_][(size_t) Tone::ToneId::BUSY], sampleRate, sampleFormat);
137 87 : tones_[(size_t) Tone::ToneId::RINGTONE]
138 174 : = std::make_shared<Tone>(toneZone[(size_t) countryId_][(size_t) Tone::ToneId::RINGTONE],
139 : sampleRate,
140 87 : sampleFormat);
141 87 : tones_[(size_t) Tone::ToneId::CONGESTION]
142 174 : = std::make_shared<Tone>(toneZone[(size_t) countryId_][(size_t) Tone::ToneId::CONGESTION],
143 : sampleRate,
144 87 : sampleFormat);
145 87 : }
146 :
147 : } // namespace jami
|