| 1 |
/* |
|---|
| 2 |
* Sweep, a sound wave editor. |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (C) 2000 Conrad Parker |
|---|
| 5 |
* Copyright (C) 2002 Commonwealth Scientific and Industrial Research |
|---|
| 6 |
* Organisation (CSIRO), Australia |
|---|
| 7 |
* |
|---|
| 8 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 9 |
* it under the terms of the GNU General Public License as published by |
|---|
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 11 |
* (at your option) any later version. |
|---|
| 12 |
* |
|---|
| 13 |
* This program is distributed in the hope that it will be useful, |
|---|
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 |
* GNU General Public License for more details. |
|---|
| 17 |
* |
|---|
| 18 |
* You should have received a copy of the GNU General Public License |
|---|
| 19 |
* along with this program; if not, write to the Free Software |
|---|
| 20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 21 |
*/ |
|---|
| 22 |
|
|---|
| 23 |
#ifdef HAVE_CONFIG_H |
|---|
| 24 |
# include <config.h> |
|---|
| 25 |
#endif |
|---|
| 26 |
|
|---|
| 27 |
#include <math.h> |
|---|
| 28 |
#include <stdio.h> |
|---|
| 29 |
#include <stdlib.h> |
|---|
| 30 |
#include <string.h> |
|---|
| 31 |
#include <errno.h> |
|---|
| 32 |
#include <glib.h> |
|---|
| 33 |
#include <gdk/gdkkeysyms.h> |
|---|
| 34 |
#include <gtk/gtk.h> |
|---|
| 35 |
|
|---|
| 36 |
#include <sweep/sweep_i18n.h> |
|---|
| 37 |
#include <sweep/sweep_types.h> |
|---|
| 38 |
#include <sweep/sweep_sample.h> |
|---|
| 39 |
|
|---|
| 40 |
#include "interface.h" |
|---|
| 41 |
|
|---|
| 42 |
#include "db_slider.h" |
|---|
| 43 |
|
|---|
| 44 |
enum { |
|---|
| 45 |
VALUE_CHANGED_SIGNAL, |
|---|
| 46 |
LAST_SIGNAL |
|---|
| 47 |
}; |
|---|
| 48 |
|
|---|
| 49 |
static gint db_slider_signals[LAST_SIGNAL] = { 0 }; |
|---|
| 50 |
|
|---|
| 51 |
static void |
|---|
| 52 |
db_slider_class_init(DbSliderClass * klass) |
|---|
| 53 |
{ |
|---|
| 54 |
GtkObjectClass *object_class; |
|---|
| 55 |
|
|---|
| 56 |
object_class = (GtkObjectClass *) klass; |
|---|
| 57 |
|
|---|
| 58 |
db_slider_signals[VALUE_CHANGED_SIGNAL] = g_signal_new ("value-changed", |
|---|
| 59 |
G_TYPE_FROM_CLASS (klass), |
|---|
| 60 |
G_SIGNAL_RUN_FIRST, |
|---|
| 61 |
G_STRUCT_OFFSET (DbSliderClass, value_changed), |
|---|
| 62 |
NULL, |
|---|
| 63 |
NULL, |
|---|
| 64 |
g_cclosure_marshal_VOID__FLOAT, |
|---|
| 65 |
G_TYPE_NONE, 1, G_TYPE_FLOAT); |
|---|
| 66 |
klass->value_changed = NULL; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
static void |
|---|
| 70 |
db_slider_init (GtkWidget * slider) |
|---|
| 71 |
{ |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
GType |
|---|
| 75 |
db_slider_get_type() |
|---|
| 76 |
{ |
|---|
| 77 |
static GType db_slider_type = 0; |
|---|
| 78 |
|
|---|
| 79 |
if (!db_slider_type) { |
|---|
| 80 |
static const GTypeInfo db_slider_info = |
|---|
| 81 |
{ |
|---|
| 82 |
sizeof(DbSliderClass), |
|---|
| 83 |
NULL, /* base_init */ |
|---|
| 84 |
NULL, /* base_finalize */ |
|---|
| 85 |
(GClassInitFunc) db_slider_class_init, |
|---|
| 86 |
NULL, /* class_finalize */ |
|---|
| 87 |
NULL, /* class_data */ |
|---|
| 88 |
sizeof (DbSlider), |
|---|
| 89 |
0, |
|---|
| 90 |
(GInstanceInitFunc) db_slider_init, |
|---|
| 91 |
|
|---|
| 92 |
}; |
|---|
| 93 |
|
|---|
| 94 |
db_slider_type = g_type_register_static(GTK_TYPE_VBOX, "DbSlider" ,&db_slider_info, 0); |
|---|
| 95 |
|
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
return db_slider_type; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
#define VALUE_TO_DB(v) (20 * log10(v)) |
|---|
| 102 |
#define DB_TO_VALUE(d) (pow (10, ((d) / 20))) |
|---|
| 103 |
|
|---|
| 104 |
#define SQR(x) ((x)*(x)) |
|---|
| 105 |
#define ADJ_TO_VALUE(a) SQR((100.0 - (a)) / 100.0) |
|---|
| 106 |
#define VALUE_TO_ADJ(v) ((1.0 - sqrt(v)) * 100.0) |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
gfloat |
|---|
| 110 |
db_slider_get_value (DbSlider * slider) |
|---|
| 111 |
{ |
|---|
| 112 |
return ADJ_TO_VALUE(GTK_ADJUSTMENT(slider->adj)->value); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
void |
|---|
| 116 |
db_slider_set_value (DbSlider * slider, gfloat value) |
|---|
| 117 |
{ |
|---|
| 118 |
gtk_adjustment_set_value (GTK_ADJUSTMENT(slider->adj), |
|---|
| 119 |
VALUE_TO_ADJ(value)); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
static void |
|---|
| 123 |
db_slider_value_changed_cb (GtkWidget * widget, gpointer data) |
|---|
| 124 |
{ |
|---|
| 125 |
GtkWidget * slider = (GtkWidget *)data; |
|---|
| 126 |
gfloat value, db_value; |
|---|
| 127 |
gchar * db_text; |
|---|
| 128 |
|
|---|
| 129 |
value = db_slider_get_value (DB_SLIDER(slider)); |
|---|
| 130 |
db_value = VALUE_TO_DB (value); |
|---|
| 131 |
|
|---|
| 132 |
if (db_value > -10.0) { |
|---|
| 133 |
db_text = g_strdup_printf ("%1.1f dB", db_value); |
|---|
| 134 |
} else { |
|---|
| 135 |
db_text = g_strdup_printf ("%2.0f dB", db_value); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
gtk_label_set_text (GTK_LABEL(DB_SLIDER(slider)->db_label), db_text); |
|---|
| 139 |
|
|---|
| 140 |
g_free (db_text); |
|---|
| 141 |
|
|---|
| 142 |
g_signal_emit_by_name (GTK_OBJECT(slider), "value-changed", |
|---|
| 143 |
value); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
static void |
|---|
| 147 |
db_slider_build (GtkWidget * slider, gchar * title, gfloat value) |
|---|
| 148 |
{ |
|---|
| 149 |
GtkWidget * vbox; |
|---|
| 150 |
GtkWidget * label; |
|---|
| 151 |
GtkWidget * vscale; |
|---|
| 152 |
|
|---|
| 153 |
GtkObject * adj; |
|---|
| 154 |
|
|---|
| 155 |
gchar * range_text; |
|---|
| 156 |
|
|---|
| 157 |
vbox = gtk_vbox_new (FALSE, 0); |
|---|
| 158 |
gtk_container_add (GTK_CONTAINER(slider), vbox); |
|---|
| 159 |
gtk_widget_show (vbox); |
|---|
| 160 |
|
|---|
| 161 |
label = gtk_label_new (NULL); |
|---|
| 162 |
gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0); |
|---|
| 163 |
gtk_widget_show (label); |
|---|
| 164 |
|
|---|
| 165 |
DB_SLIDER(slider)->db_label = label; |
|---|
| 166 |
|
|---|
| 167 |
adj = gtk_adjustment_new (VALUE_TO_ADJ(value), /* value */ |
|---|
| 168 |
VALUE_TO_ADJ(DB_SLIDER(slider)->upper), /* lower */ |
|---|
| 169 |
VALUE_TO_ADJ(DB_SLIDER(slider)->lower), /* upper */ |
|---|
| 170 |
0.3, /* step incr */ |
|---|
| 171 |
3.0, /* page incr */ |
|---|
| 172 |
0.0 /* page size */ |
|---|
| 173 |
); |
|---|
| 174 |
|
|---|
| 175 |
vscale = gtk_vscale_new (GTK_ADJUSTMENT(adj)); |
|---|
| 176 |
gtk_scale_set_draw_value (GTK_SCALE(vscale), FALSE); |
|---|
| 177 |
gtk_range_set_update_policy (GTK_RANGE(vscale), GTK_UPDATE_CONTINUOUS); |
|---|
| 178 |
gtk_widget_set_size_request (vscale, -1, gdk_screen_height() / 8); |
|---|
| 179 |
gtk_box_pack_start (GTK_BOX(vbox), vscale, TRUE, TRUE, 0); |
|---|
| 180 |
gtk_widget_show (vscale); |
|---|
| 181 |
|
|---|
| 182 |
g_signal_connect (G_OBJECT(adj), "value_changed", |
|---|
| 183 |
G_CALLBACK(db_slider_value_changed_cb), slider); |
|---|
| 184 |
|
|---|
| 185 |
DB_SLIDER(slider)->adj = adj; |
|---|
| 186 |
|
|---|
| 187 |
db_slider_value_changed_cb (NULL, slider); |
|---|
| 188 |
|
|---|
| 189 |
range_text = g_strdup_printf ("%s\n[%2.0f to %2.0f dB]", |
|---|
| 190 |
title, |
|---|
| 191 |
VALUE_TO_DB(DB_SLIDER(slider)->lower), |
|---|
| 192 |
VALUE_TO_DB(DB_SLIDER(slider)->upper)); |
|---|
| 193 |
|
|---|
| 194 |
label = gtk_label_new (range_text); |
|---|
| 195 |
gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0); |
|---|
| 196 |
gtk_widget_show (label); |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
GtkWidget * |
|---|
| 201 |
db_slider_new (gchar * title, gfloat value, gfloat lower, gfloat upper) |
|---|
| 202 |
{ |
|---|
| 203 |
DbSlider * slider = DB_SLIDER (g_object_new (db_slider_get_type (), NULL)); |
|---|
| 204 |
|
|---|
| 205 |
slider->lower = lower; |
|---|
| 206 |
slider->upper = upper; |
|---|
| 207 |
|
|---|
| 208 |
db_slider_build (GTK_WIDGET (slider), title, value); |
|---|
| 209 |
|
|---|
| 210 |
return GTK_WIDGET (slider); |
|---|
| 211 |
} |
|---|