1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2004 Masanao Izumo <iz@onicos.co.jp>
4     Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20     playmidi.h
21 */
22 
23 #ifndef ___PLAYMIDI_H_
24 #define ___PLAYMIDI_H_
25 
26 typedef struct {
27   int32 time;
28   uint8 type, channel, a, b;
29 } MidiEvent;
30 
31 #define REVERB_MAX_DELAY_OUT (4 * play_mode->rate)
32 
33 #define MIDI_EVENT_NOTE(ep) (ISDRUMCHANNEL((ep)->channel) ? (ep)->a : \
34 			     (((int)(ep)->a + note_key_offset + \
35 			       channel[ep->channel].key_shift) & 0x7f))
36 
37 #define MIDI_EVENT_TIME(ep) ((int32)((ep)->time * midi_time_ratio + 0.5))
38 
39 #define SYSEX_TAG 0xFF
40 
41 /* Midi events */
42 enum midi_event_t
43 {
44 	ME_NONE,
45 
46 	/* MIDI events */
47 	ME_NOTEOFF,
48 	ME_NOTEON,
49 	ME_KEYPRESSURE,
50 	ME_PROGRAM,
51 	ME_CHANNEL_PRESSURE,
52 	ME_PITCHWHEEL,
53 
54 	/* Controls */
55 	ME_TONE_BANK_MSB,
56 	ME_TONE_BANK_LSB,
57 	ME_MODULATION_WHEEL,
58 	ME_BREATH,
59 	ME_FOOT,
60 	ME_MAINVOLUME,
61 	ME_BALANCE,
62 	ME_PAN,
63 	ME_EXPRESSION,
64 	ME_SUSTAIN,
65 	ME_PORTAMENTO_TIME_MSB,
66 	ME_PORTAMENTO_TIME_LSB,
67 	ME_PORTAMENTO,
68 	ME_PORTAMENTO_CONTROL,
69 	ME_DATA_ENTRY_MSB,
70 	ME_DATA_ENTRY_LSB,
71 	ME_SOSTENUTO,
72 	ME_SOFT_PEDAL,
73 	ME_LEGATO_FOOTSWITCH,
74 	ME_HOLD2,
75 	ME_HARMONIC_CONTENT,
76 	ME_RELEASE_TIME,
77 	ME_ATTACK_TIME,
78 	ME_BRIGHTNESS,
79 	ME_REVERB_EFFECT,
80 	ME_TREMOLO_EFFECT,
81 	ME_CHORUS_EFFECT,
82 	ME_CELESTE_EFFECT,
83 	ME_PHASER_EFFECT,
84 	ME_RPN_INC,
85 	ME_RPN_DEC,
86 	ME_NRPN_LSB,
87 	ME_NRPN_MSB,
88 	ME_RPN_LSB,
89 	ME_RPN_MSB,
90 	ME_ALL_SOUNDS_OFF,
91 	ME_RESET_CONTROLLERS,
92 	ME_ALL_NOTES_OFF,
93 	ME_MONO,
94 	ME_POLY,
95 
96 	/* TiMidity Extensionals */
97 #if 0
98 	ME_VOLUME_ONOFF,		/* Not supported */
99 #endif
100 	ME_MASTER_TUNING,		/* Master tuning */
101 	ME_SCALE_TUNING,		/* Scale tuning */
102 	ME_BULK_TUNING_DUMP,	/* Bulk tuning dump */
103 	ME_SINGLE_NOTE_TUNING,	/* Single-note tuning */
104 	ME_RANDOM_PAN,
105 	ME_SET_PATCH,			/* Install special instrument */
106 	ME_DRUMPART,
107 	ME_KEYSHIFT,
108 	ME_PATCH_OFFS,			/* Change special instrument sample position
109 							 * Channel, LSB, MSB
110 							 */
111 
112 	/* Global channel events */
113 	ME_TEMPO,
114 	ME_CHORUS_TEXT,
115 	ME_LYRIC,
116 	ME_GSLCD,				/* GS L.C.D. Exclusive message event */
117 	ME_MARKER,
118 	ME_INSERT_TEXT,			/* for SC */
119 	ME_TEXT,
120 	ME_KARAOKE_LYRIC,		/* for KAR format */
121 	ME_MASTER_VOLUME,
122 	ME_RESET,				/* Reset and change system mode */
123 	ME_NOTE_STEP,
124 	ME_CUEPOINT,			/* skip to time segment */
125 
126 	ME_TIMESIG,				/* Time signature */
127 	ME_KEYSIG,				/* Key signature */
128 	ME_TEMPER_KEYSIG,		/* Temperament key signature */
129 	ME_TEMPER_TYPE,			/* Temperament type */
130 	ME_MASTER_TEMPER_TYPE,	/* Master temperament type */
131 	ME_USER_TEMPER_ENTRY,	/* User-defined temperament entry */
132 
133 	ME_SYSEX_LSB,			/* Universal system exclusive message (LSB) */
134 	ME_SYSEX_MSB,			/* Universal system exclusive message (MSB) */
135 	ME_SYSEX_GS_LSB,		/* GS system exclusive message (LSB) */
136 	ME_SYSEX_GS_MSB,		/* GS system exclusive message (MSB) */
137 	ME_SYSEX_XG_LSB,		/* XG system exclusive message (LSB) */
138 	ME_SYSEX_XG_MSB,		/* XG system exclusive message (MSB) */
139 
140 	ME_WRD,					/* for MIMPI WRD tracer */
141 	ME_SHERRY,				/* for Sherry WRD tracer */
142 	ME_BARMARKER,
143 	ME_STEP,				/* for Metronome */
144 
145 	ME_LAST = 254,			/* Last sequence of MIDI list.
146 							 * This event is reserved for realtime player.
147 							 */
148 	ME_EOT = 255			/* End of MIDI.  Finish to play */
149 };
150 
151 #define GLOBAL_CHANNEL_EVENT_TYPE(type)	\
152 	((type) == ME_NONE || (type) >= ME_TEMPO)
153 
154 enum rpn_data_address_t /* NRPN/RPN */
155 {
156     NRPN_ADDR_0108,
157     NRPN_ADDR_0109,
158     NRPN_ADDR_010A,
159     NRPN_ADDR_0120,
160     NRPN_ADDR_0121,
161 	NRPN_ADDR_0130,
162 	NRPN_ADDR_0131,
163 	NRPN_ADDR_0134,
164 	NRPN_ADDR_0135,
165     NRPN_ADDR_0163,
166     NRPN_ADDR_0164,
167     NRPN_ADDR_0166,
168     NRPN_ADDR_1400,
169     NRPN_ADDR_1500,
170     NRPN_ADDR_1600,
171     NRPN_ADDR_1700,
172     NRPN_ADDR_1800,
173     NRPN_ADDR_1900,
174     NRPN_ADDR_1A00,
175     NRPN_ADDR_1C00,
176     NRPN_ADDR_1D00,
177     NRPN_ADDR_1E00,
178     NRPN_ADDR_1F00,
179     NRPN_ADDR_3000,
180     NRPN_ADDR_3100,
181     NRPN_ADDR_3400,
182     NRPN_ADDR_3500,
183     RPN_ADDR_0000,
184     RPN_ADDR_0001,
185     RPN_ADDR_0002,
186     RPN_ADDR_0003,
187     RPN_ADDR_0004,
188 	RPN_ADDR_0005,
189     RPN_ADDR_7F7F,
190     RPN_ADDR_FFFF,
191     RPN_MAX_DATA_ADDR
192 };
193 
194 #define RX_PITCH_BEND (1<<0)
195 #define RX_CH_PRESSURE (1<<1)
196 #define RX_PROGRAM_CHANGE (1<<2)
197 #define RX_CONTROL_CHANGE (1<<3)
198 #define RX_POLY_PRESSURE (1<<4)
199 #define RX_NOTE_MESSAGE (1<<5)
200 #define RX_RPN (1<<6)
201 #define RX_NRPN (1<<7)
202 #define RX_MODULATION (1<<8)
203 #define RX_VOLUME (1<<9)
204 #define RX_PANPOT (1<<10)
205 #define RX_EXPRESSION (1<<11)
206 #define RX_HOLD1 (1<<12)
207 #define RX_PORTAMENTO (1<<13)
208 #define RX_SOSTENUTO (1<<14)
209 #define RX_SOFT (1<<15)
210 #define RX_NOTE_ON (1<<16)
211 #define RX_NOTE_OFF (1<<17)
212 #define RX_BANK_SELECT (1<<18)
213 #define RX_BANK_SELECT_LSB (1<<19)
214 
215 enum {
216 	EG_ATTACK = 0,
217 	EG_DECAY = 2,
218 	EG_DECAY1 = 1,
219 	EG_DECAY2 = 2,
220 	EG_RELEASE = 3,
221 	EG_NULL = 5,
222 	EG_GUS_ATTACK = 0,
223 	EG_GUS_DECAY = 1,
224 	EG_GUS_SUSTAIN = 2,
225 	EG_GUS_RELEASE1 = 3,
226 	EG_GUS_RELEASE2 = 4,
227 	EG_GUS_RELEASE3 = 5,
228 	EG_SF_ATTACK = 0,
229 	EG_SF_HOLD = 1,
230 	EG_SF_DECAY = 2,
231 	EG_SF_RELEASE = 3,
232 };
233 
234 #ifndef PART_EQ_XG
235 #define PART_EQ_XG
236 /*! shelving filter */
237 typedef struct {
238 	double freq, gain, q;
239 	int32 x1l, x2l, y1l, y2l, x1r, x2r, y1r, y2r;
240 	int32 a1, a2, b0, b1, b2;
241 } filter_shelving;
242 
243 /*! Part EQ (XG) */
244 struct part_eq_xg {
245 	int8 bass, treble, bass_freq, treble_freq;
246 	filter_shelving basss, trebles;
247 	int8 valid;
248 };
249 #endif /* PART_EQ_XG */
250 
251 typedef struct {
252   int16 val;
253   int8 pitch;	/* in +-semitones [-24, 24] */
254   int16 cutoff;	/* in +-cents [-9600, 9600] */
255   float amp;	/* [-1.0, 1.0] */
256   /* in GS, LFO1 means LFO for voice 1, LFO2 means LFO for voice2.
257      LFO2 is not supported. */
258   float lfo1_rate, lfo2_rate;	/* in +-Hz [-10.0, 10.0] */
259   int16 lfo1_pitch_depth, lfo2_pitch_depth;	/* in cents [0, 600] */
260   int16 lfo1_tvf_depth, lfo2_tvf_depth;	/* in cents [0, 2400] */
261   float lfo1_tva_depth, lfo2_tva_depth;	/* [0, 1.0] */
262   int8 variation_control_depth, insertion_control_depth;
263 } midi_controller;
264 
265 struct DrumPartEffect
266 {
267 	int32 *buf;
268 	int8 note, reverb_send, chorus_send, delay_send;
269 };
270 
271 struct DrumParts
272 {
273     int8 drum_panning;
274     int32 drum_envelope_rate[6]; /* drum instrument envelope */
275     int8 pan_random;    /* flag for drum random pan */
276 	float drum_level;
277 
278 	int8 chorus_level, reverb_level, delay_level, coarse, fine,
279 		play_note, drum_cutoff_freq, drum_resonance;
280 	int32 rx;
281 };
282 
283 typedef struct {
284   int8	bank_msb, bank_lsb, bank, program, volume,
285 	expression, sustain, panning, mono, portamento,
286 	key_shift, loop_timeout;
287 
288   /* chorus, reverb... Coming soon to a 300-MHz, eight-way superscalar
289      processor near you */
290   int8	chorus_level,	/* Chorus level */
291 	reverb_level;	/* Reverb level. */
292   int	reverb_id;	/* Reverb ID used for reverb optimize implementation
293 			   >=0 reverb_level
294 			   -1: DEFAULT_REVERB_SEND_LEVEL
295 			   */
296   int8 delay_level;	/* Delay Send Level */
297   int8 eq_gs;	/* EQ ON/OFF (GS) */
298   int8 insertion_effect;
299 
300   /* Special sample ID. (0 means Normal sample) */
301   uint8 special_sample;
302 
303   int pitchbend;
304 
305   FLOAT_T
306     pitchfactor; /* precomputed pitch bend factor to save some fdiv's */
307 
308   /* For portamento */
309   uint8 portamento_time_msb, portamento_time_lsb;
310   int porta_control_ratio, porta_dpb;
311   int32 last_note_fine;
312 
313   /* For Drum part */
314   struct DrumParts *drums[128];
315 
316   /* For NRPN Vibrato */
317   int32 vibrato_depth, vibrato_delay;
318   float vibrato_ratio;
319 
320   /* For RPN */
321   uint8 rpnmap[RPN_MAX_DATA_ADDR]; /* pseudo RPN address map */
322   uint8 rpnmap_lsb[RPN_MAX_DATA_ADDR];
323   uint8 lastlrpn, lastmrpn;
324   int8  nrpn; /* 0:RPN, 1:NRPN, -1:Undefined */
325   int rpn_7f7f_flag;		/* Boolean flag used for RPN 7F/7F */
326 
327   /* For channel envelope */
328   int32 envelope_rate[6]; /* for Envelope Generator in mix.c
329 			   * 0: value for attack rate
330 			   * 2: value for decay rate
331 			   * 3: value for release rate
332 			   */
333 
334   int mapID;			/* Program map ID */
335   AlternateAssign *altassign;	/* Alternate assign patch table */
336   int32 lasttime;     /* Last sample time of computed voice on this channel */
337 
338   /* flag for random pan */
339   int pan_random;
340 
341   /* for Voice LPF / Resonance */
342   int8 param_resonance, param_cutoff_freq;	/* -64 ~ 63 */
343   float cutoff_freq_coef, resonance_dB;
344 
345   int8 velocity_sense_depth, velocity_sense_offset;
346 
347   int8 scale_tuning[12], prev_scale_tuning;
348   int8 temper_type;
349 
350   int8 soft_pedal;
351   int8 sostenuto;
352   int8 damper_mode;
353 
354   int8 tone_map0_number;
355   FLOAT_T pitch_offset_fine;	/* in Hz */
356   int8 assign_mode;
357 
358   int8 legato;	/* legato footswitch */
359   int8 legato_flag;	/* note-on flag for legato */
360 
361   midi_controller mod, bend, caf, paf, cc1, cc2;
362 
363   ChannelBitMask channel_layer;
364   int port_select;
365 
366   struct part_eq_xg eq_xg;
367 
368   int8 dry_level;
369   int8 note_limit_high, note_limit_low;	/* Note Limit (Keyboard Range) */
370   int8 vel_limit_high, vel_limit_low;	/* Velocity Limit */
371   int32 rx;	/* Rx. ~ (Rcv ~) */
372 
373   int drum_effect_num;
374   int8 drum_effect_flag;
375   struct DrumPartEffect *drum_effect;
376 
377   int8 sysex_gs_msb_addr, sysex_gs_msb_val,
378 		sysex_xg_msb_addr, sysex_xg_msb_val, sysex_msb_addr, sysex_msb_val;
379 } Channel;
380 
381 /* Causes the instrument's default panning to be used. */
382 #define NO_PANNING -1
383 
384 typedef struct {
385 	int16 freq, last_freq, orig_freq;
386 	double reso_dB, last_reso_dB, orig_reso_dB, reso_lin;
387 	int8 type;	/* filter type. 0: Off, 1: 12dB/oct, 2: 24dB/oct */
388 	int32 f, q, p;	/* coefficients in fixed-point */
389 	int32 b0, b1, b2, b3, b4;
390 	float gain;
391 	int8 start_flag;
392 } FilterCoefficients;
393 
394 #define ENABLE_PAN_DELAY
395 #ifdef ENABLE_PAN_DELAY
396 #define PAN_DELAY_BUF_MAX 48	/* 0.5ms in 96kHz */
397 #endif	/* ENABLE_PAN_DELAY */
398 
399 typedef struct {
400   uint8
401     status, channel, note, velocity;
402   int vid, temper_instant;
403   Sample *sample;
404 #if SAMPLE_LENGTH_BITS == 32 && TIMIDITY_HAVE_INT64
405   int64 sample_offset;	/* sample_offset must be signed */
406 #else
407   splen_t sample_offset;
408 #endif
409   int32
410     orig_frequency, frequency, sample_increment,
411     envelope_volume, envelope_target, envelope_increment,
412     tremolo_sweep, tremolo_sweep_position,
413     tremolo_phase, tremolo_phase_increment,
414     vibrato_sweep, vibrato_sweep_position;
415 
416   final_volume_t left_mix, right_mix;
417 #ifdef SMOOTH_MIXING
418   int32 old_left_mix, old_right_mix,
419      left_mix_offset, right_mix_offset,
420      left_mix_inc, right_mix_inc;
421 #endif
422 
423   FLOAT_T
424     left_amp, right_amp, tremolo_volume;
425   int32
426     vibrato_sample_increment[VIBRATO_SAMPLE_INCREMENTS], vibrato_delay;
427   int
428 	vibrato_phase, orig_vibrato_control_ratio, vibrato_control_ratio,
429     vibrato_depth, vibrato_control_counter,
430     envelope_stage, control_counter, panning, panned;
431   int16 tremolo_depth;
432 
433   /* for portamento */
434   int porta_control_ratio, porta_control_counter, porta_dpb;
435   int32 porta_pb;
436 
437   int delay; /* Note ON delay samples */
438   int32 timeout;
439   struct cache_hash *cache;
440 
441   uint8 chorus_link;	/* Chorus link */
442   int8 proximate_flag;
443 
444   FilterCoefficients fc;
445 
446   FLOAT_T envelope_scale, last_envelope_volume;
447   int32 inv_envelope_scale;
448 
449   int modenv_stage;
450   int32
451     modenv_volume, modenv_target, modenv_increment;
452   FLOAT_T last_modenv_volume;
453   int32 tremolo_delay, modenv_delay;
454 
455   int32 delay_counter;
456 
457 #ifdef ENABLE_PAN_DELAY
458   int32 *pan_delay_buf, pan_delay_rpt, pan_delay_wpt, pan_delay_spt;
459 #endif	/* ENABLE_PAN_DELAY */
460 } Voice;
461 
462 /* Voice status options: */
463 #define VOICE_FREE	(1<<0)
464 #define VOICE_ON	(1<<1)
465 #define VOICE_SUSTAINED	(1<<2)
466 #define VOICE_OFF	(1<<3)
467 #define VOICE_DIE	(1<<4)
468 
469 /* Voice panned options: */
470 #define PANNED_MYSTERY 0
471 #define PANNED_LEFT 1
472 #define PANNED_RIGHT 2
473 #define PANNED_CENTER 3
474 /* Anything but PANNED_MYSTERY only uses the left volume */
475 
476 #define ISDRUMCHANNEL(c)  IS_SET_CHANNELMASK(drumchannels, c)
477 
478 extern Channel channel[];
479 extern Voice *voice;
480 
481 /* --module */
482 extern int opt_default_module;
483 extern int opt_preserve_silence;
484 
485 enum {
486 	MODULE_TIMIDITY_DEFAULT = 0x0,
487 	/* GS modules */
488 	MODULE_SC55 = 0x1,
489 	MODULE_SC88 = 0x2,
490 	MODULE_SC88PRO = 0x3,
491 	MODULE_SC8850 = 0x4,
492 	/* XG modules */
493 	MODULE_MU50 = 0x10,
494 	MODULE_MU80 = 0x11,
495 	MODULE_MU90 = 0x12,
496 	MODULE_MU100 = 0x13,
497 	/* GM modules */
498 	MODULE_SBLIVE = 0x20,
499 	MODULE_SBAUDIGY = 0x21,
500 	/* Special modules */
501 	MODULE_TIMIDITY_SPECIAL1 = 0x70,
502 	MODULE_TIMIDITY_DEBUG = 0x7f,
503 };
504 
get_module(void)505 static inline int get_module(void) {return opt_default_module;}
506 
is_gs_module(void)507 static inline int is_gs_module(void)
508 {
509 	int module = get_module();
510     return (module >= MODULE_SC55 && module <= MODULE_MU100);
511 }
512 
is_xg_module(void)513 static inline int is_xg_module(void)
514 {
515 	int module = get_module();
516     return (module >= MODULE_MU50 && module <= MODULE_MU100);
517 }
518 
519 extern int32 control_ratio, amp_with_poly, amplification;
520 
521 extern ChannelBitMask default_drumchannel_mask;
522 extern ChannelBitMask drumchannel_mask;
523 extern ChannelBitMask default_drumchannels;
524 extern ChannelBitMask drumchannels;
525 
526 extern int adjust_panning_immediately;
527 extern int max_voices;
528 extern int voices, upper_voices;
529 extern int note_key_offset;
530 extern FLOAT_T midi_time_ratio;
531 extern int opt_modulation_wheel;
532 extern int opt_portamento;
533 extern int opt_nrpn_vibrato;
534 extern int opt_reverb_control;
535 extern int opt_chorus_control;
536 extern int opt_surround_chorus;
537 extern int opt_channel_pressure;
538 extern int opt_lpf_def;
539 extern int opt_overlap_voice_allow;
540 extern int opt_temper_control;
541 extern int opt_tva_attack;
542 extern int opt_tva_decay;
543 extern int opt_tva_release;
544 extern int opt_delay_control;
545 extern int opt_eq_control;
546 extern int opt_insertion_effect;
547 extern int opt_drum_effect;
548 extern int opt_env_attack;
549 extern int opt_modulation_envelope;
550 extern int noise_sharp_type;
551 extern int32 current_play_tempo;
552 extern int opt_realtime_playing;
553 extern int reduce_voice_threshold; /* msec */
554 extern int check_eot_flag;
555 extern int special_tonebank;
556 extern int default_tonebank;
557 extern int playmidi_seek_flag;
558 extern int effect_lr_mode;
559 extern int effect_lr_delay_msec;
560 extern int auto_reduce_polyphony;
561 extern int play_pause_flag;
562 extern int reduce_quality_flag;
563 extern int no_4point_interpolation;
564 extern ChannelBitMask channel_mute;
565 extern int temper_type_mute;
566 extern int8 current_keysig;
567 extern int8 current_temper_keysig;
568 extern int temper_adj;
569 extern int8 opt_init_keysig;
570 extern int8 opt_force_keysig;
571 extern int key_adjust;
572 extern FLOAT_T tempo_adjust;
573 extern int opt_pure_intonation;
574 extern int current_freq_table;
575 extern int32 opt_drum_power;
576 extern int opt_amp_compensation;
577 extern int opt_realtime_priority;	/* interface/alsaseq_c.c */
578 extern int opt_sequencer_ports;		/* interface/alsaseq_c.c */
579 extern int opt_user_volume_curve;
580 extern int opt_pan_delay;
581 
582 extern int play_midi_file(char *fn);
583 extern int dumb_pass_playing_list(int number_of_files, char *list_of_files[]);
584 extern void default_ctl_lyric(int lyricid);
585 extern int check_apply_control(void);
586 extern void recompute_freq(int v);
587 extern int midi_drumpart_change(int ch, int isdrum);
588 extern void ctl_note_event(int noteID);
589 extern void ctl_mode_event(int type, int trace, ptr_size_t arg1, ptr_size_t arg2);
590 extern char *channel_instrum_name(int ch);
591 extern int get_reverb_level(int ch);
592 extern int get_chorus_level(int ch);
593 extern void playmidi_output_changed(int play_state);
594 extern Instrument *play_midi_load_instrument(int dr, int bk, int prog);
595 extern void midi_program_change(int ch, int prog);
596 extern void free_voice(int v);
597 extern void free_reverb_buffer(void);
598 extern void play_midi_setup_drums(int ch,int note);
599 
600 /* For stream player */
601 extern void playmidi_stream_init(void);
602 extern void playmidi_tmr_reset(void);
603 extern int play_event(MidiEvent *ev);
604 
605 extern void recompute_voice_filter(int);
606 extern int32 get_note_freq(Sample *, int);
607 
608 extern void free_drum_effect(int);
609 
610 #endif /* ___PLAYMIDI_H_ */
611