1 /*
2 Copyright (c) 2009-2010 Tero Lindeman (kometbomb)
3 
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 
27 #include "event.h"
28 #include "mused.h"
29 #include "action.h"
30 #include "edit.h"
31 #include "util/rnd.h"
32 #include <string.h>
33 #include "snd/cydwave.h"
34 #include "snd/freqs.h"
35 #include "mymsg.h"
36 #include "command.h"
37 
38 extern Mused mused;
39 
40 #define flipbit(val, bit) { val ^= bit; };
41 
editparambox(int v)42 void editparambox(int v)
43 {
44 	MusInstrument *inst = &mused.song.instrument[mused.current_instrument];
45 	Uint16 *param = &inst->program[mused.current_program_step];
46 	Uint32 mask = 0xffff0fff >> (mused.editpos*4);
47 
48 	if (*param == MUS_FX_NOP)
49 		*param = 0;
50 
51 	if (mused.editpos != 0 || v < 0xf)
52 	{
53 		// Keeps the exec next command bit intact
54 		*param = (*param & 0x8000) | (((*param & mask) | ((v&0xf) <<((3-mused.editpos)*4))) & 0x7fff);
55 	}
56 	else
57 	{
58 		*param = ((*param & mask) | ((v&0xf) <<((3-mused.editpos)*4)));
59 	}
60 
61 	if (++mused.editpos > 3)
62 	{
63 		*param = validate_command(*param);
64 		mused.editpos = 3;
65 	}
66 }
67 
68 
find_note(int sym,int oct)69 int find_note(int sym, int oct)
70 {
71 	static const int keys[] =
72 	{
73 	SDLK_z, SDLK_s, SDLK_x, SDLK_d, SDLK_c, SDLK_v, SDLK_g, SDLK_b, SDLK_h, SDLK_n, SDLK_j, SDLK_m,
74 	SDLK_q, SDLK_2, SDLK_w, SDLK_3, SDLK_e, SDLK_r, SDLK_5, SDLK_t, SDLK_6, SDLK_y, SDLK_7, SDLK_u,
75 	SDLK_i, SDLK_9, SDLK_o, SDLK_0, SDLK_p, -1};
76 
77 	int n = 0;
78 	for (const int *i = keys ; *i != -1 ; ++i, ++n)
79 	{
80 		if (*i == sym)
81 			return n + oct*12;
82 	}
83 
84 	return -1;
85 }
86 
87 
instrument_add_param(int a)88 void instrument_add_param(int a)
89 {
90 	MusInstrument *i = &mused.song.instrument[mused.current_instrument];
91 
92 	if (a < 0) a = -1; else if (a > 0) a = 1;
93 
94 	if (SDL_GetModState() & KMOD_SHIFT)
95 	{
96 		switch (mused.selected_param)
97 		{
98 			case P_BASENOTE: a *= 12; break;
99 			case P_BUZZ_SEMI: a *= 12; break;
100 			default: a *= 16; break;
101 		}
102 	}
103 
104 	switch (mused.selected_param)
105 	{
106 		case P_INSTRUMENT:
107 
108 		clamp(mused.current_instrument, a, 0, NUM_INSTRUMENTS - 1);
109 
110 		break;
111 
112 		case P_BASENOTE:
113 
114 		clamp(i->base_note, a, 0, 95);
115 
116 		break;
117 
118 		case P_FINETUNE:
119 
120 		clamp(i->finetune, a, -128, 127);
121 
122 		break;
123 
124 		case P_LOCKNOTE:
125 
126 		flipbit(i->flags, MUS_INST_LOCK_NOTE);
127 
128 		break;
129 
130 		case P_DRUM:
131 
132 		flipbit(i->flags, MUS_INST_DRUM);
133 
134 		break;
135 
136 		case P_KEYSYNC:
137 
138 		flipbit(i->cydflags, CYD_CHN_ENABLE_KEY_SYNC);
139 
140 		break;
141 
142 		case P_SYNC:
143 
144 		flipbit(i->cydflags, CYD_CHN_ENABLE_SYNC);
145 
146 		break;
147 
148 		case P_SYNCSRC:
149 		{
150 			int x = (Uint8)(i->sync_source+1);
151 			clamp(x, a, 0, MUS_MAX_CHANNELS);
152 			i->sync_source = x-1;
153 		}
154 		break;
155 
156 		case P_WAVE:
157 
158 		flipbit(i->cydflags, CYD_CHN_ENABLE_WAVE);
159 
160 		break;
161 
162 		case P_WAVE_ENTRY:
163 
164 		clamp(i->wavetable_entry, a, 0, CYD_WAVE_MAX_ENTRIES - 1);
165 
166 		break;
167 
168 		case P_WAVE_OVERRIDE_ENV:
169 
170 		flipbit(i->cydflags, CYD_CHN_WAVE_OVERRIDE_ENV);
171 
172 		break;
173 
174 		case P_WAVE_LOCK_NOTE:
175 
176 		flipbit(i->flags, MUS_INST_WAVE_LOCK_NOTE);
177 
178 		break;
179 
180 		case P_PULSE:
181 
182 		flipbit(i->cydflags, CYD_CHN_ENABLE_PULSE);
183 
184 		break;
185 
186 		case P_SAW:
187 
188 		flipbit(i->cydflags, CYD_CHN_ENABLE_SAW);
189 
190 		break;
191 
192 		case P_TRIANGLE:
193 
194 		flipbit(i->cydflags, CYD_CHN_ENABLE_TRIANGLE);
195 
196 		break;
197 
198 		case P_LFSR:
199 
200 		flipbit(i->cydflags, CYD_CHN_ENABLE_LFSR);
201 
202 		break;
203 
204 		case P_LFSRTYPE:
205 
206 		clamp(i->lfsr_type, a, 0, CYD_NUM_LFSR - 1);
207 
208 		break;
209 
210 		case P_NOISE:
211 
212 		flipbit(i->cydflags, CYD_CHN_ENABLE_NOISE);
213 
214 		break;
215 
216 		case P_METAL:
217 
218 		flipbit(i->cydflags, CYD_CHN_ENABLE_METAL);
219 
220 		break;
221 
222 		case P_RELVOL:
223 
224 		flipbit(i->flags, MUS_INST_RELATIVE_VOLUME);
225 
226 		break;
227 
228 		case P_FX:
229 
230 		flipbit(i->cydflags, CYD_CHN_ENABLE_FX);
231 
232 		break;
233 
234 		case P_FXBUS:
235 
236 		clamp(i->fx_bus, a, 0, CYD_MAX_FX_CHANNELS - 1);
237 
238 		break;
239 
240 		case P_ATTACK:
241 
242 		clamp(i->adsr.a, a, 0, 32 * ENVELOPE_SCALE - 1);
243 
244 		break;
245 
246 		case P_DECAY:
247 
248 		clamp(i->adsr.d, a, 0, 32 * ENVELOPE_SCALE - 1);
249 
250 		break;
251 
252 		case P_SUSTAIN:
253 
254 		clamp(i->adsr.s, a, 0, 31);
255 
256 		break;
257 
258 		case P_RELEASE:
259 
260 		clamp(i->adsr.r, a, 0, 32 * ENVELOPE_SCALE - 1);
261 
262 		break;
263 
264 		case P_BUZZ:
265 
266 		flipbit(i->flags, MUS_INST_YM_BUZZ);
267 
268 		break;
269 
270 		case P_BUZZ_SHAPE:
271 
272 		clamp(i->ym_env_shape, a, 0, 3);
273 
274 		break;
275 
276 		case P_BUZZ_FINE:
277 		{
278 			clamp(i->buzz_offset, a, -32768, 32767);
279 		}
280 		break;
281 
282 		case P_BUZZ_SEMI:
283 		{
284 			int f = i->buzz_offset >> 8;
285 			clamp(f, a, -99, 99);
286 			i->buzz_offset = (i->buzz_offset & 0xff) | f << 8;
287 		}
288 		break;
289 
290 		case P_PW:
291 
292 		clamp(i->pw, a*16, 0, 0x7ff);
293 
294 		break;
295 
296 		case P_1_4TH:
297 		flipbit(i->flags, MUS_INST_QUARTER_FREQ);
298 		break;
299 
300 		case P_VOLUME:
301 
302 		clamp(i->volume, a, 0, 255); // 255 = ~2x boost
303 
304 		break;
305 
306 		case P_PROGPERIOD:
307 
308 		clamp(i->prog_period, a, 0, 0xff);
309 
310 		break;
311 
312 		case P_SLIDESPEED:
313 
314 		clamp(i->slide_speed, a, 0, 0xff);
315 
316 		break;
317 
318 		case P_VIBDEPTH:
319 
320 		clamp(i->vibrato_depth, a, 0, 0xff);
321 
322 		break;
323 
324 		case P_VIBDELAY:
325 
326 		clamp(i->vib_delay, a, 0, 0xff);
327 
328 		break;
329 
330 		case P_VIBSHAPE:
331 
332 		clamp(i->vib_shape, a, 0, MUS_NUM_SHAPES - 1);
333 
334 		break;
335 
336 		case P_VIBSPEED:
337 
338 		clamp(i->vibrato_speed, a, 0, 0xff);
339 
340 		break;
341 
342 		case P_PWMDEPTH:
343 
344 		clamp(i->pwm_depth, a, 0, 0xff);
345 
346 		break;
347 
348 		case P_PWMSPEED:
349 
350 		clamp(i->pwm_speed, a, 0, 0xff);
351 
352 		break;
353 
354 		case P_PWMSHAPE:
355 
356 		clamp(i->pwm_shape, a, 0, MUS_NUM_SHAPES - 1);
357 
358 		break;
359 
360 		case P_RINGMOD:
361 
362 		flipbit(i->cydflags, CYD_CHN_ENABLE_RING_MODULATION);
363 
364 		break;
365 
366 		case P_SETPW:
367 
368 		flipbit(i->flags, MUS_INST_SET_PW);
369 
370 		break;
371 
372 		case P_SETCUTOFF:
373 
374 		flipbit(i->flags, MUS_INST_SET_CUTOFF);
375 
376 		break;
377 
378 		case P_INVVIB:
379 
380 		flipbit(i->flags, MUS_INST_INVERT_VIBRATO_BIT);
381 
382 		break;
383 
384 		case P_FILTER:
385 
386 		flipbit(i->cydflags, CYD_CHN_ENABLE_FILTER);
387 
388 		break;
389 
390 		case P_RINGMODSRC:
391 		{
392 			int x = (Uint8)(i->ring_mod+1);
393 			clamp(x, a, 0, MUS_MAX_CHANNELS);
394 			i->ring_mod = x-1;
395 		}
396 		break;
397 
398 		case P_CUTOFF:
399 
400 		clamp(i->cutoff, a*16, 0, 2047);
401 
402 		break;
403 
404 		case P_RESONANCE:
405 
406 		clamp(i->resonance, a, 0, 3);
407 
408 		break;
409 
410 		case P_FLTTYPE:
411 
412 		clamp(i->flttype, a, 0, FLT_TYPES - 1);
413 
414 		break;
415 
416 		case P_NORESTART:
417 
418 		flipbit(i->flags, MUS_INST_NO_PROG_RESTART);
419 
420 		break;
421 
422 		case P_MULTIOSC:
423 
424 		flipbit(i->flags, MUS_INST_MULTIOSC);
425 
426 		break;
427 
428 		case P_FM_MODULATION:
429 
430 		clamp(i->fm_modulation, a, 0, 0x7f);
431 
432 		break;
433 
434 		case P_FM_FEEDBACK:
435 
436 		clamp(i->fm_feedback, a, 0, 0x7);
437 
438 		break;
439 
440 		case P_FM_ATTACK:
441 
442 		clamp(i->fm_adsr.a, a, 0, 32 * ENVELOPE_SCALE - 1);
443 
444 		break;
445 
446 		case P_FM_DECAY:
447 
448 		clamp(i->fm_adsr.d, a, 0, 32 * ENVELOPE_SCALE - 1);
449 
450 		break;
451 
452 		case P_FM_SUSTAIN:
453 
454 		clamp(i->fm_adsr.s, a, 0, 31);
455 
456 		break;
457 
458 		case P_FM_RELEASE:
459 
460 		clamp(i->fm_adsr.r, a, 0, 32 * ENVELOPE_SCALE - 1);
461 
462 		break;
463 
464 		case P_FM_ENV_START:
465 
466 		clamp(i->fm_attack_start, a, 0, 31);
467 
468 		break;
469 
470 		case P_FM_WAVE:
471 
472 		flipbit(i->fm_flags, CYD_FM_ENABLE_WAVE);
473 
474 		break;
475 
476 		case P_FM_ENABLE:
477 
478 		flipbit(i->cydflags, CYD_CHN_ENABLE_FM);
479 
480 		break;
481 
482 		case P_FM_HARMONIC_CARRIER:
483 		{
484 			Uint8 carrier = (i->fm_harmonic >> 4);
485 			Uint8 modulator = i->fm_harmonic & 0xf;
486 
487 			clamp(carrier, a, 0, 15);
488 
489 			i->fm_harmonic = carrier << 4 | modulator;
490 		}
491 		break;
492 
493 		case P_FM_HARMONIC_MODULATOR:
494 		{
495 			Uint8 carrier = (i->fm_harmonic >> 4);
496 			Uint8 modulator = i->fm_harmonic & 0xf;
497 
498 			clamp(modulator, a, 0, 15);
499 
500 			i->fm_harmonic = carrier << 4 | modulator;
501 		}
502 		break;
503 
504 
505 		case P_FM_WAVE_ENTRY:
506 
507 		clamp(i->fm_wave, a, 0, CYD_WAVE_MAX_ENTRIES - 1);
508 
509 		break;
510 
511 
512 		default:
513 		break;
514 	}
515 
516 }
517 
518 
519 static int note_playing[MUS_MAX_CHANNELS] = {-1};
520 
521 
find_playing_note(int n)522 static int find_playing_note(int n)
523 {
524 	cyd_lock(&mused.cyd, 1);
525 
526 	for (int i = 0 ; i < MUS_MAX_CHANNELS && i < mused.cyd.n_channels ; ++i)
527 	{
528 		if (note_playing[i] == n && mused.mus.channel[i].instrument == &mused.song.instrument[mused.current_instrument])
529 		{
530 			cyd_lock(&mused.cyd, 0);
531 			return i;
532 		}
533 	}
534 
535 	cyd_lock(&mused.cyd, 0);
536 
537 	return -1;
538 }
539 
540 
play_note(int note)541 static void play_note(int note) {
542 	if (find_playing_note(note) == -1 && note < FREQ_TAB_SIZE)
543 	{
544 		int c = mus_trigger_instrument(&mused.mus, -1, &mused.song.instrument[mused.current_instrument], note << 8, CYD_PAN_CENTER);
545 		note_playing[c] = note;
546 
547 		mused.mus.song_track[c].extarp1 = 0;
548 		mused.mus.song_track[c].extarp2 = 0;
549 	}
550 }
551 
552 
stop_note(int note)553 static void stop_note(int note)
554 {
555 	int c;
556 	if ((c = find_playing_note(note)) != -1)
557 	{
558 		mus_release(&mused.mus, c);
559 		note_playing[c] = -1;
560 	}
561 }
562 
563 
play_the_jams(int sym,int chn,int state)564 static void play_the_jams(int sym, int chn, int state)
565 {
566 	if (sym == SDLK_SPACE && state == 0)
567 	{
568 		for (int i = 0 ; i < MUS_MAX_CHANNELS ; ++i)
569 			cyd_enable_gate(mused.mus.cyd, &mused.mus.cyd->channel[i], 0);
570 	}
571 	else
572 	{
573 		int note = find_note(sym, mused.octave);
574 		if (note != -1)
575 		{
576 			if (mused.flags & MULTIKEY_JAMMING)
577 			{
578 				if (state)
579 					play_note(note);
580 				else
581 					stop_note(note);
582 			}
583 			else
584 			{
585 				if (state == 1)
586 				{
587 					if (chn == -1 && !(mused.flags & MULTICHANNEL_PREVIEW))
588 						chn = 0;
589 
590 					int c = mus_trigger_instrument(&mused.mus, chn, &mused.song.instrument[mused.current_instrument], note << 8, CYD_PAN_CENTER);
591 
592 					mused.mus.song_track[c].extarp1 = 0;
593 					mused.mus.song_track[c].extarp2 = 0;
594 				}
595 			}
596 		}
597 	}
598 
599 }
600 
601 
wave_the_jams(int sym)602 static void wave_the_jams(int sym)
603 {
604 	if (sym == SDLK_SPACE)
605 	{
606 		for (int i = 0 ; i < MUS_MAX_CHANNELS ; ++i)
607 			cyd_enable_gate(mused.mus.cyd, &mused.mus.cyd->channel[i], 0);
608 	}
609 	else
610 	{
611 		int note = find_note(sym, mused.octave);
612 		if (note != -1)
613 		{
614 			static MusInstrument inst;
615 			mus_get_default_instrument(&inst);
616 			inst.wavetable_entry = mused.selected_wavetable;
617 			inst.cydflags &= ~WAVEFORMS;
618 			inst.cydflags |= CYD_CHN_WAVE_OVERRIDE_ENV | CYD_CHN_ENABLE_WAVE;
619 			inst.flags &= ~MUS_INST_DRUM;
620 			mus_trigger_instrument(&mused.mus, 0, &inst, note << 8, CYD_PAN_CENTER);
621 		}
622 	}
623 }
624 
625 
626 
edit_instrument_event(SDL_Event * e)627 void edit_instrument_event(SDL_Event *e)
628 {
629 	switch (e->type)
630 	{
631 		case SDL_KEYDOWN:
632 
633 		switch (e->key.keysym.sym)
634 		{
635 			case SDLK_RETURN:
636 			{
637 				if (mused.selected_param == P_NAME)
638 					set_edit_buffer(mused.song.instrument[mused.current_instrument].name, sizeof(mused.song.instrument[mused.current_instrument].name));
639 			}
640 			break;
641 
642 			case SDLK_DOWN:
643 			{
644 				++mused.selected_param;
645 
646 				if (mused.mode == EDITINSTRUMENT)
647 				{
648 					if (mused.selected_param >= P_PARAMS) mused.selected_param = P_PARAMS - 1;
649 				}
650 				else
651 				{
652 					if (mused.selected_param >= P_NAME) mused.selected_param = P_NAME;
653 				}
654 			}
655 			break;
656 
657 			case SDLK_UP:
658 			{
659 				--mused.selected_param;
660 
661 				if (mused.selected_param < 0) mused.selected_param = 0;
662 			}
663 			break;
664 
665 
666 
667 			case SDLK_RIGHT:
668 			{
669 				instrument_add_param(+1);
670 			}
671 			break;
672 
673 
674 			case SDLK_LEFT:
675 			{
676 				instrument_add_param(-1);
677 			}
678 			break;
679 
680 			default:
681 			{
682 				play_the_jams(e->key.keysym.sym, -1, 1);
683 			}
684 			break;
685 		}
686 
687 		break;
688 
689 		case SDL_KEYUP:
690 
691 			play_the_jams(e->key.keysym.sym, -1, 0);
692 
693 		break;
694 	}
695 }
696 
697 
gethex(int key)698 static int gethex(int key)
699 {
700 	if (key >= SDLK_0 && key <= SDLK_9)
701 	{
702 		return key - SDLK_0;
703 	}
704 	else if (key >= SDLK_KP_0 && key <= SDLK_KP_9)
705 	{
706 		return key - SDLK_KP_0;
707 	}
708 	else if (key >= SDLK_a && key <= SDLK_f)
709 	{
710 		return key - SDLK_a + 0xa;
711 	}
712 	else return -1;
713 }
714 
715 
getalphanum(const SDL_Keysym * keysym)716 static int getalphanum(const SDL_Keysym *keysym)
717 {
718 	int key = keysym->sym;
719 
720 	if (key >= SDLK_0 && key <= SDLK_9)
721 	{
722 		return key - SDLK_0;
723 	}
724 	else if (key >= SDLK_KP_0 && key <= SDLK_KP_9)
725 	{
726 		return key - SDLK_KP_0;
727 	}
728 	else if (!(keysym->mod & KMOD_SHIFT) && key >= SDLK_a && key <= SDLK_z)
729 	{
730 		return key - SDLK_a + 0xa;
731 	}
732 	else if ((keysym->mod & KMOD_SHIFT) && key >= SDLK_a && key <= SDLK_z)
733 	{
734 		return key - SDLK_a + 0xa + (SDLK_z - SDLK_a) + 1;
735 	}
736 	else return -1;
737 }
738 
739 
seqsort(const void * _a,const void * _b)740 static int seqsort(const void *_a, const void *_b)
741 {
742 	const MusSeqPattern *a = _a;
743 	const MusSeqPattern *b = _b;
744 	if (a->position > b->position) return 1;
745 	if (a->position < b->position) return -1;
746 
747 	return 0;
748 }
749 
750 
add_sequence(int channel,int position,int pattern,int offset)751 void add_sequence(int channel, int position, int pattern, int offset)
752 {
753 	if(mused.song.pattern[pattern].num_steps == 0)
754 		resize_pattern(&mused.song.pattern[pattern], mused.sequenceview_steps);
755 
756 	for (int i = 0 ; i < mused.song.num_sequences[channel] ; ++i)
757 		if (mused.song.sequence[channel][i].position == position)
758 		{
759 			mused.song.sequence[channel][i].pattern = pattern;
760 			mused.song.sequence[channel][i].note_offset = offset;
761 			return;
762 		}
763 
764 	if (mused.song.num_sequences[channel] >= NUM_SEQUENCES)
765 		return;
766 
767 	mused.song.sequence[channel][mused.song.num_sequences[channel]].position = position;
768 	mused.song.sequence[channel][mused.song.num_sequences[channel]].pattern = pattern;
769 	mused.song.sequence[channel][mused.song.num_sequences[channel]].note_offset = offset;
770 
771 	++mused.song.num_sequences[channel];
772 
773 	qsort(mused.song.sequence[channel], mused.song.num_sequences[channel], sizeof(mused.song.sequence[channel][0]), seqsort);
774 }
775 
776 
get_pattern_at(int channel,int position)777 Uint8 get_pattern_at(int channel, int position)
778 {
779 	for (int i = 0 ; i < mused.song.num_sequences[channel] ; ++i)
780 		if (mused.song.sequence[channel][i].position == position)
781 		{
782 			return mused.song.sequence[channel][i].pattern;
783 		}
784 
785 	return 0;
786 }
787 
788 
del_sequence(int first,int last,int track)789 void del_sequence(int first,int last,int track)
790 {
791 	if (mused.song.num_sequences[track] == 0) return;
792 
793 	for (int i = 0 ; i < mused.song.num_sequences[mused.current_sequencetrack] ; ++i)
794 		if (mused.song.sequence[track][i].position >= first && mused.song.sequence[track][i].position < last)
795 		{
796 			mused.song.sequence[track][i].position = 0xffff;
797 		}
798 
799 	qsort(mused.song.sequence[track], mused.song.num_sequences[track], sizeof(mused.song.sequence[track][0]), seqsort);
800 
801 	while (mused.song.num_sequences[track] > 0 && mused.song.sequence[track][mused.song.num_sequences[track]-1].position == 0xffff) --mused.song.num_sequences[track];
802 }
803 
804 
add_note_offset(int a)805 void add_note_offset(int a)
806 {
807 	{
808 		for (int i = (int)mused.song.num_sequences[mused.current_sequencetrack] - 1 ; i >= 0 ; --i)
809 		{
810 			if (mused.current_sequencepos >= mused.song.sequence[mused.current_sequencetrack][i].position &&
811 				mused.song.sequence[mused.current_sequencetrack][i].position + mused.song.pattern[mused.song.sequence[mused.current_sequencetrack][i].pattern].num_steps > mused.current_sequencepos)
812 			{
813 				mused.song.sequence[mused.current_sequencetrack][i].note_offset += a;
814 				break;
815 			}
816 		}
817 	}
818 }
819 
820 
update_sequence_slider(int d)821 static void update_sequence_slider(int d)
822 {
823 	int o = mused.current_sequencepos - mused.current_patternpos;
824 
825 	slider_move_position(&mused.current_sequencepos, &mused.sequence_position, &mused.sequence_slider_param, d);
826 
827 	if (!(mused.flags & SONG_PLAYING))
828 		mused.pattern_position = mused.current_patternpos = mused.current_sequencepos - o;
829 }
830 
831 
832 
update_pattern_slider(int d)833 static void update_pattern_slider(int d)
834 {
835 	/*if (mused.flags & CENTER_PATTERN_EDITOR)
836 	{
837 		mused.pattern_position = current_patternstep() + d;
838 
839 		if (mused.pattern_position < 0) mused.pattern_position += mused.song.pattern[current_pattern()].num_steps;
840 		if (mused.pattern_position >= mused.song.pattern[current_pattern()].num_steps) mused.pattern_position -= mused.song.pattern[current_pattern()].num_steps;
841 
842 		current_patternstep() = mused.pattern_position;
843 	}
844 	else */
845 	slider_move_position(&mused.current_patternpos, &mused.pattern_position, &mused.pattern_slider_param, d);
846 	mused.pattern_position = mused.current_patternpos;
847 
848 	if (!(mused.flags & SONG_PLAYING))
849 		mused.current_sequencepos = mused.current_patternpos - mused.current_patternpos % mused.sequenceview_steps;
850 }
851 
852 
update_position_sliders()853 void update_position_sliders()
854 {
855 	update_pattern_slider(0);
856 	update_sequence_slider(0);
857 }
858 
859 
update_horiz_sliders()860 void update_horiz_sliders()
861 {
862 	slider_move_position(&mused.current_sequencetrack, &mused.sequence_horiz_position, &mused.sequence_horiz_slider_param, 0);
863 	slider_move_position(&mused.current_sequencetrack, &mused.pattern_horiz_position, &mused.pattern_horiz_slider_param, 0);
864 }
865 
866 
sequence_event(SDL_Event * e)867 void sequence_event(SDL_Event *e)
868 {
869 	switch (e->type)
870 	{
871 		case SDL_KEYDOWN:
872 
873 		switch (e->key.keysym.sym)
874 		{
875 			case SDLK_SPACE:
876 			{
877 				if ((mused.flags & TOGGLE_EDIT_ON_STOP) || !(mused.flags & SONG_PLAYING))
878 						mused.flags ^= EDIT_MODE;
879 
880 				if (mused.flags & SONG_PLAYING) stop(0, 0, 0);
881 			}
882 			break;
883 
884 			case SDLK_RETURN:
885 			{
886 				if (mused.mode != EDITCLASSIC) change_mode(EDITPATTERN);
887 				else mused.focus = EDITPATTERN;
888 
889 				//mused.current_patternpos = mused.current_sequencepos;
890 
891 				update_position_sliders();
892 			}
893 			break;
894 
895 			case SDLK_LSHIFT:
896 			case SDLK_RSHIFT:
897 				begin_selection(mused.current_sequencepos);
898 			break;
899 
900 			case SDLK_PAGEDOWN:
901 			case SDLK_DOWN:
902 			{
903 				if (e->key.keysym.mod & KMOD_ALT)
904 				{
905 					snapshot(S_T_SEQUENCE);
906 					add_note_offset(-1);
907 					break;
908 				}
909 
910 				int steps = mused.sequenceview_steps;
911 				if (e->key.keysym.sym == SDLK_PAGEDOWN)
912 					steps *= 16;
913 				if (e->key.keysym.mod & KMOD_CTRL)
914 				{
915 					snapshot_cascade(S_T_SONGINFO, SI_LOOP, -1);
916 					if (e->key.keysym.mod & KMOD_SHIFT)
917 					{
918 						change_loop_point(MAKEPTR(steps), 0, 0);
919 					}
920 					else
921 					{
922 						change_song_length(MAKEPTR(steps), 0, 0);
923 					}
924 				}
925 				else
926 				{
927 					update_sequence_slider(steps);
928 				}
929 
930 				if (((e->key.keysym.mod & KMOD_SHIFT) && !(e->key.keysym.mod & KMOD_CTRL)) )
931 				{
932 					select_range(mused.current_sequencepos);
933 				}
934 			}
935 			break;
936 
937 			case SDLK_PAGEUP:
938 			case SDLK_UP:
939 			{
940 				if (e->key.keysym.mod & KMOD_ALT)
941 				{
942 					snapshot(S_T_SEQUENCE);
943 					add_note_offset(1);
944 					break;
945 				}
946 
947 				int steps = mused.sequenceview_steps;
948 				if (e->key.keysym.sym == SDLK_PAGEUP)
949 					steps *= 16;
950 
951 				if (e->key.keysym.mod & KMOD_CTRL)
952 				{
953 					snapshot_cascade(S_T_SONGINFO, SI_LOOP, -1);
954 					if (e->key.keysym.mod & KMOD_SHIFT)
955 					{
956 						change_loop_point(MAKEPTR(-steps), 0,0);
957 					}
958 					else
959 					{
960 						change_song_length(MAKEPTR(-steps), 0, 0);
961 					}
962 				}
963 				else
964 				{
965 					update_sequence_slider(-steps);
966 				}
967 
968 				if (((e->key.keysym.mod & KMOD_SHIFT) && !(e->key.keysym.mod & KMOD_CTRL)) )
969 				{
970 					select_range(mused.current_sequencepos);
971 				}
972 			}
973 			break;
974 
975 			case SDLK_INSERT:
976 			{
977 				snapshot(S_T_SEQUENCE);
978 
979 				for (int i = 0; i < mused.song.num_sequences[mused.current_sequencetrack] ; ++i)
980 				{
981 					if (mused.song.sequence[mused.current_sequencetrack][i].position >= mused.current_sequencepos)
982 						mused.song.sequence[mused.current_sequencetrack][i].position += mused.sequenceview_steps;
983 				}
984 			}
985 			break;
986 
987 			case SDLK_PERIOD:
988 			{
989 				snapshot(S_T_SEQUENCE);
990 
991 				del_sequence(mused.current_sequencepos, mused.current_sequencepos+mused.sequenceview_steps, mused.current_sequencetrack);
992 				if (mused.song.song_length > mused.current_sequencepos + mused.sequenceview_steps)
993 					mused.current_sequencepos += mused.sequenceview_steps;
994 			}
995 			break;
996 
997 			case SDLK_DELETE:
998 			{
999 				snapshot(S_T_SEQUENCE);
1000 
1001 				del_sequence(mused.current_sequencepos, mused.current_sequencepos+mused.sequenceview_steps, mused.current_sequencetrack);
1002 
1003 				if (!(mused.flags & DELETE_EMPTIES))
1004 				{
1005 					for (int i = 0; i < mused.song.num_sequences[mused.current_sequencetrack] ; ++i)
1006 					{
1007 						if (mused.song.sequence[mused.current_sequencetrack][i].position >= mused.current_sequencepos)
1008 							mused.song.sequence[mused.current_sequencetrack][i].position -= mused.sequenceview_steps;
1009 					}
1010 				}
1011 			}
1012 			break;
1013 
1014 			case SDLK_RIGHT:
1015 			{
1016 				if (e->key.keysym.mod & KMOD_CTRL)
1017 				{
1018 					change_seq_steps((void *)1, 0, 0);
1019 				}
1020 				else
1021 				{
1022 					if (mused.flags & EDIT_SEQUENCE_DIGITS)
1023 					{
1024 						if (mused.sequence_digit < 1)
1025 						{
1026 							++mused.sequence_digit;
1027 							break;
1028 						}
1029 					}
1030 
1031 					mused.sequence_digit = 0;
1032 					++mused.current_sequencetrack;
1033 					if (mused.current_sequencetrack >= mused.song.num_channels)
1034 						mused.current_sequencetrack = 0;
1035 
1036 					update_horiz_sliders();
1037 				}
1038 			}
1039 			break;
1040 
1041 			case SDLK_LEFT:
1042 			{
1043 				if (e->key.keysym.mod & KMOD_CTRL)
1044 				{
1045 					change_seq_steps((void *)-1, 0, 0);
1046 				}
1047 				else
1048 				{
1049 					if (mused.flags & EDIT_SEQUENCE_DIGITS)
1050 					{
1051 						if (mused.sequence_digit > 0)
1052 						{
1053 							--mused.sequence_digit;
1054 							break;
1055 						}
1056 					}
1057 
1058 					mused.sequence_digit = 0;
1059 					--mused.current_sequencetrack;
1060 					if (mused.current_sequencetrack < 0)
1061 						mused.current_sequencetrack = mused.song.num_channels - 1;
1062 
1063 					update_horiz_sliders();
1064 				}
1065 			}
1066 			break;
1067 
1068 			default:
1069 			{
1070 				if (mused.flags & EDIT_SEQUENCE_DIGITS)
1071 				{
1072 					int k;
1073 
1074 					if ((k = gethex(e->key.keysym.sym)) != -1)
1075 					{
1076 						snapshot(S_T_SEQUENCE);
1077 
1078 						Uint8 p = get_pattern_at(mused.current_sequencetrack, mused.current_sequencepos);
1079 
1080 						if (mused.sequence_digit == 0)
1081 							p = (p & (0x0f)) | (k << 4);
1082 						else
1083 							p = (p & (0xf0)) | k;
1084 
1085 						add_sequence(mused.current_sequencetrack, mused.current_sequencepos, p, 0);
1086 
1087 						if (mused.song.song_length > mused.current_sequencepos + mused.sequenceview_steps)
1088 						{
1089 							mused.current_sequencepos += mused.sequenceview_steps;
1090 							mused.current_patternpos += mused.sequenceview_steps;
1091 							update_position_sliders();
1092 						}
1093 					}
1094 				}
1095 				else
1096 				{
1097 					int p = getalphanum(&e->key.keysym);
1098 					if (p != -1 && p < NUM_PATTERNS)
1099 					{
1100 						snapshot(S_T_SEQUENCE);
1101 						add_sequence(mused.current_sequencetrack, mused.current_sequencepos, p, 0);
1102 						if (mused.song.song_length > mused.current_sequencepos + mused.sequenceview_steps)
1103 						{
1104 							mused.current_sequencepos += mused.sequenceview_steps;
1105 							mused.current_patternpos += mused.sequenceview_steps;
1106 							update_position_sliders();
1107 						}
1108 					}
1109 				}
1110 			}
1111 			break;
1112 		}
1113 
1114 		break;
1115 	}
1116 
1117 	//update_ghost_patterns();
1118 }
1119 
1120 
switch_track(int d)1121 static void switch_track(int d)
1122 {
1123 	mused.current_sequencetrack = (mused.current_sequencetrack + (int)mused.song.num_channels + d) % mused.song.num_channels;
1124 	update_horiz_sliders();
1125 	mused.sequence_digit = 0;
1126 }
1127 
1128 
write_note(int note)1129 static void write_note(int note)
1130 {
1131 	snapshot(S_T_PATTERN);
1132 
1133 	mused.song.pattern[current_pattern()].step[current_patternstep()].note = note;
1134 	mused.song.pattern[current_pattern()].step[current_patternstep()].instrument = mused.current_instrument;
1135 
1136 	update_pattern_slider(mused.note_jump);
1137 }
1138 
1139 
pattern_event(SDL_Event * e)1140 void pattern_event(SDL_Event *e)
1141 {
1142 	int last_param = PED_PARAMS - 1;
1143 	if (!viscol(PED_COMMAND1)) last_param = my_max(PED_COMMAND1 - 1, last_param);
1144 	else if (!viscol(PED_CTRL)) last_param = my_max(PED_CTRL - 1, last_param);
1145 	else if (!viscol(PED_VOLUME1)) last_param = my_max(PED_VOLUME1 - 1, last_param);
1146 	else if (!viscol(PED_INSTRUMENT1)) last_param = my_max(PED_INSTRUMENT1 - 1, last_param);
1147 
1148 	switch (e->type)
1149 	{
1150 		case SDL_KEYDOWN:
1151 
1152 		switch (e->key.keysym.sym)
1153 		{
1154 
1155 			case SDLK_RETURN:
1156 			{
1157 				if (e->key.keysym.mod & KMOD_CTRL)
1158 				{
1159 					Uint8 p = get_pattern_at(mused.current_sequencetrack, mused.current_sequencepos);
1160 					add_sequence(mused.current_sequencetrack, mused.current_patternpos, p, 0);
1161 				}
1162 				else
1163 				{
1164 					if (mused.mode != EDITCLASSIC) change_mode(EDITSEQUENCE);
1165 					else mused.focus = EDITSEQUENCE;
1166 
1167 					mused.flags &= ~SHOW_LOGO; // hide logo when jumping to sequence
1168 
1169 					mused.current_sequencepos = mused.current_patternpos;
1170 
1171 					update_position_sliders();
1172 				}
1173 			}
1174 			break;
1175 
1176 			case SDLK_LSHIFT:
1177 			case SDLK_RSHIFT:
1178 				begin_selection(mused.current_patternpos);
1179 			break;
1180 
1181 			case SDLK_END:
1182 			case SDLK_PAGEDOWN:
1183 			case SDLK_DOWN:
1184 			{
1185 				if (e->key.keysym.mod & KMOD_CTRL)
1186 				{
1187 					update_sequence_slider(mused.sequenceview_steps);
1188 				}
1189 				else
1190 				{
1191 					int steps = 1;
1192 					if (e->key.keysym.sym == SDLK_PAGEDOWN) steps = steps * 16;
1193 
1194 					if (get_current_pattern())
1195 					{
1196 						if (e->key.keysym.sym == SDLK_END) steps = get_current_pattern()->num_steps - current_patternstep() - 1;
1197 
1198 						if ((e->key.keysym.mod & KMOD_SHIFT) && get_current_pattern())
1199 							steps = my_min(steps, get_current_pattern()->num_steps - current_patternstep() - 1);
1200 					}
1201 
1202 					update_pattern_slider(steps);
1203 
1204 					if (e->key.keysym.mod & KMOD_SHIFT)
1205 					{
1206 						select_range(mused.current_patternpos);
1207 					}
1208 				}
1209 			}
1210 			break;
1211 
1212 			case SDLK_HOME:
1213 			case SDLK_PAGEUP:
1214 			case SDLK_UP:
1215 			{
1216 				if (e->key.keysym.mod & KMOD_CTRL)
1217 				{
1218 					update_sequence_slider(-mused.sequenceview_steps);
1219 				}
1220 				else
1221 				{
1222 
1223 					int steps = 1;
1224 					if (e->key.keysym.sym == SDLK_PAGEUP) steps = steps * 16;
1225 					if (e->key.keysym.sym == SDLK_HOME) steps = current_patternstep();
1226 
1227 					if (e->key.keysym.mod & KMOD_SHIFT)
1228 						steps = my_min(steps, current_patternstep());
1229 
1230 					update_pattern_slider(-steps);
1231 
1232 					if (e->key.keysym.mod & KMOD_SHIFT)
1233 					{
1234 						select_range(mused.current_patternpos);
1235 					}
1236 
1237 				}
1238 			}
1239 			break;
1240 
1241 			case SDLK_INSERT:
1242 			{
1243 				if (!(mused.flags & EDIT_MODE)) break;
1244 
1245 				if (get_current_step())
1246 				{
1247 					snapshot(S_T_PATTERN);
1248 
1249 					if ((e->key.keysym.mod & KMOD_ALT))
1250 					{
1251 						resize_pattern(get_current_pattern(), get_current_pattern()->num_steps + 1);
1252 						zero_step(&mused.song.pattern[current_pattern()].step[mused.song.pattern[current_pattern()].num_steps - 1]);
1253 						break;
1254 					}
1255 
1256 					for (int i = mused.song.pattern[current_pattern()].num_steps-1; i >= current_patternstep() ; --i)
1257 						memcpy(&mused.song.pattern[current_pattern()].step[i], &mused.song.pattern[current_pattern()].step[i-1], sizeof(mused.song.pattern[current_pattern()].step[0]));
1258 
1259 					zero_step(&mused.song.pattern[current_pattern()].step[current_patternstep()]);
1260 				}
1261 			}
1262 			break;
1263 
1264 			case SDLK_BACKSPACE:
1265 			case SDLK_DELETE:
1266 			{
1267 				if (!(mused.flags & EDIT_MODE)) break;
1268 
1269 				if (get_current_step())
1270 				{
1271 
1272 					if (e->key.keysym.sym == SDLK_BACKSPACE)
1273 					{
1274 						if (current_patternstep() > 0) update_pattern_slider(-1);
1275 						else break;
1276 					}
1277 
1278 					snapshot(S_T_PATTERN);
1279 
1280 					if ((e->key.keysym.mod & KMOD_ALT))
1281 					{
1282 						if (mused.song.pattern[current_pattern()].num_steps > 1)
1283 							resize_pattern(&mused.song.pattern[current_pattern()], mused.song.pattern[current_pattern()].num_steps - 1);
1284 
1285 						if (current_patternstep() >= mused.song.pattern[current_pattern()].num_steps) --mused.current_patternpos;
1286 						break;
1287 					}
1288 
1289 					if (!(mused.flags & DELETE_EMPTIES) || e->key.keysym.sym == SDLK_BACKSPACE)
1290 					{
1291 						for (int i = current_patternstep()  ; i < mused.song.pattern[current_pattern()].num_steps ; ++i)
1292 							memcpy(&mused.song.pattern[current_pattern()].step[i], &mused.song.pattern[current_pattern()].step[i+1], sizeof(mused.song.pattern[current_pattern()].step[0]));
1293 
1294 						zero_step(&mused.song.pattern[current_pattern()].step[mused.song.pattern[current_pattern()].num_steps - 1]);
1295 
1296 						if (current_patternstep() >= mused.song.pattern[current_pattern()].num_steps) --mused.current_patternpos;
1297 					}
1298 					else
1299 					{
1300 						if (e->key.keysym.mod & KMOD_SHIFT)
1301 						{
1302 							zero_step(&mused.song.pattern[current_pattern()].step[current_patternstep()]);
1303 						}
1304 						else
1305 						{
1306 							switch (mused.current_patternx)
1307 							{
1308 								case PED_NOTE:
1309 									mused.song.pattern[current_pattern()].step[current_patternstep()].note = MUS_NOTE_NONE;
1310 									mused.song.pattern[current_pattern()].step[current_patternstep()].instrument = MUS_NOTE_NO_INSTRUMENT;
1311 									break;
1312 
1313 								case PED_INSTRUMENT1:
1314 								case PED_INSTRUMENT2:
1315 									mused.song.pattern[current_pattern()].step[current_patternstep()].instrument = MUS_NOTE_NO_INSTRUMENT;
1316 									break;
1317 
1318 								case PED_VOLUME1:
1319 								case PED_VOLUME2:
1320 									mused.song.pattern[current_pattern()].step[current_patternstep()].volume = MUS_NOTE_NO_VOLUME;
1321 									break;
1322 
1323 								case PED_LEGATO:
1324 								case PED_SLIDE:
1325 								case PED_VIB:
1326 									mused.song.pattern[current_pattern()].step[current_patternstep()].ctrl = 0;
1327 									break;
1328 
1329 								case PED_COMMAND1:
1330 								case PED_COMMAND2:
1331 								case PED_COMMAND3:
1332 								case PED_COMMAND4:
1333 									mused.song.pattern[current_pattern()].step[current_patternstep()].command = 0;
1334 									break;
1335 							}
1336 						}
1337 
1338 						update_pattern_slider(mused.note_jump);
1339 					}
1340 				}
1341 			}
1342 			break;
1343 
1344 			case SDLK_RIGHT:
1345 			{
1346 				if (e->key.keysym.mod & KMOD_CTRL)
1347 				{
1348 						int x = mused.current_patternx;
1349 
1350 						switch_track(+1);
1351 
1352 						if (e->key.keysym.mod & KMOD_SHIFT)
1353 							mused.current_patternx = x;
1354 				}
1355 				else
1356 				{
1357 					++mused.current_patternx;
1358 
1359 					if (!viscol(mused.current_patternx) && (mused.current_patternx == PED_INSTRUMENT1 || mused.current_patternx == PED_INSTRUMENT2))
1360 						mused.current_patternx = PED_VOLUME1;
1361 
1362 					if (!viscol(mused.current_patternx) && (mused.current_patternx == PED_VOLUME1 || mused.current_patternx == PED_VOLUME2))
1363 						mused.current_patternx = PED_CTRL;
1364 
1365 					if (!viscol(mused.current_patternx) && (mused.current_patternx >= PED_CTRL && mused.current_patternx < PED_COMMAND1))
1366 						mused.current_patternx = PED_COMMAND1;
1367 
1368 					if (!viscol(mused.current_patternx) && (mused.current_patternx >= PED_COMMAND1 && mused.current_patternx <= PED_COMMAND4))
1369 						mused.current_patternx = PED_PARAMS;
1370 
1371 					if (mused.current_patternx >= PED_PARAMS)
1372 					{
1373 						mused.current_patternx = 0;
1374 						switch_track(+1);
1375 					}
1376 				}
1377 			}
1378 			break;
1379 
1380 			case SDLK_LEFT:
1381 			{
1382 				if (e->key.keysym.mod & KMOD_CTRL)
1383 				{
1384 					int x = mused.current_patternx;
1385 
1386 					switch_track(-1);
1387 
1388 					if (e->key.keysym.mod & KMOD_SHIFT)
1389 						mused.current_patternx = x;
1390 				}
1391 				else
1392 				{
1393 					--mused.current_patternx;
1394 
1395 					if (mused.current_patternx < 0)
1396 					{
1397 							mused.current_patternx = PED_PARAMS - 1;
1398 
1399 							switch_track(-1);
1400 					}
1401 
1402 					if (!viscol(mused.current_patternx) && (mused.current_patternx >= PED_COMMAND1 && mused.current_patternx <= PED_COMMAND4))
1403 						mused.current_patternx = PED_VIB;
1404 
1405 					if (!viscol(mused.current_patternx) && (mused.current_patternx < PED_COMMAND1 && mused.current_patternx > PED_VOLUME2))
1406 						mused.current_patternx = PED_VOLUME2;
1407 
1408 					if (!viscol(mused.current_patternx) && (mused.current_patternx == PED_VOLUME1 || mused.current_patternx == PED_VOLUME2))
1409 						mused.current_patternx = PED_INSTRUMENT2;
1410 
1411 					if (!viscol(mused.current_patternx) && (mused.current_patternx == PED_INSTRUMENT1 || mused.current_patternx == PED_INSTRUMENT2))
1412 						mused.current_patternx = PED_NOTE;
1413 
1414 					if (mused.current_patternx < 0)
1415 					{
1416 						if (mused.single_pattern_edit)
1417 						{
1418 							mused.current_patternx = 0;
1419 						}
1420 						else
1421 						{
1422 							mused.current_patternx = last_param;
1423 							switch_track(-1);
1424 						}
1425 					}
1426 				}
1427 			}
1428 			break;
1429 
1430 			default:
1431 			{
1432 				if (e->key.keysym.sym == SDLK_SPACE)
1433 				{
1434 					if ((mused.flags & TOGGLE_EDIT_ON_STOP) || !(mused.flags & SONG_PLAYING))
1435 						mused.flags ^= EDIT_MODE;
1436 
1437 					if (mused.flags & SONG_PLAYING) stop(0, 0, 0);
1438 
1439 					play_the_jams(e->key.keysym.sym, -1, 1);
1440 				}
1441 
1442 				if (!(mused.flags & EDIT_MODE))
1443 				{
1444 					/* still has to play a note */
1445 
1446 					if (mused.current_patternx == PED_NOTE)
1447 					{
1448 						play_the_jams(e->key.keysym.sym, mused.current_sequencetrack, 1);
1449 					}
1450 
1451 					break;
1452 				}
1453 
1454 				if (get_current_step())
1455 				{
1456 					if (mused.current_patternx == PED_NOTE)
1457 					{
1458 						if (e->key.keysym.sym == SDLK_PERIOD)
1459 						{
1460 							mused.song.pattern[current_pattern()].step[current_patternstep()].note = MUS_NOTE_NONE;
1461 
1462 							snapshot(S_T_PATTERN);
1463 
1464 							update_pattern_slider(mused.note_jump);
1465 						}
1466 						else if (e->key.keysym.sym == SDLK_1)
1467 						{
1468 							mused.song.pattern[current_pattern()].step[current_patternstep()].note = MUS_NOTE_RELEASE;
1469 
1470 							snapshot(S_T_PATTERN);
1471 
1472 							update_pattern_slider(mused.note_jump);
1473 						}
1474 						else
1475 						{
1476 							int note = find_note(e->key.keysym.sym, mused.octave);
1477 							if (note != -1)
1478 							{
1479 								play_the_jams(e->key.keysym.sym, mused.current_sequencetrack, 1);
1480 								write_note(note);
1481 							}
1482 						}
1483 					}
1484 					else if (mused.current_patternx == PED_INSTRUMENT1 || mused.current_patternx == PED_INSTRUMENT2)
1485 					{
1486 						if (e->key.keysym.sym == SDLK_PERIOD)
1487 						{
1488 							mused.song.pattern[current_pattern()].step[current_patternstep()].instrument = MUS_NOTE_NO_INSTRUMENT;
1489 
1490 							snapshot(S_T_PATTERN);
1491 
1492 							update_pattern_slider(mused.note_jump);
1493 						}
1494 						else if (gethex(e->key.keysym.sym) != -1)
1495 						{
1496 							if (mused.song.num_instruments > 0)
1497 							{
1498 								Uint8 inst = mused.song.pattern[current_pattern()].step[current_patternstep()].instrument;
1499 								if (inst == MUS_NOTE_NO_INSTRUMENT) inst = 0;
1500 
1501 								switch (mused.current_patternx)
1502 								{
1503 									case PED_INSTRUMENT1:
1504 									inst = (inst & 0x0f) | ((gethex(e->key.keysym.sym) << 4) & 0xf0);
1505 									break;
1506 
1507 									case PED_INSTRUMENT2:
1508 									inst = (inst & 0xf0) | gethex(e->key.keysym.sym);
1509 									break;
1510 								}
1511 
1512 								if (inst > (mused.song.num_instruments-1)) inst = (mused.song.num_instruments-1);
1513 
1514 								snapshot(S_T_PATTERN);
1515 
1516 								mused.song.pattern[current_pattern()].step[current_patternstep()].instrument = inst;
1517 								mused.current_instrument = inst % mused.song.num_instruments;
1518 							}
1519 
1520 							update_pattern_slider(mused.note_jump);
1521 						}
1522 					}
1523 					else if (mused.current_patternx == PED_VOLUME1 || mused.current_patternx == PED_VOLUME2)
1524 					{
1525 						switch (e->key.keysym.sym)
1526 						{
1527 							case  SDLK_PERIOD:
1528 								snapshot(S_T_PATTERN);
1529 
1530 								mused.song.pattern[current_pattern()].step[current_patternstep()].volume = MUS_NOTE_NO_VOLUME;
1531 
1532 								update_pattern_slider(mused.note_jump);
1533 								break;
1534 
1535 							default:
1536 								if (mused.current_patternx == PED_VOLUME1)
1537 								{
1538 									int cmd = 0;
1539 
1540 									switch (e->key.keysym.sym)
1541 									{
1542 										case SDLK_u: cmd = MUS_NOTE_VOLUME_FADE_UP; break;
1543 										case SDLK_d: cmd = MUS_NOTE_VOLUME_FADE_DN; break;
1544 										case SDLK_p: cmd = MUS_NOTE_VOLUME_SET_PAN; break;
1545 										case SDLK_l: cmd = MUS_NOTE_VOLUME_PAN_LEFT; break;
1546 										case SDLK_r: cmd = MUS_NOTE_VOLUME_PAN_RIGHT; break;
1547 										default: break;
1548 									}
1549 
1550 									snapshot(S_T_PATTERN);
1551 
1552 									if (cmd != 0)
1553 									{
1554 										if (mused.song.pattern[current_pattern()].step[current_patternstep()].volume == MUS_NOTE_NO_VOLUME)
1555 											mused.song.pattern[current_pattern()].step[current_patternstep()].volume = 0;
1556 
1557 										if (mused.current_patternx == PED_VOLUME1)
1558 											mused.song.pattern[current_pattern()].step[current_patternstep()].volume =
1559 												(mused.song.pattern[current_pattern()].step[current_patternstep()].volume & 0xf)
1560 												| cmd;
1561 
1562 										update_pattern_slider(mused.note_jump);
1563 
1564 										break;
1565 									}
1566 								}
1567 
1568 								if (gethex(e->key.keysym.sym) != -1)
1569 								{
1570 									Uint8 vol = mused.song.pattern[current_pattern()].step[current_patternstep()].volume;
1571 									if ((vol == MUS_NOTE_NO_VOLUME)) vol = 0;
1572 
1573 									switch (mused.current_patternx)
1574 									{
1575 										case PED_VOLUME1:
1576 										vol = (vol & 0x0f) | ((gethex(e->key.keysym.sym) << 4) & 0xf0);
1577 										break;
1578 
1579 										case PED_VOLUME2:
1580 										vol = (vol & 0xf0) | gethex(e->key.keysym.sym);
1581 										break;
1582 									}
1583 
1584 									snapshot(S_T_PATTERN);
1585 
1586 									if ((vol & 0xf0) != MUS_NOTE_VOLUME_FADE_UP &&
1587 										(vol & 0xf0) != MUS_NOTE_VOLUME_FADE_DN &&
1588 										(vol & 0xf0) != MUS_NOTE_VOLUME_PAN_LEFT &&
1589 										(vol & 0xf0) != MUS_NOTE_VOLUME_PAN_RIGHT &&
1590 										(vol & 0xf0) != MUS_NOTE_VOLUME_SET_PAN)
1591 										mused.song.pattern[current_pattern()].step[current_patternstep()].volume = my_min(MAX_VOLUME, vol);
1592 									else mused.song.pattern[current_pattern()].step[current_patternstep()].volume = vol;
1593 
1594 									update_pattern_slider(mused.note_jump);
1595 								}
1596 								break;
1597 						}
1598 					}
1599 					else if (mused.current_patternx >= PED_COMMAND1 && mused.current_patternx <= PED_COMMAND4)
1600 					{
1601 						if (gethex(e->key.keysym.sym) != -1)
1602 						{
1603 							Uint16 inst = mused.song.pattern[current_pattern()].step[current_patternstep()].command;
1604 
1605 							switch (mused.current_patternx)
1606 							{
1607 								case PED_COMMAND1:
1608 								inst = (inst & 0x0fff) | ((gethex(e->key.keysym.sym) << 12) & 0xf000);
1609 								break;
1610 
1611 								case PED_COMMAND2:
1612 								inst = (inst & 0xf0ff) | ((gethex(e->key.keysym.sym) << 8) & 0x0f00);
1613 								break;
1614 
1615 								case PED_COMMAND3:
1616 								inst = (inst & 0xff0f) | ((gethex(e->key.keysym.sym) << 4) & 0x00f0);
1617 								break;
1618 
1619 								case PED_COMMAND4:
1620 								inst = (inst & 0xfff0) | gethex(e->key.keysym.sym);
1621 								break;
1622 							}
1623 
1624 							snapshot(S_T_PATTERN);
1625 
1626 							mused.song.pattern[current_pattern()].step[current_patternstep()].command = validate_command(inst) & 0x7fff;
1627 
1628 							update_pattern_slider(mused.note_jump);
1629 						}
1630 					}
1631 					else if (mused.current_patternx >= PED_CTRL && mused.current_patternx < PED_COMMAND1)
1632 					{
1633 						if (e->key.keysym.sym == SDLK_PERIOD || e->key.keysym.sym == SDLK_0)
1634 						{
1635 							snapshot(S_T_PATTERN);
1636 
1637 							mused.song.pattern[current_pattern()].step[current_patternstep()].ctrl &= ~(MUS_CTRL_BIT << (mused.current_patternx - PED_CTRL));
1638 
1639 							update_pattern_slider(mused.note_jump);
1640 						}
1641 						if (e->key.keysym.sym == SDLK_1)
1642 						{
1643 							snapshot(S_T_PATTERN);
1644 
1645 							mused.song.pattern[current_pattern()].step[current_patternstep()].ctrl |= (MUS_CTRL_BIT << (mused.current_patternx - PED_CTRL));
1646 
1647 							update_pattern_slider(mused.note_jump);
1648 						}
1649 					}
1650 				}
1651 			}
1652 			break;
1653 		}
1654 
1655 		break;
1656 
1657 		case SDL_KEYUP:
1658 
1659 			play_the_jams(e->key.keysym.sym, -1, 0);
1660 
1661 		break;
1662 	}
1663 }
1664 
edit_program_event(SDL_Event * e)1665 void edit_program_event(SDL_Event *e)
1666 {
1667 	switch (e->type)
1668 	{
1669 		case SDL_KEYDOWN:
1670 
1671 		switch (e->key.keysym.sym)
1672 		{
1673 			case SDLK_LSHIFT:
1674 			case SDLK_RSHIFT:
1675 				begin_selection(mused.current_program_step);
1676 			break;
1677 
1678 			case SDLK_PERIOD:
1679 				snapshot(S_T_INSTRUMENT);
1680 
1681 				mused.song.instrument[mused.current_instrument].program[mused.current_program_step] = MUS_FX_NOP;
1682 			break;
1683 
1684 			case SDLK_SPACE:
1685 			{
1686 				snapshot(S_T_INSTRUMENT);
1687 
1688 				if ((mused.song.instrument[mused.current_instrument].program[mused.current_program_step] & 0xf000) != 0xf000)
1689 					mused.song.instrument[mused.current_instrument].program[mused.current_program_step] ^= 0x8000;
1690 			}
1691 			break;
1692 
1693 			case SDLK_RETURN:
1694 			{
1695 				MusInstrument *inst = &mused.song.instrument[mused.current_instrument];
1696 				Uint16 *param = &inst->program[mused.current_program_step];
1697 				*param = validate_command(*param);
1698 			}
1699 			break;
1700 
1701 			case SDLK_PAGEDOWN:
1702 			case SDLK_DOWN:
1703 			{
1704 				MusInstrument *inst = &mused.song.instrument[mused.current_instrument];
1705 				Uint16 *param = &inst->program[mused.current_program_step];
1706 				*param = validate_command(*param);
1707 
1708 				int steps = 1;
1709 				if (e->key.keysym.sym == SDLK_PAGEDOWN) steps *= 16;
1710 
1711 				slider_move_position(&mused.current_program_step, &mused.program_position, &mused.program_slider_param, steps);
1712 
1713 				if (e->key.keysym.mod & KMOD_SHIFT)
1714 				{
1715 					select_range(mused.current_program_step);
1716 				}
1717 			}
1718 			break;
1719 
1720 			case SDLK_PAGEUP:
1721 			case SDLK_UP:
1722 			{
1723 				MusInstrument *inst = &mused.song.instrument[mused.current_instrument];
1724 				Uint16 *param = &inst->program[mused.current_program_step];
1725 				*param = validate_command(*param);
1726 
1727 				int steps = 1;
1728 				if (e->key.keysym.sym == SDLK_PAGEUP) steps *= 16;
1729 
1730 				slider_move_position(&mused.current_program_step, &mused.program_position, &mused.program_slider_param, -steps);
1731 
1732 				if (e->key.keysym.mod & KMOD_SHIFT)
1733 				{
1734 					select_range(mused.current_program_step);
1735 				}
1736 			}
1737 			break;
1738 
1739 			case SDLK_INSERT:
1740 			{
1741 				snapshot(S_T_INSTRUMENT);
1742 				for (int i = MUS_PROG_LEN-1; i > mused.current_program_step ; --i)
1743 					mused.song.instrument[mused.current_instrument].program[i] = mused.song.instrument[mused.current_instrument].program[i-1];
1744 				mused.song.instrument[mused.current_instrument].program[mused.current_program_step] = MUS_FX_NOP;
1745 			}
1746 			break;
1747 
1748 			case SDLK_BACKSPACE:
1749 			case SDLK_DELETE:
1750 			{
1751 				snapshot(S_T_INSTRUMENT);
1752 				if (e->key.keysym.sym == SDLK_BACKSPACE)
1753 				{
1754 					if (mused.current_program_step > 0) slider_move_position(&mused.current_program_step, &mused.program_position, &mused.program_slider_param, -1);
1755 					else break;
1756 				}
1757 
1758 				if (!(mused.flags & DELETE_EMPTIES) || e->key.keysym.sym == SDLK_BACKSPACE)
1759 				{
1760 					for (int i = mused.current_program_step  ; i < MUS_PROG_LEN-1 ; ++i)
1761 						mused.song.instrument[mused.current_instrument].program[i] = mused.song.instrument[mused.current_instrument].program[i+1];
1762 					mused.song.instrument[mused.current_instrument].program[MUS_PROG_LEN-1] = MUS_FX_NOP;
1763 				}
1764 				else
1765 				{
1766 					mused.song.instrument[mused.current_instrument].program[mused.current_program_step] = MUS_FX_NOP;
1767 					++mused.current_program_step;
1768 					if (mused.current_program_step >= MUS_PROG_LEN)
1769 						mused.current_program_step = MUS_PROG_LEN - 1;
1770 				}
1771 			}
1772 			break;
1773 
1774 			case SDLK_RIGHT:
1775 			{
1776 				clamp(mused.editpos,+1,0,3);
1777 
1778 			}
1779 			break;
1780 
1781 			case SDLK_LEFT:
1782 			{
1783 				clamp(mused.editpos,-1,0,3);
1784 			}
1785 			break;
1786 
1787 			default:
1788 			{
1789 				int v = gethex(e->key.keysym.sym);
1790 
1791 				if (v != -1)
1792 				{
1793 					snapshot(S_T_INSTRUMENT);
1794 					editparambox(v);
1795 				}
1796 			}
1797 			break;
1798 		}
1799 
1800 		break;
1801 
1802 		case SDL_KEYUP:
1803 
1804 			play_the_jams(e->key.keysym.sym, -1, 0);
1805 
1806 		break;
1807 	}
1808 }
1809 
1810 
edit_text(SDL_Event * e)1811 void edit_text(SDL_Event *e)
1812 {
1813 	int r = generic_edit_text(e, mused.edit_buffer, mused.edit_buffer_size, &mused.editpos);
1814 
1815 	if (r == -1)
1816 	{
1817 		strcpy(mused.edit_buffer, mused.edit_backup_buffer);
1818 		change_mode(mused.prev_mode);
1819 	}
1820 	else if (r == 1)
1821 	{
1822 		change_mode(mused.prev_mode);
1823 	}
1824 }
1825 
1826 
set_room_size(int fx,int size,int vol,int dec)1827 void set_room_size(int fx, int size, int vol, int dec)
1828 {
1829 	snapshot(S_T_FX);
1830 
1831 	int min_delay = 5;
1832 	int ms = (CYDRVB_SIZE - min_delay) * size / 64;
1833 
1834 	if (mused.fx_room_ticks)
1835 	{
1836 		ms = 1000 * size / 16 * mused.song.song_speed / mused.song.song_rate;
1837 		min_delay = ms;
1838 	}
1839 
1840 	int low = CYDRVB_LOW_LIMIT + 300; // +30 dB
1841 
1842 	for (int i = 0 ; i < CYDRVB_TAPS ;++i)
1843 	{
1844 		int p, g, e = 1;
1845 
1846 		if (mused.fx_room_ticks)
1847 		{
1848 			p = i * ms + min_delay;
1849 			g = low - low * pow(1.0 - (double)p / CYDRVB_SIZE, (double)dec / 3) * vol / 16;
1850 		}
1851 		else
1852 		{
1853 			p = rnd(i * ms / CYDRVB_TAPS, (i + 1) * ms / CYDRVB_TAPS) + min_delay;
1854 			g = low - low * pow(1.0 - (double)my_min(p, ms - 1) / ms, (double)dec / 3) * vol / 16;
1855 		}
1856 
1857 		if (p >= CYDRVB_SIZE)
1858 		{
1859 			p = CYDRVB_SIZE - 1;
1860 			e = 0;
1861 		}
1862 
1863 		mused.song.fx[fx].rvb.tap[i].delay = p;
1864 		mused.song.fx[fx].rvb.tap[i].gain = g;
1865 		mused.song.fx[fx].rvb.tap[i].flags = e;
1866 	}
1867 
1868 	mus_set_fx(&mused.mus, &mused.song);
1869 }
1870 
1871 
fx_add_param(int d)1872 void fx_add_param(int d)
1873 {
1874 	if (d < 0) d = -1; else if (d > 0) d = 1;
1875 
1876 	if (SDL_GetModState() & KMOD_SHIFT)
1877 	{
1878 		switch (mused.edit_reverb_param)
1879 		{
1880 			case R_DELAY:
1881 				if (mused.flags & SHOW_DELAY_IN_TICKS)
1882 					d *= (1000 / (float)mused.song.song_rate) * mused.song.song_speed;
1883 				else
1884 					d *= 100;
1885 				break;
1886 
1887 			default:
1888 				d *= 10;
1889 				break;
1890 		}
1891 	}
1892 
1893 	snapshot_cascade(S_T_FX, mused.fx_bus, mused.edit_reverb_param);
1894 
1895 	switch (mused.edit_reverb_param)
1896 	{
1897 		case R_FX_BUS:
1898 		{
1899 			clamp(mused.fx_bus, d, 0, CYD_MAX_FX_CHANNELS - 1);
1900 		}
1901 		break;
1902 
1903 		case R_CRUSH:
1904 		{
1905 			flipbit(mused.song.fx[mused.fx_bus].flags, CYDFX_ENABLE_CRUSH);
1906 		}
1907 		break;
1908 
1909 		case R_CRUSHDITHER:
1910 		{
1911 			flipbit(mused.song.fx[mused.fx_bus].flags, CYDFX_ENABLE_CRUSH_DITHER);
1912 		}
1913 		break;
1914 
1915 		case R_CRUSHGAIN:
1916 		{
1917 			clamp(mused.song.fx[mused.fx_bus].crushex.gain, d, 0, 128);
1918 			mus_set_fx(&mused.mus, &mused.song);
1919 		}
1920 		break;
1921 
1922 		case R_CRUSHBITS:
1923 		{
1924 			clamp(mused.song.fx[mused.fx_bus].crush.bit_drop, d, 0, 15);
1925 			mus_set_fx(&mused.mus, &mused.song);
1926 		}
1927 		break;
1928 
1929 		case R_CRUSHDOWNSAMPLE:
1930 		{
1931 			clamp(mused.song.fx[mused.fx_bus].crushex.downsample, d, 0, 64);
1932 			mus_set_fx(&mused.mus, &mused.song);
1933 		}
1934 		break;
1935 
1936 		case R_CHORUS:
1937 		{
1938 			flipbit(mused.song.fx[mused.fx_bus].flags, CYDFX_ENABLE_CHORUS);
1939 		}
1940 		break;
1941 
1942 		case R_MINDELAY:
1943 		{
1944 			clamp(mused.song.fx[mused.fx_bus].chr.min_delay, d, 0, mused.song.fx[mused.fx_bus].chr.max_delay);
1945 			clamp(mused.song.fx[mused.fx_bus].chr.min_delay, 0, mused.song.fx[mused.fx_bus].chr.min_delay, 255);
1946 		}
1947 		break;
1948 
1949 		case R_MAXDELAY:
1950 		{
1951 			clamp(mused.song.fx[mused.fx_bus].chr.max_delay, d, mused.song.fx[mused.fx_bus].chr.min_delay, 255);
1952 			clamp(mused.song.fx[mused.fx_bus].chr.min_delay, 0, 0, mused.song.fx[mused.fx_bus].chr.max_delay);
1953 		}
1954 		break;
1955 
1956 		case R_SEPARATION:
1957 		{
1958 			clamp(mused.song.fx[mused.fx_bus].chr.sep, d, 0, 64);
1959 		}
1960 		break;
1961 
1962 		case R_RATE:
1963 		{
1964 			clamp(mused.song.fx[mused.fx_bus].chr.rate, d, 0, 255);
1965 		}
1966 		break;
1967 
1968 		case R_PITCH_INACCURACY:
1969 		{
1970 			clamp(mused.song.pitch_inaccuracy, d, 0, 12);
1971 		}
1972 		break;
1973 
1974 		case R_MULTIPLEX:
1975 		{
1976 			flipbit(mused.song.fx[mused.fx_bus].flags, MUS_ENABLE_MULTIPLEX);
1977 		}
1978 		break;
1979 
1980 		case R_MULTIPLEX_PERIOD:
1981 		{
1982 			clamp(mused.song.multiplex_period, d, 0, 255);
1983 		}
1984 		break;
1985 
1986 		case R_ENABLE:
1987 		{
1988 			flipbit(mused.song.fx[mused.fx_bus].flags, CYDFX_ENABLE_REVERB);
1989 		}
1990 		break;
1991 
1992 		case R_ROOMSIZE:
1993 		{
1994 			clamp(mused.fx_room_size, d, 1, 64);
1995 		}
1996 		break;
1997 
1998 		case R_ROOMDECAY:
1999 		{
2000 			clamp(mused.fx_room_dec, d, 1, 9);
2001 		}
2002 		break;
2003 
2004 		case R_ROOMVOL:
2005 		{
2006 			clamp(mused.fx_room_vol, d, 1, 16);
2007 		}
2008 		break;
2009 
2010 		case R_SNAPTICKS:
2011 		{
2012 			flipbit(mused.fx_room_ticks, 1);
2013 		}
2014 		break;
2015 
2016 		case R_TAP:
2017 		{
2018 			clamp(mused.fx_tap, d, 0, CYDRVB_TAPS - 1);
2019 		}
2020 		break;
2021 
2022 		case R_DELAY:
2023 		{
2024 			clamp(mused.song.fx[mused.fx_bus].rvb.tap[mused.fx_tap].delay, d * 1, 0, CYDRVB_SIZE - 1);
2025 			mus_set_fx(&mused.mus, &mused.song);
2026 		}
2027 		break;
2028 
2029 		case R_GAIN:
2030 		{
2031 			clamp(mused.song.fx[mused.fx_bus].rvb.tap[mused.fx_tap].gain, d * 1, CYDRVB_LOW_LIMIT, 0);
2032 			mus_set_fx(&mused.mus, &mused.song);
2033 		}
2034 		break;
2035 
2036 		case R_PANNING:
2037 		{
2038 			clamp(mused.song.fx[mused.fx_bus].rvb.tap[mused.fx_tap].panning, d * 8, CYD_PAN_LEFT, CYD_PAN_RIGHT);
2039 			mused.song.fx[mused.fx_bus].rvb.tap[mused.fx_tap].panning &= ~7;
2040 			mus_set_fx(&mused.mus, &mused.song);
2041 		}
2042 		break;
2043 
2044 		case R_TAPENABLE:
2045 		{
2046 			flipbit(mused.song.fx[mused.fx_bus].rvb.tap[mused.fx_tap].flags, 1);
2047 		}
2048 		break;
2049 	}
2050 
2051 }
2052 
2053 
fx_event(SDL_Event * e)2054 void fx_event(SDL_Event *e)
2055 {
2056 	switch (e->type)
2057 	{
2058 		case SDL_KEYDOWN:
2059 
2060 		switch (e->key.keysym.sym)
2061 		{
2062 			case SDLK_DOWN:
2063 			{
2064 				++mused.edit_reverb_param;
2065 				if (mused.edit_reverb_param >= R_N_PARAMS) mused.edit_reverb_param = R_N_PARAMS - 1;
2066 			}
2067 			break;
2068 
2069 			case SDLK_UP:
2070 			{
2071 				--mused.edit_reverb_param;
2072 				if (mused.edit_reverb_param < 0) mused.edit_reverb_param = 0;
2073 			}
2074 			break;
2075 
2076 			case SDLK_RIGHT:
2077 			{
2078 				fx_add_param(+1);
2079 			}
2080 			break;
2081 
2082 			case SDLK_LEFT:
2083 			{
2084 				fx_add_param(-1);
2085 			}
2086 			break;
2087 
2088 			default:
2089 				play_the_jams(e->key.keysym.sym, -1, 1);
2090 			break;
2091 		}
2092 
2093 		break;
2094 
2095 		case SDL_KEYUP:
2096 
2097 			play_the_jams(e->key.keysym.sym, -1, 0);
2098 
2099 		break;
2100 	}
2101 }
2102 
2103 
wave_add_param(int d)2104 void wave_add_param(int d)
2105 {
2106 	CydWavetableEntry *w = &mused.mus.cyd->wavetable_entries[mused.selected_wavetable];
2107 	WgOsc *osc = &mused.wgset.chain[mused.selected_wg_osc];
2108 
2109 	if (d < 0) d = -1; else if (d > 0) d = 1;
2110 
2111 	if (SDL_GetModState() & KMOD_SHIFT)
2112 	{
2113 		switch (mused.wavetable_param)
2114 		{
2115 			case W_BASE: d *= 12; break;
2116 			case W_BASEFINE: d *= 16; break;
2117 			default: d *= 256; break;
2118 		}
2119 	}
2120 
2121 	snapshot_cascade(S_T_WAVE_PARAM, mused.selected_wavetable, mused.wavetable_param);
2122 
2123 	switch (mused.wavetable_param)
2124 	{
2125 		case W_INTERPOLATE:
2126 		{
2127 			flipbit(w->flags, CYD_WAVE_NO_INTERPOLATION);
2128 		}
2129 		break;
2130 
2131 		case W_WAVE:
2132 		{
2133 			clamp(mused.selected_wavetable, d, 0, CYD_WAVE_MAX_ENTRIES - 1);
2134 		}
2135 		break;
2136 
2137 		case W_RATE:
2138 		{
2139 			clamp(w->sample_rate, d, 1, 192000);
2140 		}
2141 		break;
2142 
2143 		case W_BASE:
2144 		{
2145 			clamp(w->base_note, d * 256, 0, (FREQ_TAB_SIZE - 1) * 256);
2146 		}
2147 		break;
2148 
2149 		case W_BASEFINE:
2150 		{
2151 			clamp(w->base_note, d, 0, (FREQ_TAB_SIZE - 1) * 256);
2152 		}
2153 		break;
2154 
2155 		case W_LOOP:
2156 		{
2157 			flipbit(w->flags, CYD_WAVE_LOOP);
2158 		}
2159 		break;
2160 
2161 		case W_LOOPPINGPONG:
2162 		{
2163 			flipbit(w->flags, CYD_WAVE_PINGPONG);
2164 		}
2165 		break;
2166 
2167 		case W_LOOPBEGIN:
2168 		{
2169 			clamp(w->loop_begin, d, 0, my_min(w->samples, w->loop_end));
2170 			clamp(w->loop_end, 0, w->loop_begin, w->samples);
2171 		}
2172 		break;
2173 
2174 		case W_LOOPEND:
2175 		{
2176 			clamp(w->loop_end, d, w->loop_begin, w->samples);
2177 			clamp(w->loop_begin, 0, 0, my_min(w->samples, w->loop_end));
2178 		}
2179 		break;
2180 
2181 		case W_NUMOSCS:
2182 		{
2183 			mused.wgset.num_oscs += d;
2184 
2185 			if (mused.wgset.num_oscs < 1)
2186 				mused.wgset.num_oscs = 1;
2187 			else if (mused.wgset.num_oscs > WG_CHAIN_OSCS)
2188 				mused.wgset.num_oscs = WG_CHAIN_OSCS;
2189 		}
2190 		break;
2191 
2192 		case W_OSCTYPE:
2193 		{
2194 			osc->osc = my_max(0, my_min(WG_NUM_OSCS - 1, (int)osc->osc + d));
2195 		}
2196 		break;
2197 
2198 		case W_OSCMUL:
2199 		{
2200 			osc->mult = my_max(1, my_min(9, osc->mult + d));
2201 		}
2202 		break;
2203 
2204 		case W_OSCSHIFT:
2205 		{
2206 			osc->shift = my_max(0, my_min(7, osc->shift + d));
2207 		}
2208 		break;
2209 
2210 		case W_OSCEXP:
2211 		{
2212 			osc->exp += d * 5;
2213 
2214 			if (osc->exp < 5)
2215 				osc->exp = 5;
2216 			else if (osc->exp > 95)
2217 				osc->exp = 95;
2218 		}
2219 		break;
2220 
2221 		case W_OSCABS:
2222 			flipbit(osc->flags, WG_OSC_FLAG_ABS);
2223 		break;
2224 
2225 		case W_OSCNEG:
2226 			flipbit(osc->flags, WG_OSC_FLAG_NEG);
2227 		break;
2228 
2229 		case W_WAVELENGTH:
2230 		{
2231 			mused.wgset.length = my_max(16, my_min(65536, mused.wgset.length + d));
2232 		}
2233 		break;
2234 	}
2235 
2236 }
2237 
2238 
wave_event(SDL_Event * e)2239 void wave_event(SDL_Event *e)
2240 {
2241 	switch (e->type)
2242 	{
2243 		case SDL_KEYDOWN:
2244 
2245 		switch (e->key.keysym.sym)
2246 		{
2247 			case SDLK_DOWN:
2248 			{
2249 				++mused.wavetable_param;
2250 
2251 				if (!(mused.flags & SHOW_WAVEGEN) && mused.wavetable_param >= W_NUMOSCS && mused.wavetable_param <= W_TOOLBOX)
2252 				{
2253 					mused.wavetable_param = W_TOOLBOX + 1;
2254 				}
2255 
2256 				if (mused.wavetable_param >= W_N_PARAMS) mused.wavetable_param = W_N_PARAMS - 1;
2257 			}
2258 			break;
2259 
2260 			case SDLK_UP:
2261 			{
2262 				--mused.wavetable_param;
2263 
2264 				if (!(mused.flags & SHOW_WAVEGEN) && mused.wavetable_param >= W_NUMOSCS && mused.wavetable_param <= W_TOOLBOX)
2265 				{
2266 					mused.wavetable_param = W_NUMOSCS - 1;
2267 				}
2268 
2269 				if (mused.wavetable_param < 0) mused.wavetable_param = 0;
2270 			}
2271 			break;
2272 
2273 			case SDLK_RIGHT:
2274 			{
2275 				wave_add_param(+1);
2276 			}
2277 			break;
2278 
2279 			case SDLK_LEFT:
2280 			{
2281 				wave_add_param(-1);
2282 			}
2283 			break;
2284 
2285 			default:
2286 				wave_the_jams(e->key.keysym.sym);
2287 			break;
2288 		}
2289 
2290 		break;
2291 	}
2292 }
2293 
2294 
songinfo_add_param(int d)2295 void songinfo_add_param(int d)
2296 {
2297 	if (d < 0) d = -1; else if (d > 0) d = 1;
2298 
2299 	if (SDL_GetModState() & KMOD_SHIFT)
2300 		d *= 16;
2301 
2302 	if (d) snapshot_cascade(S_T_SONGINFO, mused.songinfo_param, -1);
2303 
2304 	switch (mused.songinfo_param)
2305 	{
2306 
2307 		case SI_MASTERVOL:
2308 			change_master_volume(MAKEPTR(d), 0, 0);
2309 			break;
2310 
2311 		case SI_LENGTH:
2312 			change_song_length(MAKEPTR(d * mused.sequenceview_steps), 0, 0);
2313 			break;
2314 
2315 		case SI_LOOP:
2316 			change_loop_point(MAKEPTR(d * mused.sequenceview_steps), 0, 0);
2317 			break;
2318 
2319 		case SI_STEP:
2320 			change_seq_steps(MAKEPTR(d), 0, 0);
2321 			break;
2322 
2323 		case SI_SPEED1:
2324 			change_song_speed(MAKEPTR(0), MAKEPTR(d), 0);
2325 			break;
2326 
2327 		case SI_SPEED2:
2328 			change_song_speed(MAKEPTR(1), MAKEPTR(d), 0);
2329 			break;
2330 
2331 		case SI_RATE:
2332 			change_song_rate(MAKEPTR(d), 0, 0);
2333 			break;
2334 
2335 		case SI_TIME:
2336 			change_timesig(MAKEPTR(d), 0, 0);
2337 			break;
2338 
2339 		case SI_OCTAVE:
2340 			change_octave(MAKEPTR(d), 0, 0);
2341 			break;
2342 
2343 		case SI_CHANNELS:
2344 			change_channels(MAKEPTR(d), 0, 0);
2345 			break;
2346 	}
2347 }
2348 
2349 
songinfo_event(SDL_Event * e)2350 void songinfo_event(SDL_Event *e)
2351 {
2352 	switch (e->type)
2353 	{
2354 		case SDL_KEYDOWN:
2355 
2356 		switch (e->key.keysym.sym)
2357 		{
2358 			case SDLK_DOWN:
2359 			{
2360 				++mused.songinfo_param;
2361 				if (mused.songinfo_param >= SI_N_PARAMS) mused.songinfo_param = SI_N_PARAMS - 1;
2362 			}
2363 			break;
2364 
2365 			case SDLK_UP:
2366 			{
2367 				--mused.songinfo_param;
2368 				if (mused.songinfo_param < 0) mused.songinfo_param = 0;
2369 			}
2370 			break;
2371 
2372 			case SDLK_RIGHT:
2373 			{
2374 				songinfo_add_param(+1);
2375 			}
2376 			break;
2377 
2378 			case SDLK_LEFT:
2379 			{
2380 				songinfo_add_param(-1);
2381 			}
2382 			break;
2383 
2384 			default: break;
2385 		}
2386 
2387 		break;
2388 	}
2389 }
2390 
2391 
note_event(SDL_Event * e)2392 void note_event(SDL_Event *e)
2393 {
2394 	switch (e->type)
2395 	{
2396 		case MSG_NOTEON:
2397 		{
2398 			play_note(e->user.code);
2399 			if (mused.focus == EDITPATTERN && (mused.flags & EDIT_MODE) && get_current_step() && mused.current_patternx == PED_NOTE)
2400 			{
2401 				write_note(e->user.code);
2402 			}
2403 		}
2404 		break;
2405 
2406 		case MSG_NOTEOFF:
2407 		{
2408 			stop_note(e->user.code);
2409 		}
2410 		break;
2411 
2412 		case MSG_PROGRAMCHANGE:
2413 		{
2414 			if (e->user.code >= 0 && e->user.code < NUM_INSTRUMENTS)
2415 				mused.current_instrument = e->user.code;
2416 		}
2417 		break;
2418 	}
2419 }
2420