File: slconsole.cpp

package info (click to toggle)
sooperlooper 1.7.8~dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,912 kB
  • sloc: cpp: 31,035; sh: 5,205; makefile: 204
file content (519 lines) | stat: -rw-r--r-- 11,156 bytes parent folder | download
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 *  Copyright (C) 2004 Steve Harris, Uwe Koloska
 *
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  $Id$
 */

#include <cstdio>
#include <cstdlib>

#include <iostream>

#include <unistd.h>
#include <pthread.h>
#include <getopt.h>
#include <time.h>

#include <curses.h>

#include <map>
#include <string>

#include <lo/lo.h>

using namespace std;


static lo_address addr;

static string our_url;
static lo_server osc_server = 0;
static pthread_t osc_thread = 0;

map<string, float> params_val_map;
map<int, string> state_map;
volatile bool updated = false;
volatile bool   _acked = false;
volatile bool do_shutdown = false;

const char *optstring = "H:P:h";
struct option long_options[] = {
	{ "help", 0, 0, 'h' },
	{ "connect-host", 1, 0, 'H' },
	{ "connect-port", 1, 0, 'P' },
	{ 0, 0, 0, 0 }
};


char * _host = 0;
int    _port=0;
int    _show_usage=0;
char ** _engine_argv = 0;

#define DEFAULT_OSC_PORT 9951

static void usage(char *argv0)
{
	fprintf(stderr, "Usage: %s [options...]\n", argv0);
	fprintf(stderr, "Options:\n");
	fprintf(stderr, "  -H host,  --connect-host=host    connect to sooperlooper engine on given host (default is localhost)\n");
	fprintf(stderr, "  -P <num>, --connect-port=<num>   connect to sooperlooper engine on given port (default is %d)\n", DEFAULT_OSC_PORT);
	fprintf(stderr, "  -h , --help                  this usage output\n");
	fprintf(stderr, "\nBy default, the gui will try to connect to an engine running\n"); 
	fprintf(stderr, "at the given (or default) host and port.\n"); 

}

static void parse_options (int argc, char **argv)
{
	int longopt_index = 0;
	int c;
	bool stop_proc = false;
	
	while (!stop_proc && (c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) >= 0) {
		if (c >= 255) break;
		
		switch (c) {
		case 1:
			/* getopt signals end of '-' options */
			stop_proc = true;
			break;
		case 'h':
			_show_usage++;
			break;
		case 'H':
			_host = optarg;
			break;
		case 'P':
			sscanf(optarg, "%d", &_port);
			break;
		default:
			fprintf (stderr, "argument error: %c\n", c);
			_show_usage++;
			break;
		}

		if (_show_usage > 0) {
			break;
		}
		
	}

	_engine_argv = argv + optind;
}


static int do_control_change(char cmd)
{
	

	float val = 0.0;
	int ret = 0;
	char * control = NULL;
	int row = 17;
	char fname[200];
	
	noraw();
	echo();
	timeout(-1);
	curs_set(2);
	
	switch (cmd)
	{
	case '1':
		control = "dry";
		mvprintw (row, 0, "Enter Dry Level (-90..0) dB : ");
		clrtoeol();
		refresh();
		ret = scanw ("%f", &val);
		break;
	case '2':
		control = "wet";
		mvprintw (row, 0, "Enter Wet Level (-90..0) dB : ");
		clrtoeol();
		refresh();
		ret = scanw ("%f", &val);
		break;
	case '3':
		control = "feedback";
		mvprintw (row, 0, "Enter Feedback (0..1) : ");
		clrtoeol();
		refresh();
		ret = scanw ("%f", &val);
		break;
	case '4':
		control = "rate";
		mvprintw (row, 0, "Enter Rate (-4..4) : ");
		clrtoeol();
		refresh();
		ret = scanw ("%f", &val);
		break;
	case '5':
		control = "quantize";
		mvprintw (row, 0, "Enter Quantize flag (0 or 1) : ");
		clrtoeol();
		refresh();
		ret = scanw ("%f", &val);
		break;
	case '6':
		control = "round";
		mvprintw (row, 0, "Enter Round flag (0 or 1) : ");
		clrtoeol();
		refresh();
		ret = scanw ("%f", &val);
		break;
	case '7':
		control = "rec_thresh";
		mvprintw (row, 0, "Enter Rec Thresh (0..1) : ");
		clrtoeol();
		refresh();
		ret = scanw ("%f", &val);
		break;

	case 'l':
		mvprintw (row, 0, "Filename to load : ");
		clrtoeol();
		refresh();
		ret = scanw ("%s", fname);
		
		break;
	case 'a':
		mvprintw (row, 0, "Filename to save loop to : ");
		clrtoeol();
		refresh();
		ret = scanw ("%s", fname);

		break;
		
	default:
		break;

	}

	if (ret > 0) {
		if (cmd == 'l') {
			lo_send(addr, "/sl/-1/load_loop", "sss", fname, our_url.c_str(), "/blah");
		}
		else if (cmd == 'a') {
			lo_send(addr, "/sl/-1/save_loop", "sssss", fname, "float", "little", our_url.c_str(), "/blah");
		}
		else {
			lo_send(addr, "/sl/-1/set", "sf", control, val);
		}
	}
	
	mvprintw (row, 0, "");
	clrtoeol ();
	refresh();
	
	raw();
	noecho();
	curs_set(0);

	return 0;
}

static int post_event(char cmd)
{
	int ret = 1;
	
	switch (cmd)
	{
	case 'q':
		lo_send (addr, "/quit", NULL);
		ret = -1;
		break;
	case 'b':
		ret = -1;
		break;
	case 'r':
		lo_send(addr, "/sl/-1/down", "s", "record");
		break;
	case 'o':
		lo_send(addr, "/sl/-1/down", "s", "overdub");
		break;
	case 'x':
		lo_send(addr, "/sl/-1/down", "s", "multiply");
		break;
	case 'i':
		lo_send(addr, "/sl/-1/down", "s", "insert");
		break;
	case 'p':
		lo_send(addr, "/sl/-1/down", "s", "replace");
		break;
	case 'm':
		lo_send(addr, "/sl/-1/down", "s", "mute");
		break;
	case 'u':
		lo_send(addr, "/sl/-1/down", "s", "undo");
		break;
	case 'd':
		lo_send(addr, "/sl/-1/down", "s", "redo");
		break;
	case 'v':
		lo_send(addr, "/sl/-1/down", "s", "reverse");
		break;
		
	default:
		ret = 0;
		break;
	}

	return ret;
}

static int ctrl_handler(const char *path, const char *types, lo_arg **argv, int argc,
			 lo_message_ *data, void *user_data)
{
	// 1st arg is instance, 2nd ctrl string, 3nd is float value
	//int index = argv[0]->i;
	string ctrl(&argv[1]->s);
	float val  = argv[2]->f;

	params_val_map[ctrl] = val;

	updated = true;

	return 0;
}

static int pingack_handler(const char *path, const char *types, lo_arg **argv, int argc,
			 lo_message_ *data, void *user_data)
{
	// pingack expects: s:engine_url s:version i:loopcount
	// 1st arg is instance, 2nd ctrl string, 3nd is float value
	//int index = argv[0]->i;
	//string eurl(&argv[0]->s);
	//string vers (&argv[1]->s);
	//int loops = argv[2]->i;
	
	_acked = true;
	return 0;
}


static void setup_param_map()
{
	params_val_map["state"] = 0.0f;
	params_val_map["loop_len"] = 0.0f;
	params_val_map["loop_pos"] = 0.0f;
	params_val_map["cycle_len"] = 0.0f;
	params_val_map["free_time"] = 0.0f;
	params_val_map["total_time"] = 0.0f;


	state_map[0] = "Off";
	state_map[1] = "Threshold Start";
	state_map[2] = "RECORDING";
	state_map[3] = "Threshold Stop";
	state_map[4] = "PLAYING";
	state_map[5] = "OVERDUBBING";
	state_map[6] = "MULTIPLYING";
	state_map[7] = "INSERTING";
	state_map[8] = "REPLACING";
	state_map[9] = "TAP DELAY";
	state_map[10] = "MUTED";
	state_map[11] = "SCRATCHING";
	state_map[12] = "ONE SHOT";
	
}

static void request_values()
{
	lo_send(addr, "/sl/0/get", "sss", "state", our_url.c_str(), "/ctrl");
	lo_send(addr, "/sl/0/get", "sss", "loop_pos", our_url.c_str(), "/ctrl");
	lo_send(addr, "/sl/0/get", "sss", "loop_len", our_url.c_str(), "/ctrl");
	lo_send(addr, "/sl/0/get", "sss", "cycle_len", our_url.c_str(), "/ctrl");

}

static void update_values()
{
	if (updated) {
		updated = false;

		mvprintw(13, 0, "State: %10s\n", state_map[(int)params_val_map["state"]].c_str());

		printw ( "Pos: %7.1f   CycleLen: %7.1f   LoopLen: %7.1f\n",
			 params_val_map["loop_pos"], params_val_map["cycle_len"], params_val_map["loop_len"]);
		
	}
}

static void * osc_receiver(void * arg)
{
	while (!do_shutdown)
	{
		lo_server_recv (osc_server);
	}

	return 0;
}

static void cleanup()
{
	do_shutdown = true;
	
	// send an event to self
	lo_address addr = lo_address_new_from_url (our_url.c_str());
	lo_send(addr, "/ping", "");
	lo_address_free (addr);

	pthread_join (osc_thread, NULL);
	lo_server_free (osc_server);
	
	endwin();
}

int main(int argc, char *argv[])
{
    int done = 0;
    char ch;
    int ret;
    char tmpstr[30];

    _engine_argv = &argv[1];
    _port = DEFAULT_OSC_PORT;
    
    parse_options(argc, argv);

    if (_show_usage > 0) {
	    usage(argv[0]);
	    exit(1);
    }
    
    /* an address to send messages to. sometimes it is better to let the server
     * pick a port number for you by passing NULL as the last argument */
    snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
    addr = lo_address_new(_host, tmpstr);

      /* send a message to /foo/bar with two float arguments, report any
       * errors */
/*       if (lo_send(t, "/sl/0/up", "ff", 0.12345678f, 23.0f) == -1) { */
/* 	printf("OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t)); */
/*       } */


    if (_engine_argv[0]) {
	    post_event (_engine_argv[0][0]);
	    exit (0);
    }

    
    setup_param_map();
    
    /* start a new server on a free port to recieve param callbacks */
    osc_server = lo_server_new(NULL, NULL);

    our_url = lo_server_get_url (osc_server);
    
    /* add handler for control param callbacks, first arg ctrl string, 2nd arg value */
    lo_server_add_method(osc_server, "/ctrl", "isf", ctrl_handler, NULL);

    // pingack expects: s:engine_url s:version i:loopcount
    lo_server_add_method(osc_server, "/pingack", "ssi", pingack_handler, NULL);
    
    // start up our thread
    pthread_create (&osc_thread, NULL, osc_receiver, NULL);

    // send ping
    lo_send(addr, "/ping", "ss", our_url.c_str(), "/pingack");

    // first make sure we are connected
    
    struct timespec ts = { 0, 100000000 };
    
    for (int i = 0; !_acked && i < 10; ++i) {
	    nanosleep(&ts, NULL);
    }

    if (!_acked) {
	    fprintf(stderr, "Error, no sooperlooper engine found\n");
	    exit(2);
    }
    

    
    /* now do some basic curses stuff */

    initscr();

    atexit(cleanup);
    
    raw();
    keypad(stdscr, TRUE);
    noecho();
    curs_set(0);
	
    printw("SooperLooper OSC test client\n");
    printw("  r - record    o - overdub  x - multiply\n");
    printw("  i - insert    p - replace  v - reverse    s - scratch\n");
    printw("  m - mute      u - undo     d - redo\n");
    printw("  l - load loop     a - save loop\n");
    printw("  q - shutdown sooperlooper and quit\n");
    printw("  b - just quit\n");
    printw("\n");
    printw("  1 - set dry level (0..1)       2 - set wet level (0..1)\n");
    printw("  3 - set feedback level (0..1)  4 - set rate (-4..4)\n");
    printw("  5 - set quantize (0 or 1)      6 - set round (0 or 1)\n");
    printw("  7 - set rec thresh (0..1) \n");
    
    refresh();
    
    /* status on line 5 */
    timeout (100);

    while (!done)
    {
	    ch = getch();
	    ret = 0;
	    
	    if (ch == 0 || ch == ERR) {
		    /* timeout */
		    request_values();
		    update_values();

		    // normal timeout
		    timeout (100);
		    
		    continue;
	    }

	    if ((ch >= '1' && ch <= '9') || ch == 'l' || ch == 'a') {

		    do_control_change (ch);
		    // normal timeout
		    timeout (100);
	    }
	    else {
		    ret = post_event (ch);

		    // faster timeout
		    timeout (50);
	    }
	    
	    if (ret < 0) {
		    done = 1;
	    }

	    request_values();
	    update_values();
    }
    
    
    endwin();
    
    return 0;
}

/* vi:set ts=8 sts=4 sw=4: */