From c3b68020e5b52b0f07abab56c97599a9bd8c6f40 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 26 May 2020 10:45:56 -0400
Subject: [PATCH] tone list: cleanup

Change-Id: I0bbcdd7e5d496ce79f31ed2c172c9b530c73f160
---
 src/manager.cpp                    |  12 +--
 src/media/audio/sound/tone.h       |  10 +-
 src/media/audio/sound/tonelist.cpp | 142 ++++++++++++++---------------
 src/media/audio/sound/tonelist.h   |  12 +--
 src/media/audio/tonecontrol.cpp    |   4 +-
 src/media/audio/tonecontrol.h      |   2 +-
 6 files changed, 90 insertions(+), 92 deletions(-)

diff --git a/src/manager.cpp b/src/manager.cpp
index 015ff69cf4..a4b2711961 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -288,7 +288,7 @@ struct Manager::ManagerPimpl
      * Play one tone
      * @return false if the driver is uninitialize
      */
-    void playATone(Tone::TONEID toneId);
+    void playATone(Tone::ToneId toneId);
 
     int getCurrentDeviceIndex(DeviceType type);
 
@@ -467,7 +467,7 @@ Manager::ManagerPimpl::parseConfiguration()
  * Multi Thread
  */
 void
-Manager::ManagerPimpl::playATone(Tone::TONEID toneId)
+Manager::ManagerPimpl::playATone(Tone::ToneId toneId)
 {
     if (not base_.voipPreferences.getPlayTones())
         return;
@@ -2021,7 +2021,7 @@ Manager::stopTone()
 void
 Manager::playTone()
 {
-    pimpl_->playATone(Tone::TONE_DIALTONE);
+    pimpl_->playATone(Tone::ToneId::DIALTONE);
 }
 
 /**
@@ -2030,7 +2030,7 @@ Manager::playTone()
 void
 Manager::playToneWithMessage()
 {
-    pimpl_->playATone(Tone::TONE_CONGESTION);
+    pimpl_->playATone(Tone::ToneId::CONGESTION);
 }
 
 /**
@@ -2039,7 +2039,7 @@ Manager::playToneWithMessage()
 void
 Manager::congestion()
 {
-    pimpl_->playATone(Tone::TONE_CONGESTION);
+    pimpl_->playATone(Tone::ToneId::CONGESTION);
 }
 
 /**
@@ -2048,7 +2048,7 @@ Manager::congestion()
 void
 Manager::ringback()
 {
-    pimpl_->playATone(Tone::TONE_RINGTONE);
+    pimpl_->playATone(Tone::ToneId::RINGTONE);
 }
 
 /**
diff --git a/src/media/audio/sound/tone.h b/src/media/audio/sound/tone.h
index d4e9441a03..a0eeb62a5c 100644
--- a/src/media/audio/sound/tone.h
+++ b/src/media/audio/sound/tone.h
@@ -45,11 +45,11 @@ class Tone : public AudioLoop {
         Tone(const std::string& definition, unsigned int sampleRate);
 
         /** The different kind of tones */
-        enum TONEID {
-            TONE_DIALTONE = 0,
-            TONE_BUSY,
-            TONE_RINGTONE,
-            TONE_CONGESTION,
+        enum class ToneId {
+            DIALTONE = 0,
+            BUSY,
+            RINGTONE,
+            CONGESTION,
             TONE_NULL
         };
 
diff --git a/src/media/audio/sound/tonelist.cpp b/src/media/audio/sound/tonelist.cpp
index 2b62f59a9c..a3342ef0b7 100644
--- a/src/media/audio/sound/tonelist.cpp
+++ b/src/media/audio/sound/tonelist.cpp
@@ -26,84 +26,31 @@
 
 namespace jami {
 
-static const char *toneZone[TelephoneTone::ZID_COUNTRIES][Tone::TONE_NULL] = {
-    {
-        // ZID_NORTH_AMERICA
-        "350+440",                // Tone::TONE_DIALTONE
-        "480+620/500,0/500",      // Tone::TONE_BUSY
-        "440+480/2000,0/4000",    // Tone::TONE_RINGTONE
-        "480+620/250,0/250",      // Tone::TONE_CONGESTION
-    },
-    {
-        //ZID_FRANCE
-        "440",
-        "440/500,0/500",
-        "440/1500,0/3500",
-        "440/250,0/250",
-    },
-    {
-        //ZID_AUSTRALIA
-        "413+438",
-        "425/375,0/375",
-        "413+438/400,0/200,413+438/400,0/2000",
-        "425/375,0/375,420/375,8/375",
-    },
-    {
-        //ZID_UNITED_KINGDOM
-        "350+440",
-        "400/375,0/375",
-        "400+450/400,0/200,400+450/400,0/2000",
-        "400/400,0/350,400/225,0/525",
-    },
-    {
-        //ZID_SPAIN
-        "425",
-        "425/200,0/200",
-        "425/1500,0/3000",
-        "425/200,0/200,425/200,0/200,425/200,0/600",
-    },
-    {
-        //ZID_ITALY
-        "425/600,0/1000,425/200,0/200",
-        "425/500,0/500",
-        "425/1000,0/4000",
-        "425/200,0/200",
-    },
-    {
-        //ZID_JAPAN
-        "400",
-        "400/500,0/500",
-        "400+15/1000,0/2000",
-        "400/500,0/500",
-    }
-};
-
-
-TelephoneTone::COUNTRYID
+TelephoneTone::CountryId
 TelephoneTone::getCountryId(const std::string& countryName)
 {
-    if (countryName == "North America")         return ZID_NORTH_AMERICA;
-    else if (countryName == "France")           return ZID_FRANCE;
-    else if (countryName == "Australia")        return ZID_AUSTRALIA;
-    else if (countryName == "United Kingdom")   return ZID_UNITED_KINGDOM;
-    else if (countryName == "Spain")            return ZID_SPAIN;
-    else if (countryName == "Italy")            return ZID_ITALY;
-    else if (countryName == "Japan")            return ZID_JAPAN;
-    else                                        return ZID_NORTH_AMERICA; // default
+    if (countryName == "North America")         return CountryId::ZID_NORTH_AMERICA;
+    else if (countryName == "France")           return CountryId::ZID_FRANCE;
+    else if (countryName == "Australia")        return CountryId::ZID_AUSTRALIA;
+    else if (countryName == "United Kingdom")   return CountryId::ZID_UNITED_KINGDOM;
+    else if (countryName == "Spain")            return CountryId::ZID_SPAIN;
+    else if (countryName == "Italy")            return CountryId::ZID_ITALY;
+    else if (countryName == "Japan")            return CountryId::ZID_JAPAN;
+    else                                        return CountryId::ZID_NORTH_AMERICA; // default
 }
 
 TelephoneTone::TelephoneTone(const std::string& countryName, unsigned int sampleRate)
     : countryId_(getCountryId(countryName))
-    , currentTone_(Tone::TONE_NULL)
+    , currentTone_(Tone::ToneId::TONE_NULL)
 {
     buildTones(sampleRate);
 }
 
 void
-TelephoneTone::setCurrentTone(Tone::TONEID toneId)
+TelephoneTone::setCurrentTone(Tone::ToneId toneId)
 {
-    if (toneId != Tone::TONE_NULL && currentTone_ != toneId)
-        tones_[toneId]->reset();
+    if (toneId != Tone::ToneId::TONE_NULL && currentTone_ != toneId)
+        tones_[(size_t)toneId]->reset();
 
     currentTone_ = toneId;
 }
@@ -117,19 +64,70 @@ TelephoneTone::setSampleRate(unsigned int sampleRate)
 std::shared_ptr<Tone>
 TelephoneTone::getCurrentTone()
 {
-    if (currentTone_ < Tone::TONE_DIALTONE or currentTone_ >= Tone::TONE_NULL)
+    if (currentTone_ < Tone::ToneId::DIALTONE or currentTone_ >= Tone::ToneId::TONE_NULL)
         return nullptr;
 
-    return tones_[currentTone_];
+    return tones_[(size_t)currentTone_];
 }
 
 void
 TelephoneTone::buildTones(unsigned int sampleRate)
 {
-    tones_[Tone::TONE_DIALTONE] = std::make_shared<Tone>(toneZone[countryId_][Tone::TONE_DIALTONE], sampleRate);
-    tones_[Tone::TONE_BUSY] = std::make_shared<Tone>(toneZone[countryId_][Tone::TONE_BUSY], sampleRate);
-    tones_[Tone::TONE_RINGTONE] = std::make_shared<Tone>(toneZone[countryId_][Tone::TONE_RINGTONE], sampleRate);
-    tones_[Tone::TONE_CONGESTION] = std::make_shared<Tone>(toneZone[countryId_][Tone::TONE_CONGESTION], sampleRate);
+    const char* toneZone[(size_t)TelephoneTone::CountryId::ZID_COUNTRIES][(size_t)Tone::ToneId::TONE_NULL] = {
+        {
+            // ZID_NORTH_AMERICA
+            "350+440",                // Tone::TONE_DIALTONE
+            "480+620/500,0/500",      // Tone::TONE_BUSY
+            "440+480/2000,0/4000",    // Tone::TONE_RINGTONE
+            "480+620/250,0/250",      // Tone::TONE_CONGESTION
+        },
+        {
+            //ZID_FRANCE
+            "440",
+            "440/500,0/500",
+            "440/1500,0/3500",
+            "440/250,0/250",
+        },
+        {
+            //ZID_AUSTRALIA
+            "413+438",
+            "425/375,0/375",
+            "413+438/400,0/200,413+438/400,0/2000",
+            "425/375,0/375,420/375,8/375",
+        },
+        {
+            //ZID_UNITED_KINGDOM
+            "350+440",
+            "400/375,0/375",
+            "400+450/400,0/200,400+450/400,0/2000",
+            "400/400,0/350,400/225,0/525",
+        },
+        {
+            //ZID_SPAIN
+            "425",
+            "425/200,0/200",
+            "425/1500,0/3000",
+            "425/200,0/200,425/200,0/200,425/200,0/600",
+        },
+        {
+            //ZID_ITALY
+            "425/600,0/1000,425/200,0/200",
+            "425/500,0/500",
+            "425/1000,0/4000",
+            "425/200,0/200",
+        },
+        {
+            //ZID_JAPAN
+            "400",
+            "400/500,0/500",
+            "400+15/1000,0/2000",
+            "400/500,0/500",
+        }
+    };
+    tones_[(size_t)Tone::ToneId::DIALTONE] = std::make_shared<Tone>(toneZone[(size_t)countryId_][(size_t)Tone::ToneId::DIALTONE], sampleRate);
+    tones_[(size_t)Tone::ToneId::BUSY] = std::make_shared<Tone>(toneZone[(size_t)countryId_][(size_t)Tone::ToneId::BUSY], sampleRate);
+    tones_[(size_t)Tone::ToneId::RINGTONE] = std::make_shared<Tone>(toneZone[(size_t)countryId_][(size_t)Tone::ToneId::RINGTONE], sampleRate);
+    tones_[(size_t)Tone::ToneId::CONGESTION] = std::make_shared<Tone>(toneZone[(size_t)countryId_][(size_t)Tone::ToneId::CONGESTION], sampleRate);
 }
 
 } // namespace jami
diff --git a/src/media/audio/sound/tonelist.h b/src/media/audio/sound/tonelist.h
index 6cab23c30a..378b8a0e83 100644
--- a/src/media/audio/sound/tonelist.h
+++ b/src/media/audio/sound/tonelist.h
@@ -34,7 +34,7 @@ namespace jami {
 class TelephoneTone {
     public:
         /** Countries */
-        enum COUNTRYID {
+        enum class CountryId {
             ZID_NORTH_AMERICA = 0,
             ZID_FRANCE,
             ZID_AUSTRALIA,
@@ -47,20 +47,20 @@ class TelephoneTone {
 
         TelephoneTone(const std::string& countryName, unsigned int sampleRate);
 
-        void setCurrentTone(Tone::TONEID toneId);
+        void setCurrentTone(Tone::ToneId toneId);
         void setSampleRate(unsigned int sampleRate);
         std::shared_ptr<Tone> getCurrentTone();
 
     private:
         NON_COPYABLE(TelephoneTone);
 
-        static COUNTRYID getCountryId(const std::string& countryName);
+        static CountryId getCountryId(const std::string& countryName);
 
         void buildTones(unsigned int sampleRate);
 
-        COUNTRYID countryId_;
-        std::array<std::shared_ptr<Tone>, Tone::TONE_NULL> tones_;
-        Tone::TONEID currentTone_;
+        CountryId countryId_;
+        std::array<std::shared_ptr<Tone>, (size_t)Tone::ToneId::TONE_NULL> tones_;
+        Tone::ToneId currentTone_;
 };
 
 } // namespace jami
diff --git a/src/media/audio/tonecontrol.cpp b/src/media/audio/tonecontrol.cpp
index 4ca1ba2f69..f6dc4b2463 100644
--- a/src/media/audio/tonecontrol.cpp
+++ b/src/media/audio/tonecontrol.cpp
@@ -103,7 +103,7 @@ ToneControl::stop()
     std::lock_guard<std::mutex> lk(mutex_);
 
     if (telephoneTone_)
-        telephoneTone_->setCurrentTone(Tone::TONE_NULL);
+        telephoneTone_->setCurrentTone(Tone::ToneId::TONE_NULL);
 
     if (audioFile_) {
         emitSignal<DRing::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
@@ -112,7 +112,7 @@ ToneControl::stop()
 }
 
 void
-ToneControl::play(Tone::TONEID toneId)
+ToneControl::play(Tone::ToneId toneId)
 {
     std::lock_guard<std::mutex> lk(mutex_);
 
diff --git a/src/media/audio/tonecontrol.h b/src/media/audio/tonecontrol.h
index 6af07045e4..82053ab445 100644
--- a/src/media/audio/tonecontrol.h
+++ b/src/media/audio/tonecontrol.h
@@ -49,7 +49,7 @@ class ToneControl {
         bool setAudioFile(const std::string& file);
         void stopAudioFile();
         void stop();
-        void play(Tone::TONEID toneId);
+        void play(Tone::ToneId toneId);
         void seek(double value);
 
     private:
-- 
GitLab