"Fossies" - the Fresh Open Source Software Archive

Member "audacious-3.10.1/src/libaudgui/equalizer.cc" (26 Dec 2018, 6548 Bytes) of package /linux/misc/audacious-3.10.1.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "equalizer.cc" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * equalizer.c
    3  * Copyright 2010-2015 John Lindgren
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions are met:
    7  *
    8  * 1. Redistributions of source code must retain the above copyright notice,
    9  *    this list of conditions, and the following disclaimer.
   10  *
   11  * 2. Redistributions in binary form must reproduce the above copyright notice,
   12  *    this list of conditions, and the following disclaimer in the documentation
   13  *    provided with the distribution.
   14  *
   15  * This software is provided "as is" and without any warranty, express or
   16  * implied. In no event shall the authors be liable for any damages arising from
   17  * the use of this software.
   18  */
   19 
   20 #include <math.h>
   21 
   22 #include <libaudcore/audstrings.h>
   23 #include <libaudcore/equalizer.h>
   24 #include <libaudcore/hook.h>
   25 #include <libaudcore/i18n.h>
   26 #include <libaudcore/runtime.h>
   27 
   28 #include "internal.h"
   29 #include "libaudgui.h"
   30 #include "libaudgui-gtk.h"
   31 
   32 static void on_off_cb (GtkToggleButton * on_off)
   33 {
   34     aud_set_bool (nullptr, "equalizer_active", gtk_toggle_button_get_active (on_off));
   35 }
   36 
   37 static void on_off_update (void *, GtkWidget * on_off)
   38 {
   39     gtk_toggle_button_set_active ((GtkToggleButton *) on_off, aud_get_bool
   40      (nullptr, "equalizer_active"));
   41 }
   42 
   43 static GtkWidget * create_on_off ()
   44 {
   45     GtkWidget * on_off = gtk_check_button_new_with_mnemonic (_("_Enable"));
   46     g_signal_connect (on_off, "toggled", (GCallback) on_off_cb, nullptr);
   47     hook_associate ("set equalizer_active", (HookFunction) on_off_update, on_off);
   48 
   49     on_off_update (nullptr, on_off);
   50     return on_off;
   51 }
   52 
   53 static void reset_to_zero ()
   54 {
   55     aud_eq_apply_preset (EqualizerPreset ());
   56 }
   57 
   58 static void slider_moved (GtkRange * slider)
   59 {
   60     int band = GPOINTER_TO_INT (g_object_get_data ((GObject *) slider, "band"));
   61     double value = round (gtk_range_get_value (slider));
   62 
   63     if (band == -1)
   64         aud_set_double (nullptr, "equalizer_preamp", value);
   65     else
   66         aud_eq_set_band (band, value);
   67 }
   68 
   69 static GtkWidget * create_slider (const char * name, int band, GtkWidget * hbox)
   70 {
   71     GtkWidget * vbox = gtk_vbox_new (false, 6);
   72 
   73     GtkWidget * label = gtk_label_new (name);
   74     gtk_label_set_angle ((GtkLabel *) label, 90);
   75     gtk_box_pack_start ((GtkBox *) vbox, label, true, false, 0);
   76 
   77     GtkWidget * slider = gtk_vscale_new_with_range (-AUD_EQ_MAX_GAIN, AUD_EQ_MAX_GAIN, 1);
   78     gtk_scale_set_draw_value ((GtkScale *) slider, true);
   79     gtk_scale_set_value_pos ((GtkScale *) slider, GTK_POS_BOTTOM);
   80     gtk_range_set_inverted ((GtkRange *) slider, true);
   81     gtk_widget_set_size_request (slider, -1, audgui_get_dpi () * 5 / 4);
   82 
   83     g_object_set_data ((GObject *) slider, "band", GINT_TO_POINTER (band));
   84     g_signal_connect (slider, "value-changed", (GCallback) slider_moved, nullptr);
   85 
   86     gtk_box_pack_start ((GtkBox *) vbox, slider, false, false, 0);
   87     gtk_box_pack_start ((GtkBox *) hbox, vbox, false, false, 0);
   88 
   89     return slider;
   90 }
   91 
   92 static void set_slider (GtkWidget * slider, double value)
   93 {
   94     g_signal_handlers_block_by_func (slider, (void *) slider_moved, nullptr);
   95     gtk_range_set_value ((GtkRange *) slider, round (value));
   96     g_signal_handlers_unblock_by_func (slider, (void *) slider_moved, nullptr);
   97 }
   98 
   99 static void update_sliders (void *, GtkWidget * window)
  100 {
  101     GtkWidget * preamp = (GtkWidget *) g_object_get_data ((GObject *) window, "preamp");
  102     set_slider (preamp, aud_get_double (nullptr, "equalizer_preamp"));
  103 
  104     double values[AUD_EQ_NBANDS];
  105     aud_eq_get_bands (values);
  106 
  107     for (int i = 0; i < AUD_EQ_NBANDS; i ++)
  108     {
  109         StringBuf slider_id = str_printf ("slider%d", i);
  110         GtkWidget * slider = (GtkWidget *) g_object_get_data ((GObject *) window, slider_id);
  111         set_slider (slider, values[i]);
  112     }
  113 }
  114 
  115 static void destroy_cb ()
  116 {
  117     hook_dissociate ("set equalizer_active", (HookFunction) on_off_update);
  118     hook_dissociate ("set equalizer_bands", (HookFunction) update_sliders);
  119     hook_dissociate ("set equalizer_preamp", (HookFunction) update_sliders);
  120 }
  121 
  122 static GtkWidget * create_window ()
  123 {
  124     const char * const names[AUD_EQ_NBANDS] = {N_("31 Hz"), N_("63 Hz"),
  125      N_("125 Hz"), N_("250 Hz"), N_("500 Hz"), N_("1 kHz"), N_("2 kHz"),
  126      N_("4 kHz"), N_("8 kHz"), N_("16 kHz")};
  127 
  128     GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  129     gtk_window_set_title ((GtkWindow *) window, _("Equalizer"));
  130     gtk_window_set_type_hint ((GtkWindow *) window, GDK_WINDOW_TYPE_HINT_DIALOG);
  131     gtk_window_set_resizable ((GtkWindow *) window, false);
  132     gtk_container_set_border_width ((GtkContainer *) window, 6);
  133     audgui_destroy_on_escape (window);
  134 
  135     GtkWidget * vbox = gtk_vbox_new (false, 6);
  136     gtk_container_add ((GtkContainer *) window, vbox);
  137 
  138     GtkWidget * top_row = gtk_hbox_new (false, 6);
  139     gtk_box_pack_start ((GtkBox *) vbox, top_row, false, false, 0);
  140 
  141     gtk_box_pack_start ((GtkBox *) top_row, create_on_off (), false, false, 0);
  142 
  143     GtkWidget * presets = audgui_button_new (_("Presets ..."), nullptr,
  144      (AudguiCallback) audgui_show_eq_preset_window, nullptr);
  145     gtk_box_pack_end ((GtkBox *) top_row, presets, false, false, 0);
  146 
  147     GtkWidget * zero = audgui_button_new (_("Reset to Zero"), nullptr,
  148      (AudguiCallback) reset_to_zero, nullptr);
  149     gtk_box_pack_end ((GtkBox *) top_row, zero, false, false, 0);
  150 
  151     GtkWidget * hbox = gtk_hbox_new (false, 6);
  152     gtk_box_pack_start ((GtkBox *) vbox, hbox, false, false, 0);
  153 
  154     GtkWidget * preamp = create_slider (_("Preamp"), -1, hbox);
  155     g_object_set_data ((GObject *) window, "preamp", preamp);
  156 
  157     gtk_box_pack_start ((GtkBox *) hbox, gtk_vseparator_new (), false, false, 0);
  158 
  159     for (int i = 0; i < AUD_EQ_NBANDS; i ++)
  160     {
  161         StringBuf slider_id = str_printf ("slider%d", i);
  162         GtkWidget * slider = create_slider (_(names[i]), i, hbox);
  163         g_object_set_data ((GObject *) window, slider_id, slider);
  164     }
  165 
  166     update_sliders (nullptr, window);
  167 
  168     hook_associate ("set equalizer_preamp", (HookFunction) update_sliders, window);
  169     hook_associate ("set equalizer_bands", (HookFunction) update_sliders, window);
  170 
  171     g_signal_connect (window, "destroy", (GCallback) destroy_cb, nullptr);
  172 
  173     return window;
  174 }
  175 
  176 EXPORT void audgui_show_equalizer_window ()
  177 {
  178     if (! audgui_reshow_unique_window (AUDGUI_EQUALIZER_WINDOW))
  179         audgui_show_unique_window (AUDGUI_EQUALIZER_WINDOW, create_window ());
  180 }
  181 
  182 EXPORT void audgui_hide_equalizer_window ()
  183 {
  184     audgui_hide_unique_window (AUDGUI_EQUALIZER_WINDOW);
  185 }