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 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
|
/* Time-stamp: <2009-01-11 17:52:48 jcs>
|
| Copyright (C) 2002-2005 Jorg Schuler <jcsjcs at users sourceforge net>
| Part of the gtkpod project.
|
| URL: http://www.gtkpod.org/
| URL: http://gtkpod.sourceforge.net/
|
| This program is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2 of the License, or
| (at your option) any later version.
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; if not, write to the Free Software
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| iTunes and iPod are trademarks of Apple
|
| This product is not supported/written/published by Apple!
|
| $Id: mp3file.c 2204 2009-01-11 10:13:15Z jcsjcs $
*/
#define LOCALDEBUG 0
/* The code in the first section of this file is taken from the
* mp3info (http://www.ibiblio.org/mp3info/) project. Only the code
* needed for the playlength calculation has been extracted. */
/* The code in the second section of this file is taken from the
* mpg123 code used in xmms-1.2.7 (Input/mpg123). Only the code needed
* for the playlength calculation has been extracted. */
/* The code in the third section of this file is taken from the
* crc code used in libmad (crc.h). */
/* The code in the last section of this file is original gtkpod
* code. */
/****************
* Declarations *
****************/
#include <glib.h>
#include <math.h>
/*
* Description of each item of the TagList list
*/
typedef struct _File_Tag File_Tag;
typedef struct _GainData GainData;
typedef struct _GaplessData GaplessData;
typedef struct _LameTag LameTag;
struct _File_Tag
{
gchar *title; /* Title of track */
gchar *artist; /* Artist name */
gchar *album; /* Album name */
gchar *year; /* Year of track */
gchar *trackstring; /* Position of track in the album */
gchar *track_total; /* The number of tracks for the album (ex: 12/20) */
gchar *genre; /* Genre of song */
gchar *comment; /* Comment */
gchar *composer; /* Composer */
guint32 songlen; /* Length of file in ms */
gchar *cdnostring; /* Position of disc in the album */
gchar *cdno_total; /* The number of discs in the album (ex: 1/2) */
gchar *compilation; /* The track is a member of a compilation */
gchar *podcasturl; /* The following are mainly used for podcasts */
gchar *sort_artist;
gchar *sort_title;
gchar *sort_album;
gchar *sort_albumartist;
gchar *sort_composer;
gchar *description;
gchar *podcastrss;
gchar *time_released;
gchar *subtitle;
gchar *BPM; /* beats per minute */
gchar *lyrics; /* does not appear to be the full lyrics --
only used to set the flag 'lyrics_flag'
of the Track structure */
gchar *albumartist; /* Album artist */
};
struct _GainData
{
guint32 peak_signal; /* LAME Peak Signal * 0x800000 */
gdouble radio_gain; /* RadioGain in dB
(as defined by www.replaygain.org) */
gdouble audiophile_gain;/* AudiophileGain in dB
(as defined by www.replaygain.org) */
gboolean peak_signal_set; /* has the peak signal been set? */
gboolean radio_gain_set; /* has the radio gain been set? */
gboolean audiophile_gain_set;/* has the audiophile gain been set? */
};
struct _GaplessData
{
guint32 pregap; /* number of pregap samples */
guint64 samplecount; /* number of actual music samples */
guint32 postgap; /* number of postgap samples */
guint32 gapless_data; /* number of bytes from the first sync frame to the 8th to last frame */
};
#define LAME_TAG_SIZE 0x24
#define INFO_TAG_CRC_SIZE 0xBE /* number of bytes to pass to crc_compute */
/* A structure to hold the various data found in a LAME info tag.
* Please see http://gabriel.mp3-tech.org/mp3infotag.html for full
* documentation.
*/
struct _LameTag
{
gchar encoder[4];
gchar version_string[5];
guint8 info_tag_revision;
guint8 vbr_method;
guint8 lowpass;
float peak_signal_amplitude;
guint16 radio_replay_gain;
guint16 audiophile_replay_gain;
guint8 encoding_flags;
guint8 ath_type;
guint8 bitrate;
guint16 delay;
guint16 padding;
guint8 noise_shaping;
guint8 stereo_mode;
gboolean unwise_settings;
guint8 source_sample_frequency;
guint8 mp3_gain;
guint8 surround_info;
guint16 preset;
guint32 music_length;
guint16 music_crc;
guint16 info_tag_crc;
guint16 calculated_info_tag_crc;
};
/* This code is taken from the mp3info code. Only the code needed for
* the playlength calculation has been extracted */
/*
mp3tech.c - Functions for handling MP3 files and most MP3 data
structure manipulation.
Copyright (C) 2000-2001 Cedric Tefft <cedric@earthling.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***************************************************************************
This file is based in part on:
* MP3Info 0.5 by Ricardo Cerqueira <rmc@rccn.net>
* MP3Stat 0.9 by Ed Sweetman <safemode@voicenet.com> and
Johannes Overmann <overmann@iname.com>
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "mp3file.h"
#include "charset.h"
#include "itdb.h"
#include "file.h"
#include "misc.h"
/* MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames
we need to see before we decide we are looking at a real MP3 file */
#define MIN_CONSEC_GOOD_FRAMES 4
#define FRAME_HEADER_SIZE 4
#define MIN_FRAME_SIZE 21
enum VBR_REPORT { VBR_VARIABLE, VBR_AVERAGE, VBR_MEDIAN };
typedef struct {
gulong sync;
guint version;
guint layer;
guint crc;
guint bitrate;
guint freq;
guint padding;
guint extension;
guint mode;
guint mode_extension;
guint copyright;
guint original;
guint emphasis;
} MP3Header;
typedef struct {
const gchar *filename;
FILE *file;
off_t datasize;
gint header_isvalid;
MP3Header header;
gint id3_isvalid;
gint vbr;
float vbr_average;
gint milliseconds;
gint frames;
gint badframes;
} MP3Info;
/* This is for xmms code */
static guint get_track_time(const gchar *path);
/* This is for soundcheck code */
gboolean mp3_read_lame_tag (const gchar *path, LameTag *lt);
/* ------------------------------------------------------------
start of first section
------------------------------------------------------------ */
void get_mp3_info(MP3Info *mp3);
gint frequencies[3][4] = {
{22050,24000,16000,50000}, /* MPEG 2.0 */
{44100,48000,32000,50000}, /* MPEG 1.0 */
{11025,12000,8000,50000} /* MPEG 2.5 */
};
/* "0" added by JCS */
gint bitrate[2][3][16] = {
{ /* MPEG 2.0 */
{0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},/* layer 1 */
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}, /* layer 2 */
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} /* layer 3 */
},
{ /* MPEG 1.0 */
{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},/* layer 1 */
{0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0}, /* layer 2 */
{0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0} /* layer 3 */
}
};
gint frame_size_index[] = {24000, 72000, 72000};
gchar *mode_text[] = {
"stereo", "joint stereo", "dual channel", "mono"
};
gchar *emphasis_text[] = {
"none", "50/15 microsecs", "reserved", "CCITT J 17"
};
static gint mp3file_header_bitrate(MP3Header *h) {
return bitrate[h->version & 1][3-h->layer][h->bitrate];
}
static gint mp3file_header_frequency(MP3Header *h) {
return frequencies[h->version][h->freq];
}
gint frame_length(MP3Header *header) {
return header->sync == 0xFFE ?
(frame_size_index[3-header->layer]*((header->version&1)+1)*
mp3file_header_bitrate(header)/(float)mp3file_header_frequency(header))+
header->padding : 1;
}
/* Get next MP3 frame header.
Return codes:
positive value = Frame Length of this header
0 = No, we did not retrieve a valid frame header
*/
gint get_header(FILE *file,MP3Header *header)
{
guchar buffer[FRAME_HEADER_SIZE];
gint fl;
if(fread(&buffer,FRAME_HEADER_SIZE,1,file)<1) {
header->sync=0;
return 0;
}
header->sync=(((gint)buffer[0]<<4) | ((gint)(buffer[1]&0xE0)>>4));
if(buffer[1] & 0x10) header->version=(buffer[1] >> 3) & 1;
else header->version=2;
header->layer=(buffer[1] >> 1) & 3;
if (header->layer == 0)
{
header->layer = 1; /* sanity added by JCS */
}
if((header->sync != 0xFFE) || (header->layer != 1)) {
header->sync=0;
return 0;
}
header->crc=buffer[1] & 1;
header->bitrate=(buffer[2] >> 4) & 0x0F;
header->freq=(buffer[2] >> 2) & 0x3;
header->padding=(buffer[2] >>1) & 0x1;
header->extension=(buffer[2]) & 0x1;
header->mode=(buffer[3] >> 6) & 0x3;
header->mode_extension=(buffer[3] >> 4) & 0x3;
header->copyright=(buffer[3] >> 3) & 0x1;
header->original=(buffer[3] >> 2) & 0x1;
header->emphasis=(buffer[3]) & 0x3;
return ((fl=frame_length(header)) >= MIN_FRAME_SIZE ? fl : 0);
}
gint sameConstant(MP3Header *h1, MP3Header *h2) {
if((*(guint*)h1) == (*(guint*)h2)) return 1;
if((h1->version == h2->version ) &&
(h1->layer == h2->layer ) &&
(h1->crc == h2->crc ) &&
(h1->freq == h2->freq ) &&
(h1->mode == h2->mode ) &&
(h1->copyright == h2->copyright ) &&
(h1->original == h2->original ) &&
(h1->emphasis == h2->emphasis ))
return 1;
else return 0;
}
gint get_first_header(MP3Info *mp3, long startpos)
{
gint k, l=0,c;
MP3Header h, h2;
long valid_start=0;
fseek(mp3->file,startpos,SEEK_SET);
while (1) {
while((c=fgetc(mp3->file)) != 255 && (c != EOF));
if(c == 255) {
ungetc(c,mp3->file);
valid_start=ftell(mp3->file);
if((l=get_header(mp3->file,&h))) {
fseek(mp3->file,l-FRAME_HEADER_SIZE,SEEK_CUR);
for(k=1; (k < MIN_CONSEC_GOOD_FRAMES) && (mp3->datasize-ftell(mp3->file) >= FRAME_HEADER_SIZE); k++) {
if(!(l=get_header(mp3->file,&h2))) break;
if(!sameConstant(&h,&h2)) break;
fseek(mp3->file,l-FRAME_HEADER_SIZE,SEEK_CUR);
}
if(k == MIN_CONSEC_GOOD_FRAMES) {
fseek(mp3->file,valid_start,SEEK_SET);
memcpy(&(mp3->header),&h2,sizeof(MP3Header));
mp3->header_isvalid=1;
return 1;
}
}
} else {
return 0;
}
}
return 0;
}
/* get_next_header() - read header at current position or look for
the next valid header if there isn't one at the current position
*/
gint get_next_header(MP3Info *mp3)
{
gint l=0,c,skip_bytes=0;
MP3Header h;
while(1) {
while((c=fgetc(mp3->file)) != 255 && (ftell(mp3->file) < mp3->datasize)) skip_bytes++;
if(c == 255) {
ungetc(c,mp3->file);
if((l=get_header(mp3->file,&h))) {
if(skip_bytes) mp3->badframes++;
fseek(mp3->file,l-FRAME_HEADER_SIZE,SEEK_CUR);
return 15-h.bitrate;
} else {
skip_bytes += FRAME_HEADER_SIZE;
}
} else {
if(skip_bytes) mp3->badframes++;
return 0;
}
}
}
void get_mp3_info(MP3Info *mp3)
{
gint frame_type[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
double milliseconds=0,total_rate=0;
gint frames=0,frame_types=0,frames_so_far=0;
gint vbr_median=-1;
guint bitrate;
gint counter=0;
MP3Header header;
struct stat filestat;
off_t data_start=0;
stat(mp3->filename,&filestat);
mp3->datasize=filestat.st_size;
if(get_first_header(mp3,0L)) {
data_start=ftell(mp3->file);
while((bitrate=get_next_header(mp3))) {
if (bitrate < 15) /* sanity added by JCS */
frame_type[15-bitrate]++;
frames++;
}
memcpy(&header,&(mp3->header),sizeof(MP3Header));
for(counter=0;counter<15;counter++) {
if(frame_type[counter]) {
float header_bitrate; /* introduced by JCS to speed up */
frame_types++;
header.bitrate=counter;
frames_so_far += frame_type[counter];
header_bitrate = mp3file_header_bitrate(&header);
if (header_bitrate != 0)
milliseconds += 8*(double)frame_length(&header)*(double)frame_type[counter]/header_bitrate;
total_rate += header_bitrate*frame_type[counter];
if((vbr_median == -1) && (frames_so_far >= frames/2))
vbr_median=counter;
}
}
mp3->milliseconds=(gint)(milliseconds+0.5);
mp3->header.bitrate=vbr_median;
mp3->vbr_average=total_rate/(float)frames;
mp3->frames=frames;
if(frame_types > 1) {
mp3->vbr=1;
}
}
}
/* ------------------------------------------------------------
xmms code
------------------------------------------------------------ */
/*
| Changed by Jorg Schuler <jcsjcs at users.sourceforge.net> to
| compile with the gtkpod project. 2003/04/01
*/
/* This code is taken from the mpg123 code used in xmms-1.2.7
* (Input/mpg123). Only the code needed for the playlength calculation
* has been extracted */
#include "mp3file.h"
#include <stdio.h>
#include <string.h>
#define FRAMES_FLAG 0x0001
#define BYTES_FLAG 0x0002
#define TOC_FLAG 0x0004
#define VBR_SCALE_FLAG 0x0008
#define SBLIMIT 32
#define SCALE_BLOCK 12
#define SSLIMIT 18
#define MPG_MD_STEREO 0
#define MPG_MD_JOINT_STEREO 1
#define MPG_MD_DUAL_CHANNEL 2
#define MPG_MD_MONO 3
#define MAXFRAMESIZE 1792
#define real float
struct bitstream_info
{
int bitindex;
unsigned char *wordpointer;
};
struct bitstream_info bsi;
real mpg123_muls[27][64]; /* also used by layer 1 */
int tabsel_123[2][3][16] =
{
{
{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
{0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
{
{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}}
};
long mpg123_freqs[9] =
{44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000};
/*
* structure to receive extracted header
*/
typedef struct
{
int frames; /* total bit stream frames from Xing header data */
int bytes; /* total bit stream bytes from Xing header data */
unsigned char toc[100]; /* "table of contents" */
} xing_header_t;
struct al_table
{
short bits;
short d;
};
struct frame
{
struct al_table *alloc;
int (*synth) (real *, int, unsigned char *, int *);
int (*synth_mono) (real *, unsigned char *, int *);
#ifdef USE_3DNOW
void (*dct36)(real *,real *,real *,real *,real *);
#endif
int stereo;
int jsbound;
int single;
int II_sblimit;
int down_sample_sblimit;
int lsf;
int mpeg25;
int down_sample;
int header_change;
int lay;
int (*do_layer) (struct frame * fr);
int error_protection;
int bitrate_index;
int sampling_frequency;
int padding;
int extension;
int mode;
int mode_ext;
int copyright;
int original;
int emphasis;
int framesize; /* computed framesize */
};
static guint32 convert_to_header(guint8 * buf)
{
return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
}
static int mpg123_head_check(unsigned long head)
{
if ((head & 0xffe00000) != 0xffe00000)
return FALSE;
if (!((head >> 17) & 3))
return FALSE;
if (((head >> 12) & 0xf) == 0xf)
return FALSE;
if (!((head >> 12) & 0xf))
return FALSE;
if (((head >> 10) & 0x3) == 0x3)
return FALSE;
if (((head >> 19) & 1) == 1 && ((head >> 17) & 3) == 3 && ((head >> 16) & 1) == 1)
return FALSE;
if ((head & 0xffff0000) == 0xfffe0000)
return FALSE;
return TRUE;
}
/*
* the code a header and write the information
* into the frame structure
*/
static int mpg123_decode_header(struct frame *fr, unsigned long newhead)
{
int ssize;
if (newhead & (1 << 20))
{
fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
fr->mpeg25 = 0;
}
else
{
fr->lsf = 1;
fr->mpeg25 = 1;
}
fr->lay = 4 - ((newhead >> 17) & 3);
if (fr->mpeg25)
{
fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
}
else
fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
fr->bitrate_index = ((newhead >> 12) & 0xf);
fr->padding = ((newhead >> 9) & 0x1);
fr->extension = ((newhead >> 8) & 0x1);
fr->mode = ((newhead >> 6) & 0x3);
fr->mode_ext = ((newhead >> 4) & 0x3);
fr->copyright = ((newhead >> 3) & 0x1);
fr->original = ((newhead >> 2) & 0x1);
fr->emphasis = newhead & 0x3;
fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
ssize = 0;
if (!fr->bitrate_index)
return (0);
switch (fr->lay)
{
case 1:
/* fr->do_layer = mpg123_do_layer1; */
/* mpg123_init_layer2(); /\* inits also shared tables with layer1 *\/ */
fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
fr->framesize /= mpg123_freqs[fr->sampling_frequency];
fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
break;
case 2:
/* fr->do_layer = mpg123_do_layer2; */
/* mpg123_init_layer2(); /\* inits also shared tables with layer1 *\/ */
fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
fr->framesize /= mpg123_freqs[fr->sampling_frequency];
fr->framesize += fr->padding - 4;
break;
case 3:
/* fr->do_layer = mpg123_do_layer3; */
if (fr->lsf)
ssize = (fr->stereo == 1) ? 9 : 17;
else
ssize = (fr->stereo == 1) ? 17 : 32;
if (fr->error_protection)
ssize += 2;
fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
fr->framesize /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
fr->framesize = fr->framesize + fr->padding - 4;
break;
default:
return (0);
}
if(fr->framesize > MAXFRAMESIZE)
return 0;
return 1;
}
#define GET_INT32BE(b) \
(i = (b[0] << 24) | (b[1] << 16) | b[2] << 8 | b[3], b += 4, i)
static int mpg123_get_xing_header(xing_header_t * xing, unsigned char *buf)
{
int i, head_flags;
int id, mode;
memset(xing, 0, sizeof(xing_header_t));
/* get selected MPEG header data */
id = (buf[1] >> 3) & 1;
mode = (buf[3] >> 6) & 3;
buf += 4;
/* Skip the sub band data */
if (id)
{
/* mpeg1 */
if (mode != 3)
buf += 32;
else
buf += 17;
}
else
{
/* mpeg2 */
if (mode != 3)
buf += 17;
else
buf += 9;
}
if (strncmp(buf, "Xing", 4))
return 0;
buf += 4;
head_flags = GET_INT32BE(buf);
if (head_flags & FRAMES_FLAG)
xing->frames = GET_INT32BE(buf);
if (xing->frames < 1)
xing->frames = 1;
if (head_flags & BYTES_FLAG)
xing->bytes = GET_INT32BE(buf);
if (head_flags & TOC_FLAG)
{
for (i = 0; i < 100; i++)
xing->toc[i] = buf[i];
buf += 100;
}
#ifdef XING_DEBUG
for (i = 0; i < 100; i++)
{
if ((i % 10) == 0)
fprintf(stderr, "\n");
fprintf(stderr, " %3d", xing->toc[i]);
}
#endif
return 1;
}
static double mpg123_compute_tpf(struct frame *fr)
{
const int bs[4] = {0, 384, 1152, 1152};
double tpf;
tpf = bs[fr->lay];
tpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
return tpf;
}
static double mpg123_compute_bpf(struct frame *fr)
{
double bpf;
switch (fr->lay)
{
case 1:
bpf = tabsel_123[fr->lsf][0][fr->bitrate_index];
bpf *= 12000.0 * 4.0;
bpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
break;
case 2:
case 3:
bpf = tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index];
bpf *= 144000;
bpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
break;
default:
bpf = 1.0;
}
return bpf;
}
unsigned int mpg123_getbits(int number_of_bits)
{
unsigned long rval;
#ifdef DEBUG_GETBITS
fprintf(stderr, "g%d", number_of_bits);
#endif
if(!number_of_bits)
return 0;
#if 0
check_buffer_range(number_of_bits + bsi.bitindex);
#endif
{
rval = bsi.wordpointer[0];
rval <<= 8;
rval |= bsi.wordpointer[1];
rval <<= 8;
rval |= bsi.wordpointer[2];
rval <<= bsi.bitindex;
rval &= 0xffffff;
bsi.bitindex += number_of_bits;
rval >>= (24-number_of_bits);
bsi.wordpointer += (bsi.bitindex >> 3);
bsi.bitindex &= 7;
}
#ifdef DEBUG_GETBITS
fprintf(stderr,":%x ",rval);
#endif
return rval;
}
void I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT], struct frame *fr)
{
unsigned int *ba = balloc;
unsigned int *sca = (unsigned int *) scale_index;
if (fr->stereo)
{
int i;
int jsbound = fr->jsbound;
for (i = 0; i < jsbound; i++)
{
*ba++ = mpg123_getbits(4);
*ba++ = mpg123_getbits(4);
}
for (i = jsbound; i < SBLIMIT; i++)
*ba++ = mpg123_getbits(4);
ba = balloc;
for (i = 0; i < jsbound; i++)
{
if ((*ba++))
*sca++ = mpg123_getbits(6);
if ((*ba++))
*sca++ = mpg123_getbits(6);
}
for (i = jsbound; i < SBLIMIT; i++)
if ((*ba++))
{
*sca++ = mpg123_getbits(6);
*sca++ = mpg123_getbits(6);
}
}
else
{
int i;
for (i = 0; i < SBLIMIT; i++)
*ba++ = mpg123_getbits(4);
ba = balloc;
for (i = 0; i < SBLIMIT; i++)
if ((*ba++))
*sca++ = mpg123_getbits(6);
}
}
void I_step_two(real fraction[2][SBLIMIT], unsigned int balloc[2 * SBLIMIT],
unsigned int scale_index[2][SBLIMIT], struct frame *fr)
{
int i, n;
int smpb[2 * SBLIMIT]; /* values: 0-65535 */
int *sample;
register unsigned int *ba;
register unsigned int *sca = (unsigned int *) scale_index;
if (fr->stereo)
{
int jsbound = fr->jsbound;
register real *f0 = fraction[0];
register real *f1 = fraction[1];
ba = balloc;
for (sample = smpb, i = 0; i < jsbound; i++)
{
if ((n = *ba++))
*sample++ = mpg123_getbits(n + 1);
if ((n = *ba++))
*sample++ = mpg123_getbits(n + 1);
}
for (i = jsbound; i < SBLIMIT; i++)
if ((n = *ba++))
*sample++ = mpg123_getbits(n + 1);
ba = balloc;
for (sample = smpb, i = 0; i < jsbound; i++)
{
if ((n = *ba++))
*f0++ = (real) (((-1) << n) + (*sample++) + 1) * mpg123_muls[n + 1][*sca++];
else
*f0++ = 0.0;
if ((n = *ba++))
*f1++ = (real) (((-1) << n) + (*sample++) + 1) * mpg123_muls[n + 1][*sca++];
else
*f1++ = 0.0;
}
for (i = jsbound; i < SBLIMIT; i++)
{
if ((n = *ba++))
{
real samp = (((-1) << n) + (*sample++) + 1);
*f0++ = samp * mpg123_muls[n + 1][*sca++];
*f1++ = samp * mpg123_muls[n + 1][*sca++];
}
else
*f0++ = *f1++ = 0.0;
}
for (i = fr->down_sample_sblimit; i < 32; i++)
fraction[0][i] = fraction[1][i] = 0.0;
}
else
{
register real *f0 = fraction[0];
ba = balloc;
for (sample = smpb, i = 0; i < SBLIMIT; i++)
if ((n = *ba++))
*sample++ = mpg123_getbits(n + 1);
ba = balloc;
for (sample = smpb, i = 0; i < SBLIMIT; i++)
{
if ((n = *ba++))
*f0++ = (real) (((-1) << n) + (*sample++) + 1) * mpg123_muls[n + 1][*sca++];
else
*f0++ = 0.0;
}
for (i = fr->down_sample_sblimit; i < 32; i++)
fraction[0][i] = 0.0;
}
}
static guint get_track_time_file(FILE * file)
{
guint32 head;
guchar tmp[4], *buf;
struct frame frm;
xing_header_t xing_header;
double tpf, bpf;
guint32 len;
if (!file)
return -1;
fseek(file, 0, SEEK_SET);
if (fread(tmp, 1, 4, file) != 4)
return 0;
head = convert_to_header(tmp);
while (!mpg123_head_check(head))
{
head <<= 8;
if (fread(tmp, 1, 1, file) != 1)
return 0;
head |= tmp[0];
}
if (mpg123_decode_header(&frm, head))
{
buf = g_malloc(frm.framesize + 4);
fseek(file, -4, SEEK_CUR);
fread(buf, 1, frm.framesize + 4, file);
tpf = mpg123_compute_tpf(&frm);
if (mpg123_get_xing_header(&xing_header, buf))
{
g_free(buf);
return ((guint) (tpf * xing_header.frames * 1000));
}
g_free(buf);
bpf = mpg123_compute_bpf(&frm);
fseek(file, 0, SEEK_END);
len = ftell(file);
fseek(file, -128, SEEK_END);
fread(tmp, 1, 3, file);
if (!strncmp(tmp, "TAG", 3))
len -= 128;
return ((guint) ((guint)(len / bpf) * tpf * 1000));
}
return 0;
}
static guint get_track_time (const gchar *path)
{
guint result = 0;
if (path)
{
FILE *file = fopen (path, "r");
result = get_track_time_file (file);
if (file) fclose (file);
}
return result;
}
/* libid3tag stuff */
#include <id3tag.h>
#include "prefs.h"
#ifndef ID3_FRAME_GROUP
#define ID3_FRAME_GROUP "TPE2"
#endif
static const gchar* id3_get_binary (struct id3_tag *tag,
char *frame_name,
id3_length_t *len,
int index)
{
const id3_byte_t *binary = NULL;
struct id3_frame *frame;
union id3_field *field;
g_return_val_if_fail (len, NULL);
*len = 0;
frame = id3_tag_findframe (tag, frame_name, index);
#if LOCALDEBUG
printf ("frame: %p\n", frame);
#endif
if (!frame) return NULL;
#if LOCALDEBUG
printf (" nfields: %d\n", frame->nfields);
if (strncmp (frame_name, "APIC", 4) == 0)
{
field = id3_frame_field (frame, 2);
printf (" picture type: %ld\n", field->number.value);
}
#endif
#if 0
/*-----------------*/
/* just to show that this field (before last) contains the d8 ff e0 ff
part of the start of a jpeg file when the coverart war embedded by iTunes */
const id3_ucs4_t *string = NULL;
gchar *raw = NULL;
/* The last field contains the data */
field = id3_frame_field (frame, frame->nfields-2);
#if LOCALDEBUG
printf (" field: %p\n", field);
#endif
if (!field) return NULL;
#if LOCALDEBUG
printf (" type: %d\n", field->type);
#endif
switch (field->type)
{
case ID3_FIELD_TYPE_STRING:
string = id3_field_getstring (field);
break;
default:
break;
}
/* ISO_8859_1 is just a "marker" -- most people just drop
whatever coding system they are using into it, so we use
charset_to_utf8() to convert to utf8 */
if (string)
{
raw = id3_ucs4_latin1duplicate (string);
}
#if LOCALDEBUG
{
FILE *file;
printf (" string len: %d\n", raw?strlen(raw):0);
file = fopen ("/tmp/folder1.jpg", "w");
fwrite (raw, 1, raw?strlen(raw):0, file);
fclose (file);
}
#endif
g_free (raw);
/*-----------------*/
#endif
/* The last field contains the data */
field = id3_frame_field (frame, frame->nfields-1);
#if LOCALDEBUG
printf (" field: %p\n", field);
#endif
if (!field) return NULL;
#if LOCALDEBUG
printf (" type: %d\n", field->type);
#endif
switch (field->type)
{
case ID3_FIELD_TYPE_BINARYDATA:
binary = id3_field_getbinarydata(field, len);
break;
default:
break;
}
#if LOCALDEBUG
{
FILE *file;
printf (" binary len: %ld\n", *len);
file = fopen ("/tmp/folder2.jpg", "w");
fwrite (binary, 1, *len, file);
fclose (file);
}
#endif
return binary;
}
static gchar* id3_get_string (struct id3_tag *tag, char *frame_name)
{
const id3_ucs4_t *string = NULL;
const id3_byte_t *binary = NULL;
id3_length_t len = 0;
struct id3_frame *frame;
union id3_field *field;
gchar *utf8 = NULL;
enum id3_field_textencoding encoding = ID3_FIELD_TEXTENCODING_ISO_8859_1;
frame = id3_tag_findframe (tag, frame_name, 0);
#if LOCALDEGUB
printf ("frame: %p\n", frame);
#endif
if (!frame) return NULL;
/* Find the encoding used for the field */
field = id3_frame_field (frame, 0);
#if LOCALDEBUG
printf ("field: %p\n", field);
printf ("type: %d\n", id3_field_type (field));
#endif
if (field && (id3_field_type (field) == ID3_FIELD_TYPE_TEXTENCODING))
{
encoding = field->number.value;
#if LOCALDEBUG
printf ("encoding: %d\n", encoding);
#endif
}
/* The last field contains the data */
field = id3_frame_field (frame, frame->nfields-1);
#if LOCALDEBUG
printf ("field: %p\n", field);
#endif
if (!field) return NULL;
#if LOCALDEBUG
printf ("type: %d\n", field->type);
#endif
switch (field->type)
{
case ID3_FIELD_TYPE_STRINGLIST:
string = id3_field_getstrings (field, 0);
break;
case ID3_FIELD_TYPE_STRINGFULL:
string = id3_field_getfullstring (field);
break;
case ID3_FIELD_TYPE_BINARYDATA:
binary = id3_field_getbinarydata(field, &len);
#if LOCALDEBUG
printf ("len: %ld\nbinary: %s\n", len, binary+1);
#endif
if (len > 0)
return charset_to_utf8 (binary+1);
break;
default:
break;
}
/* printf ("string: %p\n", string); */
if (!string) return NULL;
if (strcmp (frame_name, ID3_FRAME_GENRE) == 0)
string = id3_genre_name (string);
if (encoding == ID3_FIELD_TEXTENCODING_ISO_8859_1)
{
/* ISO_8859_1 is just a "marker" -- most people just drop
whatever coding system they are using into it, so we use
charset_to_utf8() to convert to utf8 */
id3_latin1_t *raw = id3_ucs4_latin1duplicate (string);
utf8 = charset_to_utf8 (raw);
g_free (raw);
}
else
{
/* Standard unicode is being used -- we won't have to worry
about charsets then. */
utf8 = id3_ucs4_utf8duplicate (string);
}
return utf8;
}
static void id3_set_string (struct id3_tag *tag,
const char *frame_name,
const char *data,
enum id3_field_textencoding encoding)
{
int res;
struct id3_frame *frame;
union id3_field *field;
id3_ucs4_t *ucs4;
/* clear the frame, because of bug in libid3tag see
http://www.mars.org/mailman/public/mad-dev/2004-October/001113.html
*/
while ((frame = id3_tag_findframe (tag, frame_name, 0)))
{
id3_tag_detachframe (tag, frame);
id3_frame_delete (frame);
}
if ((data == NULL) || (strlen(data) == 0))
return;
frame = id3_frame_new (frame_name);
id3_tag_attachframe (tag, frame);
/* Use the specified text encoding */
field = id3_frame_field (frame, 0);
id3_field_settextencoding(field, encoding);
if ((strcmp (frame_name, ID3_FRAME_COMMENT) == 0) ||
(strcmp (frame_name, "USLT") == 0))
{
field = id3_frame_field (frame, 3);
field->type = ID3_FIELD_TYPE_STRINGFULL;
}
else
{
field = id3_frame_field (frame, 1);
field->type = ID3_FIELD_TYPE_STRINGLIST;
}
/* maybe could be optimized see
http://www.mars.org/mailman/public/mad-dev/2002-October/000739.html
*/
/* don't handle the genre frame differently any more */
#define USE_GENRE_IDS 0
#if USE_GENRE_IDS
if (strcmp (frame_name, ID3_FRAME_GENRE) == 0)
{
id3_ucs4_t *tmp_ucs4 = id3_utf8_ucs4duplicate ((id3_utf8_t *)data);
int index = id3_genre_number (tmp_ucs4);
if (index != -1)
{
/* valid genre -- simply store the genre number */
gchar *tmp = g_strdup_printf("(%d)", index);
ucs4 = id3_latin1_ucs4duplicate (tmp);
g_free (tmp);
}
else
{
/* oups -- not a valid genre -- save the entire genre string */
if (encoding == ID3_FIELD_TEXTENCODING_ISO_8859_1)
{
/* we read 'ISO_8859_1' to stand for 'any locale
charset' -- most programs seem to work that way */
id3_latin1_t *raw = charset_from_utf8 (data);
ucs4 = id3_latin1_ucs4duplicate (raw);
g_free (raw);
}
else
{
/* Yeah! We use unicode encoding and won't have to
worry about charsets */
ucs4 = tmp_ucs4;
tmp_ucs4 = NULL;
}
}
g_free (tmp_ucs4);
}
else
{
#endif
if (encoding == ID3_FIELD_TEXTENCODING_ISO_8859_1)
{
/* we read 'ISO_8859_1' to stand for 'any locale charset'
-- most programs seem to work that way */
id3_latin1_t *raw = charset_from_utf8 (data);
ucs4 = id3_latin1_ucs4duplicate (raw);
g_free (raw);
}
else
{
/* Yeah! We use unicode encoding and won't have to
worry about charsets */
ucs4 = id3_utf8_ucs4duplicate ((id3_utf8_t *)data);
}
#if USE_GENRE_IDS
}
#endif
if ((strcmp (frame_name, ID3_FRAME_COMMENT) == 0) || (strcmp (frame_name, "USLT") == 0))
res = id3_field_setfullstring (field, ucs4);
else
res = id3_field_setstrings (field, 1, &ucs4);
g_free (ucs4);
if (res != 0)
g_print(_("Error setting ID3 field: %s\n"), frame_name);
}
/***
* Reads id3v1.x / id3v2 apic data
* @returns: TRUE on success, else FALSE.
*/
static gboolean id3_apic_read (const gchar *filename,
guchar **image_data, gsize *image_data_len)
{
struct id3_file *id3file;
struct id3_tag *id3tag;
g_return_val_if_fail (filename, FALSE);
g_return_val_if_fail (image_data, FALSE);
g_return_val_if_fail (image_data_len, FALSE);
*image_data = NULL;
*image_data_len = 0;
if (!(id3file = id3_file_open (filename, ID3_FILE_MODE_READONLY)))
{
gchar *fbuf = charset_to_utf8 (filename);
g_print(_("ERROR while opening file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return FALSE;
}
if ((id3tag = id3_file_tag(id3file)))
{
id3_length_t len;
const guchar *coverart = NULL;
int i;
struct id3_frame *frame;
/* Loop through APIC tags and set coverart. The picture type should be
* 3 -- Cover (front), but iTunes has been known to use 0 -- Other. */
for (i = 0; (frame = id3_tag_findframe(id3tag, "APIC", i)) != NULL; i++)
{
union id3_field *field = id3_frame_field (frame, 2);
int pictype = field->number.value;
/* printf ("%s: found apic type %d\n", filename, pictype);*/
/* We'll prefer type 3 (cover) over type 0 (other) */
if (pictype == 3)
{
coverart = id3_get_binary (id3tag, "APIC", &len, i);
break;
}
if ((pictype == 0) && !coverart)
{
coverart = id3_get_binary (id3tag, "APIC", &len, i);
}
}
if (coverart)
{ /* I guess iTunes is doing something wrong -- the
* beginning of the coverart data ends up in a different
field... We'll just add the missing data manually. */
const guchar itunes_broken_jfif_marker[] =
{ 0x10, 'J', 'F', 'I', 'F'};
if (len >= 5)
{
if (strncmp (itunes_broken_jfif_marker, coverart, 5) == 0)
{
const guchar itunes_missing_header[] =
{ 0xff, 0xd8, 0xff, 0xe0, 0x00 };
*image_data = g_malloc (len+5);
memcpy (*image_data, itunes_missing_header, 5);
memcpy ((*image_data)+5, coverart, len);
*image_data_len = len+5;
}
}
if (!*image_data)
{
*image_data = g_malloc (len);
memcpy (*image_data, coverart, len);
*image_data_len = len;
}
#if LOCALDEBUG
if (*image_data)
{
FILE *file;
file = fopen ("/tmp/folder.jpg", "w");
fwrite (*image_data, 1, *image_data_len, file);
fclose (file);
}
#endif
}
}
id3_file_close (id3file);
return TRUE;
}
/* Do some checks on the genre string -- ideally this should
* be done within the id3tag library, I think */
static void handle_genre_variations (gchar **genrep)
{
/* http://www.id3.org/id3v2.3.0#head-42b02d20fb8bf48e38ec5415e34909945dd849dc */
/* The 'Content type', which previously was stored as a one byte
* numeric value only, is now a numeric string. You may use one or
* several of the types as ID3v1.1 did or, since the category list
* would be impossible to maintain with accurate and up to date
* categories, define your own.
* References to the ID3v1 genres can be made by, as first byte, enter
* "(" followed by a number from the genres list (appendix A) and
* ended with a ")" character. This is optionally followed by a
* refinement, e.g. "(21)" or "(4)Eurodisco". Several references can
* be made in the same frame, e.g. "(51)(39)". If the refinement
* should begin with a "(" character it should be replaced with "((",
* e.g. "((I can figure out any genre)" or "(55)((I think...)". The
* following new content types is defined in ID3v2 and is implemented
* in the same way as the numeric content types, e.g. "(RX)".
*/
/* If a "refinement" exists, we will forget about the ID's. */
/* If only an ID is given, we will translate that ID into a string */
gchar *genre, *oldgenre, *utf8_genre=NULL;
const gchar *newgenre = NULL;
g_return_if_fail (genrep);
genre = *genrep;
oldgenre = *genrep;
if (genre == NULL) return;
while (*genre)
{
if (genre[0] == '(')
{
if (genre[1] == '(')
{
/* starting with "((" */
newgenre = &genre[1];
break;
}
if (isdigit (genre[1]))
{ /* possibly a genre ID */
int num, genreid;
num = sscanf (genre, "(%d)", &genreid);
if (num != 1)
{ /* invalid ID -> give up */
newgenre = &genre[0];
break;
}
genre = strchr (&genre[1], ')');
g_return_if_fail (genre);
++genre;
if (!newgenre)
{ /* retrieve genre string from ID -- we only
* convert the first ID */
id3_ucs4_t const *ucs4_genre = id3_genre_index (genreid);
if (ucs4_genre == NULL)
{
break;
}
utf8_genre = id3_ucs4_utf8duplicate (ucs4_genre);
newgenre = utf8_genre;
}
}
else
{
newgenre = &genre[0];
break;
}
}
else
{
newgenre = &genre[0];
break;
}
}
if (newgenre && (newgenre != oldgenre))
{
*genrep = g_strdup (newgenre);
g_free (oldgenre);
}
g_free (utf8_genre);
}
/***
* Reads id3v1.x / id3v2 tag and load data into the Id3tag structure.
* If a tag entry exists (ex: title), we allocate memory, else value
* stays to NULL
* @returns: TRUE on success, else FALSE.
*/
gboolean id3_tag_read (const gchar *filename, File_Tag *tag)
{
struct id3_file *id3file;
struct id3_tag *id3tag;
gchar* string;
gchar* string2;
g_return_val_if_fail (filename, FALSE);
g_return_val_if_fail (tag, FALSE);
memset (tag, 0, sizeof (File_Tag));
if (!(id3file = id3_file_open (filename, ID3_FILE_MODE_READONLY)))
{
gchar *fbuf = charset_to_utf8 (filename);
g_print(_("ERROR while opening file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return FALSE;
}
if ((id3tag = id3_file_tag(id3file)))
{
tag->title = id3_get_string (id3tag, ID3_FRAME_TITLE);
tag->artist = id3_get_string (id3tag, ID3_FRAME_ARTIST);
if (!tag->artist || !*tag->artist)
{
g_free (tag->artist);
tag->artist = id3_get_string (id3tag, ID3_FRAME_GROUP);
} else {
tag->albumartist = id3_get_string (id3tag, ID3_FRAME_GROUP);
}
tag->album = id3_get_string (id3tag, ID3_FRAME_ALBUM);
tag->year = id3_get_string (id3tag, ID3_FRAME_YEAR);
tag->composer = id3_get_string (id3tag, "TCOM");
tag->comment = id3_get_string (id3tag, ID3_FRAME_COMMENT);
tag->genre = id3_get_string (id3tag, ID3_FRAME_GENRE);
tag->compilation = id3_get_string (id3tag, "TCMP");
tag->subtitle = id3_get_string (id3tag, "TIT3");
tag->lyrics = id3_get_string (id3tag, "USLT");
tag->podcasturl = id3_get_string (id3tag, "YTID");
tag->podcastrss = id3_get_string (id3tag, "YWFD");
tag->description = id3_get_string (id3tag, "YTDS");
tag->time_released = id3_get_string (id3tag, "YTDR");
tag->BPM = id3_get_string (id3tag, "TBPM");
tag->sort_artist = id3_get_string (id3tag, "TSOP");
tag->sort_album = id3_get_string (id3tag, "TSOA");
tag->sort_title = id3_get_string (id3tag, "TSOT");
tag->sort_albumartist = id3_get_string (id3tag, "TSO2");
tag->sort_composer = id3_get_string (id3tag, "TSOC");
string = id3_get_string (id3tag, "TLEN");
if (string)
{
tag->songlen = (guint32) strtoul (string, 0, 10);
g_free (string);
}
string = id3_get_string (id3tag, ID3_FRAME_TRACK);
if (string)
{
string2 = strchr(string,'/');
if (string2)
{
tag->track_total = g_strdup_printf ("%.2d", atoi (string2+1));
*string2 = '\0';
}
tag->trackstring = g_strdup_printf ("%.2d", atoi (string));
g_free(string);
}
/* CD/disc number tag handling */
string = id3_get_string (id3tag, "TPOS");
if (string)
{
string2 = strchr(string,'/');
if (string2)
{
tag->cdno_total = g_strdup_printf ("%.2d", atoi (string2+1));
*string2 = '\0';
}
tag->cdnostring = g_strdup_printf ("%.2d", atoi (string));
g_free(string);
}
/* Do some checks on the genre string -- ideally this should
* be done within the id3tag library, I think */
handle_genre_variations (&tag->genre);
}
id3_file_close (id3file);
return TRUE;
}
static enum id3_field_textencoding get_encoding_of (struct id3_tag *tag, const char *frame_name)
{
struct id3_frame *frame;
enum id3_field_textencoding encoding = -1;
frame = id3_tag_findframe (tag, frame_name, 0);
if (frame)
{
union id3_field *field = id3_frame_field (frame, 0);
if (field && (id3_field_type (field) == ID3_FIELD_TYPE_TEXTENCODING))
encoding = field->number.value;
}
return encoding;
}
/* Find out which encoding is being used. If in doubt, return
* latin1. This code assumes that the same encoding is used in all
* fields. */
static enum id3_field_textencoding get_encoding (struct id3_tag *tag)
{
enum id3_field_textencoding enc;
enc = get_encoding_of (tag, ID3_FRAME_TITLE);
if (enc != -1) return enc;
enc = get_encoding_of (tag, ID3_FRAME_ARTIST);
if (enc != -1) return enc;
enc = get_encoding_of (tag, ID3_FRAME_ALBUM);
if (enc != -1) return enc;
enc = get_encoding_of (tag, "TCOM");
if (enc != -1) return enc;
enc = get_encoding_of (tag, ID3_FRAME_COMMENT);
if (enc != -1) return enc;
enc = get_encoding_of (tag, ID3_FRAME_YEAR);
if (enc != -1) return enc;
return ID3_FIELD_TEXTENCODING_ISO_8859_1;
}
/* I'm not really sure about this: The original TAG identifier was
"TID", but no matter what I do I end up writing "YTID" */
void set_uncommon_tag (struct id3_tag *id3tag,
const gchar *id,
const gchar *text,
enum id3_field_textencoding encoding)
{
#if 0
struct id3_frame *frame;
union id3_field *field;
frame = id3_tag_findframe (id3tag, id, 0);
frame->flags = 0;
field = id3_frame_field (frame, 0);
if (field)
{
string1 = g_strdup_printf ("%c%s", '\0',
track->podcasturl);
id3_field_setbinarydata (field, string1,
strlen(track->podcasturl)+1);
g_free (string1);
}
#endif
}
/**
* Write the ID3 tags to the file.
* @returns: TRUE on success, else FALSE.
*/
gboolean mp3_write_file_info (const gchar *filename, Track *track)
{
struct id3_tag* id3tag;
struct id3_file* id3file;
gint error = 0;
id3file = id3_file_open (filename, ID3_FILE_MODE_READWRITE);
if (!id3file)
{
gchar *fbuf = charset_to_utf8 (filename);
g_print(_("ERROR while opening file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return FALSE;
}
if ((id3tag = id3_file_tag(id3file)))
{
char *string1;
enum id3_field_textencoding encoding;
/* use the same coding as before... */
encoding = get_encoding (id3tag);
/* ...unless it's ISO_8859_1 and prefs say we should use
unicode (i.e. ID3v2.4) */
if (prefs_get_int("id3_write_id3v24") &&
(encoding == ID3_FIELD_TEXTENCODING_ISO_8859_1))
encoding = ID3_FIELD_TEXTENCODING_UTF_8;
/* always render id3v1 to prevent dj studio from crashing */
id3_tag_options(id3tag, ID3_TAG_OPTION_ID3V1, ~0);
/* turn off frame compression and crc information to let
itunes read tags see
http://www.mars.org/mailman/public/mad-dev/2002-October/000742.html
*/
id3_tag_options(id3tag, ID3_TAG_OPTION_COMPRESSION, 0);
id3_tag_options(id3tag, ID3_TAG_OPTION_CRC, 0);
id3_set_string (id3tag, ID3_FRAME_TITLE, track->title, encoding);
id3_set_string (id3tag, ID3_FRAME_ARTIST, track->artist, encoding);
id3_set_string (id3tag, ID3_FRAME_GROUP, track->albumartist, encoding);
id3_set_string (id3tag, ID3_FRAME_ALBUM, track->album, encoding);
id3_set_string (id3tag, ID3_FRAME_GENRE, track->genre, encoding);
id3_set_string (id3tag, ID3_FRAME_COMMENT, track->comment, encoding);
id3_set_string (id3tag, "TIT3", track->subtitle, encoding);
id3_set_string (id3tag, "TSOP", track->sort_artist, encoding);
id3_set_string (id3tag, "TSOA", track->sort_album, encoding);
id3_set_string (id3tag, "TSOT", track->sort_title, encoding);
id3_set_string (id3tag, "TSO2", track->sort_albumartist, encoding);
id3_set_string (id3tag, "TSOC", track->sort_composer, encoding);
set_uncommon_tag (id3tag, "YTID", track->podcasturl, encoding);
set_uncommon_tag (id3tag, "YTDS", track->description, encoding);
set_uncommon_tag (id3tag, "YWFD", track->podcastrss, encoding);
id3_set_string (id3tag, "TCOM", track->composer, encoding);
string1 = g_strdup_printf("%d", track->year);
id3_set_string(id3tag, ID3_FRAME_YEAR, string1, encoding);
g_free(string1);
string1 = g_strdup_printf("%d", track->BPM);
id3_set_string(id3tag, "TBPM", string1, encoding);
g_free(string1);
if (track->tracks)
string1 = g_strdup_printf ("%d/%d",
track->track_nr, track->tracks);
else
string1 = g_strdup_printf ("%d", track->track_nr);
id3_set_string (id3tag, ID3_FRAME_TRACK, string1, encoding);
g_free(string1);
if (track->cds)
string1 = g_strdup_printf ("%d/%d",
track->cd_nr, track->cds);
else
string1 = g_strdup_printf ("%d", track->cd_nr);
id3_set_string (id3tag, "TPOS", string1, encoding);
g_free(string1);
string1 = g_strdup_printf ("%d", track->compilation);
id3_set_string (id3tag, "TCMP", string1, encoding);
g_free(string1);
}
if (id3_file_update(id3file) != 0)
{
gchar *fbuf = charset_to_utf8 (filename);
g_print(_("ERROR while writing tag to file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return FALSE;
}
id3_file_close (id3file);
if (error) return FALSE;
else return TRUE;
}
/*
* Code used to calculate the CRC-16 of the info tag. Used to check the
* validity of the data read from the lame tag.
* Code taken from the libmad project, licensed under GPL
*/
static
unsigned short const crc_table[256] = {
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
};
unsigned short crc_compute(char const *data, unsigned int length,
unsigned short init)
{
register unsigned int crc;
for (crc = init; length >= 8; length -= 8) {
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
}
switch (length) {
case 7: crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
case 6: crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
case 5: crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
case 4: crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
case 3: crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
case 2: crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
case 1: crc = crc_table[(crc ^ *data++) & 0xff] ^ (crc >> 8);
case 0: break;
}
return crc;
}
/*
* Code to read the ReplayGain Values stored by LAME in its own tag.
*
* Most of the relevant information has been extracted from them LAME sources
* (http://lame.sourceforge.net/).
* The "Mp3 info Tag rev 1 specifications - draft 0"
* (http://gabriel.mp3-tech.org/mp3infotag.html) by Gabriel Bouvigne describes
* the actual Tag (except for small changes).
* Details on the actual ReplayGain fields have been obtained from
* http://www.replaygain.org .
*
* Apart from that, some information has been derived from phwip's LameTag
* (http://www.silisoftware.com/applets/?scriptname=LameTag)
*
*
* Code to read the ReplayGain Values stored in an Ape tag.
*
* Info on Lyrics3 V2.00 can be found at:
* http://www.id3.org/lyrics3200.html
* On the actual Ape Tag V2.0:
* http://www.personal.uni-jena.de/~pfk/mpp/sv8/apetag.html
*
* Copyright (C) 2004 Jens Taprogge <jens.taprogge at post.rwth-aachen.de>
*
* Provided under GPL according to Jens Taprogge. (JCS -- 12 March 2004)
*/
#define TAG_FOOTER 0x10
#define LAME_OFFSET 0x74
#define SIDEINFO_MPEG1_MONO 17
#define SIDEINFO_MPEG1_MULTI 32
#define SIDEINFO_MPEG2_MONO 9
#define SIDEINFO_MPEG2_MULTI 17
#define ID3V1_SIZE 0x80
#define APE_FOOTER_SIZE 0x20
#define LYRICS_FOOTER_SIZE 0x0f
static gint lame_vcmp(gchar a[5], gchar b[5]) {
int r;
r = strncmp(a, b, 4);
if (r) return r;
if (a[4] == b[4]) return 0;
/* check for '.': indicates subminor version */
if (a[4] == '.') return 1;
if (b[4] == '.') return -1;
/* check for alpha or beta versions */
if (a[4] == ' ') return 1;
if (b[4] == ' ') return -1;
/* check for alpha, beta etc. indicated by a, b... */
return strncmp(&a[4], &b[4], 1);
}
/* buf[] must be declared unsigned -- otherwise the casts, shifts and
additions below produce funny results */
static void read_lame_replaygain(unsigned char buf[],
GainData *gd, int gain_adjust) {
char oc, nc;
gint gain;
g_return_if_fail (gd);
/* buf[0] and buf[1] are a bit field:
3 bits: name (mask: 0xe0 = 11100000)
3 bits: originator (mask: 0x1c = 00011100)
1 bit: negative if set (mask: 0x02 = 00000010)
9 bits: value
*/
/* check originator */
oc = (buf[0] & 0x1c) >> 2;
if ((oc <= 0) || (oc > 3)) return;
/* check name code */
nc = buf[0] & 0xe0;
if (!((nc == 0x20) || (nc == 0x40))) return;
gain = ((((guint)buf[0]) & 0x1) << 8) + buf[1];
/* This would be a value of -0.
* That value however is illegal by current standards and reserved for
* future use. */
if ((!gain) && (buf[0] & 0x02)) return;
if (buf[0] & 2) gain = -gain;
gain += gain_adjust;
switch (nc) {
case 0x20:
if (gd->radio_gain_set) return;
gd->radio_gain = (gdouble)gain / 10;
gd->radio_gain_set = TRUE;
/* printf("radio_gain (lame): %i\n", gd->radio_gain); */
break;
case 0x40:
if (gd->audiophile_gain_set) return;
gd->audiophile_gain = (gdouble)gain / 10;
gd->audiophile_gain_set = TRUE;
/* printf("audiophile_gain (lame): %i\n",
gd->audiophile_gain);*/
break;
}
}
static inline guint32 parse_ape_uint32(char *buf) {
return (buf[0] & 0xff) | (buf[1] & 0xff) << 8
| (buf[2] & 0xff) << 16 | (buf[3] & 0xff) << 24;
}
static inline guint32 parse_lame_uint32(char *buf) {
return (buf[0] & 0xff) << 24 | (buf[1] & 0xff) << 16
| (buf[2] & 0xff) << 8 | (buf[3] & 0xff);
}
static inline guint16 parse_lame_uint16(char *buf) {
return (buf[0] & 0xff) << 8 | (buf[1] & 0xff);
}
/*
* mp3_get_track_lame_replaygain - read the specified file and scan for LAME Tag
* ReplayGain information.
*
* @path: localtion of the file
* @track: structure holding track information
*
* FIXME: Are there other encoders writing a LAME Tag using a different magic
* string?
* TODO: Check CRC.
*/
gboolean mp3_get_track_lame_replaygain (const gchar *path, GainData *gd)
{
unsigned char ubuf[2];
int gain_adjust = 0;
LameTag lt;
g_return_val_if_fail (path, FALSE);
if (!mp3_read_lame_tag (path, <))
goto rg_fail;
g_return_val_if_fail (gd, FALSE);
gd->radio_gain = 0;
gd->audiophile_gain = 0;
gd->peak_signal = 0;
gd->radio_gain_set = FALSE;
gd->audiophile_gain_set = FALSE;
gd->peak_signal_set = FALSE;
if (lame_vcmp(lt.version_string, "3.90") < 0) {
/* fprintf(stderr, "Old lame version (%c%c%c%c%c). Not used.\n",
version[0], version[1], version[2], version[3], version[4]); */
goto rg_fail;
}
/* Don't know when fixed-point PeakSingleAmplitude
* was introduced exactly. 3.94b will be used for now.) */
if ((lame_vcmp(lt.version_string, "3.94b") >= 0)) {
if ((!gd->peak_signal_set) && lt.peak_signal_amplitude) {
gd->peak_signal = lt.peak_signal_amplitude;
gd->peak_signal_set = TRUE;
/* printf("peak_signal (lame): %f\n", (double)
gd->peak_signal / 0x800000);*/
}
} else {
float f = *((float *) (void *) (<.peak_signal_amplitude)) * 0x800000;
gd->peak_signal = (guint32) f;
/* I would like to see an example of that. */
/* printf("peak_signal (lame floating point): %f. PLEASE report.\n",
(double) gd->peak_signal / 0x800000);*/
}
/*
* Versions prior to 3.95.1 used a reference volume of 83dB.
* (As compared to the currently used 89dB.)
*/
if ((lame_vcmp(lt.version_string, "3.95.") < 0)) {
gain_adjust = 60;
/* fprintf(stderr, "Old lame version (%c%c%c%c%c). Adjusting gain.\n",
version[0], version[1], version[2], version[3], version[4]); */
}
/* radio gain */
memcpy(ubuf,<.radio_replay_gain,2);
read_lame_replaygain (ubuf, gd, gain_adjust);
/* audiophile gain */
memcpy(ubuf,<.audiophile_replay_gain,2);
read_lame_replaygain (ubuf, gd, gain_adjust);
return TRUE;
rg_fail:
return FALSE;
}
/*
* mp3_get_track_ape_replaygain - read the specified file and scan for Ape Tag
* ReplayGain information.
*
* @path: localtion of the file
* @track: structure holding track information
*
* The function only modifies the gains if they have not previously been set.
*/
gboolean mp3_get_track_ape_replaygain(const gchar *path, GainData *gd)
{
/* The Ape Tag is located a t the end of the file. Or at least that
* seems where it can most likely be found. Either it is at the very end
* or before a trailing ID3v1 Tag. Sometimes a Lyrics3 Tag is placed
* between the ID3v1 and the Ape Tag.
* If you find files that have the Tags located in different
* positions please let me know. */
FILE *file = NULL;
char buf[16];
char *dbuf = NULL, *ep;
int offset = 0;
int i;
int pos = 0, pos2 = 0;
guint32 version;
guint32 data_length;
guint32 entry_length = 0;
guint32 entries;
double d;
g_return_val_if_fail (gd, FALSE);
g_return_val_if_fail (path, FALSE);
file = fopen (path, "r");
if (!file)
goto rg_fail;
/* check for ID3v1 Tag */
if (fseek(file, -ID3V1_SIZE, SEEK_END) ||
fread(&buf, 1, 3, file) != 3)
goto rg_fail;
if (!strncmp(buf, "TAG", 3)) offset -= ID3V1_SIZE;
/* check for Lyrics3 Tag */
if (fseek(file, -9 + offset, SEEK_END) ||
fread(&buf, 1, 9, file) != 9)
goto rg_fail;
if (!strncmp(buf, "LYRICS200", 9)) {
if (fseek(file, -LYRICS_FOOTER_SIZE + offset, SEEK_END) ||
fread(&buf, 1, 9, file) != 9)
goto rg_fail;
data_length = buf[0] - '0';
for (i = 1; i < 6; i++) {
data_length *= 10;
data_length += buf[i] - '0';
}
if (fseek(file, -LYRICS_FOOTER_SIZE - data_length + offset,
SEEK_END) ||
fread(&buf, 1, 11, file) != 11)
goto rg_fail;
if (!strncmp(buf, "LYRICSBEGIN", 11))
offset -= LYRICS_FOOTER_SIZE + data_length;
}
/* check for APE Tag */
if (fseek(file, -APE_FOOTER_SIZE + offset, SEEK_END) ||
fread(&buf, 1, 8, file) != 8)
goto rg_fail;
if (strncmp(buf, "APETAGEX", 8)) goto rg_fail;
/* Check the version of the tag. 1000 and 2000 (v1.0 and 2.0) are the
* only ones I know about. Make suer things do not break in the future.
* */
if (fread(&buf, 1, 4, file) != 4)
goto rg_fail;
version = parse_ape_uint32(buf);
if (version != 1000 && version != 2000)
goto rg_fail;
/* determine data length */
if (fread(&buf, 1, 4, file) != 4)
goto rg_fail;
data_length = parse_ape_uint32(buf) - APE_FOOTER_SIZE;
/* determine number of entries */
if (fread(&buf, 1, 4, file) != 4)
goto rg_fail;
entries = parse_ape_uint32(buf);
/* seek to first entry and read the whole buffer*/
if (fseek(file, -APE_FOOTER_SIZE + offset - data_length, SEEK_END))
goto rg_fail;
if (!(dbuf = malloc(data_length)))
goto rg_fail;
if (fread(dbuf, 1, data_length, file) != data_length)
goto rg_fail;
for (i = 0; i < entries; i++) {
if (gd->radio_gain_set && gd->peak_signal_set) break;
pos = pos2 + entry_length;
if (pos > data_length - 10) break;
entry_length = parse_ape_uint32(&dbuf[pos]); pos += 4;
pos += 4;
pos2 = pos;
while (dbuf[pos2] && pos2 < data_length) pos2++;
if (pos2 == data_length) break;
pos2++;
if (entry_length + 1 > sizeof(buf))
continue;
/* printf ("%s:%d:%d\n",&dbuf[pos], pos2, pos); */
if (!gd->radio_gain_set && !strcasecmp(&dbuf[pos],
"REPLAYGAIN_TRACK_GAIN")) {
memcpy(buf, &dbuf[pos2], entry_length);
buf[entry_length] = '\0';
d = g_ascii_strtod(buf, &ep);
/* printf("%f\n", d); */
if ((ep == buf + entry_length - 3)
&& (!strncasecmp(ep, " dB", 3))) {
gd->radio_gain = d;
gd->radio_gain_set = TRUE;
/* printf("radio_gain (ape): %i\n", gd->radio_gain);*/
}
continue;
}
if (!gd->peak_signal_set && !strcasecmp(&dbuf[pos],
"REPLAYGAIN_TRACK_PEAK")) {
memcpy(buf, &dbuf[pos2], entry_length);
buf[entry_length] = '\0';
d = g_ascii_strtod(buf, &ep);
if (ep == buf + entry_length) {
d *= 0x800000;
gd->peak_signal = (guint32) floor(d + 0.5);
gd->peak_signal_set = TRUE;
/* printf("peak_signal (ape): %f\n", (double) gd->peak_signal / 0x800000);*/
}
continue;
}
}
free(dbuf);
fclose(file);
return TRUE;
rg_fail:
if (dbuf)
free(dbuf);
if (file)
fclose(file);
return FALSE;
}
/* ----------------------------------------------------------------------
mp3gain code
---------------------------------------------------------------------- */
#include <sys/wait.h>
#include <fcntl.h>
/**
* mp3_read_soundcheck:
*
* try to read the ReplayGain values from the LAME or Ape Tags and set
* the track's soundcheck field accordingly.
*
* @path: localtion of the file
* @track: structure holding track information
*
* The function always rereads the gain from the file.
*
* Returns TRUE if the soundcheck field could be set.
*/
gboolean mp3_read_soundcheck (const gchar *path, Track *track)
{
GainData gd;
g_return_val_if_fail (track, FALSE);
memset (&gd, 0, sizeof (GainData));
gd.radio_gain_set = FALSE;
gd.audiophile_gain_set = FALSE;
gd.peak_signal_set = FALSE;
mp3_get_track_lame_replaygain (path, &gd);
if (gd.radio_gain_set)
{
track->soundcheck = replaygain_to_soundcheck (gd.radio_gain);
return TRUE;
}
mp3_get_track_ape_replaygain (path, &gd);
if (gd.radio_gain_set)
{
track->soundcheck = replaygain_to_soundcheck (gd.radio_gain);
return TRUE;
}
return FALSE;
}
/* ----------------------------------------------------------------------
From here starts original gtkpod code
---------------------------------------------------------------------- */
/* mpeg audio slot size in bytes */
int slotsize[3] = {4,1,1}; /* layer 1, layer 2, layer 3 */
int samplesperframe[2][3] = {
{ /* MPEG 2.0 */
384,1152,576 /* layer 1, layer 2, layer 3 */
},
{ /* MPEG 1.0 */
384,1152,1152 /* layer 1, layer 2, layer 3 */
}
};
/*
* mp3_read_lame_tag - read the data from the lame tag (if it exists)
*
* @path: location of the file
* @lt: pointer to structure to be filled
*/
gboolean mp3_read_lame_tag (const gchar *path, LameTag *lt)
{
MP3Info *mp3i = NULL;
MP3Header h;
int flags;
int toskip = 0;
FILE *file;
unsigned char ubuf[LAME_TAG_SIZE];
int sideinfo;
unsigned char full_info_tag[INFO_TAG_CRC_SIZE];
g_return_val_if_fail (path, FALSE);
/* Attempt to open the file */
file = fopen (path, "r");
if (!file)
goto lt_fail;
mp3i = g_malloc0 (sizeof (MP3Info));
mp3i->filename = path;
mp3i->file = file;
get_mp3_info (mp3i);
/* use get_first_header() to seek to the first mp3 header */
get_first_header (mp3i, 0);
if (fread (full_info_tag, 1, INFO_TAG_CRC_SIZE, mp3i->file) != INFO_TAG_CRC_SIZE)
goto lt_fail;
fseek(mp3i->file, -INFO_TAG_CRC_SIZE, SEEK_CUR);
if (!get_header (mp3i->file, &h))
goto lt_fail;
/* Determine offset of Xing header based on sideinfo size */
if (h.version & 0x1)
{
sideinfo = (h.mode & 0x2) ?
SIDEINFO_MPEG1_MONO : SIDEINFO_MPEG1_MULTI;
}
else
{
sideinfo = (h.mode & 0x2) ?
SIDEINFO_MPEG2_MONO : SIDEINFO_MPEG2_MULTI;
}
if (fseek (mp3i->file, sideinfo, SEEK_CUR) ||
(fread (&ubuf[0], 1, 4, mp3i->file) != 4))
goto lt_fail;
/* Is this really a Xing or Info Header?
* FIXME: Apparently (according to madplay sources) there is a different
* possible location for the Xing header ("due to an unfortunate
* historical event"). I do not thing we need to care though since
* ReplayGain information is only contained in recent files. */
if (strncmp (ubuf, "Xing", 4) && strncmp (ubuf, "Info", 4))
goto lt_fail;
/* Determine the offset of the LAME tag based on contents of the Xing header */
fread (&flags, 4, 1, mp3i->file);
if (flags & FRAMES_FLAG)
{ /* frames field is set */
toskip += 4;
}
if (flags & BYTES_FLAG)
{ /* bytes field is set */
toskip += 4;
}
if (flags & TOC_FLAG)
{ /* TOC field is set */
toskip += 100;
}
if (flags & VBR_SCALE_FLAG)
{ /* quality field is set */
toskip += 4;
}
/* Check for LAME Tag */
if (fseek (mp3i->file, toskip, SEEK_CUR) || (fread (ubuf, 1, LAME_TAG_SIZE, mp3i->file) != LAME_TAG_SIZE))
goto lt_fail;
if (strncmp (ubuf, "LAME", 4))
goto lt_fail;
strncpy(lt->encoder, &ubuf[0x0], 4);
strncpy(lt->version_string, &ubuf[0x4], 5);
lt->info_tag_revision = (ubuf[0x9] >> 4);
lt->vbr_method = (ubuf[0x9] & 0xf);
lt->lowpass = ubuf[0xa];
memcpy(<->peak_signal_amplitude,&ubuf[0xb],4);
memcpy(<->radio_replay_gain,&ubuf[0xf],2);
memcpy(<->audiophile_replay_gain,&ubuf[0x11],2);
lt->encoding_flags = ubuf[0x13] >> 4;
lt->ath_type = ubuf[0x13] & 0xf;
lt->bitrate = ubuf[0x14];
lt->delay = (ubuf[0x15] << 4) + (ubuf[0x16] >> 4);
lt->padding = ((ubuf[0x16] & 0xf) << 8) + ubuf[0x17];
lt->noise_shaping = ubuf[0x18] & 0x3;
lt->stereo_mode = (ubuf[0x18] >> 2) & 0x7;
lt->unwise_settings = (ubuf[0x18] >> 5) & 0x1;
lt->source_sample_frequency = (ubuf[0x18] >> 6) & 0x3;
lt->mp3_gain = ubuf[0x19];
lt->surround_info = (ubuf[0x1a] >> 3) & 0x7;
lt->preset = ((ubuf[0x1a] & 0x7) << 8) + ubuf[0x1b];
lt->music_length = parse_lame_uint32(&ubuf[0x1c]);
lt->music_crc = parse_lame_uint16(&ubuf[0x20]);
lt->info_tag_crc = parse_lame_uint16(&ubuf[0x22]);
lt->calculated_info_tag_crc = crc_compute(full_info_tag, INFO_TAG_CRC_SIZE, 0x0000);
fclose(file);
g_free (mp3i);
return (lt->calculated_info_tag_crc == lt->info_tag_crc);
lt_fail:
if (file)
fclose(file);
g_free (mp3i);
return FALSE;
}
/*
* mp3_get_track_gapless - read the specified file and calculate gapless
* information: totalsamples and gapless_data
*
* @mp3i: MP3Info of the file; should already have run get_mp3_info() on it
* @gd: structure holding gapless information; should have pregap and
* postgap already filled
*/
gboolean mp3_get_track_gapless (MP3Info *mp3i, GaplessData *gd)
{
int i;
int xing_header_offset;
int mysamplesperframe;
int totaldatasize;
int lastframes[8];
int totalframes;
int finaleight;
int l;
g_return_val_if_fail (mp3i, FALSE);
g_return_val_if_fail (gd, FALSE);
/* use get_first_header() to seek to the first mp3 header */
get_first_header (mp3i, 0);
xing_header_offset = ftell (mp3i->file);
get_header(mp3i->file, &(mp3i->header));
mysamplesperframe = samplesperframe[mp3i->header.version & 1][3 - mp3i->header.layer];
/* jump to the end of the frame with the xing header */
if (fseek (mp3i->file, xing_header_offset + frame_length (&(mp3i->header)), SEEK_SET))
goto gp_fail;
/* counts bytes from the start of the 1st sync frame */
totaldatasize = frame_length (&(mp3i->header));
/* lastframes keeps track of the last 8 frame sizes */
/* counts number of music frames */
totalframes = 0;
/* quickly parse the file, reading only frame headers */
l = 0;
while ((l = get_header (mp3i->file, &(mp3i->header))) != 0)
{
lastframes[totalframes%8] = l;
totaldatasize += l;
totalframes++;
if (fseek (mp3i->file, l - FRAME_HEADER_SIZE, SEEK_CUR))
goto gp_fail;
}
finaleight = 0;
for (i = 0; i < 8; i++)
{
finaleight += lastframes[i];
}
/* For some reason, iTunes appears to add an extra frames worth of
* samples to the samplecount for CBR files. CBR files don't currently
* (2 Jul 07) play gaplessly whether uploaded from iTunes or gtkpod,
* even with apparently correct values, but we will attempt to emulate
* iTunes' behavior */
if (mp3i->vbr == 0) // CBR
totalframes++;
/* all but last eight frames */
gd->gapless_data = totaldatasize - finaleight;
/* total samples minus pre/postgap */
gd->samplecount = totalframes * mysamplesperframe - gd->pregap - gd->postgap;
return TRUE;
gp_fail:
return FALSE;
}
/**
* mp3_read_gapless:
*
* try to read the gapless values from the LAME Tag and set
* the track's pregap, postgap, samplecount, and gapless_data fields
* accordingly.
*
* @path: location of the file
* @track: structure holding track information
*
* The function always rereads the data from the file.
*
* Returns TRUE if all four gapless fields could be
* set. etrack->tchanged is set to TRUE if data has been changed,
* FALSE otherwise.
*/
gboolean mp3_read_gapless (const gchar *path, Track *track) {
MP3Info *mp3i=NULL;
FILE *file;
GaplessData gd;
ExtraTrackData *etr;
g_return_val_if_fail (track, FALSE);
etr = track->userdata;
g_return_val_if_fail (etr, FALSE);
memset (&gd, 0, sizeof (GaplessData));
g_return_val_if_fail (path, FALSE);
/* Attempt to open the file */
file = fopen (path, "r");
if (file)
{
mp3i = g_malloc0 (sizeof (MP3Info));
mp3i->filename = path;
mp3i->file = file;
get_mp3_info (mp3i);
/* Try the LAME tag for pregap and postgap */
LameTag lt;
if (mp3_read_lame_tag (path, <)) {
gd.pregap = lt.delay;
gd.postgap = lt.padding;
} else {
/* insert non-LAME methods of finding pregap and postgap */
fclose(file);
g_free (mp3i);
return FALSE;
}
mp3_get_track_gapless (mp3i, &gd);
etr->tchanged = FALSE;
if ((gd.pregap) && (gd.samplecount) && (gd.postgap) && (gd.gapless_data))
{
if ((track->pregap != gd.pregap) ||
(track->samplecount != gd.samplecount) ||
(track->postgap != gd.postgap) ||
(track->gapless_data != gd.gapless_data) ||
(track->gapless_track_flag == FALSE))
{
etr->tchanged = TRUE;
track->pregap = gd.pregap;
track->samplecount = gd.samplecount;
track->postgap = gd.postgap;
track->gapless_data = gd.gapless_data;
track->gapless_track_flag = TRUE;
}
}
else
{ /* remove gapless data which doesn't seem to be valid any
* more */
if (track->gapless_track_flag == TRUE)
{
etr->tchanged = TRUE;
}
track->pregap = 0;
track->samplecount = 0;
track->postgap = 0;
track->gapless_data = 0;
track->gapless_track_flag = FALSE;
}
fclose(file);
g_free (mp3i);
return TRUE;
}
return FALSE;
}
/* Read ID3 tags of filename @name into track structure @track */
/* Return value: TRUE if tags could be read, FALSE if an error
occured */
gboolean id3_read_tags (const gchar *name, Track *track)
{
File_Tag filetag;
g_return_val_if_fail (name && track, FALSE);
if (id3_tag_read (name, &filetag))
{
guchar *image_data = NULL;
gsize image_data_len = 0;
if (filetag.album)
{
track->album = filetag.album;
}
if (filetag.artist)
{
track->artist = filetag.artist;
}
if (filetag.albumartist)
{
track->albumartist = filetag.albumartist;
}
if (filetag.title)
{
track->title = filetag.title;
}
if (filetag.genre)
{
track->genre = filetag.genre;
}
if (filetag.composer)
{
track->composer = filetag.composer;
}
if (filetag.comment)
{
track->comment = filetag.comment;
}
if (filetag.podcasturl)
{
track->podcasturl = filetag.podcasturl;
}
if (filetag.podcastrss)
{
track->podcastrss = filetag.podcastrss;
}
if (filetag.subtitle)
{
track->subtitle = filetag.subtitle;
}
if (filetag.description)
{
track->description = filetag.description;
}
if (filetag.sort_artist)
{
track->sort_artist = filetag.sort_artist;
}
if (filetag.sort_title)
{
track->sort_title = filetag.sort_title;
}
if (filetag.sort_album)
{
track->sort_album = filetag.sort_album;
}
if (filetag.sort_albumartist)
{
track->sort_albumartist = filetag.sort_albumartist;
}
if (filetag.sort_composer)
{
track->sort_composer = filetag.sort_composer;
}
if (filetag.year == NULL)
{
track->year = 0;
}
else
{
track->year = atoi(filetag.year);
g_free (filetag.year);
}
if (filetag.trackstring == NULL)
{
track->track_nr = 0;
}
else
{
track->track_nr = atoi(filetag.trackstring);
g_free (filetag.trackstring);
}
if (filetag.track_total == NULL)
{
track->tracks = 0;
}
else
{
track->tracks = atoi(filetag.track_total);
g_free (filetag.track_total);
}
/* CD/disc number handling */
if (filetag.cdnostring == NULL)
{
track->cd_nr = 0;
}
else
{
track->cd_nr = atoi(filetag.cdnostring);
g_free (filetag.cdnostring);
}
if (filetag.cdno_total == NULL)
{
track->cds = 0;
}
else
{
track->cds = atoi(filetag.cdno_total);
g_free (filetag.cdno_total);
}
if (filetag.compilation == NULL)
{
track->compilation = 0;
}
else
{
track->compilation = atoi(filetag.compilation);
g_free (filetag.compilation);
}
if (filetag.BPM == NULL)
{
track->BPM = 0;
}
else
{
track->BPM = atoi(filetag.BPM);
g_free (filetag.BPM);
}
if (filetag.lyrics)
{
track->lyrics_flag = 0x01;
g_free (filetag.lyrics);
}
else
{
track->lyrics_flag = 0x00;
}
if (prefs_get_int("coverart_apic") &&
(id3_apic_read (name, &image_data, &image_data_len) == TRUE))
{
if (image_data)
{
gp_track_set_thumbnails_from_data (track,
image_data,
image_data_len);
g_free (image_data);
}
}
return TRUE;
}
return FALSE;
}
/* ----------------------------------------------------------------------
From here starts original gtkpod code
---------------------------------------------------------------------- */
/* Return a Track structure with all information read from the mp3
file filled in */
Track *mp3_get_file_info (const gchar *name)
{
Track *track = NULL;
MP3Info *mp3i=NULL;
FILE *file;
g_return_val_if_fail (name, NULL);
/* Attempt to open the file */
file = fopen (name, "r");
if (file)
{
mp3i = g_malloc0 (sizeof (MP3Info));
mp3i->filename = name;
mp3i->file = file;
get_mp3_info (mp3i);
mp3i->file = NULL;
fclose (file);
}
else
{
gchar *fbuf = charset_to_utf8 (name);
gtkpod_warning(_("ERROR while opening file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return NULL;
}
track = gp_track_new ();
track->filetype = g_strdup ("MPEG audio file");
if (prefs_get_int("readtags"))
{
id3_read_tags (name, track);
}
mp3_read_soundcheck (name, track);
mp3_read_gapless (name, track);
/* Get additional info (play time and bitrate */
if (mp3i)
{
track->tracklen = mp3i->milliseconds;
track->bitrate = (gint)(mp3i->vbr_average);
track->samplerate = mp3file_header_frequency (&mp3i->header);
g_free (mp3i);
}
/* Fall back to xmms code if tracklen is 0 */
if (track->tracklen == 0)
{
track->tracklen = get_track_time (name);
if (track->tracklen)
track->bitrate = (float)track->size*8/track->tracklen;
}
if (track->tracklen == 0)
{
/* Tracks with zero play length are ignored by iPod... */
gtkpod_warning (_("File \"%s\" has zero play length. Ignoring.\n"),
name);
gp_track_free (track);
track = NULL;
}
return track;
}
/*
*
* @returns: TRUE on success, else FALSE.
*/
gboolean id3_lyrics_read (const gchar *filename,gchar **lyrics)
{
struct id3_file *id3file;
struct id3_tag *id3tag;
g_return_val_if_fail (filename, FALSE);
g_return_val_if_fail (lyrics, FALSE);
if (!(id3file = id3_file_open (filename, ID3_FILE_MODE_READONLY)))
{
gchar *fbuf = charset_to_utf8 (filename);
g_print(_("ERROR while opening file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return FALSE;
}
if ((id3tag = id3_file_tag(id3file)))
{
*lyrics = id3_get_string (id3tag, "USLT");
}
id3_file_close (id3file);
return TRUE;
}
gboolean id3_lyrics_save (const gchar *filename,const gchar *lyrics)
{
struct id3_file *id3file;
struct id3_tag *id3tag;
g_return_val_if_fail (filename, FALSE);
id3file = id3_file_open (filename, ID3_FILE_MODE_READWRITE);
if (!id3file)
{
gchar *fbuf = charset_to_utf8 (filename);
g_print(_("ERROR while opening file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return FALSE;
}
if ((id3tag = id3_file_tag(id3file)))
{
enum id3_field_textencoding encoding;
/* actually the iPod only understands UTF8 lyrics, but for
consistency's sake we'll do the same as for the other tags
*/
/* use the same coding as before... */
encoding = get_encoding (id3tag);
/* ...unless it's ISO_8859_1 and prefs say we should use
unicode (i.e. ID3v2.4) */
if (prefs_get_int("id3_write_id3v24") &&
(encoding == ID3_FIELD_TEXTENCODING_ISO_8859_1))
encoding = ID3_FIELD_TEXTENCODING_UTF_8;
/* always render id3v1 to prevent dj studio from crashing */
id3_tag_options(id3tag, ID3_TAG_OPTION_ID3V1, ~0);
/* turn off frame compression and crc information to let
itunes read tags see
http://www.mars.org/mailman/public/mad-dev/2002-October/000742.html
*/
id3_tag_options(id3tag, ID3_TAG_OPTION_COMPRESSION, 0);
id3_tag_options(id3tag, ID3_TAG_OPTION_CRC, 0);
id3_set_string (id3tag, "USLT", lyrics, encoding);
}
if (id3_file_update(id3file) != 0)
{
gchar *fbuf = charset_to_utf8 (filename);
g_print(_("ERROR while writing tag to file: '%s' (%s).\n"),
fbuf, g_strerror(errno));
g_free (fbuf);
return FALSE;
}
id3_file_close (id3file);
return TRUE;
}
|