/[paths]/src/main.c
ViewVC logotype

Contents of /src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (show annotations)
Sun Aug 26 03:23:50 2012 UTC (7 years, 1 month ago) by ben
File MIME type: text/plain
File size: 6164 byte(s)
highlight nodes while playing
1 /* Copyright (c) 2012, Benoit Rouits <brouits@free.fr>
2 * Some rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * ALTHOUGH THIS SOFTWARE IS MADE OF WIN AND SCIENCE, IT IS PROVIDED BY THE
14 * AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
16 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
17 * THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <jack/jack.h>
32 #include <jack/midiport.h>
33
34 #include <glib.h>
35 #include <gtk/gtk.h>
36
37 #include <glib/gi18n.h>
38
39 #include <config.h>
40 #include "main.h"
41 #include "jack.h"
42 #include "graph.h"
43 #include "paths.h"
44 #include "egg_view.h"
45
46 int main (int argc, char **argv)
47 {
48 struct app app;
49 GtkWidget *window;
50 GtkWidget *scrolled;
51 struct graph *graph;
52 struct instrument instrument1;
53 struct instrument instrument2;
54 struct voice voice1;
55 struct voice voice2;
56 struct voice voice3;
57 struct voice voice4;
58 struct jack jack;
59 int status;
60
61 int i, err;
62
63 /* create random graph */
64 graph = graph_new(20);
65 graph_randomize2(graph, 3);
66
67 /* create 2 random routes à la Nancarrow */
68 memset(&voice1, 0, sizeof(voice1));
69 voice1.route = malloc(graph->count * sizeof(*voice1.route));
70 voice1.count = graph->count;
71 for (i = 0; i < voice1.count; i++)
72 voice1.route[i] = &(graph->nodes[i].event);
73
74 memset(&voice2, 0, sizeof(voice2));
75 voice2.route = malloc(graph->count * sizeof(*voice2.route));
76 voice2.count = graph->count;
77 for (i = 0; i < voice2.count; i++)
78 voice2.route[i] = &(graph->nodes[graph->count - i - 1].event);
79
80 /* create 2 other lol routes */
81 memset(&voice3, 0, sizeof(voice3));
82 voice3.route = malloc(graph->count * sizeof(*voice3.route));
83 voice3.count = graph->count;
84 for (i = 0; i < voice3.count; i++)
85 voice3.route[i] = &(graph->nodes[rand()%graph->count].event);
86
87 memset(&voice4, 0, sizeof(voice4));
88 voice4.route = malloc(graph->count * sizeof(*voice4.route));
89 voice4.count = graph->count;
90 for (i = 0; i < voice4.count; i++)
91 voice4.route[i] = &(graph->nodes[rand()%graph->count].event);
92
93 /* initialize instruments */
94 memset(&instrument1, 0, sizeof(instrument1));
95 instrument1.graph = graph;
96
97 instrument1.voices[0] = voice1;
98 instrument1.count = 1;
99
100 instrument1.voices[1] = voice2;
101 instrument1.count = 2;
102
103 memset(&instrument2, 0, sizeof(instrument2));
104 instrument2.graph = graph;
105
106 instrument2.voices[0] = voice3;
107 instrument2.voices[1] = voice4;
108 instrument2.count = 2;
109
110 /* initialize application context */
111 memset(&app, 0, sizeof(app));
112 app.graph = graph;
113 app.jack = &jack;
114 app.count = 0;
115
116 app.instruments[0] = instrument1;
117 app.count = 1;
118
119 app.instruments[1] = instrument2;
120 app.count = 2;
121
122 /* mark it (...) */
123 app.instruments[0].chan = 0;
124 app.instruments[0].prog = 42;
125 app.instruments[0].voices[0].route[0]->pitch = 42;
126 app.instruments[0].voices[0].route[0]->duration = 4200000;
127 app.instruments[0].voices[1].route[0]->pitch = 43;
128 app.instruments[0].voices[1].route[0]->duration = 4300000;
129
130 app.instruments[1].chan = 1;
131 app.instruments[1].prog = 0;
132 app.instruments[1].voices[0].route[0]->pitch = 44;
133 app.instruments[1].voices[0].route[0]->duration = 4400000;
134 app.instruments[1].voices[1].route[0]->pitch = 45;
135 app.instruments[1].voices[1].route[0]->duration = 4500000;
136
137 /* init jack */
138 jack.client = jack_client_open(PACKAGE_NAME, JackNullOption, NULL);
139 if (jack.client == NULL) {
140 error("could not connect to jackd");
141 exit(EXIT_FAILURE);
142 }
143
144 err = jack_set_process_callback(jack.client, jack_process_callback, &app);
145
146 if (err) {
147 error("could not set jack process callback");
148 exit(EXIT_FAILURE);
149 }
150
151 err = jack_set_sync_callback(jack.client, jack_sync_callback, &app);
152 if (err) {
153 error("could not set jack sync callback");
154 exit(EXIT_FAILURE);
155 }
156
157 jack_set_error_function(jack_error_function);
158 jack_set_info_function(jack_info_function);
159
160 jack_on_shutdown(jack.client, jack_shutdown, &app);
161
162 /* allocate one jack port per instrument (midi outs) */
163 for (i = 0; i < app.count; i++) {
164 char name[32];
165 snprintf(name, 32, "out %d", i);
166 app.instruments[i].out = jack_port_register(jack.client, name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
167 if (app.instruments[i].out == NULL) {
168 error("could not register a jack midi out port");
169 exit(EXIT_FAILURE);
170 }
171 }
172
173 /* activate jack */
174 if (jack_activate(jack.client)) {
175 error("could not activate jack client");
176 exit(EXIT_FAILURE);
177 }
178
179 /* graphical window */
180 gtk_init(&argc, &argv);
181 app.ui = gtk_builder_new();
182 gtk_builder_add_from_file(app.ui, "paths.ui", NULL);
183 gtk_builder_connect_signals(app.ui, &app);
184 app.view = EGG_VIEW(egg_view_new());
185
186 egg_view_populate(app.view, graph);
187
188 scrolled = GTK_WIDGET(gtk_builder_get_object(app.ui, "scrolledwindow"));
189 gtk_container_add(GTK_CONTAINER(scrolled), GTK_WIDGET(app.view));
190
191 window = GTK_WIDGET(gtk_builder_get_object(app.ui, "window"));
192 gtk_widget_show_all (window);
193
194 /* user event loop */
195 gtk_main();
196
197 /* close jack */
198 jack_client_close (jack.client);
199
200 /* exit */
201 free(voice1.route);
202 free(voice2.route);
203 free(voice3.route);
204 free(voice4.route);
205 graph_destroy(graph);
206
207
208 return status;
209 }

  ViewVC Help
Powered by ViewVC 1.1.26