@@ -18,16 +18,18 @@ waybar::modules::cava::CavaBackend::CavaBackend(const Json::Value& config) {
1818  //  Load cava config
1919  error_.length  = 0 ;
2020
21-   if  (!load_config (cfgPath, &prm_, false , &error_)) {
21+   if  (!load_config (cfgPath, &prm_, false , &error_,  0 )) {
2222    spdlog::error (" cava backend. Error loading config. {0}" message );
2323    exit (EXIT_FAILURE);
2424  }
2525
2626  //  Override cava parameters by the user config
2727  prm_.inAtty  = 0 ;
2828  prm_.output  = ::cava::output_method::OUTPUT_RAW;
29-   strcpy (prm_.data_format , " ascii" 
30-   strcpy (prm_.raw_target , " /dev/stdout" 
29+   if  (prm_.data_format ) free (prm_.data_format );
30+   prm_.data_format  = strdup (" ascii" 
31+   if  (prm_.raw_target ) free (prm_.raw_target );
32+   prm_.raw_target  = strdup (" /dev/stdout" 
3133  prm_.ascii_range  = config[" format-icons" size () - 1 ;
3234
3335  prm_.bar_width  = 2 ;
@@ -54,7 +56,10 @@ waybar::modules::cava::CavaBackend::CavaBackend(const Json::Value& config) {
5456  if  (config[" sleep_timer" isInt ()) prm_.sleep_timer  = config[" sleep_timer" asInt ();
5557  if  (config[" method" isString ())
5658    prm_.input  = ::cava::input_method_by_name (config[" method" asString ().c_str ());
57-   if  (config[" source" isString ()) prm_.audio_source  = config[" source" asString ().data ();
59+   if  (config[" source" isString ()) {
60+     if  (prm_.audio_source ) free (prm_.audio_source );
61+     prm_.audio_source  = config[" source" asString ().data ();
62+   }
5863  if  (config[" sample_rate" isNumeric ()) prm_.samplerate  = config[" sample_rate" asLargestInt ();
5964  if  (config[" sample_bits" isInt ()) prm_.samplebits  = config[" sample_bits" asInt ();
6065  if  (config[" stereo" isBool ()) prm_.stereo  = config[" stereo" asBool ();
@@ -67,25 +72,14 @@ waybar::modules::cava::CavaBackend::CavaBackend(const Json::Value& config) {
6772  if  (config[" input_delay" isInt ())
6873    fetch_input_delay_ = std::chrono::seconds (config[" input_delay" asInt ());
6974
70-   //  Make cava parameters configuration
71-   plan_ = new  ::cava::cava_plan{};
72- 
7375  audio_raw_.height  = prm_.ascii_range ;
7476  audio_data_.format  = -1 ;
75-   audio_data_.source  = new  char [1  + strlen (prm_.audio_source )];
76-   audio_data_.source [0 ] = ' \0 ' 
77-   strcpy (audio_data_.source , prm_.audio_source );
78- 
7977  audio_data_.rate  = 0 ;
8078  audio_data_.samples_counter  = 0 ;
8179  audio_data_.channels  = 2 ;
8280  audio_data_.IEEE_FLOAT  = 0 ;
83- 
8481  audio_data_.input_buffer_size  = BUFFER_SIZE * audio_data_.channels ;
8582  audio_data_.cava_buffer_size  = audio_data_.input_buffer_size  * 8 ;
86- 
87-   audio_data_.cava_in  = new  double [audio_data_.cava_buffer_size ]{0.0 };
88- 
8983  audio_data_.terminate  = 0 ;
9084  audio_data_.suspendFlag  = false ;
9185  input_source_ = get_input (&audio_data_, &prm_);
@@ -95,8 +89,9 @@ waybar::modules::cava::CavaBackend::CavaBackend(const Json::Value& config) {
9589    exit (EXIT_FAILURE);
9690  }
9791
92+   //  Make cava parameters configuration
9893  //  Init cava plan, audio_raw structure
99-   audio_raw_init (&audio_data_, &audio_raw_, &prm_, plan_);
94+   audio_raw_init (&audio_data_, &audio_raw_, &prm_, & plan_);
10095  if  (!plan_) spdlog::error (" cava backend plan is not provided" 
10196  audio_raw_.previous_frame [0 ] = -1 ;  //  For first Update() call need to rePaint text message
10297  //  Read audio source trough cava API. Cava orginizes this process via infinity loop
@@ -118,8 +113,16 @@ waybar::modules::cava::CavaBackend::CavaBackend(const Json::Value& config) {
118113waybar::modules::cava::CavaBackend::~CavaBackend () {
119114  thread_.stop ();
120115  read_thread_.stop ();
116+   cava_destroy (plan_);
121117  delete  plan_;
122118  plan_ = nullptr ;
119+   audio_raw_clean (&audio_raw_);
120+   pthread_mutex_lock (&audio_data_.lock );
121+   audio_data_.terminate  = 1 ;
122+   pthread_mutex_unlock (&audio_data_.lock );
123+   config_clean (&prm_);
124+   free (audio_data_.source );
125+   free (audio_data_.cava_in );
123126}
124127
125128static  void  upThreadDelay (std::chrono::milliseconds& delay, std::chrono::seconds& delta) {
0 commit comments