-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqprojectM-jack.cpp
349 lines (266 loc) · 8.49 KB
/
qprojectM-jack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* projectM -- Milkdrop-esque visualisation SDK
* Copyright (C)2003-2004 projectM Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* See 'LICENSE.txt' included within this release
*
*/
#include <qprojectm_mainwindow.hpp>
#include <libprojectM/projectM.h>
#include <jack/jack.h>
#include <QApplication>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstdio>
#include <sys/stat.h> // for mkdir
#define QPROJECTM_JACK_CONFIG_FILE "/config.inp"
QString read_config();
jack_port_t *input_port;
jack_client_t *client;
jack_default_audio_sample_t out[1024*1000];
#ifdef DEBUG
FILE *debugFile = NULL;
#endif
volatile enum ClientState {
Init,
Run,
Exit
} client_state = Init;
projectm* globalPM = nullptr;
int dumpFrame = 0;
int frameNumber = 0;
int texsize=512;
int gx=32,gy=24;
int wvw=512,wvh=512;
int fvw=1024,fvh=768;
int fps=30, fullscreen=0;
class ProjectMApplication : public QApplication {
public:
ProjectMApplication(int& argc, char ** argv) :
QApplication(argc, argv) { }
virtual ~ProjectMApplication() { }
// catch exceptions which are thrown in slots
virtual bool notify(QObject * receiver, QEvent * event) {
try {
return QApplication::notify(receiver, event);
} catch (std::exception& e) {
qCritical() << "Exception thrown:" << e.what();
}
return false;
}
};
QString read_config()
{
char num[512];
FILE *f_in;
FILE *f_out;
char * home;
char projectM_home[1024];
char projectM_config[1024];
int len;
strcpy(projectM_config, PROJECTM_PREFIX);
strcpy(projectM_config + (len = strlen(PROJECTM_PREFIX)), "/");
strcpy(projectM_config+(len += strlen("/")), RESOURCE_PREFIX);
strcpy(projectM_config+(len += strlen(RESOURCE_PREFIX)), QPROJECTM_JACK_CONFIG_FILE);
projectM_config[len += strlen(QPROJECTM_JACK_CONFIG_FILE)]='\0';
printf("dir:%s \n",projectM_config);
home=getenv("HOME");
strcpy(projectM_home, home);
strcpy(projectM_home+strlen(home), "/.projectM/config.inp");
projectM_home[strlen(home)+strlen("/.projectM/config.inp")]='\0';
if ((f_in = fopen(projectM_home, "r")) != 0)
{
printf("reading ~/.projectM/config.inp \n");
fclose(f_in);
return projectM_home;
}
else
{
printf("trying to create ~/.projectM/config.inp \n");
strcpy(projectM_home, home);
strcpy(projectM_home+strlen(home), "/.projectM");
projectM_home[strlen(home)+strlen("/.projectM")]='\0';
mkdir(projectM_home,0755);
strcpy(projectM_home, home);
strcpy(projectM_home+strlen(home), "/.projectM/config.inp");
projectM_home[strlen(home)+strlen("/.projectM/config.inp")]='\0';
if((f_out = fopen(projectM_home,"w"))!=0)
{
if ((f_in = fopen(projectM_config, "r")) != 0)
{
while(fgets(num,80,f_in)!=NULL)
{
fputs(num,f_out);
}
fclose(f_in);
fclose(f_out);
if ((f_in = fopen(projectM_home, "r")) != 0)
{
printf("created ~/.projectM/config.inp successfully\n");
fclose(f_in);
return projectM_home;
}
else{printf("This shouldn't happen, using implementation defaults\n");abort();}
}
else{printf("Cannot find projectM default config, using implementation defaults\n");abort();}
}
else
{
printf("Cannot create ~/.projectM/config.inp, using default config file\n");
if ((f_in = fopen(projectM_config, "r")) != 0)
{ printf("Successfully opened default config file\n");
fclose(f_in);
return projectM_config;}
else{ printf("Using implementation defaults, your system is really messed up, I'm surprised we even got this far\n"); abort();}
}
}
abort();
}
int
process (jack_nframes_t nframes, void *arg)
{
Q_UNUSED(arg);
jack_default_audio_sample_t *in;
in = (jack_default_audio_sample_t*)jack_port_get_buffer (input_port, nframes);
//memcpy (out, in,sizeof (jack_default_audio_sample_t) * nframes);
projectm_pcm_add_float(globalPM, in, nframes, PROJECTM_MONO);
// printf("%x %f\n",nframes,in[128]);
return 0;
}
void jack_shutdown (void *arg)
{
Q_UNUSED(arg);
exit (1);
}
int main (int argc, char **argv) {
const char **ports;
const char *client_name;
const char *server_name = NULL;
jack_options_t options = JackNullOption;
jack_status_t status;
int i;
// Start a new application
ProjectMApplication app(argc, argv);
auto config_file = read_config();
QProjectM_MainWindow * mainWindow = new QProjectM_MainWindow(config_file, 0);
mainWindow->show();
globalPM = mainWindow->GetProjectM();
//JACK INIT
//----------------------------------------------
if (argc >= 2) { /* client name specified? */
client_name = argv[1];
if (argc >= 3) { /* server name specified? */
server_name = argv[2];
// options |= JackServerName;
}
} else { /* use basename of argv[0] */
client_name = strrchr(argv[0], '/');
if (client_name == 0) {
client_name = argv[0];
} else {
client_name++;
}
}
/* open a client connection to the JACK server */
client = jack_client_open (client_name, options, &status, server_name);
if (client == NULL) {
fprintf (stderr, "jack_client_open() failed, "
"status = 0x%2.0x\n", status);
if (status & JackServerFailed) {
fprintf (stderr, "Unable to connect to JACK server\n");
}
exit (1);
}
if (status & JackServerStarted) {
fprintf (stderr, "JACK server started\n");
}
if (status & JackNameNotUnique) {
client_name = jack_get_client_name(client);
fprintf (stderr, "unique name `%s' assigned\n", client_name);
}
/* tell the JACK server to call `process()' whenever
there is work to be done.
*/
if (jack_set_process_callback (client, process, 0)) {
std::cerr << "qprojectM-jack: failed to set process callbank!. quitting..." << std::endl;
exit(EXIT_FAILURE);
}
/* tell the JACK server to call `jack_shutdown()' if
it ever shuts down, either entirely, or if it
just decides to stop calling us.
*/
jack_on_shutdown (client, jack_shutdown, 0);
/* display the current sample rate.
*/
printf ("engine sample rate: %d\n",
jack_get_sample_rate (client));
/* create two ports */
input_port = jack_port_register (client, "input",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput, 0);
if (input_port == NULL) {
fprintf(stderr, "no more JACK ports available\n");
exit (1);
}
/* Tell the JACK server that we are ready to roll. Our
* process() callback will start running now. */
// END JACK INIT ----------------------------------
//JACK BEGIN-----------------------------
if (jack_activate (client)) {
fprintf (stderr, "cannot activate client");
exit (1);
}
/* Connect the ports. You can't do this before the client is
* activated, because we can't make connections to clients
* that aren't running. Note the confusing (but necessary)
* orientation of the driver backend ports: playback ports are
* "input" to the backend, and capture ports are "output" from
* it.
*/
ports = jack_get_ports (client, NULL, NULL, JackPortIsOutput);
if (ports == NULL) {
fprintf(stderr, "no physical capture ports\n");
exit (1);
}
i=0;
while (ports[i]!=NULL)
{
printf("Connecting to Jack port %s\n",ports[i]);
if (jack_connect (client, ports[i], jack_port_name (input_port))) {
fprintf (stderr, "cannot connect input ports\n");
}
i++;
}
free (ports);
#ifdef HTTP_REMOTE_CONTROL
uint port=qgetenv("HTTP_PORT").toUInt();
QxtHttpServerConnector connector;
QxtHttpSessionManager session;
printf("session.setPort(%i\n", port); fflush(stdout);
session.setPort(port);
session.setConnector(&connector);
HTTPRemoteControl s1(&session, mainWindow->qprojectM());
session.setStaticContentService ( &s1);
printf("session.start()\n"); fflush(stdout);
if(port>0) { // I think it didn't work when the conditional was further up
session.start();
}
#endif
//----------------------------------END
//return 1;
return app.exec();
}