/[pidim]/src/pidim_ctrl_swap.c
ViewVC logotype

Contents of /src/pidim_ctrl_swap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (show annotations)
Fri Aug 27 00:11:30 2010 UTC (10 years, 9 months ago) by ben
File MIME type: text/plain
File size: 5733 byte(s)
factorised cli code
1 /* pidim_swap, a small Gtk+/ALSA MIDI control/value swapper
2 * Copyright (C) 2010 Benoit Rouits
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <libintl.h>
24 #include <glib.h>
25 #include <gtk/gtk.h>
26
27 #include <stdlib.h>
28 #include <alsa/asoundlib.h>
29 #include "pidim_common.h"
30
31 /* alsa-related type */
32 /* this application has one input and one output port */
33 typedef struct {
34 snd_seq_t* handler; /* sequencer handler */
35 int inport_id; /* id of input port 0 */
36 int outport_id; /* id of output port 0 */
37 } alsa_t;
38
39 /* globals */
40 GtkBuilder* ui = NULL;
41 gboolean Swap = FALSE;
42 int Param = 0;
43 int Channel = 0;
44 char Instance[64];
45
46 /* Gtk stuff */
47 void ui_new (void)
48 {
49 gchar* ui_path = NULL;
50 GtkWidget *param_spinbutton, *channel_spinbutton;
51
52 if (g_file_test("./pidim_ctrl_swap.ui", G_FILE_TEST_EXISTS))
53 ui_path = "./pidim_ctrl_swap.ui";
54 else
55 ui_path = PACKAGE_DATA_DIR"/pidim/ui/pidim_ctrl_swap.ui";
56
57 ui = gtk_builder_new();
58 gtk_builder_add_from_file(ui, ui_path, NULL);
59
60 param_spinbutton = GTK_WIDGET(gtk_builder_get_object(ui, "param_spinbutton"));
61 gtk_spin_button_set_range(GTK_SPIN_BUTTON(param_spinbutton), 0, CTRL_MAX);
62 gtk_spin_button_set_increments(GTK_SPIN_BUTTON(param_spinbutton), 1, 0);
63
64 channel_spinbutton = GTK_WIDGET(gtk_builder_get_object(ui, "channel_spinbutton"));
65 gtk_spin_button_set_range(GTK_SPIN_BUTTON(channel_spinbutton), 0, CHAN_MAX);
66 gtk_spin_button_set_increments(GTK_SPIN_BUTTON(channel_spinbutton), 1, 0);
67
68 gtk_builder_connect_signals(ui, NULL);
69 gtk_window_set_default_icon_from_file(PACKAGE_DATA_DIR"/pidim/ui/pidim.png", NULL);
70 }
71
72 void ui_show()
73 {
74 GtkWidget* main_window = GTK_WIDGET(gtk_builder_get_object(ui, "main_window"));
75 gtk_window_set_title(GTK_WINDOW(main_window), Instance);
76 gtk_widget_show_all(main_window);
77
78 }
79
80 gboolean on_main_window_delete_event(GtkWidget* main_window, GdkEvent* null1, gpointer null2) {
81 null1 = NULL;
82 null2 = NULL;
83 gtk_main_quit();
84 return TRUE;
85 }
86
87 /* callback called when the user interacts on param spin button */
88 void on_param_spinbutton_value_changed(GtkSpinButton* spin_button, gpointer* null)
89 {
90 Param = (unsigned char) gtk_spin_button_get_value_as_int(spin_button);
91 null = NULL;
92 }
93
94 /* callback called when the user interacts on channel spin button */
95 void on_channel_spinbutton_value_changed(GtkSpinButton* spin_button, gpointer* null) {
96 Channel = (int) gtk_spin_button_get_value_as_int(spin_button);
97 null = NULL;
98 }
99
100 /* callback called when the user interacts on swap check button */
101 void on_swap_checkbutton_toggled(GtkCheckButton* check_button, gpointer* null) {
102 Swap = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_button));
103 null = NULL;
104 }
105
106 /* swapping operation on midi events */
107 void do_swap (snd_seq_event_t* ev)
108 {
109 int swap;
110
111 if ((ev->type == SND_SEQ_EVENT_NOTEON) && (ev->data.note.velocity == 0))
112 ev->type = SND_SEQ_EVENT_NOTEOFF;
113
114 if (ev->type == SND_SEQ_EVENT_CONTROLLER) {
115 if (!Channel || ev->data.control.channel == Channel) {
116 if (ev->data.control.param == Param) {
117 if (Swap == TRUE) {
118 swap = ev->data.control.value;
119 ev->data.control.value = ev->data.control.param;
120 ev->data.control.param = swap;
121 }
122 }
123 }
124 }
125 return;
126 }
127
128 /* MIDI input callback */
129 gboolean handle_midi_input (GIOChannel* midi_input, GIOCondition cond, gpointer user_data)
130 {
131 snd_seq_event_t* ev;
132 alsa_t* alsa = (alsa_t*) user_data;
133 do {
134 snd_seq_event_input(alsa->handler, &ev);
135 do_swap(ev);
136 snd_seq_ev_set_source(ev, alsa->outport_id);
137 snd_seq_ev_set_subs(ev);
138 snd_seq_event_output_direct(alsa->handler, ev);
139 } while (snd_seq_event_input_pending(alsa->handler, 0) > 0);
140 cond = 0;
141 midi_input = NULL;
142 return TRUE;
143 }
144
145 /* starting point */
146 int main (int argc, char* argv[])
147 {
148 char* argv1;
149 GIOChannel* midi_input;
150 int alsa_input_fd;
151 alsa_t alsa;
152
153 /* initialize language translation */
154 #ifdef ENABLE_NLS
155 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
156 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
157 textdomain (GETTEXT_PACKAGE);
158 #endif
159 /* initialize gtk+ */
160 gtk_set_locale();
161 gtk_init(&argc, &argv);
162
163 memset(Instance, '\0', 1);
164 strncat(Instance, "PIDIM Control Swap ", 64);
165
166 /* read extra parameters */
167 argv1 = cli_parse(argc, argv, \
168 "A MIDI ControChange param/value swapper.", \
169 "Enter the CC number to transform a single Controller with its 127 values into 127 controllers with one value (the one of the original CC.)");
170 if (argv1) {
171 strncat(Instance, argv1, 48);
172 }
173
174 /* create window */
175 ui_new();
176 ui_show();
177
178 /* initialize alsa stuff */
179 alsa.handler = alsa_open_client(Instance);
180 if (!alsa.handler) {
181 exit(EXIT_FAILURE);
182 }
183 alsa.inport_id = alsa_open_input_port(alsa.handler, "IN");
184 alsa.outport_id = alsa_open_output_port(alsa.handler, "OUT");
185 alsa_input_fd = alsa_get_fd(alsa.handler);
186 midi_input = g_io_channel_unix_new(alsa_input_fd);
187 g_io_add_watch(midi_input, G_IO_IN, handle_midi_input, &alsa);
188
189 gtk_main();
190 exit(EXIT_SUCCESS);
191 }
192

  ViewVC Help
Powered by ViewVC 1.1.26