diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2012-05-26 17:43:24 +0000 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2012-05-26 17:43:24 +0000 |
commit | 50d600686c74fe2042d3d653427561b8832d5f70 (patch) | |
tree | d7e5378aad0ff54ac693c04d73c206999c8410c2 /tests/test4 | |
parent | eaf60defac761cd8ff305c3fc3875346ad3478b2 (diff) | |
download | 2012-violon-leds-50d600686c74fe2042d3d653427561b8832d5f70.tar.gz 2012-violon-leds-50d600686c74fe2042d3d653427561b8832d5f70.tar.bz2 2012-violon-leds-50d600686c74fe2042d3d653427561b8832d5f70.zip |
Ajout vu meters GTK
git-svn-id: file:///var/svn/2012-violon-leds/trunk@5 6be1fa4d-33ac-4c33-becc-79fcb3794bb6
Diffstat (limited to 'tests/test4')
-rwxr-xr-x | tests/test4/compil.sh | 7 | ||||
-rw-r--r-- | tests/test4/compute.c | 42 | ||||
-rw-r--r-- | tests/test4/compute.h | 10 | ||||
-rw-r--r-- | tests/test4/gtkvumeter.c | 408 | ||||
-rw-r--r-- | tests/test4/gtkvumeter.h | 80 | ||||
-rw-r--r-- | tests/test4/test4.c | 56 | ||||
-rw-r--r-- | tests/test4/win_main.c | 65 | ||||
-rw-r--r-- | tests/test4/win_main.h | 9 |
8 files changed, 664 insertions, 13 deletions
diff --git a/tests/test4/compil.sh b/tests/test4/compil.sh index 4bab218..7679e1f 100755 --- a/tests/test4/compil.sh +++ b/tests/test4/compil.sh @@ -1,3 +1,6 @@ #!/bin/bash -ex -gcc -g -c test4.c $(pkg-config --cflags gtk+-2.0) -gcc -o test4 test4.o $(pkg-config --libs gtk+-2.0) +gcc -Wall -g -c gtkvumeter.c $(pkg-config --cflags gtk+-2.0) +gcc -Wall -g -c win_main.c $(pkg-config --cflags gtk+-2.0) +gcc -Wall -g -c compute.c $(pkg-config --cflags gtk+-2.0) +gcc -Wall -g -c test4.c $(pkg-config --cflags gtk+-2.0 gthread-2.0) +gcc -Wall -o test4 gtkvumeter.o win_main.o compute.o test4.o $(pkg-config --libs gtk+-2.0 gthread-2.0) diff --git a/tests/test4/compute.c b/tests/test4/compute.c new file mode 100644 index 0000000..91a5455 --- /dev/null +++ b/tests/test4/compute.c @@ -0,0 +1,42 @@ +#include "compute.h" + +void function audio2hsv_1(gint audio_level, gint *light_h, gint *light_s, gint *light_v) { + // Dummy code + *light_h=-audio_level; + *light_s=audio_level; + *light_v=65535; +} + + +void hsv2rgb(gint h, gint s, gint v, gint *r, gint *g, gint *b) { + /* + * Purpose: + * Convert HSV values to RGB values + * All values are in the range [0..65535] + */ + float F, M, N, K; + int I; + + if ( s == 0 ) { + /* + * Achromatic case, set level of grey + */ + *r = v; + *g = v; + *b = v; + } else { + I = (int) h/(65535/6); /* should be in the range 0..5 */ + F = h - I; /* fractional part */ + + M = v * (1 - s); + N = v * (1 - s * F); + K = v * (1 - s * (1 - F)); + + if (I == 0) { *r = v; *g = K; *b = M; } + if (I == 1) { *r = N; *g = v; *b = M; } + if (I == 2) { *r = M; *g = v; *b = K; } + if (I == 3) { *r = M; *g = N; *b = v; } + if (I == 4) { *r = K; *g = M; *b = v; } + if (I == 5) { *r = v; *g = M; *b = N; } + } +} diff --git a/tests/test4/compute.h b/tests/test4/compute.h new file mode 100644 index 0000000..2454e56 --- /dev/null +++ b/tests/test4/compute.h @@ -0,0 +1,10 @@ +#ifndef COMPUTE_H +#define COMPUTE_H + +#include <gtk/gtk.h> + +void audio2hsv_1(gint audio_level, gint *light_h, gint *light_s, gint *light_v); +void hsv2rgb(gint h, gint s, gint v, gint *r, gint *g, gint *b); + +#endif + diff --git a/tests/test4/gtkvumeter.c b/tests/test4/gtkvumeter.c new file mode 100644 index 0000000..2cc1b61 --- /dev/null +++ b/tests/test4/gtkvumeter.c @@ -0,0 +1,408 @@ +/*************************************************************************** + * gtkvumeter.c + * + * Fri Jan 10 20:06:23 2003 + * Copyright 2003 Todd Goyen + * wettoad@knighthoodofbuh.org + ****************************************************************************/ + +#include <math.h> +#include <gtk/gtk.h> +#include "gtkvumeter.h" + +#define MIN_HORIZONTAL_VUMETER_WIDTH 40 +#define HORIZONTAL_VUMETER_HEIGHT 20 +#define VERTICAL_VUMETER_WIDTH 20 +#define MIN_VERTICAL_VUMETER_HEIGHT 40 + +static void gtk_vumeter_init (GtkVUMeter *vumeter); +static void gtk_vumeter_class_init (GtkVUMeterClass *class); +static void gtk_vumeter_destroy (GtkObject *object); +static void gtk_vumeter_realize (GtkWidget *widget); +static void gtk_vumeter_size_request (GtkWidget *widget, GtkRequisition *requisition); +static void gtk_vumeter_size_allocate (GtkWidget *widget, GtkAllocation *allocation); +static gint gtk_vumeter_expose (GtkWidget *widget, GdkEventExpose *event); +static void gtk_vumeter_free_colors (GtkVUMeter *vumeter); +static void gtk_vumeter_setup_colors (GtkVUMeter *vumeter); +static gint gtk_vumeter_sound_level_to_draw_level (GtkVUMeter *vumeter); + +static GdkColor default_f_gradient_keys[3] = {{0,65535,0,0},{0,65535,65535,0},{0,0,65535,0}}; +static GdkColor default_b_gradient_keys[3] = {{0,49151,0,0},{0,49151,49151,0},{0,0,49151,0}}; +static GtkWidgetClass *parent_class = NULL; + + + +GtkType gtk_vumeter_get_type (void) +{ + static GType vumeter_type = 0; + + if (!vumeter_type) { + static const GTypeInfo vumeter_info = { + sizeof (GtkVUMeterClass), + NULL, NULL, + (GClassInitFunc) gtk_vumeter_class_init, + NULL, NULL, sizeof (GtkVUMeter), 0, + (GInstanceInitFunc) gtk_vumeter_init, + }; + vumeter_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkVUMeter", &vumeter_info, 0); + } + + return vumeter_type; +} + +GtkWidget* gtk_vumeter_new (gboolean vertical) +{ + GtkVUMeter *vumeter; + vumeter = gtk_type_new (GTK_TYPE_VUMETER); + vumeter->vertical = vertical; + return GTK_WIDGET (vumeter); +} + +static void gtk_vumeter_init (GtkVUMeter *vumeter) +{ + vumeter->colormap = NULL; + vumeter->colors = 0; + vumeter->f_gc = NULL; + vumeter->b_gc = NULL; + vumeter->f_colors = NULL; + vumeter->b_colors = NULL; + + vumeter->level = 0; + vumeter->min = 0; + vumeter->max = 32767; + vumeter->peaks_falloff = GTK_VUMETER_PEAKS_FALLOFF_MEDIUM; + vumeter->peak_level = 0; + + vumeter->scale = GTK_VUMETER_SCALE_LINEAR; + + //XXX A bit heavy... + gtk_vumeter_set_gradient(vumeter, 3, default_f_gradient_keys, 3, default_b_gradient_keys); +} + +static void gtk_vumeter_class_init (GtkVUMeterClass *class) +{ + GtkObjectClass *object_class; + GtkWidgetClass *widget_class; + + object_class = (GtkObjectClass*) class; + widget_class = (GtkWidgetClass*) class; + parent_class = gtk_type_class(gtk_widget_get_type()); + + object_class->destroy = gtk_vumeter_destroy; + + widget_class->realize = gtk_vumeter_realize; + widget_class->expose_event = gtk_vumeter_expose; + widget_class->size_request = gtk_vumeter_size_request; + widget_class->size_allocate = gtk_vumeter_size_allocate; +} + +static void gtk_vumeter_destroy (GtkObject *object) +{ + GtkVUMeter *vumeter = GTK_VUMETER (object); + + gtk_vumeter_free_colors (vumeter); + + GTK_OBJECT_CLASS (parent_class)->destroy (object); +} + +static void gtk_vumeter_realize (GtkWidget *widget) +{ + GtkVUMeter *vumeter; + GdkWindowAttr attributes; + gint attributes_mask; + + g_return_if_fail (widget != NULL); + g_return_if_fail (GTK_IS_VUMETER (widget)); + + GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); + vumeter = GTK_VUMETER (widget); + + attributes.x = widget->allocation.x; + attributes.y = widget->allocation.y; + attributes.width = widget->allocation.width; + attributes.height = widget->allocation.height; + attributes.wclass = GDK_INPUT_OUTPUT; + attributes.window_type = GDK_WINDOW_CHILD; + attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK; + attributes.visual = gtk_widget_get_visual (widget); + attributes.colormap = gtk_widget_get_colormap (widget); + attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; + widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask); + + widget->style = gtk_style_attach (widget->style, widget->window); + + gdk_window_set_user_data (widget->window, widget); + gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL); + + /* colors */ + vumeter->colormap = gdk_colormap_get_system (); + gtk_vumeter_setup_colors (vumeter); +} + +static void gtk_vumeter_size_request (GtkWidget *widget, GtkRequisition *requisition) +{ + GtkVUMeter *vumeter; + + g_return_if_fail (GTK_IS_VUMETER (widget)); + g_return_if_fail (requisition != NULL); + + vumeter = GTK_VUMETER (widget); + + if (vumeter->vertical == TRUE) { + requisition->width = VERTICAL_VUMETER_WIDTH; + requisition->height = MIN_VERTICAL_VUMETER_HEIGHT; + } else { + requisition->width = MIN_HORIZONTAL_VUMETER_WIDTH; + requisition->height = HORIZONTAL_VUMETER_HEIGHT; + } +} + +static void gtk_vumeter_size_allocate (GtkWidget *widget, GtkAllocation *allocation) +{ + GtkVUMeter *vumeter; + + g_return_if_fail (widget != NULL); + g_return_if_fail (GTK_IS_VUMETER (widget)); + g_return_if_fail (allocation != NULL); + + widget->allocation = *allocation; + vumeter = GTK_VUMETER (widget); + + if (GTK_WIDGET_REALIZED (widget)) { + if (vumeter->vertical == TRUE) { /* veritcal */ + gdk_window_move_resize (widget->window, allocation->x, allocation->y, + VERTICAL_VUMETER_WIDTH, MAX(allocation->height, MIN_VERTICAL_VUMETER_HEIGHT)); + } else { /* horizontal */ + gdk_window_move_resize (widget->window, allocation->x, allocation->y, + MAX(allocation->width, MIN_HORIZONTAL_VUMETER_WIDTH), HORIZONTAL_VUMETER_HEIGHT); + } + /* Fix the colours */ + gtk_vumeter_setup_colors (vumeter); + } +} + +static gint gtk_vumeter_expose (GtkWidget *widget, GdkEventExpose *event) +{ + GtkVUMeter *vumeter; + gint index, level; + gint width, height; + + g_return_val_if_fail (widget != NULL, FALSE); + g_return_val_if_fail (GTK_IS_VUMETER (widget), FALSE); + g_return_val_if_fail (event != NULL, FALSE); + + if (event->count > 0) + return FALSE; + + vumeter = GTK_VUMETER (widget); + level = gtk_vumeter_sound_level_to_draw_level (vumeter); + + if (vumeter->vertical == TRUE) { + width = widget->allocation.width - 2; + height = widget->allocation.height; + + /* draw border */ + gtk_paint_box (widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_IN, + NULL, widget, "trough", 0, 0, widget->allocation.width, height); + /* draw background gradient */ + for (index = 0; index < level; index++) { + gdk_draw_line (widget->window, vumeter->b_gc[index], 1, index + 1, width, index + 1); + } + /* draw foreground gradient */ + for (index = level; index < height - 2; index++) { + gdk_draw_line (widget->window, vumeter->f_gc[index], 1, index + 1, width, index + 1); + } + } else { /* Horizontal */ + width = widget->allocation.width; + height = widget->allocation.height - 2; + + /* draw border */ + gtk_paint_box (widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_IN, + NULL, widget, "trough", 0, 0, width, widget->allocation.height); + /* draw background gradient */ + for (index = 0; index < level; index++) { + gdk_draw_line (widget->window, vumeter->b_gc[index], index + 1, 1, index + 1, height); + } + /* draw foreground gradient */ + for (index = level; index < height - 2; index++) { + gdk_draw_line (widget->window, vumeter->f_gc[index], index + 1, 1, index + 1, height); + } + } + + return FALSE; +} + +static void gtk_vumeter_free_colors (GtkVUMeter *vumeter) +{ + gint index; + + /* Free old gc's */ + if (vumeter->f_gc && vumeter->b_gc) { + for (index = 0; index < vumeter->colors; index++) { + if (vumeter->f_gc[index]) { + g_object_unref (G_OBJECT(vumeter->f_gc[index])); + } + if (vumeter->b_gc[index]) { + g_object_unref (G_OBJECT(vumeter->b_gc[index])); + } + } + g_free(vumeter->f_gc); + g_free(vumeter->b_gc); + vumeter->f_gc = NULL; + vumeter->b_gc = NULL; + } + + /* Free old Colors */ + if (vumeter->f_colors) { + gdk_colormap_free_colors (vumeter->colormap, vumeter->f_colors, vumeter->colors); + g_free (vumeter->f_colors); + vumeter->f_colors = NULL; + } + if (vumeter->b_colors) { + gdk_colormap_free_colors (vumeter->colormap, vumeter->b_colors, vumeter->colors); + g_free (vumeter->b_colors); + vumeter->b_colors = NULL; + } +} + +static void gtk_vumeter_setup_colors (GtkVUMeter *vumeter) +{ + gint index; + gint max; + gint f_key_len, b_key_len; + gint f_key_index, b_key_index; + gdouble f_key_pos, b_key_pos; + GdkColor *fgk, *bgk; + + g_return_if_fail (vumeter->colormap != NULL); + + gtk_vumeter_free_colors (vumeter); + + /* Set new size */ + if (vumeter->vertical == TRUE) { + vumeter->colors = MAX(GTK_WIDGET(vumeter)->allocation.height - 2, 0); + } else { + vumeter->colors = MAX(GTK_WIDGET(vumeter)->allocation.width - 2, 0); + } + + /* allocate new memory */ + vumeter->f_colors = g_malloc (vumeter->colors * sizeof(GdkColor)); + vumeter->b_colors = g_malloc (vumeter->colors * sizeof(GdkColor)); + vumeter->f_gc = g_malloc (vumeter->colors * sizeof(GdkGC *)); + vumeter->b_gc = g_malloc (vumeter->colors * sizeof(GdkGC *)); + + /* Initialize stuff */ + + max=vumeter->colors; + f_key_len = max / (vumeter->f_gradient_key_count-1) + 1; + b_key_len = max / (vumeter->b_gradient_key_count-1) + 1; + fgk=vumeter->f_gradient_keys; + bgk=vumeter->b_gradient_keys; + + for (index = 0; index < max; index++) { + f_key_index=index/f_key_len; + f_key_pos=((gdouble) (index%f_key_len)/f_key_len); + b_key_index=index/f_key_len; + b_key_pos=((gdouble) (index%b_key_len)/b_key_len); + + /* Generate the Colours */ + /* foreground */ + vumeter->f_colors[index].red = fgk[f_key_index].red*(1.0-f_key_pos) + fgk[f_key_index+1].red*f_key_pos; + vumeter->f_colors[index].green = fgk[f_key_index].green*(1.0-f_key_pos) + fgk[f_key_index+1].green*f_key_pos; + vumeter->f_colors[index].blue = fgk[f_key_index].blue*(1.0-f_key_pos) + fgk[f_key_index+1].blue*f_key_pos; + /* background */ + vumeter->b_colors[index].red = bgk[b_key_index].red*(1.0-b_key_pos) + bgk[b_key_index+1].red*b_key_pos; + vumeter->b_colors[index].green = bgk[b_key_index].green*(1.0-b_key_pos) + bgk[b_key_index+1].green*b_key_pos; + vumeter->b_colors[index].blue = bgk[b_key_index].blue*(1.0-b_key_pos) + bgk[b_key_index+1].blue*b_key_pos; + + /* Allocate the Colours */ + /* foreground */ + //TODO : try to use gdk_colormap_alloc_colors or suppress f_colors array + gdk_colormap_alloc_color (vumeter->colormap, &vumeter->f_colors[index], FALSE, TRUE); + vumeter->f_gc[index] = gdk_gc_new(GTK_WIDGET(vumeter)->window); + gdk_gc_set_foreground(vumeter->f_gc[index], &vumeter->f_colors[index]); + /* background */ + gdk_colormap_alloc_color (vumeter->colormap, &vumeter->b_colors[index], FALSE, TRUE); + vumeter->b_gc[index] = gdk_gc_new(GTK_WIDGET(vumeter)->window); + gdk_gc_set_foreground(vumeter->b_gc[index], &vumeter->b_colors[index]); + } +} + +static gint gtk_vumeter_sound_level_to_draw_level (GtkVUMeter *vumeter) +{ + gdouble draw_level; + gdouble level, min, max, height; + gdouble log_level, log_max; + + level = (gdouble)vumeter->level; + min = (gdouble)vumeter->min; + max = (gdouble)vumeter->max; + height = (gdouble)vumeter->colors; + + if (vumeter->scale == GTK_VUMETER_SCALE_LINEAR) { + draw_level = (1.0 - (level - min)/(max - min)) * height; + } else { + log_level = log10((level - min + 1)/(max - min + 1)); + log_max = log10(1/(max - min + 1)); + draw_level = log_level/log_max * height; + } + + return ((gint)draw_level); +} + +void gtk_vumeter_set_min_max (GtkVUMeter *vumeter, gint min, gint max) +{ + g_return_if_fail (vumeter != NULL); + g_return_if_fail (GTK_IS_VUMETER (vumeter)); + + vumeter->max = MAX(max, min); + vumeter->min = MIN(min, max); + if (vumeter->max == vumeter->min) { + vumeter->max++; + } + vumeter->level = CLAMP (vumeter->level, vumeter->min, vumeter->max); + gtk_widget_queue_draw (GTK_WIDGET(vumeter)); +} + +void gtk_vumeter_set_level (GtkVUMeter *vumeter, gint level) +{ + g_return_if_fail (vumeter != NULL); + g_return_if_fail (GTK_IS_VUMETER (vumeter)); + + vumeter->level = CLAMP (level, vumeter->min, vumeter->max); + gtk_widget_queue_draw (GTK_WIDGET(vumeter)); +} + +void gtk_vumeter_set_peaks_falloff (GtkVUMeter *vumeter, gint peaks_falloff) +{ + g_return_if_fail (vumeter != NULL); + g_return_if_fail (GTK_IS_VUMETER (vumeter)); +} + +void gtk_vumeter_set_scale (GtkVUMeter *vumeter, gint scale) +{ + g_return_if_fail (vumeter != NULL); + g_return_if_fail (GTK_IS_VUMETER (vumeter)); + + if (scale != vumeter->scale) { + vumeter->scale = CLAMP(scale, GTK_VUMETER_SCALE_LINEAR, GTK_VUMETER_SCALE_LAST - 1); + if (GTK_WIDGET_REALIZED(vumeter)) { + gtk_vumeter_setup_colors (vumeter); + gtk_widget_queue_draw (GTK_WIDGET(vumeter)); + } + } +} + +void gtk_vumeter_set_gradient (GtkVUMeter *vumeter, gint f_gradient_key_count, GdkColor *f_gradient_keys, gint b_gradient_key_count, GdkColor *b_gradient_keys) { + //XXX : memdup is a bad idea ? + GdkColor *fgk = g_memdup(f_gradient_keys, f_gradient_key_count*sizeof(GdkColor)); + GdkColor *bgk = g_memdup(b_gradient_keys, b_gradient_key_count*sizeof(GdkColor)); + g_return_if_fail (fgk != NULL); + g_return_if_fail (bgk != NULL); + + vumeter->f_gradient_keys = fgk; + vumeter->f_gradient_key_count=f_gradient_key_count; + vumeter->b_gradient_keys = bgk; + vumeter->b_gradient_key_count=b_gradient_key_count; +} + diff --git a/tests/test4/gtkvumeter.h b/tests/test4/gtkvumeter.h new file mode 100644 index 0000000..ea64dcb --- /dev/null +++ b/tests/test4/gtkvumeter.h @@ -0,0 +1,80 @@ +/*************************************************************************** + * gtkvumeter.h + * + * Fri Jan 10 20:06:41 2003 + * Copyright 2003 Todd Goyen + * wettoad@knighthoodofbuh.org + ****************************************************************************/ + +#ifndef __GTKVUMETER_H__ +#define __GTKVUMETER_H__ + +#include <gtk/gtk.h> + +G_BEGIN_DECLS + +#define GTK_TYPE_VUMETER (gtk_vumeter_get_type ()) +#define GTK_VUMETER(obj) (GTK_CHECK_CAST ((obj), GTK_TYPE_VUMETER, GtkVUMeter)) +#define GTK_VUMETER_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_VUMETER GtkVUMeterClass)) +#define GTK_IS_VUMETER(obj) (GTK_CHECK_TYPE ((obj), GTK_TYPE_VUMETER)) +#define GTK_IS_VUMETER_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_VUMETER)) +#define GTK_VUMETER_GET_CLASS(obj) (GTK_CHECK_GET_CLASS ((obj), GTK_TYPE_VUMETER, GtkVUMeterClass)) + +typedef struct _GtkVUMeter GtkVUMeter; +typedef struct _GtkVUMeterClass GtkVUMeterClass; + +struct _GtkVUMeter { + GtkWidget widget; + + gint f_gradient_key_count; + GdkColor *f_gradient_keys; + gint b_gradient_key_count; + GdkColor *b_gradient_keys; + + GdkColormap *colormap; + gint colors; + + GdkGC **f_gc; + GdkGC **b_gc; + GdkColor *f_colors; + GdkColor *b_colors; + + gboolean vertical; + gint level; + gint min; + gint max; + + gint peaks_falloff; + gint peak_level; + + gint scale; +}; + +struct _GtkVUMeterClass { + GtkWidgetClass parent_class; +}; + +enum { + GTK_VUMETER_PEAKS_FALLOFF_SLOW, + GTK_VUMETER_PEAKS_FALLOFF_MEDIUM, + GTK_VUMETER_PEAKS_FALLOFF_FAST, + GTK_VUMETER_PEAKS_FALLOFF_LAST +}; + +enum { + GTK_VUMETER_SCALE_LINEAR, + GTK_VUMETER_SCALE_LOG, + GTK_VUMETER_SCALE_LAST +}; + +GtkType gtk_vumeter_get_type (void) G_GNUC_CONST; +GtkWidget *gtk_vumeter_new (gboolean vertical); +void gtk_vumeter_set_min_max (GtkVUMeter *vumeter, gint min, gint max); +void gtk_vumeter_set_level (GtkVUMeter *vumeter, gint level); +void gtk_vumeter_set_peaks_falloff (GtkVUMeter *vumeter, gint peaks_falloff); +void gtk_vumeter_set_scale (GtkVUMeter *vumeter, gint scale); +void gtk_vumeter_set_gradient (GtkVUMeter *vumeter, gint f_gradient_key_count, GdkColor *f_gradient_keys, gint b_gradient_key_count, GdkColor *b_gradient_keys); + +G_END_DECLS + +#endif /* __GTKVUMETER_H__ */ diff --git a/tests/test4/test4.c b/tests/test4/test4.c index e5bca86..787f7c2 100644 --- a/tests/test4/test4.c +++ b/tests/test4/test4.c @@ -1,22 +1,56 @@ #include <gtk/gtk.h> -//#include <locale.h> +#include <stdlib.h> +#include <pthread.h> +#include "gtkvumeter.h" +#include "win_main.h" +#include "compute.h" + +void audio_thread(void *args) { + gint min=-32767, max=32767; + gint *audio_vumeter_val=((gint*)args)+0; + gint *light_h=((gint*)args)+1; + gint *light_s=((gint*)args)+2; + gint *light_v=((gint*)args)+3; + gint *light_r=((gint*)args)+4; + gint *light_g=((gint*)args)+5; + gint *light_b=((gint*)args)+6; + + while(1) { + // Dummy code for audio capture + *audio_vumeter_val=min+rand()/(float)RAND_MAX*(max-min); + usleep(10); + + // Transfert Function + audio2hsv_1(*audio_vumeter_val,*light_h,*light_s,*light_v); + + // Conversion + hsv2rgb(*light_h,*light_s,*light_v,light_r,light_g,light_b); + + // Send to DMX + //TODO + } +} int main (int argc, char **argv) { GtkWidget *mainwin; + gint vals_for_vumeters[7]={0,0,0,0,0,0}; //sound,h,s,v,r,g,b - /* Initialize i18n support */ - //setlocale(LC_ALL, ""); //(implicite) - /* Initialize the widget set */ + pthread_t audio_analyzer; + + g_thread_init(NULL); + gdk_threads_init(); + gdk_threads_enter(); gtk_init (&argc, &argv); - /* Create the main window */ - mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL); - /* Set up our GUI elements */ - //... - /* Show the application window */ + + mainwin=win_main_build(); gtk_widget_show_all (mainwin); - /* Enter the main event loop, and wait for user interaction */ + + pthread_create (&audio_analyzer, (void *)NULL, (void *)audio_thread, (void *)vals_for_vumeters); + g_timeout_add (33, win_main_update_vumeters, (gpointer)vals_for_vumeters); + gtk_main (); - /* The user lost interest */ + gdk_threads_leave(); + return 0; } diff --git a/tests/test4/win_main.c b/tests/test4/win_main.c new file mode 100644 index 0000000..c0249cf --- /dev/null +++ b/tests/test4/win_main.c @@ -0,0 +1,65 @@ +#include "win_main.h" +#include "gtkvumeter.h" + +GtkWidget *vumeter_sound, *vumeter_r, *vumeter_g, *vumeter_b, *vumeter_h, *vumeter_s, *vumeter_v; + +GtkWidget *win_main_build() { + GtkWidget *win, *hbox1; + + GdkColor f_gradient_red[2] = {{0,65535,0,0},{0,0,0,0}}; + GdkColor b_gradient_red[2] = {{0,49151,0,0},{0,0,0,0}}; + GdkColor f_gradient_green[2] = {{0,0,65535,0},{0,0,0,0}}; + GdkColor b_gradient_green[2] = {{0,0,49151,0},{0,0,0,0}}; + GdkColor f_gradient_blue[2] = {{0,0,0,65535},{0,0,0,0}}; + GdkColor b_gradient_blue[2] = {{0,0,0,49151},{0,0,0,0}}; + GdkColor f_gradient_hue[7] = {{0,65535,0,0},{0,65535,0,65535},{0,0,0,65535},{0,0,65535,65535},{0,0,65535,0},{0,65535,65535,0},{0,65535,0,0}}; + GdkColor b_gradient_hue[7] = {{0,49151,0,0},{0,49151,0,49151},{0,0,0,49151},{0,0,49151,49151},{0,0,49151,0},{0,49151,49151,0},{0,49151,0,0}}; + + win = gtk_window_new (GTK_WINDOW_TOPLEVEL); + + hbox1 = gtk_hbox_new(FALSE, 0); + gtk_container_add(GTK_CONTAINER(win), hbox1); + gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5); + +//TODO : gtk_vumeter_set_min_max (GTK_VUMETER(vumeter), min, max); + vumeter_sound = gtk_vumeter_new (TRUE); + gtk_box_pack_start(GTK_BOX(hbox1), vumeter_sound, FALSE, FALSE, 0); + + vumeter_h = gtk_vumeter_new (TRUE); + gtk_vumeter_set_gradient(GTK_VUMETER(vumeter_h), 7, f_gradient_hue, 7, b_gradient_hue); + gtk_box_pack_start(GTK_BOX(hbox1), vumeter_h, FALSE, FALSE, 0); + + vumeter_s = gtk_vumeter_new (TRUE); + gtk_vumeter_set_gradient(GTK_VUMETER(vumeter_s), 2, f_gradient_red, 2, b_gradient_red); + gtk_box_pack_start(GTK_BOX(hbox1), vumeter_s, FALSE, FALSE, 0); + + vumeter_v = gtk_vumeter_new (TRUE); + gtk_vumeter_set_gradient(GTK_VUMETER(vumeter_v), 2, f_gradient_red, 2, b_gradient_red); + gtk_box_pack_start(GTK_BOX(hbox1), vumeter_v, FALSE, FALSE, 0); + + + vumeter_r = gtk_vumeter_new (TRUE); + gtk_vumeter_set_gradient(GTK_VUMETER(vumeter_r), 2, f_gradient_red, 2, b_gradient_red); + gtk_box_pack_start(GTK_BOX(hbox1), vumeter_r, FALSE, FALSE, 0); + + vumeter_g = gtk_vumeter_new (TRUE); + gtk_vumeter_set_gradient(GTK_VUMETER(vumeter_g), 2, f_gradient_green, 2, b_gradient_green); + gtk_box_pack_start(GTK_BOX(hbox1), vumeter_g, FALSE, FALSE, 0); + + vumeter_b = gtk_vumeter_new (TRUE); + gtk_vumeter_set_gradient(GTK_VUMETER(vumeter_b), 2, f_gradient_blue, 2, b_gradient_blue); + gtk_box_pack_start(GTK_BOX(hbox1), vumeter_b, FALSE, FALSE, 0); + + return win; +} + +void win_main_update_vumeters(void *vals) { + gtk_vumeter_set_level(GTK_VUMETER(vumeter_sound), ((gint*)vals)[0]); + gtk_vumeter_set_level(GTK_VUMETER(vumeter_h), ((gint*)vals)[1]); + gtk_vumeter_set_level(GTK_VUMETER(vumeter_s), ((gint*)vals)[2]); + gtk_vumeter_set_level(GTK_VUMETER(vumeter_v), ((gint*)vals)[3]); + gtk_vumeter_set_level(GTK_VUMETER(vumeter_r), ((gint*)vals)[4]); + gtk_vumeter_set_level(GTK_VUMETER(vumeter_g), ((gint*)vals)[5]); + gtk_vumeter_set_level(GTK_VUMETER(vumeter_b), ((gint*)vals)[6]); +} + diff --git a/tests/test4/win_main.h b/tests/test4/win_main.h new file mode 100644 index 0000000..4f0ee1d --- /dev/null +++ b/tests/test4/win_main.h @@ -0,0 +1,9 @@ +#ifndef WIN_MAIN_H +#define WIN_MAIN_H +#include <gtk/gtk.h> + +GtkWidget *win_main_build(); +void win_main_update_vumeters(void *vals); + +#endif + |