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 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
|
#include "snd.h"
/* -------------------------------- EDIT LISTS -------------------------------- *
*
* each channel has a list of lists containing the current edit history and the associated sound temp files or buffers
* undo: back up current list position
* redo: push position foward
* No actual changes are flushed out to the file system until the file is saved.
*
* the three editing possibilities are insert, change, delete. All input goes through these lists.
*/
static char edit_buf[256];
/* each block in an edit-list list describes one fragment of the current sound */
#define ED_OUT 0
#define ED_SND 1
#define ED_BEG 2
#define ED_END 3
#define ED_SIZE 4
#define EDIT_LIST_END_MARK -2
#define EDIT_ALLOC_SIZE 128
/* ED_SND is either an index into the cp->sounds array (snd_data structs) or EDIT_LIST_END_MARK */
/* ED_BEG and ED_END are indices into the associated data => start/end point of data used in current segment */
/* ED_OUT is running segment location within current overall edited data */
/* EDIT_ALLOC_SIZE is the allocation amount (pointers) each time cp->sounds is (re)allocated */
#define INSERTION_EDIT 0
#define DELETION_EDIT 1
#define CHANGE_EDIT 2
#define INITIALIZE_EDIT 3
#define PACK_EDIT(a,b) ((a)<<16 | (b))
#define EDIT_TYPE(a) (((a) >> 16) & 0xffff)
#define EDIT_LOCATION(a) ((a) & 0xffff)
/* edit history decoding info */
/* color could be added at this level ED_COLOR 4, ED_SIZE 5, then sf->cb[ED_COLOR] during edit tree read */
static char *edit_names[4] = {"insert","delete","set",""};
static void display_ed_list(int i, ed_list *ed)
{
int len,j,type;
len=ed->size; /* number of fragments in this list */
type = EDIT_TYPE(ed->sfnum);
switch (type)
{
case INSERTION_EDIT: snd_error("\n (insert %d %d) ",ed->beg,ed->len); break;
case DELETION_EDIT: snd_error("\n (delete %d %d) ",ed->beg,ed->len); break;
case CHANGE_EDIT: snd_error("\n (set %d %d) ",ed->beg,ed->len); break;
case INITIALIZE_EDIT: snd_error("\n (begin) "); break;
}
if (ed->origin) snd_error("; %s ",ed->origin);
snd_error("[%d:%d]:",i,len);
for (j=0;j<len;j++)
{
snd_error("\n (at %d, cp->sounds[%d][%d:%d])",
ed->fragments[j*ED_SIZE+ED_OUT],
ed->fragments[j*ED_SIZE+ED_SND],
ed->fragments[j*ED_SIZE+ED_BEG],
ed->fragments[j*ED_SIZE+ED_END]);
}
}
static char edbuf[128];
char *edit_to_string(ed_list *ed)
{
/* only for edit list in snd-xchn.c */
sprintf(edbuf,"%s : (%s %d %d)",ed->origin,edit_names[EDIT_TYPE(ed->sfnum)],ed->beg,ed->len);
return(edbuf);
}
static void display_edits(chan_info *cp)
{
int eds,i;
ed_list *ed;
eds = cp->edit_ctr;
snd_error("\nEDITS: %d\n",eds);
for (i=0;i<=eds;i++)
{
ed = cp->edits[i];
if (!ed) {snd_error("ctr is %d, but [%d] is nil!",eds,i); abort();}
display_ed_list(i,ed);
}
}
static void edit_data_to_file(FILE *fd, ed_list *ed, chan_info *cp)
{
snd_state *ss;
snd_data *sf;
int i,snd;
snd = EDIT_LOCATION(ed->sfnum);
if (snd < cp->sound_size)
{
sf = cp->sounds[snd];
if (sf->type == SND_DATA_BUFFER)
{
for (i=0;i<ed->len;i++)
fprintf(fd,"%d ",sf->data[i]);
}
else
{
/* read at very low level */
int ifd,idataloc,bufnum,n,cursamples,samples,sample;
int *buffer;
int **ibufs;
ss = cp->state;
ifd = mus_open_read(sf->filename);
mus_open_file_descriptors(ifd,sound_data_format(sf->filename),sound_datum_size(sf->filename),sound_data_location(sf->filename));
idataloc = sound_data_location(sf->filename);
samples = sound_samples(sf->filename);
mus_seek(ifd,idataloc,SEEK_SET);
ibufs = (int **)CALLOC(1,sizeof(int *));
ibufs[0] = (int *)CALLOC(FILE_BUFFER_SIZE,sizeof(int));
bufnum = (FILE_BUFFER_SIZE);
sample = 0;
for (n=0;n<samples;n+=bufnum)
{
if ((n+bufnum)<samples) cursamples = bufnum; else cursamples = (samples-n);
mus_read(ifd,0,cursamples-1,1,ibufs);
buffer = (int *)(ibufs[0]);
for (i=0;i<cursamples;i++)
{
fprintf(fd,"%d ",buffer[i]);
sample++;
if (sample == ed->len)
{
mus_close(ifd);
return;
}
}
}
mus_close(ifd);
}
}
}
void edit_history_to_file(FILE *fd, chan_info *cp)
{
/* write edit list as a snd-guile program to fd (open for writing) for subsequent load */
/* this needs only 3 operations: delete-samples, insert-samples, and change-samples */
/* the actual user-operations that produced these are included as comments */
/* the data is included as a vector, so the file can be very large! */
/* the entire current list is written, then the edit_ctr is fixed up to reflect its current state */
int i,edits;
ed_list *ed;
edits = cp->edit_ctr;
while ((edits<(cp->edit_size-1)) && (cp->edits[edits+1])) edits++;
/* 0 case = open-sound */
for (i=1;i<=edits;i++)
{
ed = cp->edits[i];
if (ed)
{
switch (EDIT_TYPE(ed->sfnum))
{
case INSERTION_EDIT:
/* samp data snd chn */
fprintf(fd," (%s %d %d \"%s\" #(",S_insert_int_samples,ed->beg,ed->len,(ed->origin) ? ed->origin : "");
edit_data_to_file(fd,ed,cp);
fprintf(fd,") sfile %d)\n",cp->chan);
break;
case DELETION_EDIT:
/* samp samps snd chn */
fprintf(fd," (%s %d %d \"%s\" sfile %d)\n",S_delete_int_samples,ed->beg,ed->len,(ed->origin) ? ed->origin : "",cp->chan);
break;
case CHANGE_EDIT:
fprintf(fd," (%s %d %d \"%s\" #(",S_set_int_samples,ed->beg,ed->len,(ed->origin) ? ed->origin : "");
edit_data_to_file(fd,ed,cp);
fprintf(fd,") sfile %d)\n",cp->chan);
break;
}
}
}
if (cp->edit_ctr < edits) fprintf(fd," (undo %d sfile %d)\n",edits-cp->edit_ctr,cp->chan);
save_mark_list(fd,cp);
}
static void copy_ed_blocks(int *new_list, int *old_list, int new_beg, int old_beg, int num_lists)
{
/* beg and num_lists here are in terms of blocks, not ints */
int i,end,k;
if (num_lists > 0)
{
end = (old_beg+num_lists)*ED_SIZE;
for (k=new_beg*ED_SIZE,i=old_beg*ED_SIZE;i<end;i++,k++) {new_list[k] = old_list[i];}
}
}
static ed_list *make_ed_list(int size)
{
ed_list *ed;
ed=(ed_list *)CALLOC(1,sizeof(ed_list));
ed->size = size;
ed->fragments = (int *)CALLOC(size*ED_SIZE,sizeof(int));
ed->origin = NULL;
return(ed);
}
static ed_list *free_ed_list(ed_list *ed)
{
if (ed)
{
if (ed->fragments) FREE(ed->fragments);
if (ed->origin) FREE(ed->origin);
FREE(ed);
}
return(NULL);
}
void backup_edit_list(chan_info *cp)
{
int cur;
cur = cp->edit_ctr;
free_ed_list(cp->edits[cur - 1]);
cp->edits[cur - 1] = cp->edits[cur];
cp->edits[cur] = NULL;
cp->samples[cur - 1] = cp->samples[cur];
cp->edit_ctr--;
reflect_edit_history_change(cp);
}
void free_edit_list(chan_info *cp)
{
int i;
if (cp)
{
if (cp->edits)
{
for (i=0;i<cp->edit_size;i++)
{
/* cp->edit_ctr follows current edit state (redo/undo) */
if (cp->edits[i]) free_ed_list(cp->edits[i]);
}
FREE(cp->edits);
}
cp->edits = NULL;
cp->edit_ctr = -1;
cp->edit_size = 0;
}
}
ed_list *initial_ed_list(int beg, int end)
{
ed_list *ed;
ed = make_ed_list(2);
ed->beg = beg;
ed->len = end+1;
ed->sfnum = PACK_EDIT(INITIALIZE_EDIT,0);
/* origin (channel %s %d) desc channel should be obvious from context */
ed->fragments[0*ED_SIZE + ED_BEG] = beg;
ed->fragments[0*ED_SIZE + ED_END] = end;
ed->fragments[0*ED_SIZE + ED_SND] = 0;
ed->fragments[0*ED_SIZE + ED_OUT] = 0;
/* second block is our end-of-tree marker */
ed->fragments[1*ED_SIZE + ED_BEG] = 0;
ed->fragments[1*ED_SIZE + ED_END] = 0;
ed->fragments[1*ED_SIZE + ED_SND] = EDIT_LIST_END_MARK;
ed->fragments[1*ED_SIZE + ED_OUT] = end+1;
return(ed);
}
static int find_split_loc (chan_info *cp, int samp, ed_list *current_state)
{
int i,k;
if (!current_state) {snd_error("%s[%d] %s: current state missing!",__FILE__,__LINE__,__FUNCTION__); abort();}
for (i=0,k=0;i<current_state->size;i++,k+=ED_SIZE)
{
if (current_state->fragments[k+ED_OUT] >= samp) return(i);
}
snd_error("%s[%d] %s: failed at %d!\n",__FILE__,__LINE__,__FUNCTION__,samp);
snd_error("looking at: ");
display_ed_list(-1,current_state);
snd_error("\n in: \n");
display_edits(cp);
abort();
return(0); /* make sgi compiler happy */
}
snd_data *make_snd_data_file(char *name, int *io, int *data, file_info *hdr, int temp, int ctr, int temp_chan)
{
snd_data *sf;
sf = (snd_data *)CALLOC(1,sizeof(snd_data));
sf->type = SND_DATA_FILE;
sf->data = data;
sf->io = io;
sf->filename = (char *)CALLOC(strlen(name)+1,sizeof(char));
strcpy(sf->filename,name);
sf->hdr = hdr;
sf->temporary = temp;
sf->edit_ctr = ctr;
sf->open = FD_OPEN;
sf->inuse = FALSE;
sf->copy = FALSE;
sf->chan = temp_chan;
sf->len = (hdr->samples)*(mus_format2bytes(hdr->format)) + hdr->data_location;
return(sf);
}
static snd_data *copy_snd_data(snd_data *sd, chan_info *cp)
{
/* see note under init_sample_read */
snd_data *sf;
int *datai;
int fd;
file_info *hdr;
hdr = sd->hdr;
fd = snd_open_read(cp->state,sd->filename);
mus_open_file_descriptors(fd,hdr->format,mus_format2bytes(hdr->format),hdr->data_location);
datai = make_file_state(fd,hdr,SND_IO_IN_FILE,sd->chan,MIX_FILE_BUFFER_SIZE,cp->state);
sf = (snd_data *)CALLOC(1,sizeof(snd_data));
sf->type = sd->type;
#if LONG_INT_P
sf->data = delist_ptr(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+sd->chan]);
#else
sf->data = (int *)(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+sd->chan]);
#endif
sf->io = datai;
sf->filename = (char *)CALLOC(strlen(sd->filename)+1,sizeof(char));
strcpy(sf->filename,sd->filename);
sf->hdr = hdr;
sf->temporary = 0;
sf->edit_ctr = sd->edit_ctr;
sf->open = FD_OPEN;
sf->inuse = FALSE;
sf->copy = TRUE;
return(sf);
}
snd_data *make_snd_data_buffer(int *data, int len, int ctr)
{
snd_data *sf;
int i;
sf = (snd_data *)CALLOC(1,sizeof(snd_data));
sf->type = SND_DATA_BUFFER;
sf->data = (int *)CALLOC(len+1,sizeof(int));
/* sigh... using len+1 rather than len to protect against access to inserted buffer at end mixups (final fragment uses end+1) */
/* the real problem here is that I never decided whether insert starts at the cursor or just past it */
/* when the cursor is on the final sample, this causes cross-fragment ambiguity as to the length of a trailing insertion */
/* C > (make-region 1000 2000) (insert-region (cursor)) C-v hits this empty slot and gets confused about the previously final sample value */
for (i=0;i<len;i++) sf->data[i] = data[i];
sf->edit_ctr = ctr;
sf->copy = FALSE;
sf->inuse = FALSE;
sf->len = len*4;
return(sf);
}
static snd_data *free_snd_data(snd_data *sf)
{
/* in the snd file case, these pointers are dealt with elsewhere */
if (sf)
{
if ((sf->type != SND_DATA_BUFFER) && (sf->type != SND_DATA_FILE))
{
snd_error("%s[%d] %s: data type = %d?",__FILE__,__LINE__,__FUNCTION__,sf->type);
#ifdef DEBUGGING
abort();
#endif
return(NULL); /* if not debugging, try to go on... */
}
if ((sf->type == SND_DATA_BUFFER) && (sf->data)) FREE(sf->data);
sf->data = NULL;
if ((!(sf->copy)) && (sf->hdr)) free_file_info(sf->hdr);
sf->hdr = NULL;
if (sf->io)
{
if (sf->open == FD_OPEN) snd_close(sf->io[SND_IO_FD]);
sf->io = free_file_state(sf->io);
if (sf->temporary == DELETE_ME)
{
remove(sf->filename);
forget_sound(sf->filename);
}
FREE(sf->filename);
}
FREE(sf);
}
return(NULL);
}
void free_sound_list (chan_info *cp)
{
int i;
if (cp)
{
if (cp->sounds)
{
for (i=0;i<cp->sound_size;i++)
{
if (cp->sounds[i]) cp->sounds[i] = free_snd_data(cp->sounds[i]);
}
FREE(cp->sounds);
cp->sounds = NULL;
}
cp->sound_ctr = -1;
cp->sound_size = 0;
}
}
static int gather_usage_stats_1(chan_info *cp, void *ptr)
{
int i;
snd_data *sf;
if (cp)
{
if (cp->stats)
{for (i=0;i<6;i++) cp->stats[i] = 0;}
else cp->stats = (int *)CALLOC(6,sizeof(int));
if (cp->sounds)
{
for (i=0;i<cp->sound_size;i++)
{
if ((sf = (cp->sounds[i])))
{
if (sf->type == SND_DATA_BUFFER)
{
if (sf->data)
{
cp->stats[ARRAY_USAGE] += sf->len;
cp->stats[ARRAYS_ACTIVE]++;
}
}
else
{
if (sf->io)
{
cp->stats[ARRAY_USAGE] += (sf->io[SND_IO_BUFSIZ]*4);
cp->stats[ARRAYS_ACTIVE]++;
}
if (sf->temporary == DELETE_ME)
{
cp->stats[TEMP_USAGE] += sf->len;
cp->stats[TEMPS_ACTIVE]++;
if (sf->open == FD_OPEN) cp->stats[TEMPS_OPEN]++;
}
else
cp->stats[FILE_USAGE] += (sf->len)/((cp->sound)->nchans);
}}}}}
return(0);
}
void gather_usage_stats(chan_info *cp)
{
gather_usage_stats_1(cp,NULL);
update_stats_display(cp->state,FALSE);
}
void update_all_usage_stats(snd_state *ss)
{
map_over_chans(ss,gather_usage_stats_1,NULL);
}
static void release_pending_sounds(chan_info *cp, int edit_ctr)
{
/* look for buffers or open temp files that are no longer reachable after pruning the edit tree */
int i,j,del;
snd_data *sf;
if (cp)
{
if (cp->sounds)
{
del= -1;
for (i=0;i<cp->sound_size;i++)
{
if ((sf = (cp->sounds[i])))
{
if (sf->edit_ctr >= edit_ctr)
{
cp->sounds[i] = free_snd_data(sf);
if (del == -1) del = i;
}
}
}
if (del != -1)
{
for (j=del,i=del;i<cp->sound_size;i++)
{
if (cp->sounds[i])
{
if (j != i)
{
cp->sounds[j] = cp->sounds[i];
cp->sounds[i] = NULL;
j++;
}
}
}
cp->sound_ctr = j-1;
}
}
if (show_usage_stats(cp->state)) gather_usage_stats(cp);
}
}
static void prepare_sound_list (chan_info *cp)
{
int i;
cp->sound_ctr++;
/* this is the only place the sound set is incremented */
if (cp->sound_ctr >= cp->sound_size)
{
cp->sound_size += EDIT_ALLOC_SIZE;
cp->sounds = (snd_data **)REALLOC(cp->sounds,cp->sound_size * sizeof(snd_data *));
for (i=cp->sound_ctr;i<cp->sound_size;i++) cp->sounds[i] = NULL;
}
if (cp->sounds[cp->sound_ctr]) cp->sounds[cp->sound_ctr] = free_snd_data(cp->sounds[cp->sound_ctr]);
}
static int add_sound_buffer_to_edit_list(chan_info *cp, int *data, int len)
{
prepare_sound_list(cp);
cp->sounds[cp->sound_ctr] = make_snd_data_buffer(data,len,cp->edit_ctr);
if (show_usage_stats(cp->state)) gather_usage_stats(cp);
return(cp->sound_ctr);
}
static int add_sound_file_to_edit_list(chan_info *cp, char *name, int *io, int *data, file_info *hdr, int temp, int chan)
{
prepare_sound_list(cp);
cp->sounds[cp->sound_ctr] = make_snd_data_file(name,io,data,hdr,temp,cp->edit_ctr,chan);
if (show_usage_stats(cp->state)) gather_usage_stats(cp);
return(cp->sound_ctr);
}
static void ripple_out(int *list,int beg,int num, int len)
{
int i,k;
for (i=beg,k=beg*ED_SIZE;i<len;i++,k+=ED_SIZE) list[k+ED_OUT] += num;
}
static void prepare_edit_list(chan_info *cp, int len)
{
int i;
snd_info *sp;
sp = cp->sound;
if ((sp) && (sp->playing)) stop_playing_sound(sp);
cp->edit_ctr++;
if (cp->edit_ctr >= cp->edit_size)
{
cp->edit_size += EDIT_ALLOC_SIZE;
if (!cp->edits) cp->edits = (ed_list **)CALLOC(cp->edit_size,sizeof(ed_list *));
else cp->edits = (ed_list **)REALLOC(cp->edits,cp->edit_size*sizeof(ed_list *));
for (i=cp->edit_ctr;i<cp->edit_size;i++) cp->edits[i] = NULL;
if (!cp->samples) cp->samples = (int *)CALLOC(cp->edit_size,sizeof(int));
else cp->samples = (int *)REALLOC(cp->samples,cp->edit_size*sizeof(int));
}
if (cp->edits[cp->edit_ctr])
{
for (i=cp->edit_ctr;i<cp->edit_size;i++) cp->edits[i] = free_ed_list(cp->edits[i]);
release_pending_marks(cp,cp->edit_ctr);
release_pending_mixes(cp,cp->edit_ctr);
release_pending_sounds(cp,cp->edit_ctr);
reflect_no_more_redo_in_menu();
}
reflect_undo_ok_in_menu();
cp->samples[cp->edit_ctr] = len;
}
int current_ed_samples(chan_info *cp)
{
if (cp)
return(cp->samples[cp->edit_ctr]);
else return(0);
}
static void reflect_sample_change_in_axis(chan_info *cp)
{
axis_info *ap;
int samps;
ap = cp->axis;
if (ap)
{
samps = current_ed_samples(cp);
ap->xmax = (double)samps/(double)snd_SRATE(cp);
if (ap->x1 > ap->xmax) ap->x1 = ap->xmax;
if ((samps == 0) || (ap->no_data))
{
ap->no_data = (samps == 0);
if (ap->xlabel) FREE(ap->xlabel);
if (samps == 0) ap->xlabel = copy_string(STR_no_data); else ap->xlabel = copy_string(STR_time);
}
set_x_bounds(ap);
}
clobber_amp_env(cp);
}
#ifdef DEBUGGING
static void fixup_edlist_endmark(ed_list *new_state, ed_list *current_state, int len, char *name, int samp, int num, chan_info *cp, char *origin)
#else
static void fixup_edlist_endmark(ed_list *new_state, ed_list *current_state, int len)
#endif
{
int k;
if (new_state->fragments[(new_state->size - 1)*ED_SIZE + ED_SND] != EDIT_LIST_END_MARK)
{
for (k = new_state->size-1;k>0;k--)
if (new_state->fragments[k*ED_SIZE + ED_SND] == EDIT_LIST_END_MARK) break;
if (k>0)
new_state->size = k+1;
else
{
if (new_state->size == current_state->size)
{
if (new_state->size < (len+1))
{
k = new_state->size;
new_state->size += 1;
new_state->fragments[ED_SIZE*new_state->size + ED_SND] = EDIT_LIST_END_MARK;
new_state->fragments[ED_SIZE*new_state->size + ED_BEG] = new_state->fragments[ED_SIZE*k + ED_BEG];
new_state->fragments[ED_SIZE*new_state->size + ED_OUT] = new_state->fragments[ED_SIZE*k + ED_OUT];
new_state->fragments[ED_SIZE*new_state->size + ED_END] = new_state->fragments[ED_SIZE*k + ED_END];
#ifdef DEBUGGING
k=1;
#endif
}
}
}
#ifdef DEBUGGING
if (k==0)
{
/* any change has to end up with a 2-list? */
if (current_state->fragments[(current_state->size - 1)*ED_SIZE + ED_SND] != EDIT_LIST_END_MARK)
{
snd_error("current state is screwed up");
abort();
}
snd_error("we appear to have %d extra blocks\n",new_state->size - k -1);
snd_error("%s (%s) samp: %d, num: %d, cp: %d (%s chan %d)\n",
name,origin,samp,num,(int)cp,(cp->sound)->shortname,cp->chan);
display_ed_list(cp->edit_ctr-1,current_state);
display_ed_list(cp->edit_ctr,new_state);
abort();
}
#endif
}
}
static ed_list *insert_samples_1 (int samp, int num, int* vals, ed_list *current_state, chan_info *cp, int **cb_back, char *origin)
{
int len,k,old_beg,old_end,old_snd,old_out;
ed_list *new_state;
int *cb,*cbback,*ed;
len = current_state->size;
ed = current_state->fragments;
k=find_split_loc(cp,samp,current_state);
cb = (int *)(ed + k*ED_SIZE);
if ((samp == cb[ED_OUT]) || (samp == (cb[ED_OUT] - 1)))
{
new_state = make_ed_list(len+1);
copy_ed_blocks(new_state->fragments,ed,0,0,k);
copy_ed_blocks(new_state->fragments,ed,k+1,k,len-k);
len++;
}
else
{
cbback = (int *)(ed + (k-1)*ED_SIZE);
old_beg = cbback[ED_BEG];
old_end = cbback[ED_END];
old_snd = cbback[ED_SND];
old_out = cbback[ED_OUT];
new_state = make_ed_list(len+2);
copy_ed_blocks(new_state->fragments,ed,0,0,k);
copy_ed_blocks(new_state->fragments,ed,k+2,k,len-k);
cb = (int *)(new_state->fragments + (k+1)*ED_SIZE); /* old after split */
cbback = (int *)(new_state->fragments + (k-1)*ED_SIZE); /* old before split */
cb[ED_SND] = old_snd;
cb[ED_OUT] = samp;
cb[ED_BEG] = old_beg+samp-old_out;
cb[ED_END] = old_end;
cbback[ED_END] = old_beg+samp-old_out-1;
len += 2;
}
cb = (int *)(new_state->fragments + k*ED_SIZE); /* new */
cb[ED_BEG] = 0;
cb[ED_END] = num-1;
cb[ED_OUT] = samp;
if (vals)
{
cb[ED_SND] = add_sound_buffer_to_edit_list(cp,vals,num);
new_state->sfnum = PACK_EDIT(INSERTION_EDIT,cb[ED_SND]);
}
else (*cb_back) = cb;
/* vals is null if data is in a file -- handled later */
new_state->beg = samp;
new_state->len = num;
if (origin) new_state->origin = copy_string(origin);
ripple_out(new_state->fragments,k+1,num,len);
ripple_marks(cp,samp,num);
ripple_mixes(cp,samp,num);
ripple_selection(cp,samp,num);
reflect_sample_change_in_axis(cp);
#ifdef DEBUGGING
fixup_edlist_endmark(new_state,current_state,len,"insert-samples",samp,num,cp,origin);
#else
fixup_edlist_endmark(new_state,current_state,len);
#endif
return(new_state);
}
static void extend_insertion(int beg, int num, int *vals, chan_info *cp, char *origin)
{
int k,len;
int *cb;
len = current_ed_samples(cp);
k=cp->edit_ctr;
prepare_edit_list(cp,len+num);
cp->edits[cp->edit_ctr] = insert_samples_1(beg,num,vals,cp->edits[k],cp,&cb,origin);
reflect_edit_history_change(cp);
}
void file_insert_samples(int beg, int num, char *inserted_file, chan_info *cp, int chan, int auto_delete, char *origin)
{
int k,len;
int *cb,*zeros;
int fd;
int *datai;
ed_list *ed;
file_info *hdr;
len = current_ed_samples(cp);
if (beg >= len)
{
zeros = (int *)CALLOC(beg-len+1,sizeof(int));
extend_insertion(len,beg-len+1,zeros,cp,"(insert-extend)");
check_for_first_edit(cp); /* needed to activate revert menu option */
FREE(zeros);
len = current_ed_samples(cp);
}
k=cp->edit_ctr;
prepare_edit_list(cp,len+num);
cp->edits[cp->edit_ctr] = insert_samples_1(beg,num,NULL,cp->edits[k],cp,&cb,origin);
reflect_edit_history_change(cp);
hdr = make_file_info(inserted_file,cp->state);
if (hdr)
{
fd = snd_open_read(cp->state,inserted_file);
mus_open_file_descriptors(fd,hdr->format,mus_format2bytes(hdr->format),hdr->data_location);
datai = make_file_state(fd,hdr,SND_IO_IN_FILE,chan,FILE_BUFFER_SIZE,cp->state);
#if LONG_INT_P
cb[ED_SND] = add_sound_file_to_edit_list(cp,inserted_file,datai,delist_ptr(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+chan]),hdr,auto_delete,chan);
#else
cb[ED_SND] = add_sound_file_to_edit_list(cp,inserted_file,datai,(int *)(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+chan]),hdr,auto_delete,chan);
#endif
ed = cp->edits[cp->edit_ctr];
ed->sfnum = PACK_EDIT(INSERTION_EDIT,cb[ED_SND]);
lock_affected_mixes(cp,beg,beg+num);
}
}
void insert_samples(int beg, int num, int *vals, chan_info *cp, char *origin)
{
int k,len;
int *cb,*zeros;
len = current_ed_samples(cp);
if (beg >= len)
{
zeros = (int *)CALLOC(beg-len+1,sizeof(int));
extend_insertion(len,beg-len+1,zeros,cp,"(insert-extend)");
check_for_first_edit(cp); /* needed to activate revert menu option */
FREE(zeros);
len = current_ed_samples(cp);
}
k=cp->edit_ctr;
prepare_edit_list(cp,len+num);
cp->edits[cp->edit_ctr] = insert_samples_1(beg,num,vals,cp->edits[k],cp,&cb,origin);
reflect_edit_history_change(cp);
lock_affected_mixes(cp,beg,beg+num);
}
static ed_list *delete_samples_1(int beg, int num, ed_list *current_state, chan_info *cp, char *origin)
{
int len,k,need_to_delete,curbeg,old_out,cbi,start_del,len_fixup;
int *cb,*temp_cb;
ed_list *new_state;
len=current_state->size;
len_fixup = -1;
k=find_split_loc(cp,beg,current_state);
need_to_delete = num;
start_del = k;
curbeg = beg;
cb = (int *)(current_state->fragments + k*ED_SIZE);
if (cb[ED_OUT]>beg) start_del--;
new_state = make_ed_list(len+1);
copy_ed_blocks(new_state->fragments,current_state->fragments,0,0,start_del);
cbi=start_del;
temp_cb = (int *)(current_state->fragments + start_del*ED_SIZE);
old_out = temp_cb[ED_OUT];
if (beg>old_out)
{
cb = (int *)(new_state->fragments + start_del*ED_SIZE);
cb[ED_OUT] = old_out;
cb[ED_SND] = temp_cb[ED_SND];
cb[ED_BEG] = temp_cb[ED_BEG];
cb[ED_END] = temp_cb[ED_BEG]+beg-old_out-1;
start_del++;
len_fixup++;
}
while (need_to_delete > 0)
{
old_out = (current_state->fragments[(cbi+1)*ED_SIZE+ED_OUT]);
need_to_delete -= (old_out-curbeg);
if (need_to_delete > 0)
{
cbi++;
curbeg = old_out;
}
}
if (need_to_delete < 0)
{
temp_cb = (int *)(current_state->fragments+cbi*ED_SIZE);
cb = (int *)(new_state->fragments+start_del*ED_SIZE);
cb[ED_OUT] = beg;
cb[ED_SND] = temp_cb[ED_SND];
cb[ED_BEG] = temp_cb[ED_END]+1+need_to_delete;
cb[ED_END] = temp_cb[ED_END];
start_del++;
len_fixup++;
}
cbi++;
copy_ed_blocks(new_state->fragments,current_state->fragments,start_del,cbi,len-cbi); /* ??? */
new_state->beg = beg;
new_state->len = num;
if (origin) new_state->origin = copy_string(origin);
new_state->sfnum = PACK_EDIT(DELETION_EDIT,0);
ripple_out(new_state->fragments,start_del,-num,len+len_fixup);
ripple_marks(cp,beg,-num);
ripple_mixes(cp,beg,-num);
ripple_selection(cp,beg,-num);
reflect_sample_change_in_axis(cp);
new_state->size = len+len_fixup; /* don't propogate useless trailing blocks */
if (new_state->fragments[(new_state->size - 1)*ED_SIZE + ED_SND] != EDIT_LIST_END_MARK)
{
for (k = new_state->size-1;k>0;k--)
if (new_state->fragments[k*ED_SIZE + ED_SND] == EDIT_LIST_END_MARK) break;
if (k>0)
new_state->size = k+1;
}
if (new_state->size == 1)
{
/* if cut all, no data remains, so (I think) it is legit to have a one-length fragment = end_mark */
/* but just to be safe, I'll copy that mark */
new_state->size = 2;
if (new_state->fragments[ED_BEG] < 0) new_state->fragments[ED_BEG] = 0;
if (new_state->fragments[ED_OUT] < 0) new_state->fragments[ED_OUT] = 0;
new_state->fragments[ED_SIZE + ED_SND] = EDIT_LIST_END_MARK;
new_state->fragments[ED_SIZE + ED_BEG] = new_state->fragments[ED_BEG];
new_state->fragments[ED_SIZE + ED_OUT] = new_state->fragments[ED_OUT];
new_state->fragments[ED_SIZE + ED_END] = new_state->fragments[ED_END];
}
return(new_state);
}
void delete_samples(int beg, int num, chan_info *cp, char *origin)
{
int k,len;
char *errstr;
len = current_ed_samples(cp);
if ((beg < len) && (beg >= 0))
{
if ((beg+num) > len) num = len-beg;
k=cp->edit_ctr;
prepare_edit_list(cp,len-num);
cp->edits[cp->edit_ctr] = delete_samples_1(beg,num,cp->edits[k],cp,origin);
reflect_edit_history_change(cp);
lock_affected_mixes(cp,beg,beg+num);
}
else
{
errstr = (char *)CALLOC(64,sizeof(char));
if (num == 1)
sprintf(errstr,"can't delete sample %d (current len=%d)",beg,len);
else sprintf(errstr,"can't delete samples %d to %d (current len=%d)",beg,beg+num-1,len);
report_in_minibuffer(cp->sound,errstr);
FREE(errstr);
}
}
static ed_list *change_samples_1(int beg, int num, int *vals, ed_list *current_state, chan_info *cp, int **cb_back, int lengthen, char *origin)
{
/* changed 18-June-97 to incorporate lengthened file possibility */
int len,k,start_del,cbi,curbeg,len_fixup,need_to_delete,old_out;
ed_list *new_state;
int *cb,*temp_cb;
len = current_state->size;
len_fixup = -1;
k=find_split_loc(cp,beg,current_state);
need_to_delete = num - lengthen;
start_del = k;
curbeg = beg;
cbi=0;
cb = (int *)(current_state->fragments + k*ED_SIZE);
if (cb[ED_OUT]>beg) start_del--;
new_state = make_ed_list(len+2);
copy_ed_blocks(new_state->fragments,current_state->fragments,0,0,start_del);
cbi=start_del;
temp_cb = (int *)(current_state->fragments + start_del*ED_SIZE);
old_out = temp_cb[ED_OUT];
if (beg>old_out)
{
cb = (int *)(new_state->fragments + start_del*ED_SIZE);
cb[ED_OUT] = old_out;
cb[ED_SND] = temp_cb[ED_SND];
cb[ED_BEG] = temp_cb[ED_BEG];
cb[ED_END] = temp_cb[ED_BEG]+beg-old_out-1;
start_del++;
len_fixup++;
}
while (need_to_delete > 0)
{
old_out = (current_state->fragments[(cbi+1)*ED_SIZE+ED_OUT]);
need_to_delete -= (old_out-curbeg);
if (need_to_delete > 0)
{
cbi++;
curbeg = old_out;
}
}
cb = (int *)(new_state->fragments+start_del*ED_SIZE);
if (vals)
{
cb[ED_SND] = add_sound_buffer_to_edit_list(cp,vals,num);
new_state->sfnum = PACK_EDIT(CHANGE_EDIT,cb[ED_SND]);
}
else (*cb_back) = cb;
cb[ED_OUT] = beg;
cb[ED_BEG] = 0;
cb[ED_END] = num-1;
start_del++;
len_fixup++;
if (need_to_delete < 0)
{
temp_cb = (int *)(current_state->fragments+cbi*ED_SIZE);
cb = (int *)(new_state->fragments+start_del*ED_SIZE);
cb[ED_OUT] = beg+num;
cb[ED_SND] = temp_cb[ED_SND];
cb[ED_BEG] = temp_cb[ED_END]+1+need_to_delete;
cb[ED_END] = temp_cb[ED_END];
start_del++;
len_fixup++;
}
cbi++;
copy_ed_blocks(new_state->fragments,current_state->fragments,start_del,cbi,len-cbi);
new_state->beg = beg;
new_state->len = num;
if (origin) new_state->origin = copy_string(origin);
if (lengthen)
{
ripple_out(new_state->fragments,k+1,lengthen,len+len_fixup);
reflect_sample_change_in_axis(cp);
}
else clobber_amp_env(cp);
new_state->size = len+len_fixup; /* don't propogate useless trailing blocks */
ripple_marks(cp,0,0);
#ifdef DEBUGGING
fixup_edlist_endmark(new_state,current_state,len,"change-samples",beg,num,cp,origin);
#else
fixup_edlist_endmark(new_state,current_state,len);
#endif
return(new_state);
}
void file_change_samples(int beg, int num, char *tempfile, chan_info *cp, int chan, int auto_delete, int lock, char *origin)
{
int k,prev_len,new_len;
int *cb,*zeros;
int fd;
int *datai;
ed_list *ed;
file_info *hdr;
prev_len = current_ed_samples(cp);
if (beg >= prev_len)
{
zeros = (int *)CALLOC(beg-prev_len+1,sizeof(int));
extend_insertion(prev_len,beg-prev_len+1,zeros,cp,"(change-extend)");
check_for_first_edit(cp); /* needed to activate revert menu option */
FREE(zeros);
prev_len = current_ed_samples(cp);
}
new_len = beg+num;
if (new_len < prev_len) new_len = prev_len;
k=cp->edit_ctr;
prepare_edit_list(cp,new_len);
cp->edits[cp->edit_ctr] = change_samples_1(beg,num,NULL,cp->edits[k],cp,&cb,new_len - prev_len,origin);
reflect_edit_history_change(cp);
if (lock == LOCK_MIXES) lock_affected_mixes(cp,beg,beg+num);
hdr = make_file_info(tempfile,cp->state);
if (hdr)
{
fd = snd_open_read(cp->state,tempfile);
mus_open_file_descriptors(fd,hdr->format,mus_format2bytes(hdr->format),hdr->data_location);
datai = make_file_state(fd,hdr,SND_IO_IN_FILE,chan,FILE_BUFFER_SIZE,cp->state);
#if LONG_INT_P
cb[ED_SND] = add_sound_file_to_edit_list(cp,tempfile,datai,delist_ptr(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+chan]),hdr,auto_delete,chan);
#else
cb[ED_SND] = add_sound_file_to_edit_list(cp,tempfile,datai,(int *)(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+chan]),hdr,auto_delete,chan);
#endif
ed = cp->edits[cp->edit_ctr];
ed->sfnum = PACK_EDIT(CHANGE_EDIT,cb[ED_SND]);
}
}
void file_override_samples(int num, char *tempfile, chan_info *cp, int chan, int auto_delete, int lock, char *origin)
{
int fd;
ed_list *e;
int *datai;
file_info *hdr;
snd_state *ss;
ss = cp->state;
prepare_edit_list(cp,num);
hdr = make_file_info(tempfile,ss);
if (hdr)
{
if (num == -1) num = (hdr->samples/hdr->chans);
fd = snd_open_read(ss,tempfile);
mus_open_file_descriptors(fd,hdr->format,mus_format2bytes(hdr->format),hdr->data_location);
datai = make_file_state(fd,hdr,SND_IO_IN_FILE,chan,FILE_BUFFER_SIZE,cp->state);
e = initial_ed_list(0,num-1);
if (origin) e->origin = copy_string(origin);
cp->edits[cp->edit_ctr] = e;
if (lock == LOCK_MIXES) lock_affected_mixes(cp,0,num);
#if LONG_INT_P
e->fragments[0 + ED_SND] = add_sound_file_to_edit_list(cp,tempfile,datai,delist_ptr(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+chan]),hdr,auto_delete,chan);
#else
e->fragments[0 + ED_SND] = add_sound_file_to_edit_list(cp,tempfile,datai,(int *)(datai[SND_IO_DATS + SND_AREF_HEADER_SIZE+chan]),hdr,auto_delete,chan);
#endif
e->sfnum = PACK_EDIT(CHANGE_EDIT,e->fragments[0 + ED_SND]);
reflect_edit_history_change(cp);
reflect_sample_change_in_axis(cp);
ripple_marks(cp,0,0);
check_for_first_edit(cp);
update_graph(cp,NULL);
}
}
static int sample_1 (int samp, chan_info *cp);
void change_samples(int beg, int num, int *vals, chan_info *cp, int lock, char *origin)
{
int k,prev_len,new_len;
char *errstr;
int *zeros;
if ((num == 1) && (vals != NULL) && (vals[0] == sample_1(beg,cp)))
{
errstr = (char *)CALLOC(64,sizeof(char));
sprintf(errstr,"sample %d is already %.3f",beg,vals[0] * SNDLIB_SNDFLT);
report_in_minibuffer(cp->sound,errstr);
FREE(errstr);
}
else
{
prev_len = current_ed_samples(cp);
if (beg >= prev_len)
{
zeros = (int *)CALLOC(beg-prev_len+1,sizeof(int));
extend_insertion(prev_len,beg-prev_len+1,zeros,cp,"(change-extend)");
check_for_first_edit(cp); /* needed to activate revert menu option */
FREE(zeros);
prev_len = current_ed_samples(cp);
}
new_len = beg+num;
if (new_len < prev_len) new_len = prev_len;
k=cp->edit_ctr;
prepare_edit_list(cp,new_len);
cp->edits[cp->edit_ctr] = change_samples_1(beg,num,vals,cp->edits[k],cp,NULL,new_len - prev_len,origin);
reflect_edit_history_change(cp);
if (lock == LOCK_MIXES) lock_affected_mixes(cp,beg,beg+num);
}
}
int *load_samples(int beg, int num, chan_info *cp)
{
snd_fd *sf;
int *data;
int i;
data = (int *)CALLOC(num,sizeof(int));
sf = init_sample_read(beg,cp,READ_FORWARD);
for (i=0;i<num;i++) {NEXT_SAMPLE(data[i],sf);}
free_snd_fd(sf);
return(data);
}
static int snd_file_read(snd_state *ss, snd_data *ur_sd, int index, chan_info *cp)
{
int val,copied;
snd_data *sd = NULL;
copied = 0;
/* first try to grab the sample without moving any buffers */
if ((index >= ur_sd->io[SND_IO_BEG]) && (index <= ur_sd->io[SND_IO_END]))
return(ur_sd->data[index - ur_sd->io[SND_IO_BEG]]);
/* not in current buffer, so create a new reader and go looking for it */
if (ur_sd->inuse)
{
sd = copy_snd_data(ur_sd,cp);
copied = 1;
}
else sd = ur_sd;
sd->inuse = TRUE;
if ((index < sd->io[SND_IO_BEG]) || (index > sd->io[SND_IO_END]))
snd_file_reset(ss,sd,index);
val = sd->data[index - sd->io[SND_IO_BEG]];
if (copied)
{
sd->inuse = FALSE;
free_snd_data(sd);
}
return(val);
}
static int sample_1 (int samp, chan_info *cp)
{ /* slow access */
ed_list *current_state;
snd_data *sd;
int len,i,cb,true_cb,index;
int *data;
if (samp < 0) return(0);
if (samp > cp->samples[cp->edit_ctr]) return(0);
current_state = cp->edits[cp->edit_ctr];
data = current_state->fragments;
len = current_state->size;
for (i=0,cb=0;i<len;i++,cb+=ED_SIZE)
{
if (samp < data[cb+ED_OUT])
{
true_cb = cb-ED_SIZE;
if (data[true_cb+ED_SND] == EDIT_LIST_END_MARK) return(0);
index = data[true_cb+ED_BEG]+samp-data[true_cb+ED_OUT];
sd = cp->sounds[data[true_cb+ED_SND]];
if (sd->type == SND_DATA_BUFFER)
return(sd->data[index]);
else return(snd_file_read(cp->state,sd,index,cp));
}
}
return(0);
}
float sample (int samp, chan_info *cp) {return(SNDLIB_SNDFLT * sample_1(samp,cp));}
/* now for optimized sample access -- since everything goes through these lists, we want the access to be fast */
static snd_fd *make_snd_fd (void)
{
snd_fd *sf;
sf=(snd_fd *)CALLOC(1,sizeof(snd_fd));
return(sf);
}
snd_fd *free_snd_fd(snd_fd *sf)
{
snd_data *sd;
if (sf)
{
sd = sf->current_sound;
if (sd)
{
sd->inuse = FALSE;
if (sd->copy) free_snd_data(sd);
}
FREE(sf);
}
return(NULL);
}
static void massage_snd_file(int ind0, int ind1, int indx, snd_fd *sf, snd_data *cur_snd)
{
/* need to track in-core buffer and file-relative index */
if ((indx < cur_snd->io[SND_IO_BEG]) || (indx > cur_snd->io[SND_IO_END]))
snd_file_reset((sf->cp)->state,cur_snd,indx);
sf->data = (int *)(cur_snd->data + indx - cur_snd->io[SND_IO_BEG]);
/* only indx is guaranteed to be within the current in-core buffer */
if (ind0 >= cur_snd->io[SND_IO_BEG])
sf->first = (int *)(cur_snd->data + ind0 - cur_snd->io[SND_IO_BEG]);
else sf->first = cur_snd->data;
if (ind1 <= cur_snd->io[SND_IO_END])
{
sf->last = (int *)(cur_snd->data + ind1 - cur_snd->io[SND_IO_BEG]);
sf->eof = 1;
}
else
{
sf->last = (int *)(cur_snd->data + cur_snd->io[SND_IO_BUFSIZ] -1);
sf->eof = 0;
}
sf->beg = cur_snd->io[SND_IO_BEG];
sf->end = cur_snd->io[SND_IO_END];
}
static void massage_snd_file_back(int ind0, int ind1, int indx, snd_fd *sf, snd_data *cur_snd)
{
if ((indx > cur_snd->io[SND_IO_END]) || (indx < cur_snd->io[SND_IO_BEG]))
snd_file_reset((sf->cp)->state,cur_snd,indx - cur_snd->io[SND_IO_BUFSIZ] + 1);
sf->data = (int *)(cur_snd->data + indx - cur_snd->io[SND_IO_BEG]);
if (ind1 <= cur_snd->io[SND_IO_END])
sf->last = (int *)(cur_snd->data + ind1 - cur_snd->io[SND_IO_BEG]);
else sf->last = (int *)(cur_snd->data + cur_snd->io[SND_IO_BUFSIZ] -1);
if (ind0 >= cur_snd->io[SND_IO_BEG])
{
sf->first = (int *)(cur_snd->data + ind0 - cur_snd->io[SND_IO_BEG]);
sf->eof = 1;
}
else
{
sf->first = cur_snd->data;
sf->eof = 0;
}
sf->beg = cur_snd->io[SND_IO_BEG];
sf->end = cur_snd->io[SND_IO_END];
}
#if LONG_INT_P
int current_location(snd_fd *sf) {return(sf->cb[ED_BEG] + sf->beg + (int)(((long)(sf->data) - (long)(sf->first))>>2));}
#else
int current_location(snd_fd *sf) {return(sf->cb[ED_BEG] + sf->beg + (((int)(sf->data) - (int)(sf->first))>>2));}
#endif
snd_fd *init_sample_read (int samp, chan_info *cp, int direction)
{
snd_fd *sf;
snd_info *sp;
snd_state *ss;
ed_list *ed;
int len,i,k,ind0,ind1,indx,curlen;
int *cb;
snd_data *first_snd;
sf = make_snd_fd();
sf->direction = 0;
sf->cp = cp;
sp = cp->sound;
if (sp->need_update)
{
ss = cp->state;
if ((snd_probe_file(ss,sp->fullname)) == FILE_DOES_NOT_EXIST)
snd_error(STR_no_longer_exists,sp->shortname);
else snd_error(STR_file_has_changed,sp->shortname);
}
ed = (ed_list *)(cp->edits[cp->edit_ctr]);
curlen = cp->samples[cp->edit_ctr];
sf->current_state = ed;
if ((curlen == 0) || /* no samples, not ed->len (delete->len = #deleted samps) */
(samp >= curlen) || /* this is questionable, but we can't access data we don't have */
/* but as we define a selection, we can easily start beyond the current end of sync'd */
/* sound (or channel) -- will need experimentation */
(samp < 0)) /* this should never happen */
{
sf->eof = 1;
sf->data = (int *)1;
sf->last = (int *)0; /* can I get away with this?? -- we're trying to do something with an empty file here */
sf->first = (int *)2;
/* data>last and data<first are triggers to ignore data and try to move in the fragment list */
sf->current_sound = NULL;
sf->cbi = 0;
return(sf);
}
len = ed->size;
for (i=0,k=0;i<len;i++,k+=ED_SIZE)
{
cb = (int *)(ed->fragments+k);
if ((cb[ED_OUT] > samp) || (cb[ED_SND] == EDIT_LIST_END_MARK))
{
sf->cb = (int *)(ed->fragments+k-ED_SIZE);
sf->cbi = i-1;
ind0 = sf->cb[ED_BEG];
indx = sf->cb[ED_BEG]+samp-sf->cb[ED_OUT];
ind1 = sf->cb[ED_END];
sf->sounds = (snd_data **)(cp->sounds);
first_snd = sf->sounds[sf->cb[ED_SND]];
if (first_snd->type == SND_DATA_FILE)
{
/* since arbitrarily many work procs can be running in parallel, reading the same
* data (edit tree sound file entries), we can't share the clm-style IO buffers since these contain
* a local notion of current position which is not accessed on every sample by the
* sample readers (they trust their snd_fd indices); we wouldn't want to be
* constantly jumping around and re-reading data buffers (in the worst case
* many times per sample) anyway, so we copy the IO buffer, allocate a relatively
* small data buffer, and then free all the copied snd_data stuff as soon as
* the current reader is done.
*/
if (first_snd->inuse) first_snd = copy_snd_data(first_snd,cp);
first_snd->inuse = TRUE;
sf->current_sound = first_snd;
if (direction == READ_FORWARD)
massage_snd_file(ind0,ind1,indx,sf,first_snd);
else massage_snd_file_back(ind0,ind1,indx,sf,first_snd);
}
else
{
sf->current_sound = NULL;
sf->data = (int *)(first_snd->data+indx);
sf->first = (int *)(first_snd->data+ind0);
sf->last = (int *)(first_snd->data+ind1);
sf->eof = 1;
}
sf->current_value = (*sf->data);
return(sf);
}
}
display_edits(cp);
snd_error("%s[%d] %s: big trouble",__FILE__,__LINE__,__FUNCTION__); abort();
return(NULL);
}
int previous_sound (snd_fd *sf)
{
int ind0,ind1,indx;
snd_data *prev_snd;
if (sf->eof)
{
if (sf->current_sound)
{
prev_snd = sf->current_sound;
prev_snd->inuse = FALSE;
sf->current_sound = NULL;
if (prev_snd->copy) free_snd_data(prev_snd);
}
if (sf->cbi == 0) return(0); /* can't back up any further */
sf->cbi--;
/* now start in the final portion of this block (if a file) */
sf->cb = (int *)((sf->current_state)->fragments+sf->cbi*ED_SIZE);
ind0 = sf->cb[ED_BEG];
ind1 = sf->cb[ED_END];
prev_snd = sf->sounds[sf->cb[ED_SND]];
if (prev_snd->type == SND_DATA_FILE)
{
if (prev_snd->inuse) prev_snd = copy_snd_data(prev_snd,sf->cp);
prev_snd->inuse = TRUE;
sf->current_sound = prev_snd;
massage_snd_file_back(ind0,ind1,ind1,sf,prev_snd);
}
else
{
sf->data = (int *)(prev_snd->data+ind1);
sf->first = (int *)(prev_snd->data+ind0);
sf->last = sf->data;
sf->eof = 1;
}
}
else
{
/* back up in current file */
ind0 = sf->cb[ED_BEG];
ind1 = sf->cb[ED_END];
indx = sf->beg-1;
massage_snd_file_back(ind0,ind1,indx,sf,sf->current_sound);
}
return(sf->current_value = *sf->data--);
}
int next_sound (snd_fd *sf)
{
int ind0,ind1,indx;
snd_data *nxt_snd;
if (sf->eof) /* a convenience -- we could figure this out from various pointers */
{
if (sf->current_sound)
{
nxt_snd = sf->current_sound;
nxt_snd->inuse = FALSE;
sf->current_sound = NULL;
if (nxt_snd->copy) free_snd_data(nxt_snd);
}
if (sf->last == (int *)0) return(0);
sf->cbi++;
if (sf->cbi >= (sf->current_state)->size)
{
sf->data = (int *)1;
sf->last = (int *)0;
return(0);
}
sf->cb = (int *)((sf->current_state)->fragments+sf->cbi*ED_SIZE);
if ((!(sf->cb)) || (sf->cb[ED_SND] == EDIT_LIST_END_MARK))
{
sf->data = (int *)1;
sf->last = (int *)0;
return(0);
}
ind0 = sf->cb[ED_BEG];
ind1 = sf->cb[ED_END];
nxt_snd = sf->sounds[sf->cb[ED_SND]];
if (nxt_snd->type == SND_DATA_FILE)
{
if (nxt_snd->inuse) nxt_snd = copy_snd_data(nxt_snd,sf->cp);
nxt_snd->inuse = TRUE;
sf->current_sound = nxt_snd;
massage_snd_file(ind0,ind1,ind0,sf,nxt_snd);
}
else
{
sf->data = (int *)(nxt_snd->data+ind0);
sf->first = sf->data;
sf->last = (int *)(nxt_snd->data+ind1);
sf->eof = 1;
}
}
else
{
ind0 = sf->cb[ED_BEG];
ind1 = sf->cb[ED_END];
indx = sf->end+1;
massage_snd_file(ind0,ind1,indx,sf,sf->current_sound);
}
return(sf->current_value = *sf->data++);
}
int next_sample_1 (snd_fd *sf)
{
if (sf->data > sf->last) return(next_sound(sf));
return(sf->current_value = *sf->data++);
}
int previous_sample_1(snd_fd *sf)
{
if (sf->data < sf->first) return(previous_sound(sf));
return(sf->current_value = *sf->data--);
}
float any_sample(snd_fd *sf, int off)
{
int true_off;
int *sadr;
/* for search routines, depends on current_value, direction etc -- not a general function! */
if (off == 0) return(SNDLIB_SNDFLT * (sf->current_value));
/* postincrement/decrement assumed here -- that is don't ask for offset value immediately after init_sample_read */
true_off = off-sf->direction;
/* now look for current block bounds -- maybe we'll luck out */
sadr = (int *)(sf->data + true_off);
if ((sadr >= sf->first) && (sadr <= sf->last))
return(SNDLIB_SNDFLT * (*sadr));
return(sample(current_location(sf) + off,sf->cp));
}
int next_sub_sound (snd_fd *sf, int inc)
{
int i,val,val1;
NEXT_SAMPLE(val1,sf);
for (i=0;i<inc-1;i++) NEXT_SAMPLE(val,sf);
return(val1);
}
float next_sample (snd_fd *sf) {return(SNDLIB_SNDFLT * next_sample_1(sf));}
float previous_sample (snd_fd *sf) {return(SNDLIB_SNDFLT * previous_sample_1(sf));}
#define NEXT_SAMPLES(data,num,sf) {int i; for (i=0;i<num;i++) NEXT_SAMPLE(data[i],sf);}
int read_sample_eof (snd_fd *sf)
{
if (sf->cb)
return((sf->cb[ED_SND] == EDIT_LIST_END_MARK) || ((sf->cbi == 0) && (sf->data < sf->first)));
else return(sf->data >= sf->last);
}
/* -------------------------------- EDITS -------------------------------- */
static int snd_io_error;
int snd_IO_error(void) {return(snd_io_error);}
void set_snd_IO_error(int err) {snd_io_error = err;}
static char *snd_error_names[] = {
STR_no_error,STR_cant_write_header,STR_cant_open_file,
STR_cant_allocate_IO_buffers,STR_cant_write_data,
STR_cant_remove_file,STR_cant_rename_file,STR_cant_read_header,
STR_unsupported_header_type,STR_cant_find_file,STR_unsupported_data_format};
char *snd_error_name(int i) {return(snd_error_names[i]);}
int open_temp_file(char *ofile, int chans, file_info *hdr, snd_state *ss)
{
int ofd,len;
len = snd_strlen(hdr->comment);
if (!(mus_header_writable(hdr->type,hdr->format)))
{
hdr->type = default_output_type(ss);
if (hdr->type != RIFF_sound_file)
hdr->format = SNDLIB_16_LINEAR;
else hdr->format = SNDLIB_16_LINEAR_LITTLE_ENDIAN;
}
if ((snd_write_header(ss,ofile,hdr->type,hdr->srate,chans,0,0,hdr->format,hdr->comment,len)) == -1) return(-1);
if ((ofd = snd_reopen_write(ss,ofile)) == -1) return(-1);
hdr->data_location = mus_header_data_location(); /* header might have changed size (aiff extras) */
mus_open_file_descriptors(ofd,hdr->format,mus_format2bytes(hdr->format),hdr->data_location);
mus_seek(ofd,hdr->data_location,SEEK_SET);
return(ofd);
}
int close_temp_file(int ofd, file_info *hdr, long bytes, snd_info *sp)
{
int kleft,kused;
char *prtbuf;
mus_update_header_with_fd(ofd,hdr->type,bytes);
kleft = disk_kspace(ofd);
if (kleft < 0)
snd_error("close temp file: %s",strerror(errno));
else
{
kused = bytes>>10;
if ((kused > kleft) && (sp))
{
prtbuf = (char *)CALLOC(64,sizeof(char));
sprintf(prtbuf,STR_were_getting_short_on_disk_space,kused,kleft);
report_in_minibuffer(sp,prtbuf);
FREE(prtbuf);
}
}
snd_close(ofd);
return(0);
}
int snd_make_file(char *ofile, int chans, file_info *hdr, snd_fd **sfs, int length, snd_state *ss)
{
/* create ofile, fill it by following sfs, use hdr for srate/type/format decisions */
int ofd;
int i,j,len,datumb;
int **obufs;
snd_io_error = SND_NO_ERROR;
ofd = open_temp_file(ofile,chans,hdr,ss);
if (ofd == -1) return(SND_CANNOT_OPEN_TEMP_FILE);
datumb = mus_format2bytes(hdr->format);
obufs = (int **)CALLOC(chans,sizeof(int *));
for (i=0;i<chans;i++)
{
obufs[i] = (int *)CALLOC(FILE_BUFFER_SIZE,sizeof(int));
}
j=0;
if (chans == 1)
{
for (len=0;len<length;len++)
{
NEXT_SAMPLE(obufs[0][j],sfs[0]);
j++;
if (j == FILE_BUFFER_SIZE)
{
mus_write(ofd,0,j-1,1,obufs);
if (snd_io_error != SND_NO_ERROR) return(SND_CANNOT_WRITE_DATA);
j=0;
}
}
}
else
{
for (len=0;len<length;len++)
{
for (i=0;i<chans;i++)
{
NEXT_SAMPLE(obufs[i][j],sfs[i]);
}
j++;
if (j == FILE_BUFFER_SIZE)
{
mus_write(ofd,0,j-1,chans,obufs);
if (snd_io_error != SND_NO_ERROR) return(SND_CANNOT_WRITE_DATA);
j=0;
}
}
}
if (j > 0)
{
mus_write(ofd,0,j-1,chans,obufs);
if (snd_io_error != SND_NO_ERROR) return(SND_CANNOT_WRITE_DATA);
}
close_temp_file(ofd,hdr,len*chans*datumb,any_selected_sound(ss));
alert_new_file();
for (i=0;i<chans;i++) FREE(obufs[i]);
FREE(obufs);
return(0);
}
static int only_save_edits(snd_info *sp, file_info *nhdr, char *ofile)
{
snd_state *ss;
int i,err;
snd_fd **sf;
ss = sp->state;
sf = (snd_fd **)CALLOC(sp->nchans,sizeof(snd_fd *));
for (i=0;i<sp->nchans;i++) sf[i] = init_sample_read(0,sp->chans[i],READ_FORWARD);
err = snd_make_file(ofile,sp->nchans,nhdr,sf,current_ed_samples(sp->chans[0]),ss);
for (i=0;i<sp->nchans;i++) free_snd_fd(sf[i]);
FREE(sf);
if (show_edit_history(ss))
{
for (i=0;i<sp->nchans;i++) reflect_save_as_in_edit_history(sp->chans[i],ofile);
}
return(err);
}
static int save_edits_1(snd_info *sp)
{
/* open temp, write current state, rename to old, reopen and clear all state */
/* can't overwrite current because we may have cut/paste backpointers scattered around the current edit list */
/* have to decide here what header/data type to write as well -- original? */
/* if latter, must be able to write all headers! -- perhaps warn user and use snd/aiff/riff/ircam */
char *ofile;
int err,saved_errno = 0;
snd_state *ss;
int i,samples;
chan_info *cp;
axis_info *ap;
snd_fd **sf;
float *axis_data;
int *ffts,*waves;
file_info *sphdr;
ss = sp->state;
samples = current_ed_samples(sp->chans[0]);
snd_io_error = SND_NO_ERROR;
ofile = snd_tempnam(ss);
/* this will use user's TMPDIR if temp_dir(ss) is not set, else stdio.h's P_tmpdir else /tmp */
axis_data = (float *)CALLOC(4*sp->nchans,sizeof(float));
ffts = (int *)CALLOC(sp->nchans,sizeof(int));
waves = (int *)CALLOC(sp->nchans,sizeof(int));
for (i=0;i<sp->nchans;i++)
{
cp = sp->chans[i];
ap = cp->axis;
axis_data[(i*4)+0]=ap->x0;
axis_data[(i*4)+1]=ap->x1;
axis_data[(i*4)+2]=ap->y0;
axis_data[(i*4)+3]=ap->y1;
waves[i] = cp->waving;
ffts[i] = cp->ffting;
}
sf = (snd_fd **)CALLOC(sp->nchans,sizeof(snd_fd *));
for (i=0;i<sp->nchans;i++) sf[i] = init_sample_read(0,sp->chans[i],READ_FORWARD);
sprintf(edit_buf,STR_saving,sp->shortname);
report_in_minibuffer(sp,edit_buf);
sphdr = sp->hdr;
/* if old-style AIFF header, maintain it across save */
if ((sphdr->type == AIFF_sound_file) && (sound_aiff_p(sp->fullname)))
mus_set_aifc_header(0);
snd_io_error = snd_make_file(ofile,sp->nchans,sp->hdr,sf,samples,ss);
if (sphdr->type == AIFF_sound_file) mus_set_aifc_header(1);
if (snd_io_error != SND_NO_ERROR)
{
for (i=0;i<sp->nchans;i++) free_snd_fd(sf[i]);
FREE(sf);
FREE(ffts);
FREE(waves);
FREE(axis_data);
return(snd_io_error);
}
sphdr->samples = samples*sp->nchans;
collapse_marks(sp);
for (i=0;i<sp->nchans;i++)
{
cp = sp->chans[i];
if (cp->mixes) reset_mix_list(cp);
if (cp->edits) free_edit_list(cp);
free_snd_fd(sf[i]); /* must precede free_sound_list since it access the snd_data structs that free_sound_list frees */
if (cp->sounds) free_sound_list(cp);
free_chan_env(cp);
}
FREE(sf);
#if defined(_MSC_VER)
err = 0;
#else
err = access(sp->fullname,W_OK);
#endif
/* very weird -- in Linux we can write a write-protected file?? */
if (err == 0)
{
err = snd_copy_file(ss,ofile,sp->fullname);
if (err) saved_errno = errno;
}
else saved_errno = errno;
sp->write_date = file_write_date(sp->fullname);
add_sound_data(sp->fullname,sp,ss);
for (i=0;i<sp->nchans;i++)
{
cp = sp->chans[i];
set_axes(cp,axis_data[(i*4)+0],axis_data[(i*4)+1],axis_data[(i*4)+2],axis_data[(i*4)+3]);
update_graph(cp,NULL); /* get normalized state before messing with it */
if (ffts[i]) fftb(cp,TRUE);
if (!(waves[i])) waveb(cp,FALSE);
reflect_edit_history_change(cp);
}
FREE(axis_data);
FREE(waves);
FREE(ffts);
reflect_file_revert_in_label(sp);
reflect_file_save_in_menu(ss);
if (err)
sprintf(edit_buf,STR_write_failed_temp_saved,strerror(saved_errno),ofile);
else sprintf(edit_buf,STR_wrote,sp->fullname);
report_in_minibuffer(sp,edit_buf);
if (ofile) {FREE(ofile); ofile=NULL;}
if (auto_update(ss)) map_over_sounds(ss,snd_not_current,NULL);
return(SND_NO_ERROR); /* don't erase our error message for the special write-permission problem */
}
int save_edits_2(snd_info *sp, char *new_name, int type, int format, int srate, char *comment)
{ /* file save as menu option -- changed 19-June-97 to retain current state after writing */
file_info *hdr,*ohdr;
int res;
ohdr = sp->hdr;
hdr = copy_header(new_name,ohdr);
hdr->format = format;
hdr->srate = srate;
hdr->type = type;
hdr->comment = comment;
hdr->data_location = 0; /* in case comment changes it */
res = only_save_edits(sp,hdr,new_name);
free_file_info(hdr);
return(res);
}
int chan_save_edits(chan_info *cp, char *ofile)
{
/* channel extraction -- does not (normally) cause reversion of edits, or change of in-window file, etc */
snd_info *sp;
snd_fd **sf;
int err;
char *nfile;
snd_state *ss;
ss = cp->state;
sp = cp->sound;
snd_io_error = SND_NO_ERROR;
if (!(snd_overwrite_ok(ss,ofile))) return(SND_NO_ERROR); /* no error because decision was explicit */
if (strcmp(ofile,sp->fullname) == 0)
{
if (sp->read_only)
{
sprintf(edit_buf,"can't save channel as %s (%s is write-protected)",ofile,sp->shortname);
report_in_minibuffer(sp,edit_buf);
return(SND_CANNOT_WRITE_DATA);
}
/* here we're overwriting the current (possibly multi-channel) file with one of its channels */
nfile = snd_tempnam(ss);
sf = (snd_fd **)CALLOC(1,sizeof(snd_fd *));
sf[0] = init_sample_read(0,cp,READ_FORWARD);
snd_io_error = snd_make_file(nfile,1,sp->hdr,sf,current_ed_samples(cp),cp->state);
free_snd_fd(sf[0]);
FREE(sf);
if (snd_io_error != SND_NO_ERROR)
{
sprintf(edit_buf,"save channel as temp: %s: %s (%s)",nfile,strerror(errno),snd_error_name(snd_io_error));
report_in_minibuffer(sp,edit_buf);
}
else err = snd_copy_file(ss,nfile,ofile);
if (show_edit_history(ss)) reflect_save_as_in_edit_history(cp,ofile);
snd_update(ss,sp);
FREE(nfile);
}
else
{
sf = (snd_fd **)CALLOC(1,sizeof(snd_fd *));
sf[0] = init_sample_read(0,cp,READ_FORWARD);
snd_io_error = snd_make_file(ofile,1,sp->hdr,sf,current_ed_samples(cp),cp->state);
free_snd_fd(sf[0]);
FREE(sf);
}
return(snd_io_error);
}
int save_edits(snd_info *sp, void *ptr)
{
int i,need_save,err;
time_t current_write_date;
chan_info *cp;
if (!sp->read_only)
{
need_save = 0;
for (i=0;i<sp->nchans;i++)
{
cp = sp->chans[i];
if (cp->edit_ctr > 0)
{
need_save = 1;
break;
}
}
if (need_save)
{
errno = 0;
/* check for change to file while we were editing it */
current_write_date = file_write_date(sp->fullname);
if (current_write_date != sp->write_date)
{
sprintf(edit_buf,STR_changed_on_disk_p,sp->shortname);
err = snd_yes_or_no_p(sp->state,edit_buf);
if (err == 0) return(0);
}
err = save_edits_1(sp);
if (err)
{
sprintf(edit_buf,"%s: %s (%s)",sp->fullname,strerror(errno),snd_error_name(err));
report_in_minibuffer(sp,edit_buf);
}
else
{
if (sp->edited_region) save_region_backpointer(sp);
}
}
else
{
report_in_minibuffer(sp,"(no changes need to be saved)");
}
}
else
{
sprintf(edit_buf,STR_cant_write,sp->shortname);
strcat(edit_buf," ("); strcat(edit_buf,STR_read_only); strcat(edit_buf,")");
report_in_minibuffer(sp,edit_buf);
}
return(0);
}
int revert_edits(chan_info *cp, void *ptr)
{
snd_info *sp;
sp = cp->sound;
if (sp->playing) stop_playing_sound(sp);
cp->edit_ctr = 0;
reflect_edit_counter_change(cp);
reflect_sample_change_in_axis(cp);
update_graph(cp,NULL);
update_groups();
return(0);
}
void undo_edit(chan_info *cp, int count)
{
snd_info *sp;
if ((cp) && (cp->edit_ctr > 0) && (count != 0))
{
sp = cp->sound;
if (sp->playing) stop_playing_sound(sp);
cp->edit_ctr -= count;
if (cp->edit_ctr < 0) cp->edit_ctr = 0;
reflect_edit_counter_change(cp);
reflect_sample_change_in_axis(cp);
reflect_undo_in_menu();
if (cp->edit_ctr == 0)
{
reflect_file_revert_in_label(sp);
reflect_file_revert_in_menu(cp->state);
}
update_graph(cp,NULL);
update_groups();
}
}
void undo_EDIT(void *ptr, int count)
{
chan_info *cp;
snd_info *sp;
int i;
sync_info *si;
if (count < 0)
redo_edit((chan_info *)ptr,-count);
else
{
si = NULL;
cp = current_channel(ptr);
if (cp)
{
sp = cp->sound;
if (sp->syncing != 0) si = snd_sync(cp->state,sp->syncing);
if (si)
{
for (i=0;i<si->chans;i++) undo_edit(si->cps[i],count);
free_sync_info(si);
}
else undo_edit(cp,count);
}
}
}
static void reflect_file_change_in_label (chan_info *cp)
{
snd_info *sp = cp->sound;
char *starred_name;
int len;
len = strlen(shortname(sp)) + 2;
if (sp->read_only) len+=2;
starred_name = (char *)CALLOC(len,sizeof(char));
strcpy(starred_name,shortname(sp));
if (sp->read_only)
{
starred_name[len-4]='(';
starred_name[len-3]='*';
starred_name[len-2]=')';
}
else starred_name[len-2]='*';
starred_name[len-1]='\0';
make_name_label(w_snd_name(sp),starred_name);
make_a_big_star_outa_me(sp->state,sp->shortname,1);
FREE(starred_name);
}
void redo_edit(chan_info *cp, int count)
{
snd_info *sp;
if (cp)
{
if (cp->edit_ctr == 0)
{
reflect_file_change_in_label(cp);
reflect_redo_in_menu();
}
sp = cp->sound;
if (sp->playing) stop_playing_sound(sp);
cp->edit_ctr += count;
while ((cp->edit_ctr >= cp->edit_size) || (!(cp->edits[cp->edit_ctr]))) {cp->edit_ctr--;}
if (((cp->edit_ctr+1) == cp->edit_size) || (!(cp->edits[cp->edit_ctr+1])))
reflect_no_more_redo_in_menu();
reflect_edit_counter_change(cp);
reflect_sample_change_in_axis(cp);
update_graph(cp,NULL);
update_groups();
}
}
void redo_EDIT(void *ptr, int count)
{
chan_info *cp;
snd_info *sp;
int i;
sync_info *si;
if (count < 0)
undo_EDIT(ptr,-count);
else
{
si = NULL;
cp = current_channel(ptr);
if (cp)
{
sp = cp->sound;
if (sp->syncing != 0) si = snd_sync(cp->state,sp->syncing);
if (si)
{
for (i=0;i<si->chans;i++) redo_edit(si->cps[i],count);
free_sync_info(si);
}
else redo_edit(cp,count);
}
}
}
void check_for_first_edit(chan_info *cp)
{
if (cp->edit_ctr == 1) /* first edit on this file (?) */
{
reflect_file_change_in_menu();
reflect_file_change_in_label(cp);
}
}
|