File: emacs_c.c

package info (click to toggle)
timidity 2.14.0-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,364 kB
  • sloc: ansic: 168,735; sh: 3,730; makefile: 1,405; tcl: 1,048; perl: 285; ruby: 126
file content (604 lines) | stat: -rw-r--r-- 12,197 bytes parent folder | download | duplicates (3)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
    TiMidity++ -- MIDI to WAVE converter and player
    Copyright (C) 1999-2004 Masanao Izumo <iz@onicos.co.jp>
    Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>

    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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

    emacs_c.c
    Emacs control mode - written by Masanao Izumo <mo@goice.co.jp>
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include "timidity.h"
#include "common.h"
#include "output.h"
#include "controls.h"
#include "instrum.h"
#include "playmidi.h"
#include "miditrace.h"

/*
 * miditrace functions
 *
 * ctl_current_time
 * ctl_note
 * ctl_program
 * ctl_volume
 * ctl_expression
 * ctl_panning
 * ctl_sustain
 * ctl_pitch_bend
 */

/*
 * commands:
 * LIST CMSG TIME MVOL DRUMS FILE CURT NOTE PROG VOL EXP PAN SUS PIT RESET
 */

static void ctl_refresh(void);
static void ctl_total_time(int tt);
static void ctl_master_volume(int mv);
static void ctl_file_name(char *name);
static void ctl_current_time(int ct, int nv);
static void ctl_note(int status, int ch, int note, int vel);
static void ctl_program(int ch, int val);
static void ctl_volume(int channel, int val);
static void ctl_expression(int channel, int val);
static void ctl_panning(int channel, int val);
static void ctl_sustain(int channel, int val);
static void ctl_pitch_bend(int channel, int val);
static void ctl_reset(void);
static int ctl_open(int using_stdin, int using_stdout);
static void ctl_close(void);
static int ctl_read(int32 *valp);
static int cmsg(int type, int verbosity_level, char *fmt, ...);
static int ctl_pass_playing_list(int number_of_files, char *list_of_files[]);
static void ctl_event(CtlEvent *e);
static int read_ready(void);
static int emacs_type = 0; /* 0:emacs, 1:mule, 2:??
			      Note that this variable not used yet.
			      */
enum emacs_type_t
{
    ETYPE_OF_EMACS,
    ETYPE_OF_MULE,
    ETYPE_OF_OTHER
};

/**********************************/
/* export the interface functions */

#define ctl emacs_control_mode

ControlMode ctl=
{
    "Emacs interface (invoked from `M-x timidity')", 'e',
    "emacs",
    1, 0, 0,
    0,
    ctl_open,
    ctl_close,
    ctl_pass_playing_list,
    ctl_read,
    NULL,
    cmsg,
    ctl_event
};

static FILE *outfp;

static void quote_string_out(char *str)
{
    char *s;

    s = NULL;
    if(emacs_type == ETYPE_OF_MULE)
    {
	int len;

	len = SAFE_CONVERT_LENGTH(strlen(str));
	s = (char *)new_segment(&tmpbuffer, len);
	code_convert(str, s, len, NULL, "EUC");
	str = s;
    }

    while(*str)
    {
	if(*str == '\\' || *str == '\"')
	    putc('\\', outfp);
	putc(*str, outfp);
	str++;
    }

    if(s != NULL)
	reuse_mblock(&tmpbuffer);
}

/*ARGSUSED*/
static int ctl_open(int using_stdin, int using_stdout)
{
    if(using_stdout)
	outfp = stderr;
    else
	outfp = stdout;
    ctl.opened = 1;
    output_text_code = "NOCNV";
    fprintf(outfp, "(timidity-VERSION \"");
    quote_string_out(timidity_version);
    fprintf(outfp, "\")\n");
    ctl_refresh();
    return 0;
}

static void ctl_close(void)
{
    fflush(outfp);
    ctl.opened = 0;
}

static int ctl_read(int32 *valp)
{
    char cmd[BUFSIZ];
    int n;

    if(read_ready() <= 0)
	return RC_NONE;
    if(fgets(cmd, sizeof(cmd), stdin) == NULL)
	return RC_QUIT; /* Emacs may down */
    n = atoi(cmd + 1);
    switch(cmd[0])
    {
      case 'L':
	return RC_LOAD_FILE;
      case 'V':
	*valp = 10 * n;
	return RC_CHANGE_VOLUME;
      case 'v':
	*valp = -10 * n;
	return RC_CHANGE_VOLUME;
      case '1':
      case '2':
      case '3':
	*valp = cmd[0] - '2';
	return RC_CHANGE_REV_EFFB;
      case '4':
      case '5':
      case '6':
	*valp = cmd[0] - '5';
	return RC_CHANGE_REV_TIME;
      case 'Q':
	return RC_QUIT;
      case 'r':
	return RC_RESTART;
      case 'f':
	*valp = play_mode->rate * n;
	return RC_FORWARD;
      case 'b':
	*valp = play_mode->rate * n;
	return RC_BACK;
      case ' ':
	return RC_TOGGLE_PAUSE;
      case '+':
	*valp = n;
	return RC_KEYUP;
      case '-':
	*valp = -n;
	return RC_KEYDOWN;
      case '>':
	*valp = n;
	return RC_SPEEDUP;
      case '<':
	*valp = n;
	return RC_SPEEDDOWN;
      case 'O':
	*valp = n;
	return RC_VOICEINCR;
      case 'o':
	*valp = n;
	return RC_VOICEDECR;
      case 'd':
	*valp = n;
	return RC_TOGGLE_DRUMCHAN;
      case 'g':
	return RC_TOGGLE_SNDSPEC;
    }

    return RC_NONE;
}

static char *chomp(char *s)
{
    int len = strlen(s);

    if(len < 2)
    {
	if(len == 0)
	    return s;
	if(s[0] == '\n' || s[0] == '\r')
	    s[0] = '\0';
	return s;
    }
    if(s[len - 1] == '\n')
	s[--len] = '\0';
    if(s[len - 1] == '\r')
	s[--len] = '\0';
    return s;
}

static int ctl_pass_playing_list(int argc, char *argv[])
{
    int i;
    char cmd[BUFSIZ];

    if(argc > 0)
    {
	if(!strcmp(argv[0], "emacs"))
	{
	    emacs_type = ETYPE_OF_EMACS;
	    argc--; argv++;
	}
	else if(!strcmp(argv[0], "mule"))
	{
	    emacs_type = ETYPE_OF_MULE;
	    argc--; argv++;
	}
	else
	    emacs_type = ETYPE_OF_OTHER;
    }

    if(argc > 0 && !strcmp(argv[0], "debug"))
    {
	for(i = 1; i < argc; i++)
	    play_midi_file(argv[i]);
	return 0;
    }

    /* Main Loop */
    for(;;)
    {
	int rc;

	if(fgets(cmd, sizeof(cmd), stdin) == NULL)
	    return 0; /* Emacs may down */
	chomp(cmd);
	if(!strncmp(cmd, "PLAY", 4))
	{
	    rc = play_midi_file(cmd + 5);
	    switch(rc)
	    {
	      case RC_TUNE_END:
	      case RC_NEXT:
		fprintf(outfp, "(timidity-NEXT)\n");
		ctl_refresh();
		break;
	      case RC_QUIT:
		return 0;
	    } /* skipping others command */
	}
	else if(!strncmp(cmd, "QUIT", 4))
	    return 0;
	else
	    continue; /* skipping unknown command */
    }
    /*NOTREACHED*/
}

static int cmsg(int type, int verbosity_level, char *fmt, ...)
{
    va_list ap;
    char buff[BUFSIZ];

    if((type==CMSG_TEXT || type==CMSG_INFO || type==CMSG_WARNING) &&
       ctl.verbosity < verbosity_level)
	return 0;
    va_start(ap, fmt);

    vsprintf(buff, fmt, ap);
    fprintf(outfp, "(timidity-CMSG %d \"", type);
    quote_string_out(buff);
    fprintf(outfp, "\")\n");
    va_end(ap);
    ctl_refresh();
    return 0;
}

static void ctl_refresh(void)
{
    fflush(stdout);
}

static void ctl_total_time(int tt)
{
    int secs;
    secs = tt/play_mode->rate;
    fprintf(outfp, "(timidity-TIME %d)\n", secs);
    ctl_refresh();
}

static void ctl_master_volume(int mv)
{
    fprintf(outfp, "(timidity-MVOL %d)\n", mv);
    ctl_refresh();
}

static void ctl_file_name(char *name)
{
    fprintf(outfp, "(timidity-FILE \"");
    quote_string_out(name);
    fprintf(outfp, "\")\n");
    ctl_refresh();
}

static void ctl_current_time(int secs, int v)
{
    fprintf(outfp, "(timidity-CURT %d %d)\n", secs, v);
    ctl_refresh();
}

static int status_number(int s)
{
    switch(s)
    {
      case VOICE_FREE:
	return 0;
      case VOICE_ON:
	return 1;
      case VOICE_SUSTAINED:
	return 2;
      case VOICE_OFF:
	return 3;
      case VOICE_DIE:
	return 4;
    }
    /* dmy */
    return 3;
}

static void ctl_note(int status, int ch, int note, int vel)
{
    if(ch >= 16)
	return;
    if(midi_trace.flush_flag)
	return;
    fprintf(outfp, "(timidity-NOTE %d %d %d)\n", ch, note,
	    status_number(status));
    ctl_refresh();
}

static void ctl_program(int ch, int val)
{
    if(ch >= 16)
	return;
    if(midi_trace.flush_flag)
	return;
    if(channel[ch].special_sample)
	val = channel[ch].special_sample;
    else
	val += progbase;
    fprintf(outfp, "(timidity-PROG %d %d)\n", ch, val);
    ctl_refresh();
}

static void ctl_volume(int ch, int val)
{
    if(ch >= 16)
	return;
    if(midi_trace.flush_flag)
	return;
    fprintf(outfp, "(timidity-VOL %d %d)\n", ch, (val*100)/127);
    ctl_refresh();
}

static void ctl_expression(int ch, int val)
{
    if(ch >= 16)
	return;
    if(midi_trace.flush_flag)
	return;
    fprintf(outfp, "(timidity-EXP %d %d)\n", ch, (val*100)/127);
    ctl_refresh();
}

static void ctl_panning(int ch, int val)
{
    if(ch >= 16)
	return;
    if(midi_trace.flush_flag)
	return;
    fprintf(outfp, "(timidity-PAN %d %d)\n", ch, val);
    ctl_refresh();
}

static void ctl_sustain(int ch, int val)
{
    if(ch >= 16)
	return;
    if(midi_trace.flush_flag)
	return;
    fprintf(outfp, "(timidity-SUS %d %d)\n", ch, val);
    ctl_refresh();
}

static void ctl_pitch_bend(int ch, int val)
{
    if(ch >= 16)
	return;
    if(midi_trace.flush_flag)
	return;
    fprintf(outfp, "(timidity-PIT %d %d)\n", ch, val);
    ctl_refresh();
}

static void ctl_reset(void)
{
    int i;
    uint32 drums;

    /* Note that Emacs is 24 bit integer. */
    drums = 0;
    for(i = 0; i < 16; i++)
	if(ISDRUMCHANNEL(i))
	    drums |= (1u << i);
    fprintf(outfp, "(timidity-DRUMS %lu)\n", (unsigned long)drums);

    fprintf(outfp, "(timidity-RESET)\n");
    for(i = 0; i < 16; i++)
    {
	if(ISDRUMCHANNEL(i))
	    ctl_program(i, channel[i].bank);
	else
	    ctl_program(i, channel[i].program);
	ctl_volume(i, channel[i].volume);
	ctl_expression(i, channel[i].expression);
	ctl_panning(i, channel[i].panning);
	ctl_sustain(i, channel[i].sustain);
	ctl_pitch_bend(i, channel[i].pitchbend);
    }
    ctl_refresh();
}


#if defined(sgi)
#include <sys/time.h>
#include <bstring.h>
#endif

#if defined(SOLARIS) || defined(__FreeBSD__)
#include <unistd.h>
#include <sys/filio.h>
#endif

static int read_ready(void)
{
#if defined(sgi)
    fd_set fds;
    int cnt;
    struct timeval timeout;
    int fd;

    fd = fileno(stdin);

    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    timeout.tv_sec = timeout.tv_usec = 0;

    if((cnt = select(fd + 1, &fds, NULL, NULL, &timeout)) < 0)
    {
	fprintf(outfp, "(error \"select system call is failed\")\n");
	ctl_refresh();
	return -1;
    }

    return cnt > 0 && FD_ISSET(fd, &fds) != 0;
#else
    int num;
    int fd;

    fd = fileno(stdin);
    if(ioctl(fd, FIONREAD, &num) < 0) /* see how many chars in buffer. */
    {
	fprintf(outfp, "(error \"ioctl system call is failed\")\n");
	ctl_refresh();
	return -1;
    }
    return num;
#endif
}

static void ctl_event(CtlEvent *e)
{
    switch(e->type)
    {
      case CTLE_NOW_LOADING:
	ctl_file_name((char *)e->v1);
	break;
      case CTLE_LOADING_DONE:
	break;
      case CTLE_PLAY_START:
	ctl_total_time((int)e->v1);
	break;
      case CTLE_PLAY_END:
	break;
      case CTLE_TEMPO:
	break;
      case CTLE_METRONOME:
	break;
      case CTLE_CURRENT_TIME:
	ctl_current_time((int)e->v1, (int)e->v2);
	break;
      case CTLE_NOTE:
	ctl_note((int)e->v1, (int)e->v2, (int)e->v3, (int)e->v4);
	break;
      case CTLE_MASTER_VOLUME:
	ctl_master_volume((int)e->v1);
	break;
      case CTLE_PROGRAM:
	ctl_program((int)e->v1, (int)e->v2);
	break;
      case CTLE_VOLUME:
	ctl_volume((int)e->v1, (int)e->v2);
	break;
      case CTLE_EXPRESSION:
	ctl_expression((int)e->v1, (int)e->v2);
	break;
      case CTLE_PANNING:
	ctl_panning((int)e->v1, (int)e->v2);
	break;
      case CTLE_SUSTAIN:
	ctl_sustain((int)e->v1, (int)e->v2);
	break;
      case CTLE_PITCH_BEND:
	ctl_pitch_bend((int)e->v1, (int)e->v2);
	break;
      case CTLE_MOD_WHEEL:
	ctl_pitch_bend((int)e->v1, e->v2 ? -1 : 0x2000);
	break;
      case CTLE_CHORUS_EFFECT:
	break;
      case CTLE_REVERB_EFFECT:
	break;
      case CTLE_LYRIC:
	default_ctl_lyric((int)e->v1);
	break;
      case CTLE_REFRESH:
	ctl_refresh();
	break;
      case CTLE_RESET:
	ctl_reset();
	break;
    }
}

/*
 * interface_<id>_loader();
 */
ControlMode *interface_e_loader(void)
{
    return &ctl;
}