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 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
|
#include "track.h"
#include "dsp/db.h"
#include "song.h"
#include <stdio.h>
void Automation::_ui_changed_callbacks(void *p_ud) {
Automation *automation = (Automation *)p_ud;
automation->_ui_changed_callback();
}
void Automation::_ui_changed_callback() {
if (has_pre_play_capture) {
pre_play_capture_value = port->get(); //update pre play capture because UI modified it
}
}
void Automation::set_point(int p_pattern, Tick p_offset, uint8_t p_value) {
_AUDIO_LOCK_
if (p_value == EMPTY) {
remove_point(p_pattern, p_offset);
return;
}
if (!data.has(p_pattern)) {
data[p_pattern] = ValueStream<Tick, uint8_t>();
}
data[p_pattern].insert(p_offset, p_value);
}
bool Automation::has_point(int p_pattern, Tick p_offset) const {
if (!data.has(p_pattern))
return false;
return data[p_pattern].find_exact(p_offset) >= 0;
}
uint8_t Automation::get_point(int p_pattern, Tick p_offset) const {
if (!data.has(p_pattern))
return EMPTY;
int idx = data[p_pattern].find_exact(p_offset);
if (idx < 0)
return EMPTY;
return data[p_pattern][idx];
}
void Automation::remove_point(int p_pattern, Tick p_offset) {
if (!data.has(p_pattern))
return;
_AUDIO_LOCK_
int idx = data[p_pattern].find_exact(p_offset);
if (idx < 0)
return;
data[p_pattern].erase(idx);
if (data[p_pattern].size() == 0)
data.erase(p_pattern);
}
Tick Automation::get_point_tick_by_index(int p_pattern, int p_index) const {
// this is used super often when playing, so it should be more optimized
const Map<int, ValueStream<Tick, uint8_t> >::Element *E = data.find(p_pattern);
ERR_FAIL_COND_V(!E, 0);
ERR_FAIL_INDEX_V(p_index, E->get().size(), 0);
return E->get().get_pos(p_index);
}
uint8_t Automation::get_point_by_index(int p_pattern, int p_index) const {
// this is used super often when playing, so it should be more optimized
const Map<int, ValueStream<Tick, uint8_t> >::Element *E = data.find(p_pattern);
ERR_FAIL_COND_V(!E, 0);
ERR_FAIL_INDEX_V(p_index, E->get().size(), 0);
return E->get()[p_index];
}
int Automation::get_point_count(int p_pattern) const {
const Map<int, ValueStream<Tick, uint8_t> >::Element *E = data.find(p_pattern);
if (!E)
return 0;
return E->get().size();
}
float Automation::interpolate_offset(int p_pattern, Tick p_offset) const {
const Map<int, ValueStream<Tick, uint8_t> >::Element *E = data.find(p_pattern);
if (!E) {
return -1;
}
const ValueStream<Tick, uint8_t> &vs = E->get();
int pos = vs.find(p_offset);
int total = vs.size();
if (pos < 0 || pos >= total)
return -1;
int n = pos + 1;
if (n >= total)
return -1;
float c = float(p_offset - vs.get_pos(pos)) / float(vs.get_pos(n) - vs.get_pos(pos));
float a = (vs[pos] / float(VALUE_MAX));
float b = (vs[n] / float(VALUE_MAX));
return b * c + a * (1.0 - c);
}
void Automation::get_points_in_range(int p_pattern, Tick p_from, Tick p_to, int &r_first, int &r_count) const {
const Map<int, ValueStream<Tick, uint8_t> >::Element *E = data.find(p_pattern);
if (!E) {
r_count = 0;
return;
}
const ValueStream<Tick, uint8_t> &vs = E->get();
if (vs.size() == 0) {
r_count = 0;
return;
}
int pos_beg = vs.find(p_from);
int pos_end = vs.find(p_to);
if (pos_end >= 0 && p_to == vs.get_pos(pos_end)) {
pos_end--;
}
if (pos_end < 0) {
r_count = 0;
return;
}
if (pos_beg < 0 || vs.get_pos(pos_beg) < p_from)
pos_beg++;
if (pos_beg > pos_end) {
r_count = 0;
return;
}
r_first = pos_beg;
r_count = pos_end - pos_beg + 1;
}
ControlPort *Automation::get_control_port() {
return port;
}
AudioEffect *Automation::get_owner() {
return owner;
}
void Automation::set_edit_mode(EditMode p_mode) {
display_mode = p_mode;
}
Automation::EditMode Automation::get_edit_mode() const {
return display_mode;
}
bool Automation::is_empty() const {
return (display_mode == EDIT_ROWS_DISCRETE && data.empty());
}
void Automation::pre_play_capture() {
if (has_pre_play_capture) {
return; //do not re-capture
}
pre_play_capture_value = port->get();
has_pre_play_capture = true;
}
void Automation::pre_play_restore() {
if (has_pre_play_capture) {
port->set(pre_play_capture_value);
has_pre_play_capture = false;
};
}
void Automation::add_notify() {
has_pre_play_capture = false; //should be false but just in case
port->set_ui_changed_callback(&_ui_changed_callbacks, this);
}
void Automation::remove_notify() {
pre_play_restore();
port->set_ui_changed_callback(NULL, NULL);
}
Automation::Automation(ControlPort *p_port, AudioEffect *p_owner) {
port = p_port;
owner = p_owner;
display_mode = EDIT_ROWS_DISCRETE;
has_pre_play_capture = false;
pre_play_capture_value = 0;
}
/* audio effects */
void Track::set_name(String p_name) {
name = p_name;
}
String Track::get_name() const {
return name;
}
int Track::get_audio_effect_count() const {
return effects.size();
}
void Track::add_audio_effect(AudioEffect *p_effect, int p_pos) {
_AUDIO_LOCK_
if (p_pos < 0)
p_pos = effects.size();
ERR_FAIL_COND(p_pos > effects.size());
effects.insert(p_pos, p_effect);
p_effect->set_process_block_size(process_buffer.size());
p_effect->set_sampling_rate(sampling_rate);
}
void Track::remove_audio_effect(int p_pos) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_pos, effects.size());
AudioEffect *fx = effects[p_pos];
for (int i = 0; i < automations.size(); i++) {
if (automations[i]->get_owner() == fx) {
automations.remove(i);
i--;
}
}
effects.remove(p_pos);
}
void Track::swap_audio_effects(int p_effect, int p_with_effect) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_effect, effects.size());
ERR_FAIL_INDEX(p_with_effect, effects.size());
SWAP(effects[p_effect], effects[p_with_effect]);
}
AudioEffect *Track::get_audio_effect(int p_pos) {
ERR_FAIL_INDEX_V(p_pos, effects.size(), NULL);
return effects[p_pos];
}
/* automations */
int Track::get_automation_count() const {
return automations.size();
}
void Track::add_automation(Automation *p_automation, int p_pos) {
_AUDIO_LOCK_
if (p_pos < 0)
p_pos = automations.size();
ERR_FAIL_COND(p_pos > automations.size());
p_automation->add_notify();
automations.insert(p_pos, p_automation);
}
void Track::remove_automation(int p_pos) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_pos, automations.size());
automations[p_pos]->remove_notify();
automations.remove(p_pos);
}
Automation *Track::get_automation(int p_pos) const {
ERR_FAIL_INDEX_V(p_pos, automations.size(), NULL);
return automations[p_pos];
}
void Track::swap_automations(int p_which, int p_by_which) {
_AUDIO_LOCK_
SWAP(automations[p_which], automations[p_by_which]);
}
// disabled automations
int Track::get_disabled_automation_count() const {
return disabled_automations.size();
}
void Track::add_disabled_automation(Automation *p_automation, int p_pos) {
_AUDIO_LOCK_
if (p_pos < 0) {
p_pos = disabled_automations.size();
}
ERR_FAIL_COND(p_pos > disabled_automations.size());
disabled_automations.insert(p_pos, p_automation);
}
void Track::remove_disabled_automation(int p_pos) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_pos, disabled_automations.size());
disabled_automations.remove(p_pos);
}
Automation *Track::get_disabled_automation(int p_pos) const {
ERR_FAIL_INDEX_V(p_pos, disabled_automations.size(), NULL);
return disabled_automations[p_pos];
}
////
void Track::set_columns(int p_columns) {
_AUDIO_LOCK_
ERR_FAIL_COND(p_columns < 1);
note_columns = p_columns;
column_state.resize(note_columns);
for (int i = 0; i < column_state.size(); i++) {
column_state[i] = Note::EMPTY;
}
}
int Track::get_column_count() const {
return note_columns;
}
void Track::set_note(int p_pattern, Pos p_pos, Note p_note) {
_AUDIO_LOCK_
if (!note_data.has(p_pattern))
note_data[p_pattern] = ValueStream<Pos, Note>();
if (p_note.is_empty()) {
int idx = note_data[p_pattern].find_exact(p_pos);
if (idx < 0)
return;
note_data[p_pattern].erase(idx);
} else {
note_data[p_pattern].insert(p_pos, p_note);
}
}
Track::Note Track::get_note(int p_pattern, Pos p_pos) const {
const Map<int, ValueStream<Pos, Note> >::Element *E = note_data.find(p_pattern);
if (!E)
return Note();
int idx = E->get().find_exact(p_pos);
if (idx < 0)
return Note();
else
return E->get()[idx];
}
void Track::get_notes_in_range(int p_pattern, const Pos &p_from, const Pos &p_to, int &r_first, int &r_count) const {
const Map<int, ValueStream<Pos, Note> >::Element *E = note_data.find(p_pattern);
if (!E) {
r_count = 0;
return;
}
const ValueStream<Pos, Note> &vs = E->get();
if (vs.size() == 0) {
r_count = 0;
return;
}
int pos_beg = vs.find(p_from);
int pos_end = vs.find(p_to);
if (pos_end >= 0 && p_to == vs.get_pos(pos_end)) {
pos_end--;
}
if (pos_end < 0) {
r_count = 0;
return;
}
if (pos_beg < 0 || vs.get_pos(pos_beg) < p_from)
pos_beg++;
if (pos_beg > pos_end) {
r_count = 0;
return;
}
r_first = pos_beg;
r_count = pos_end - pos_beg + 1;
}
int Track::get_note_count(int p_pattern) const {
const Map<int, ValueStream<Pos, Note> >::Element *E = note_data.find(p_pattern);
if (!E)
return 0;
return E->get().size();
}
Track::Note Track::get_note_by_index(int p_pattern, int p_index) const {
const Map<int, ValueStream<Pos, Note> >::Element *E = note_data.find(p_pattern);
if (!E)
return Note();
const ValueStream<Pos, Note> &vs = E->get();
ERR_FAIL_INDEX_V(p_index, vs.size(), Note());
return vs[p_index];
}
Track::Pos Track::get_note_pos_by_index(int p_pattern, int p_index) const {
const Map<int, ValueStream<Pos, Note> >::Element *E = note_data.find(p_pattern);
if (!E)
return Pos();
const ValueStream<Pos, Note> &vs = E->get();
ERR_FAIL_INDEX_V(p_index, vs.size(), Pos());
return vs.get_pos(p_index);
}
void Track::get_notes_in_range(int p_pattern, const Pos &p_from, const Pos &p_to, List<PosNote> *r_notes) const {
Pos from = p_from;
Pos to = p_to;
if (from.column > to.column) {
SWAP(from.column, to.column);
}
if (from.tick > to.tick) {
SWAP(from.tick, to.tick);
}
int fromidx;
int count;
get_notes_in_range(p_pattern, from, to, fromidx, count);
for (int i = 0; i < count; i++) {
PosNote pn;
pn.pos = get_note_pos_by_index(p_pattern, i + fromidx);
if (pn.pos.column < from.column || pn.pos.column > to.column)
continue;
pn.note = get_note_by_index(p_pattern, i + fromidx);
r_notes->push_back(pn);
}
}
////
void Track::set_command_columns(int p_columns) {
_AUDIO_LOCK_
ERR_FAIL_COND(p_columns < 0);
command_columns = p_columns;
for (int i = 0; i < column_state.size(); i++) {
column_state[i] = Command::EMPTY;
}
}
int Track::get_command_column_count() const {
return command_columns;
}
void Track::set_command(int p_pattern, Pos p_pos, Command p_command) {
_AUDIO_LOCK_
if (!command_data.has(p_pattern))
command_data[p_pattern] = ValueStream<Pos, Command>();
if (p_command.is_empty()) {
int idx = command_data[p_pattern].find_exact(p_pos);
if (idx < 0)
return;
command_data[p_pattern].erase(idx);
} else {
command_data[p_pattern].insert(p_pos, p_command);
}
}
Track::Command Track::get_command(int p_pattern, Pos p_pos) const {
const Map<int, ValueStream<Pos, Command> >::Element *E = command_data.find(p_pattern);
if (!E)
return Command();
int idx = E->get().find_exact(p_pos);
if (idx < 0)
return Command();
else
return E->get()[idx];
}
void Track::get_commands_in_range(int p_pattern, const Pos &p_from, const Pos &p_to, int &r_first, int &r_count) const {
const Map<int, ValueStream<Pos, Command> >::Element *E = command_data.find(p_pattern);
if (!E) {
r_count = 0;
return;
}
const ValueStream<Pos, Command> &vs = E->get();
if (vs.size() == 0) {
r_count = 0;
return;
}
int pos_beg = vs.find(p_from);
int pos_end = vs.find(p_to);
if (pos_end >= 0 && p_to == vs.get_pos(pos_end)) {
pos_end--;
}
if (pos_end < 0) {
r_count = 0;
return;
}
if (pos_beg < 0 || vs.get_pos(pos_beg) < p_from)
pos_beg++;
if (pos_beg > pos_end) {
r_count = 0;
return;
}
r_first = pos_beg;
r_count = pos_end - pos_beg + 1;
}
int Track::get_command_count(int p_pattern) const {
const Map<int, ValueStream<Pos, Command> >::Element *E = command_data.find(p_pattern);
if (!E)
return 0;
return E->get().size();
}
Track::Command Track::get_command_by_index(int p_pattern, int p_index) const {
const Map<int, ValueStream<Pos, Command> >::Element *E = command_data.find(p_pattern);
if (!E)
return Command();
const ValueStream<Pos, Command> &vs = E->get();
ERR_FAIL_INDEX_V(p_index, vs.size(), Command());
return vs[p_index];
}
Track::Pos Track::get_command_pos_by_index(int p_pattern, int p_index) const {
const Map<int, ValueStream<Pos, Command> >::Element *E = command_data.find(p_pattern);
if (!E)
return Pos();
const ValueStream<Pos, Command> &vs = E->get();
ERR_FAIL_INDEX_V(p_index, vs.size(), Pos());
return vs.get_pos(p_index);
}
void Track::get_commands_in_range(int p_pattern, const Pos &p_from, const Pos &p_to, List<PosCommand> *r_commands) const {
Pos from = p_from;
Pos to = p_to;
if (from.column > to.column) {
SWAP(from.column, to.column);
}
if (from.tick > to.tick) {
SWAP(from.tick, to.tick);
}
int fromidx;
int count;
get_commands_in_range(p_pattern, from, to, fromidx, count);
for (int i = 0; i < count; i++) {
PosCommand pn;
pn.pos = get_command_pos_by_index(p_pattern, i + fromidx);
if (pn.pos.column < from.column || pn.pos.column > to.column)
continue;
pn.command = get_command_by_index(p_pattern, i + fromidx);
r_commands->push_back(pn);
}
}
///
int Track::get_event_column_count() const {
return note_columns + command_columns + automations.size();
}
void Track::set_event(int p_pattern, int p_column, Tick p_pos, const Event &p_event) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_column, get_event_column_count());
if (p_column < note_columns) {
//note
ERR_FAIL_COND(p_event.type != Event::TYPE_NOTE);
Pos p;
p.column = p_column;
p.tick = p_pos;
set_note(p_pattern, p, p_event);
return;
}
p_column -= note_columns;
if (p_column < command_columns) {
//command
ERR_FAIL_COND(p_event.type != Event::TYPE_COMMAND);
Pos p;
p.column = p_column;
p.tick = p_pos;
set_command(p_pattern, p, p_event);
return;
}
p_column -= command_columns;
{
ERR_FAIL_COND(p_event.type != Event::TYPE_AUTOMATION);
for (int i = 0; i < automations.size(); i++) {
if (p_column == 0) {
get_automation(i)->set_point(p_pattern, p_pos, p_event.a);
return;
}
p_column--;
}
}
}
Track::Event::Type Track::get_event_column_type(int p_column) const {
if (p_column < note_columns)
return Event::TYPE_NOTE;
else if (p_column < note_columns + command_columns)
return Event::TYPE_COMMAND;
else
return Event::TYPE_AUTOMATION;
}
Track::Event Track::get_event(int p_pattern, int p_column, Tick p_pos) const {
ERR_FAIL_INDEX_V(p_column, get_event_column_count(), Event());
if (p_column < note_columns) {
//note
Pos p;
p.column = p_column;
p.tick = p_pos;
return get_note(p_pattern, p);
}
p_column -= note_columns;
if (p_column < command_columns) {
//command
Pos p;
p.column = p_column;
p.tick = p_pos;
return get_command(p_pattern, p);
}
p_column -= command_columns;
{
for (int i = 0; i < automations.size(); i++) {
if (p_column == 0) {
return get_automation(i)->get_point(p_pattern, p_pos);
}
p_column--;
}
}
ERR_FAIL_COND_V(true, Event());
}
void Track::get_events_in_range(int p_pattern, const Pos &p_from, const Pos &p_to, List<PosEvent> *r_events) const {
Map<Pos, Event> events;
Pos events_from = p_from;
Pos events_to = p_to;
if (events_from.column < note_columns) {
//has notes
List<PosNote> pn;
Pos end = events_to;
if (end.column >= note_columns)
end.column = note_columns - 1;
get_notes_in_range(p_pattern, events_from, end, &pn);
for (const List<PosNote>::Element *E = pn.front(); E; E = E->next()) {
events.insert(E->get().pos, E->get().note);
}
}
events_from.column -= note_columns;
events_to.column -= note_columns;
if (!(events_to.column < 0 || events_from.column >= command_columns)) {
//has commands
List<PosCommand> pn;
Pos from = events_from;
from.column = MAX(0, from.column);
Pos end = events_to;
end.column = MIN(command_columns - 1, end.column);
get_commands_in_range(p_pattern, from, end, &pn);
for (const List<PosCommand>::Element *E = pn.front(); E; E = E->next()) {
Pos evpos;
evpos = E->get().pos;
evpos.column += note_columns;
events.insert(evpos, E->get().command);
}
}
events_from.column -= command_columns;
events_to.column -= command_columns;
if (!(events_to.column < 0 || events_from.column >= automations.size())) {
//has commands
int begin = MAX(0, events_from.column);
int end = MIN(automations.size() - 1, events_to.column);
for (int i = begin; i <= end; i++) {
int f, c;
automations[i]->get_points_in_range(p_pattern, events_from.tick, events_to.tick, f, c);
for (int j = 0; j < c; j++) {
uint8_t v = automations[i]->get_point_by_index(p_pattern, j + f);
Tick t = automations[i]->get_point_tick_by_index(p_pattern, j + f);
Pos p;
p.column = i + note_columns + command_columns;
p.tick = t;
events.insert(p, v);
}
}
}
//add everything beautifully ordered
for (Map<Pos, Event>::Element *E = events.front(); E; E = E->next()) {
PosEvent pe;
pe.pos = E->key();
pe.event = E->get();
r_events->push_back(pe);
}
}
void Track::set_muted(bool p_mute) {
_AUDIO_LOCK_
if (muted == p_mute) {
return;
}
muted = p_mute;
first_mix = true; //clear memories when unmuted
}
bool Track::is_muted() const {
return muted;
}
void Track::set_mix_volume_db(float p_db) {
mix_volume = p_db;
}
float Track::get_mix_volume_db() const {
return mix_volume;
}
float Track::get_peak_volume_db_l() const {
float pvolume = linear2db(peak_volume_l);
peak_volume_l = 0;
return pvolume;
}
float Track::get_peak_volume_db_r() const {
float pvolume = linear2db(peak_volume_r);
peak_volume_r = 0;
return pvolume;
}
void Track::add_send(int p_track, int p_pos) {
_AUDIO_LOCK_
for (int i = 0; i < sends.size(); i++) {
ERR_FAIL_COND(sends[i].track == p_track);
}
Send send;
send.amount = 1.0;
send.track = p_track;
send.mute = false;
if (p_pos < 0 || p_pos >= sends.size()) {
sends.push_back(send);
} else {
sends.insert(p_pos, send);
}
}
void Track::set_send_amount(int p_send, float p_amount) {
ERR_FAIL_INDEX(p_send, sends.size());
sends[p_send].amount = p_amount;
}
void Track::set_send_track(int p_send, int p_track) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_send, sends.size());
//cant validate, unfortunately
#if 0
for (int i = 0; i < sends.size(); i++) {
if (sends[i].track == p_send) {
continue;
}
ERR_FAIL_COND(sends[i].track == p_track);
}
#endif
sends[p_send].track = p_track;
}
void Track::set_send_mute(int p_send, bool p_mute) {
ERR_FAIL_INDEX(p_send, sends.size());
sends[p_send].mute = p_mute;
}
int Track::get_send_track(int p_send) const {
ERR_FAIL_INDEX_V(p_send, sends.size(), SEND_SPEAKERS);
return sends[p_send].track;
}
float Track::get_send_amount(int p_send) const {
ERR_FAIL_INDEX_V(p_send, sends.size(), SEND_SPEAKERS);
return sends[p_send].amount;
}
bool Track::is_send_muted(int p_send) const {
ERR_FAIL_INDEX_V(p_send, sends.size(), false);
return sends[p_send].mute;
}
int Track::get_send_count() {
return sends.size();
}
void Track::remove_send(int p_send) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_send, sends.size());
sends.remove(p_send);
}
void Track::swap_sends(int p_send, int p_with_send) {
_AUDIO_LOCK_
ERR_FAIL_INDEX(p_send, sends.size());
ERR_FAIL_INDEX(p_with_send, sends.size());
SWAP(sends[p_send], sends[p_with_send]);
}
bool Track::has_send(int p_send) const {
for (int i = 0; i < sends.size(); i++) {
if (sends[i].track == p_send) {
return true;
}
}
return false;
}
void Track::set_process_buffer_size(int p_frames) {
_AUDIO_LOCK_
if (input_buffer.size() == p_frames) {
return;
}
input_buffer.resize(p_frames);
process_buffer.resize(p_frames);
process_buffer2.resize(p_frames);
for (int i = 0; i < effects.size(); i++) {
effects[i]->set_process_block_size(p_frames);
}
}
void Track::set_sampling_rate(int p_hz) {
if (sampling_rate == p_hz) {
return;
}
sampling_rate = p_hz;
for (int i = 0; i < effects.size(); i++) {
effects[i]->set_sampling_rate(p_hz);
}
}
Tick Track::_get_swinged_tick(Tick p_tick, int p_swing_divisor, float p_swing) {
if (p_swing_divisor < 0 || p_swing_divisor >= Song::SWING_BEAT_DIVISOR_MAX) {
return p_tick;
}
static const int divisors[Song::SWING_BEAT_DIVISOR_MAX] = { 1, 2, 3, 4, 6, 8 };
Tick tick_frac_size = TICKS_PER_BEAT / divisors[p_swing_divisor];
Tick tick_frac = p_tick % tick_frac_size;
Tick tick_debased = p_tick - tick_frac;
Tick swing_split = int(((1.0 + p_swing) * (double)tick_frac_size) / 2.0);
if (tick_frac <= swing_split) {
tick_frac = tick_frac * (tick_frac_size / 2) / swing_split;
} else {
tick_frac = tick_frac_size - tick_frac; //invert
Tick diff = tick_frac_size - swing_split;
if (diff == 0)
tick_frac = tick_frac_size;
else
tick_frac = tick_frac * (tick_frac_size / 2) / diff;
tick_frac = tick_frac_size - tick_frac; //revert
}
return tick_frac + tick_debased;
}
void Track::add_single_event(const AudioEffect::Event &p_event) {
if (event_buffer_size == EVENT_BUFFER_MAX) {
return;
}
event_buffer[event_buffer_size] = p_event;
event_buffer_size++;
}
void Track::process_events(int p_pattern, Tick p_offset, Tick p_from_tick, Tick p_to_tick, int p_bpm, int p_swing_divisor, float p_swing, int p_from, int p_to) {
if (event_buffer_size == EVENT_BUFFER_MAX) {
return;
}
if (p_offset == 0) {
//add the bpm
event_buffer[event_buffer_size].type = AudioEffect::Event::TYPE_BPM;
event_buffer[event_buffer_size].param8 = p_bpm;
event_buffer[event_buffer_size].paramf = 0;
event_buffer_size++;
}
p_from_tick = _get_swinged_tick(p_from_tick, p_swing_divisor, p_swing);
p_to_tick = _get_swinged_tick(p_to_tick, p_swing_divisor, p_swing);
int first, count;
get_notes_in_range(p_pattern, p_from_tick, p_to_tick, first, count);
double tick_to_frame = ((60.0 / double(p_bpm)) / double(TICKS_PER_BEAT)) * double(sampling_rate);
for (int i = 0; i < count; i++) {
if (event_buffer_size == EVENT_BUFFER_MAX) {
return;
}
Note note = get_note_by_index(p_pattern, first + i);
Pos pos = get_note_pos_by_index(p_pattern, first + i);
if (pos.column >= note_columns) {
continue;
}
if (p_from != -1 && p_from > pos.column) {
continue;
}
if (p_to != -1 && p_to < pos.column) {
continue;
}
int sample_offset = int(double(p_offset + (pos.tick - p_from_tick)) * tick_to_frame);
if (note.note != Note::EMPTY && column_state[pos.column] != Note::EMPTY) {
//note or note off, and note was playing in column: must turn off existing note
event_buffer[event_buffer_size].type = AudioEffect::Event::TYPE_NOTE_OFF;
event_buffer[event_buffer_size].param8 = column_state[pos.column];
if (note.volume == Note::EMPTY || note.note == Note::OFF) { //new note, note-off quickly
event_buffer[event_buffer_size].paramf = 1.0;
} else {
event_buffer[event_buffer_size].paramf = note.volume / 99.0; // map 0 .. 1
}
event_buffer[event_buffer_size].offset = sample_offset;
event_buffer_size++;
column_state[pos.column] = Note::EMPTY; //clear
}
if (note.note <= Note::MAX_NOTE) {
//note on
event_buffer[event_buffer_size].type = AudioEffect::Event::TYPE_NOTE;
event_buffer[event_buffer_size].param8 = note.note;
if (note.volume == Note::EMPTY) {
event_buffer[event_buffer_size].paramf = 1.0;
} else {
event_buffer[event_buffer_size].paramf = note.volume / 99.0; // map 0 .. 1
}
event_buffer[event_buffer_size].offset = sample_offset;
column_state[pos.column] = note.note; //clear
event_buffer_size++;
} else if (note.note == Note::EMPTY && note.volume != Note::EMPTY && column_state[pos.column] != Note::EMPTY) {
//send volume change alone (may be supported via midi note pressure for regular MIDI)
event_buffer[event_buffer_size].type = AudioEffect::Event::TYPE_AFTERTOUCH;
event_buffer[event_buffer_size].param8 = column_state[pos.column];
event_buffer[event_buffer_size].paramf = note.volume / 99.0; // map 0 .. 1
event_buffer[event_buffer_size].offset = sample_offset;
event_buffer_size++;
}
}
int from_ofs = column_state.size();
//process events
get_commands_in_range(p_pattern, p_from_tick, p_to_tick, first, count);
for (int i = 0; i < count; i++) {
Command command = get_command_by_index(p_pattern, first + i);
if (command.command == Command::EMPTY) {
//well, do nothing.
continue;
}
Pos pos = get_command_pos_by_index(p_pattern, first + i);
if (pos.column >= command_columns) {
//may be hidden
continue;
}
if (p_from != -1 && p_from > pos.column + from_ofs) {
continue;
}
if (p_to != -1 && p_to < pos.column + from_ofs) {
continue;
}
//go on a quest to find the control ports and send commands, this should be optimized, though not as many commands
//are processed, so its not too bad.
float param_value = float(command.parameter) / float(Command::MAX_PARAM);
for (int j = 0; j < effects.size(); j++) {
AudioEffect *fx = effects[j];
for (int k = 0; k < fx->get_control_port_count(); k++) {
ControlPort *control_port = fx->get_control_port(k);
if (control_port->get_command() == command.command) {
control_port->set_normalized(param_value);
}
}
}
}
from_ofs += command_columns;
for (int j = 0; j < automations.size(); j++) {
if (p_from != -1 && p_from > (j + from_ofs)) {
continue;
}
if (p_to != -1 && p_to < (j + from_ofs)) {
continue;
}
Automation *a = automations[j];
if (a->get_edit_mode() == Automation::EDIT_ROWS_DISCRETE) {
//set the row, without interpolation
a->get_points_in_range(p_pattern, p_from_tick, p_to_tick, first, count);
for (int i = 0; i < count; i++) {
int value = a->get_point_by_index(p_pattern, first + i);
float valuef = float(value) / 99.0;
a->get_control_port()->set_normalized(valuef);
}
} else {
//interpolate
float value = a->interpolate_offset(p_pattern, p_to_tick);
if (value == -1) {
//if to is empty, use the value in from
value = a->interpolate_offset(p_pattern, p_from_tick);
}
if (value != -1) {
//must be something in there
a->get_control_port()->set_normalized(value);
}
}
}
}
const AudioFrame *Track::process_audio_step() {
//see if any of the effects uses the secondary input
int effect_count = effects.size();
AudioEffect **effects_ptr = effect_count ? &effects[0] : NULL;
bool has_side_input = false;
for (int i = 0; i < effect_count; i++) {
if (effects_ptr[i]->has_secondary_input() && !effects_ptr[i]->is_skipped()) {
has_side_input = true;
}
}
int buffer_len = input_buffer.size();
const AudioFrame *input_buffer_ptr = &input_buffer[0];
AudioFrame *process_buffer_src_ptr = &process_buffer[0];
AudioFrame *process_buffer_dst_ptr = &process_buffer2[0];
if (!has_side_input || muted) {
//no side input, just copy input to process
for (int i = 0; i < buffer_len; i++) {
process_buffer_src_ptr[i] = input_buffer_ptr[i];
}
} else {
//has side input, which will eventually be used.
//zero the buffer
for (int i = 0; i < buffer_len; i++) {
process_buffer_src_ptr[i] = AudioFrame(0, 0);
}
}
if (!muted) {
for (int i = 0; i < effect_count; i++) {
if (effects_ptr[i]->is_skipped()) {
continue;
}
if (effects_ptr[i]->has_secondary_input()) {
effects_ptr[i]->process_with_secondary(event_buffer, event_buffer_size, process_buffer_src_ptr, input_buffer_ptr, process_buffer_dst_ptr, first_mix);
} else {
effects_ptr[i]->process(event_buffer, event_buffer_size, process_buffer_src_ptr, process_buffer_dst_ptr, first_mix);
}
SWAP(process_buffer_src_ptr, process_buffer_dst_ptr);
}
}
//apply volume
{
float volume = db2linear(mix_volume);
for (int i = 0; i < buffer_len; i++) {
process_buffer_src_ptr[i] *= volume;
float energy_l = ABS(process_buffer_src_ptr[i].l);
if (energy_l > peak_volume_l) {
peak_volume_l = energy_l;
}
float energy_r = ABS(process_buffer_src_ptr[i].r);
if (energy_r > peak_volume_r) {
peak_volume_r = energy_r;
}
}
}
//clear mix and event count
first_mix = false;
event_buffer_size = 0;
return process_buffer_src_ptr;
}
void Track::stop() {
for (int i = 0; i < effects.size(); i++) {
AudioEffect *fx = effects[i];
fx->reset();
}
for (int i = 0; i < column_state.size(); i++) {
column_state[i] = Note::EMPTY;
}
event_buffer_size = 0;
first_mix = true;
}
void Track::automations_pre_play_capture() {
for (int i = 0; i < automations.size(); i++) {
automations[i]->pre_play_capture();
}
}
void Track::automations_pre_play_restore() {
for (int i = 0; i < automations.size(); i++) {
automations[i]->pre_play_restore();
}
}
Track::Track() {
name = "New Track";
note_columns = 1;
command_columns = 0;
sampling_rate = 44100;
mix_volume = 0;
event_buffer_size = 0;
muted = false;
first_mix = true;
column_state.resize(1);
column_state[0] = Note::EMPTY;
peak_volume_l = peak_volume_r = -900;
}
Track::~Track() {
for (int i = 0; i < effects.size(); i++) {
delete effects[i];
}
for (int i = 0; i < automations.size(); i++) {
delete automations[i];
}
for (int i = 0; i < disabled_automations.size(); i++) {
delete disabled_automations[i];
}
}
|