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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
|
#include "snd.h"
/* TODO: save actions as CLM list
* need start point (for mark-oriented and selection-related play)
* for selection play, should we enforce stop at selection end?
*/
/* support for the record/replay/apply/s/r buttons in the control pane */
/* record watches the control panel state and makes a timed event list of all actions/settings */
/* replay re-runs the previous record. If record is also on, new events are merged, but */
/* all subsequent same-control changes are flushed (that is, change to a control erases */
/* the rest of the recorded changes for that control) */
/* apply is like replay except that the data becomes the newly edited state of the current channels */
#define ACTION_SIZE 128
#define LONGEST_LABEL 16
typedef struct {
int time;
int ui_val;
float snd_val;
} action;
typedef struct {
int action_size;
action **amp;
int amp_ctr,amp_max,amp_init;
action **speed;
int speed_ctr,speed_max,speed_init;
action **direction;
int direction_ctr,direction_max,direction_init;
action **expand;
int expand_ctr,expand_max,expand_init;
action **contrast;
int contrast_ctr,contrast_max,contrast_init;
action **reverb;
int reverb_ctr,reverb_max,reverb_init;
int revlen;
int filter_order;
env *filter;
snd_state *ss;
snd_info *sp;
} apply_info;
/* X times appear to be rounded to .01 secs or worse, but in this context that is probably ok */
/* we're not firing up notes by pushing keys, or anything that the user will perceive as rhythmic */
static int playing = 0;
static int allocate_actions(snd_info *sp, int size)
{
apply_info *sap;
int i;
sap = (apply_info *)(sp->actions);
if (!sap)
{
sap = (apply_info *)CALLOC(1,sizeof(apply_info));
sap->amp = (action **)CALLOC(size,sizeof(action *));
sap->speed = (action **)CALLOC(size,sizeof(action *));
sap->direction = (action **)CALLOC(size,sizeof(action *));
sap->expand = (action **)CALLOC(size,sizeof(action *));
sap->contrast = (action **)CALLOC(size,sizeof(action *));
sap->reverb = (action **)CALLOC(size,sizeof(action *));
sap->action_size = size;
sap->ss = sp->state;
sap->sp = sp;
sp->actions = sap;
}
else
{
if (size > sap->action_size)
{
sap->amp = (action **)REALLOC(sap->amp,size*sizeof(action *));
sap->speed = (action **)REALLOC(sap->speed,size*sizeof(action *));
sap->direction = (action **)REALLOC(sap->direction,size*sizeof(action *));
sap->expand = (action **)REALLOC(sap->expand,size*sizeof(action *));
sap->contrast = (action **)REALLOC(sap->contrast,size*sizeof(action *));
sap->reverb = (action **)REALLOC(sap->reverb,size*sizeof(action *));
for (i=sap->action_size;i<size;i++)
{
sap->amp[i] = NULL;
sap->speed[i] = NULL;
sap->direction[i] = NULL;
sap->expand[i] = NULL;
sap->contrast[i] = NULL;
sap->reverb[i] = NULL;
}
sap->action_size = size;
}
}
return(0);
}
void flush_actions(snd_info *sp)
{
apply_info *sap;
if ((sp) && (sap = (apply_info*)(sp->actions)))
{
/* no need to deallocate -- just make sure we ignore current setup next time around */
sap->amp_max = 0;
sap->speed_max = 0;
sap->direction_max = 0;
sap->expand_max = 0;
sap->contrast_max = 0;
sap->reverb_max = 0;
}
}
static void restart_actions(apply_info *sap)
{
sap->amp_ctr = 0;
sap->speed_ctr = 0;
sap->direction_ctr = 0;
sap->expand_ctr = 0;
sap->contrast_ctr = 0;
sap->reverb_ctr = 0;
}
/* ---------------- REPLAY ---------------- */
void initialize_replay(snd_info *sp)
{
allocate_actions(sp,ACTION_SIZE);
restart_actions((apply_info *)(sp->actions));
}
static int replay_ok(apply_info *ap)
{
snd_info *sp;
sp = ap->sp;
return(sp->playing);
}
static void replay_amp_change(snd_info *sp, int val)
{
int old_rec;
old_rec = sp->recording;
sp->recording = 0;
snd_amp_changed(sp,val);
set_raw_value(w_snd_amp(sp),val);
sp->recording = old_rec;
}
void replay_amp(void *ap1)
{
apply_info *ap = (apply_info *)ap1;
int time;
action *an_amp,*next_amp;
if (replay_ok(ap))
{
an_amp = ap->amp[ap->amp_ctr];
replay_amp_change(ap->sp,an_amp->ui_val);
ap->amp_ctr++;
if (ap->amp_ctr <= ap->amp_max)
{
next_amp = ap->amp[ap->amp_ctr];
time = (next_amp->time - an_amp->time);
if (time > 0) call_replay_amp(ap->ss,ap,time);
}
}
}
static void replay_speed_change(snd_info *sp, int val)
{
int old_rec;
old_rec = sp->recording;
sp->recording = 0;
snd_srate_changed(sp,val);
set_raw_value(w_snd_srate(sp),val);
sp->recording = old_rec;
}
void replay_speed(void *ap1)
{
apply_info *ap = (apply_info *)ap1;
int time;
action *a_speed,*next_speed;
if (replay_ok(ap))
{
a_speed = ap->speed[ap->speed_ctr];
replay_speed_change(ap->sp,a_speed->ui_val);
ap->speed_ctr++;
if (ap->speed_ctr <= ap->speed_max)
{
next_speed = ap->speed[ap->speed_ctr];
time = (next_speed->time - a_speed->time);
if (time > 0) call_replay_speed(ap->ss,ap,time);
}
}
}
static void replay_contrast_change(snd_info *sp, int val)
{
int old_rec;
old_rec = sp->recording;
sp->recording = 0;
snd_contrast_changed(sp,val);
set_raw_value(w_snd_contrast(sp),val);
sp->recording = old_rec;
}
void replay_contrast(void *ap1)
{
apply_info *ap = (apply_info *)ap1;
int time;
action *a_contrast,*next_contrast;
if (replay_ok(ap))
{
a_contrast = ap->contrast[ap->contrast_ctr];
replay_contrast_change(ap->sp,a_contrast->ui_val);
ap->contrast_ctr++;
if (ap->contrast_ctr <= ap->contrast_max)
{
next_contrast = ap->contrast[ap->contrast_ctr];
time = (next_contrast->time - a_contrast->time);
if (time > 0) call_replay_contrast(ap->ss,ap,time);
}
}
}
static void replay_expand_change(snd_info *sp, int val)
{
int old_rec;
old_rec = sp->recording;
sp->recording = 0;
snd_expand_changed(sp,val);
set_raw_value(w_snd_expand(sp),val);
sp->recording = old_rec;
}
void replay_expand(void *ap1)
{
apply_info *ap = (apply_info *)ap1;
int time;
action *an_expand,*next_expand;
if (replay_ok(ap))
{
an_expand = ap->expand[ap->expand_ctr];
replay_expand_change(ap->sp,an_expand->ui_val);
ap->expand_ctr++;
if (ap->expand_ctr <= ap->expand_max)
{
next_expand = ap->expand[ap->expand_ctr];
time = (next_expand->time - an_expand->time);
if (time > 0) call_replay_expand(ap->ss,ap,time);
}
}
}
static void replay_reverb_change(snd_info *sp, int val)
{
int old_rec;
old_rec = sp->recording;
sp->recording = 0;
snd_revscl_changed(sp,val);
set_raw_value(w_snd_revscl(sp),val);
sp->recording = old_rec;
}
void replay_reverb(void *ap1)
{
apply_info *ap = (apply_info *)ap1;
int time;
action *a_reverb,*next_reverb;
if (replay_ok(ap))
{
a_reverb = ap->reverb[ap->reverb_ctr];
replay_reverb_change(ap->sp,a_reverb->ui_val);
ap->reverb_ctr++;
if (ap->reverb_ctr <= ap->reverb_max)
{
next_reverb = ap->reverb[ap->reverb_ctr];
time = (next_reverb->time - a_reverb->time);
if (time > 0) call_replay_reverb(ap->ss,ap,time);
}
}
}
static void replay_revlen_change(snd_info *sp, int val)
{
int old_rec;
old_rec = sp->recording;
sp->recording = 0;
snd_revlen_changed(sp,val);
set_raw_value(w_snd_revlen(sp),val);
sp->recording = old_rec;
}
static void replay_direction_change(snd_info *sp, int val)
{
int old_rec;
old_rec = sp->recording;
sp->recording = 0;
toggle_direction_arrow(sp,val);
sp->recording = old_rec;
}
void replay_direction(void *ap1)
{
apply_info *ap = (apply_info *)ap1;
int time;
action *a_direction,*next_direction;
if (replay_ok(ap))
{
a_direction = ap->direction[ap->direction_ctr];
replay_direction_change(ap->sp,a_direction->ui_val);
ap->direction_ctr++;
if (ap->direction_ctr <= ap->direction_max)
{
next_direction = ap->direction[ap->direction_ctr];
time = (next_direction->time - a_direction->time);
if (time > 0) call_replay_direction(ap->ss,ap,time);
}
}
}
/* ---------------- RECORD ---------------- */
void initialize_record(snd_info *sp)
{
allocate_actions(sp,ACTION_SIZE);
restart_actions((apply_info*)(sp->actions));
}
static int apply_simple(snd_info *sp)
{
apply_info *sap;
if (sp)
{
sap = (apply_info *)(sp->actions);
if (sap)
{
if ((sap->expand_max > 0) || (sap->speed_max > 0)) return(FALSE);
}
}
return(TRUE);
}
void record_amp_change(snd_info *sp,int scroll_val,float amp_val)
{
apply_info *sap;
action *amp;
action **amps;
if (!playing) return;
sap = (apply_info *)(sp->actions);
if (sap->action_size <= sap->amp_ctr) allocate_actions(sp,ACTION_SIZE+sap->action_size);
amps = sap->amp;
if (!(amps[sap->amp_ctr])) amps[sap->amp_ctr] = (action *)CALLOC(1,sizeof(action));
amp = amps[sap->amp_ctr];
amp->ui_val = scroll_val;
amp->snd_val = amp_val;
amp->time = main_TIME(sp->state);
if ((sp->replaying) || (sap->amp_ctr > sap->amp_max)) sap->amp_max = sap->amp_ctr;
sap->amp_ctr++;
}
void record_speed_change(snd_info *sp,int scroll_val,float speed_val)
{
apply_info *sap;
action *speed;
action **speeds;
if (!playing) return;
sap = (apply_info *)(sp->actions);
if (sap->action_size <= sap->speed_ctr) allocate_actions(sp,ACTION_SIZE+sap->action_size);
speeds = sap->speed;
if (!(speeds[sap->speed_ctr])) speeds[sap->speed_ctr] = (action *)CALLOC(1,sizeof(action));
speed = speeds[sap->speed_ctr];
speed->ui_val = scroll_val;
speed->snd_val = speed_val;
speed->time = main_TIME(sp->state);
if ((sp->replaying) || (sap->speed_ctr > sap->speed_max)) sap->speed_max = sap->speed_ctr;
sap->speed_ctr++;
}
void record_expand_change(snd_info *sp,int scroll_val,float expand_val)
{
apply_info *sap;
action *expand;
action **expands;
if (!playing) return;
sap = (apply_info *)(sp->actions);
if (sap->action_size <= sap->expand_ctr) allocate_actions(sp,ACTION_SIZE+sap->action_size);
expands = sap->expand;
if (!(expands[sap->expand_ctr])) expands[sap->expand_ctr] = (action *)CALLOC(1,sizeof(action));
expand = expands[sap->expand_ctr];
expand->ui_val = scroll_val;
expand->snd_val = expand_val;
expand->time = main_TIME(sp->state);
if ((sp->replaying) || (sap->expand_ctr > sap->expand_max)) sap->expand_max = sap->expand_ctr;
sap->expand_ctr++;
}
void record_reverb_change(snd_info *sp,int scroll_val,float reverb_val)
{
apply_info *sap;
action *reverb;
action **reverbs;
if (!playing) return;
sap = (apply_info *)(sp->actions);
if (sap->action_size <= sap->reverb_ctr) allocate_actions(sp,ACTION_SIZE+sap->action_size);
reverbs = sap->reverb;
if (!(reverbs[sap->reverb_ctr])) reverbs[sap->reverb_ctr] = (action *)CALLOC(1,sizeof(action));
reverb = reverbs[sap->reverb_ctr];
reverb->ui_val = scroll_val;
reverb->snd_val = reverb_val;
reverb->time = main_TIME(sp->state);
if ((sp->replaying) || (sap->reverb_ctr > sap->reverb_max)) sap->reverb_max = sap->reverb_ctr;
sap->reverb_ctr++;
}
void record_contrast_change(snd_info *sp,int scroll_val,float contrast_val)
{
apply_info *sap;
action *contrast;
action **contrasts;
if (!playing) return;
sap = (apply_info *)(sp->actions);
if (sap->action_size <= sap->contrast_ctr) allocate_actions(sp,ACTION_SIZE+sap->action_size);
contrasts = sap->contrast;
if (!(contrasts[sap->contrast_ctr])) contrasts[sap->contrast_ctr] = (action *)CALLOC(1,sizeof(action));
contrast = contrasts[sap->contrast_ctr];
contrast->ui_val = scroll_val;
contrast->snd_val = contrast_val;
contrast->time = main_TIME(sp->state);
if ((sp->replaying) || (sap->contrast_ctr > sap->contrast_max)) sap->contrast_max = sap->contrast_ctr;
sap->contrast_ctr++;
}
void record_direction_change(snd_info *sp,int scroll_val,float direction_val)
{
apply_info *sap;
action *direction;
action **directions;
if (!playing) return;
sap = (apply_info *)(sp->actions);
if (sap->action_size <= sap->direction_ctr) allocate_actions(sp,ACTION_SIZE+sap->action_size);
directions = sap->direction;
if (!(directions[sap->direction_ctr])) directions[sap->direction_ctr] = (action *)CALLOC(1,sizeof(action));
direction = directions[sap->direction_ctr];
direction->ui_val = scroll_val;
direction->snd_val = direction_val;
direction->time = main_TIME(sp->state);
if ((sp->replaying) || (sap->direction_ctr > sap->direction_max)) sap->direction_max = sap->direction_ctr;
sap->direction_ctr++;
}
void finish_record_and_replay(void)
{
playing = 0;
}
void start_record_and_replay(snd_state *ss, snd_info *sp)
{
/* called when the dac is actually started -- none of these buttons may be down when we're called */
/* at startup save initial state (at record time?) */
apply_info *sap;
action **amps,**speeds,**contrasts,**expands,**reverbs,**directions;
int old_rec;
playing = 1;
sap = (apply_info *)(sp->actions);
if (sap)
{
restart_actions(sap);
if ((sp->recording) && (!(sp->replaying)))
{
flush_actions(sp); /* if record on, but not replay, assume start from scratch */
sap->amp_init = get_raw_value(w_snd_amp(sp));
sap->speed_init = get_raw_value(w_snd_srate(sp));
sap->direction_init = get_raw_value(w_snd_srate_arrow(sp));
sap->expand_init = get_raw_value(w_snd_expand(sp));
sap->contrast_init = get_raw_value(w_snd_contrast(sp));
sap->reverb_init = get_raw_value(w_snd_revscl(sp));
sap->filter_order = sp->filter_order;
if (sp->filter_env) sap->filter = copy_env(sp->filter_env);
}
if (sp->replaying)
{
/* for each sync'd sound, we need timeout procs running down the associated action lists */
old_rec = sp->recording;
sp->recording = 0;
if ((amps = sap->amp) && (sap->amp_max > 0))
{
snd_amp_changed(sp,sap->amp_init);
set_raw_value(w_snd_amp(sp),sap->amp_init);
call_replay_amp(ss,sap,amps[0]->time);
}
if ((sp->contrasting) && (contrasts = sap->contrast) && (sap->contrast_max > 0))
{
snd_contrast_changed(sp,sap->contrast_init);
set_raw_value(w_snd_contrast(sp),sap->contrast_init);
call_replay_contrast(ss,sap,contrasts[0]->time);
}
if ((sp->expanding) && (expands = sap->expand) && (sap->expand_max > 0))
{
snd_expand_changed(sp,sap->expand_init);
set_raw_value(w_snd_expand(sp),sap->expand_init);
call_replay_expand(ss,sap,expands[0]->time);
}
if ((sp->reverbing) && (reverbs = sap->reverb) && (sap->reverb_max > 0))
{
snd_revscl_changed(sp,sap->reverb_init);
set_raw_value(w_snd_revscl(sp),sap->reverb_init);
call_replay_reverb(ss,sap,reverbs[0]->time);
}
if ((speeds = sap->speed) && (sap->speed_max > 0))
{
snd_srate_changed(sp,sap->speed_init);
set_raw_value(w_snd_srate(sp),sap->speed_init);
call_replay_speed(ss,sap,speeds[0]->time);
}
if ((directions = sap->direction) && (sap->direction_max > 0))
{
toggle_direction_arrow(sp,sap->direction_init);
call_replay_direction(ss,sap,directions[0]->time);
}
filter_order_changed(sp,sap->filter_order);
if (sp->filter_env) sp->filter_env = free_env(sp->filter_env);
if (sap->filter) sp->filter_env = copy_env(sap->filter);
filter_env_changed(sp,sp->filter_env);
sp->recording = old_rec;
}
}
}
/* ---------------- SAVE and RESTORE buttons ----------------*/
typedef struct {
int amp,srate,contrast,expand,revscl,revlen;
env *filter;
int expand_on,contrast_on,reverb_on,filter_on,direction,filter_order;
} ctrl_state;
void free_controls(snd_info *sp)
{
ctrl_state *cs;
cs = (ctrl_state *)(sp->initial_controls);
if (cs)
{
if (cs->filter) free_env(cs->filter);
FREE(cs);
sp->initial_controls = NULL;
}
}
void save_control_state(snd_info *sp)
{
ctrl_state *cs;
cs = (ctrl_state *)(sp->initial_controls);
if (!cs)
{
sp->initial_controls = (ctrl_state *)CALLOC(1,sizeof(ctrl_state));
cs = (ctrl_state *)(sp->initial_controls);
}
cs->amp = get_raw_value(w_snd_amp(sp));
cs->srate = get_raw_value(w_snd_srate(sp));
cs->expand = get_raw_value(w_snd_expand(sp));
cs->revscl = get_raw_value(w_snd_revscl(sp));
cs->revlen = get_raw_value(w_snd_revlen(sp));
cs->contrast = get_raw_value(w_snd_contrast(sp));
cs->expand_on = sp->expanding;
cs->reverb_on = sp->reverbing;
cs->contrast_on = sp->contrasting;
cs->filter_on = sp->filtering;
cs->filter_order = sp->filter_order;
if (sp->filter_env) cs->filter = copy_env(sp->filter_env);
if (sp->play_direction == 1) cs->direction = 0; else cs->direction = 1;
}
void restore_control_state(snd_info *sp)
{
ctrl_state *cs;
int do_all = 0;
cs = (ctrl_state *)(sp->initial_controls);
if (!cs)
{
sp->initial_controls = (ctrl_state *)CALLOC(1,sizeof(ctrl_state));
cs = (ctrl_state *)(sp->initial_controls);
cs->amp = SCROLLBAR_MID;
cs->srate = 450;
cs->expand_on = 0;
cs->reverb_on = 0;
cs->contrast_on = 0;
cs->filter_on = 0;
cs->direction = 0; /* 0 = forward, 1 = backward (this is the button's view) */
}
else do_all = 1;
toggle_expand_button(sp,cs->expand_on);
toggle_contrast_button(sp,cs->contrast_on);
toggle_reverb_button(sp,cs->reverb_on);
toggle_filter_button(sp,cs->filter_on);
toggle_direction_arrow(sp,cs->direction);
replay_amp_change(sp,cs->amp);
replay_speed_change(sp,cs->srate);
if (do_all)
{
replay_contrast_change(sp,cs->contrast);
replay_reverb_change(sp,cs->revscl);
replay_expand_change(sp,cs->expand);
replay_revlen_change(sp,cs->revlen);
if (sp->filter_env) free_env(sp->filter_env);
sp->filter_env = copy_env(cs->filter);
filter_env_changed(sp,sp->filter_env);
filter_order_changed(sp,cs->filter_order);
}
}
/* ---------------- APPLY ---------------- */
/* TODO:
* apply to one channel, apply to selection; in each case if length changes, mix extra/zero pad or replace?
* how to handle reverb portion? -- 3 sections: original, new direct, new reverb decay?
* apply to selection can span several files -- use each one's current controls or just current sound's?
*/
static void initialize_queues(snd_info *sp)
{
apply_info *sap;
sap = (apply_info *)(sp->actions);
if (sap)
{
restart_actions(sap);
if ((sap->amp) && (sap->amp_max > 0)) snd_amp_changed(sp,sap->amp_init);
if ((sp->contrasting) && (sap->contrast) && (sap->contrast_max > 0)) snd_contrast_changed(sp,sap->contrast_init);
if ((sp->expanding) && (sap->expand) && (sap->expand_max > 0)) snd_expand_changed(sp,sap->expand_init);
if ((sp->reverbing) && (sap->reverb) && (sap->reverb_max > 0)) snd_revscl_changed(sp,sap->reverb_init);
if ((sap->speed) && (sap->speed_max > 0)) snd_srate_changed(sp,sap->speed_init);
if ((sap->direction) && (sap->direction_max > 0)) toggle_direction_arrow(sp,sap->direction_init);
filter_order_changed(sp,sap->filter_order);
if (sp->filter_env) sp->filter_env = free_env(sp->filter_env);
if (sap->filter)
{
sp->filter_env = copy_env(sap->filter);
filter_env_changed(sp,sp->filter_env);
}
}
}
static void finalize_queues(snd_info *sp)
{
apply_info *sap;
sap = (apply_info *)(sp->actions);
if (sap) restart_actions(sap); /* not really needed */
}
static void run_queues(snd_info *sp, int samp)
{
apply_info *sap;
action **amps,**speeds,**contrasts,**expands,**reverbs,**directions;
float srscl;
srscl = snd_SRATE(sp)/1000.0;
sap = (apply_info *)(sp->actions);
if (sap)
{
if ((amps = sap->amp) && (sap->amp_max > 0) && (sap->amp_ctr <= sap->amp_max))
{
if (samp >= (srscl*(amps[sap->amp_ctr]->time)))
{
snd_amp_changed(sp,amps[sap->amp_ctr]->ui_val);
sap->amp_ctr++;
}
}
if ((sp->contrasting) && (contrasts = sap->contrast) && (sap->contrast_max > 0) && (sap->contrast_ctr <= sap->contrast_max))
{
if (samp >= (srscl*(contrasts[sap->contrast_ctr]->time)))
{
snd_contrast_changed(sp,contrasts[sap->contrast_ctr]->ui_val);
sap->contrast_ctr++;
}
}
if ((sp->expanding) && (expands = sap->expand) && (sap->expand_max > 0) && (sap->expand_ctr <= sap->expand_max))
{
if (samp >= (srscl*(expands[sap->expand_ctr]->time)))
{
snd_expand_changed(sp,expands[sap->expand_ctr]->ui_val);
sap->expand_ctr++;
}
}
if ((sp->reverbing) && (reverbs = sap->reverb) && (sap->reverb_max > 0) && (sap->reverb_ctr <= sap->reverb_max))
{
if (samp >= (srscl*(reverbs[sap->reverb_ctr]->time)))
{
snd_revscl_changed(sp,reverbs[sap->reverb_ctr]->ui_val);
sap->reverb_ctr++;
}
}
if ((speeds = sap->speed) && (sap->speed_max > 0) && (sap->speed_ctr <= sap->speed_max))
{
if (samp >= (srscl*(speeds[sap->speed_ctr]->time)))
{
snd_srate_changed(sp,speeds[sap->speed_ctr]->ui_val);
sap->speed_ctr++;
}
}
if ((directions = sap->direction) && (sap->direction_max > 0) && (sap->direction_ctr <= sap->direction_max))
{
if (samp >= (srscl*(directions[sap->direction_ctr]->time)))
{
toggle_direction_arrow(sp,directions[sap->direction_ctr]->ui_val);
sap->direction_ctr++;
}
}
}
}
void stop_applying(snd_info *sp)
{
/* called if user unset the apply button during the apply process */
sp->apply_ok = 0;
}
typedef struct {
int slice;
snd_info *sp;
int i;
int ofd;
char *ofile;
ctrl_state *cs;
file_info *hdr;
} apply_manager;
void *make_apply_state(void *xp)
{
/* set up initial state for apply_controls */
apply_manager *ap;
ap = (apply_manager *)CALLOC(1,sizeof(apply_manager));
ap->slice = 0;
ap->hdr = NULL;
ap->sp = (snd_info *)xp;
return((void *)ap);
}
static int apply_controls_1(apply_manager *ap)
{
snd_state *ss;
snd_context *sgx;
snd_info *sp;
chan_info *cp;
float ratio;
int i,len,simple,dac_op,over_selection;
sp = ap->sp;
if (!(sp->inuse)) return(TRUE);
ss = sp->state;
switch (ap->slice)
{
case 0:
ap->ofile = NULL;
lock_apply(ss,sp);
lock_play_and_record(ss,sp);
finish_keyboard_selection();
deactivate_selection(); /* any edit will change the data within the selection highlight region */
/* ap->cs = (ctrl_state *)(sp->initial_controls); */
/* sp->initial_controls = NULL; */
/* save_control_state(sp); */
ap->ofile = snd_tempnam(ss);
ap->hdr = make_temp_header(ap->ofile,sp->hdr,0); /* assumes NeXT 16-bit */
ap->ofd = open_temp_file(ap->ofile,sp->nchans,ap->hdr,ss);
sp->apply_ok = 1;
initialize_queues(sp);
initialize_apply(sp);
ap->i = 0;
ap->slice++;
return(FALSE);
break;
case 1:
if (!(sp->apply_ok))
ap->slice++;
else
{
len = run_apply(sp,ap->ofd); /* returns samps in chan (dac_buffer_size/chans) */
if (len > 0)
{
run_queues(sp,ap->i);
ap->i += len;
}
else
{
ap->slice++;
if (len < 0) ap->i -= len;
}
}
return(FALSE);
break;
case 2:
dac_op = finalize_apply(sp);
simple = apply_simple(sp);
finalize_queues(sp);
close_temp_file(ap->ofd,ap->hdr,ap->i*sp->nchans*2,sp); /* 2 because we're using dac procs to write sound = 16-bit samps */
if (sp->apply_ok)
{
switch (ss->apply_choice)
{
case APPLY_TO_SOUND:
for (i=0;i<sp->nchans;i++)
file_override_samples(ap->i,ap->ofile,sp->chans[i],i,(i == 0) ? DELETE_ME : DONT_DELETE_ME,LOCK_MIXES,"Apply");
break;
case APPLY_TO_CHANNEL:
file_override_samples(ap->i,ap->ofile,sp->chans[sp->selected_channel],0,DELETE_ME,LOCK_MIXES,"Apply to channel");
break;
case APPLY_TO_SELECTION:
/* cannot work across sounds or with weird channel collections -- see snd-clip.c load_region */
for (i=0;i<region_chans(0);i++)
file_change_samples(selection_beg(sp->chans[i]),region_len(0),ap->ofile,sp->chans[i],i,
(i == 0) ? DELETE_ME : DONT_DELETE_ME,LOCK_MIXES,"Apply to selection");
for (i=0;i<region_chans(0);i++)
update_graph(sp->chans[i],NULL);
break;
}
report_in_minibuffer(sp,"");
set_apply_button(sp,0);
sp->apply_ok = 0;
if ((simple) && (dac_op != JUST_AMP) && (dac_op != NO_CHANGE))
{
for (i=0;i<sp->nchans;i++)
{
cp = sp->chans[i];
if (cp->marks)
{
if (!(sp->expanding))
ratio = sp->srate;
else ratio = sp->srate / sp->expand;
if (ratio != 1.0)
{
over_selection = (ss->apply_choice == APPLY_TO_SELECTION);
src_marks(cp,ratio,apply_duration(),ap->i,(over_selection) ? selection_beg(cp) : 0,over_selection);
update_graph(cp,NULL);
}
}
}
}
/* if apply_simple false, there were recorded expand/srate envelopes in use, and we'll have to handle the marks in a fancier way */
}
else
{
remove(ap->ofile);
forget_sound(ap->ofile);
report_in_minibuffer(sp,STR_apply_flushed);
}
FREE(ap->ofile); /* safe only if tempnam, not tmpnam used */
ap->ofile=NULL;
if (ap->hdr) ap->hdr = free_file_info(ap->hdr);
free_controls(sp);
sp->initial_controls = NULL;
restore_control_state(sp); /* i.e. clear it */
/* sp->initial_controls = ap->cs; */
sp->applying = 0;
sgx = sp->sgx;
if (sgx->apply_in_progress) sgx->apply_in_progress = 0;
FREE(ap);
unlock_apply(ss,sp);
unlock_play_and_record(ss,sp);
return(TRUE);
break;
}
return(TRUE);
}
int apply_controls(void *xp)
{
/* a background procedure so that it's interruptible */
return(apply_controls_1((apply_manager *)xp));
}
void run_apply_to_completion(snd_info *sp)
{
/* this version called from Guile, so we want it to complete before returning */
apply_manager *ap;
sp->applying = 1;
ap = (apply_manager *)make_apply_state((void *)sp);
while (apply_controls_1(ap) == FALSE);
}
|