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
|
/*
* libopenmpt_c.cpp
* ----------------
* Purpose: libopenmpt C interface implementation
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "BuildSettings.h"
#include "libopenmpt_internal.h"
#include "libopenmpt.h"
#include "libopenmpt_ext.h"
#include "libopenmpt_impl.hpp"
#include "libopenmpt_ext_impl.hpp"
#include <limits>
#include <new>
#include <stdexcept>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#if defined(_MSC_VER)
#pragma warning(disable:4702) /* unreachable code */
#endif
#ifndef NO_LIBOPENMPT_C
namespace openmpt {
static const char * strdup( const char * src ) {
char * dst = (char*)std::calloc( std::strlen( src ) + 1, sizeof( char ) );
if ( !dst ) {
return NULL;
}
std::strcpy( dst, src );
return dst;
}
class logfunc_logger : public log_interface {
private:
openmpt_log_func m_logfunc;
void * m_user;
public:
logfunc_logger( openmpt_log_func func, void * user ) : m_logfunc(func), m_user(user) {
return;
}
void log( const std::string & message ) const override {
if ( m_logfunc ) {
m_logfunc( message.c_str(), m_user );
} else {
openmpt_log_func_default( message.c_str(), m_user );
}
}
}; // class logfunc_logger
namespace interface {
class invalid_module_pointer : public openmpt::exception {
public:
invalid_module_pointer()
: openmpt::exception("module * not valid")
{
return;
}
invalid_module_pointer(const invalid_module_pointer&) = default;
virtual ~invalid_module_pointer() noexcept = default;
};
class argument_null_pointer : public openmpt::exception {
public:
argument_null_pointer()
: openmpt::exception("argument null pointer")
{
return;
}
argument_null_pointer(const argument_null_pointer&) = default;
virtual ~argument_null_pointer() noexcept = default;
};
} // namespace interface
static std::string format_exception( const char * const function ) {
std::string err;
try {
throw;
} catch ( const openmpt::exception & e ) {
err += function;
err += ": ";
err += "ERROR: ";
const char * what = e.what();
err += what ? what : "";
} catch ( const std::bad_alloc & e ) {
err += function;
err += ": ";
err += "OUT OF MEMORY: ";
const char * what = e.what();
err += what ? what : "";
} catch ( const std::exception & e ) {
err += function;
err += ": ";
err += "INTERNAL ERROR: ";
const char * what = e.what();
err += what ? what : "";
} catch ( ... ) {
err += function;
err += ": ";
err += "UNKNOWN INTERNAL ERROR";
}
return err;
}
static void error_message_from_exception( const char * * error_message, const std::exception & e ) {
if ( error_message ) {
const char * what = e.what();
*error_message = ( what ? openmpt::strdup( what ) : openmpt::strdup( "" ) );
}
}
static int error_from_exception( const char * * error_message ) {
int error = 0;
if ( error_message ) {
if ( *error_message ) {
openmpt_free_string( *error_message );
*error_message = NULL;
}
}
try {
throw;
} catch ( const std::bad_alloc & e ) {
error = OPENMPT_ERROR_OUT_OF_MEMORY;
error_message_from_exception( error_message, e );
} catch ( const openmpt::interface::invalid_module_pointer & e ) {
error = OPENMPT_ERROR_INVALID_MODULE_POINTER;
error_message_from_exception( error_message, e );
} catch ( const openmpt::interface::argument_null_pointer & e ) {
error = OPENMPT_ERROR_ARGUMENT_NULL_POINTER;
error_message_from_exception( error_message, e );
} catch ( const openmpt::exception & e ) {
error = OPENMPT_ERROR_GENERAL;
error_message_from_exception( error_message, e );
} catch ( const std::invalid_argument & e ) {
error = OPENMPT_ERROR_INVALID_ARGUMENT;
error_message_from_exception( error_message, e );
} catch ( const std::out_of_range & e ) {
error = OPENMPT_ERROR_OUT_OF_RANGE;
error_message_from_exception( error_message, e );
} catch ( const std::length_error & e ) {
error = OPENMPT_ERROR_LENGTH;
error_message_from_exception( error_message, e );
} catch ( const std::domain_error & e ) {
error = OPENMPT_ERROR_DOMAIN;
error_message_from_exception( error_message, e );
} catch ( const std::logic_error & e ) {
error = OPENMPT_ERROR_LOGIC;
error_message_from_exception( error_message, e );
} catch ( const std::underflow_error & e ) {
error = OPENMPT_ERROR_UNDERFLOW;
error_message_from_exception( error_message, e );
} catch ( const std::overflow_error & e ) {
error = OPENMPT_ERROR_OVERFLOW;
error_message_from_exception( error_message, e );
} catch ( const std::range_error & e ) {
error = OPENMPT_ERROR_RANGE;
error_message_from_exception( error_message, e );
} catch ( const std::runtime_error & e ) {
error = OPENMPT_ERROR_RUNTIME;
error_message_from_exception( error_message, e );
} catch ( const std::exception & e ) {
error = OPENMPT_ERROR_EXCEPTION;
error_message_from_exception( error_message, e );
} catch ( ... ) {
error = OPENMPT_ERROR_UNKNOWN;
}
return error;
}
} // namespace openmpt
extern "C" {
struct openmpt_module {
openmpt_log_func logfunc;
void * loguser;
openmpt_error_func errfunc;
void * erruser;
int error;
const char * error_message;
openmpt::module_impl * impl;
};
struct openmpt_module_ext {
openmpt_module mod;
openmpt::module_ext_impl * impl;
};
} // extern "C"
namespace openmpt {
static void do_report_exception( const char * const function, openmpt_log_func const logfunc = 0, void * const loguser = 0, openmpt_error_func errfunc = 0, void * const erruser = 0, openmpt::module_impl * const impl = 0, openmpt_module * const mod = 0, int * const err = 0, const char * * err_msg = 0 ) {
int error = OPENMPT_ERROR_OK;
const char * error_message = NULL;
int error_func_result = OPENMPT_ERROR_FUNC_RESULT_DEFAULT;
if ( errfunc || mod || err || err_msg ) {
error = error_from_exception( mod ? &error_message : NULL );
}
if ( errfunc ) {
error_func_result = errfunc( error, erruser );
}
if ( mod && ( error_func_result & OPENMPT_ERROR_FUNC_RESULT_STORE ) ) {
mod->error = error;
mod->error_message = ( error_message ? openmpt::strdup( error_message ) : openmpt::strdup( "" ) );
}
if ( err ) {
*err = error;
}
if ( err_msg ) {
*err_msg = ( error_message ? openmpt::strdup( error_message ) : openmpt::strdup( "" ) );
}
if ( error_message ) {
openmpt_free_string( error_message );
error_message = NULL;
}
if ( error_func_result & OPENMPT_ERROR_FUNC_RESULT_LOG ) {
try {
const std::string message = format_exception( function );
if ( impl ) {
impl->PushToCSoundFileLog( message );
} else if ( logfunc ) {
logfunc( message.c_str(), loguser );
} else {
openmpt_log_func_default( message.c_str(), NULL );
}
} catch ( ... ) {
fprintf( stderr, "openmpt: %s:%i: UNKNOWN INTERNAL ERROR in error handling: function='%s', logfunc=%p, loguser=%p, errfunc=%p, erruser=%p, impl=%p\n", __FILE__, static_cast<int>( __LINE__ ), function ? function : "", reinterpret_cast<void*>( logfunc ), loguser, reinterpret_cast<void*>( errfunc ), erruser, static_cast<void*>( impl ) );
fflush( stderr );
}
}
}
static void report_exception( const char * const function, openmpt_module * mod = 0, int * error = 0, const char * * error_message = 0 ) {
do_report_exception( function, mod ? mod->logfunc : NULL, mod ? mod->loguser : NULL, mod ? mod->errfunc : NULL, mod ? mod->erruser : NULL, mod ? mod->impl : 0, mod ? mod : NULL, error ? error : NULL, error_message ? error_message : NULL );
}
static void report_exception( const char * const function, openmpt_log_func const logfunc, void * const loguser, openmpt_error_func errfunc, void * const erruser, int * error, const char * * error_message ) {
do_report_exception( function, logfunc, loguser, errfunc, erruser, 0, 0, error, error_message );
}
namespace interface {
template < typename T >
void check_soundfile( T * mod ) {
if ( !mod ) {
throw openmpt::interface::invalid_module_pointer();
}
}
template < typename T >
void check_pointer( T * p ) {
if ( !p ) {
throw openmpt::interface::argument_null_pointer();
}
}
} // namespace interface
} // namespace openmpt
extern "C" {
uint32_t openmpt_get_library_version(void) {
try {
return openmpt::get_library_version();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__ );
}
return 0;
}
uint32_t openmpt_get_core_version(void) {
try {
return openmpt::get_core_version();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__ );
}
return 0;
}
void openmpt_free_string( const char * str ) {
try {
std::free( const_cast< char * >( str ) );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__ );
}
return;
}
const char * openmpt_get_string( const char * key ) {
try {
if ( !key ) {
return openmpt::strdup( "" );
}
return openmpt::strdup( openmpt::string::get( key ).c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__ );
}
return NULL;
}
const char * openmpt_get_supported_extensions(void) {
try {
std::string retval;
bool first = true;
std::vector<std::string> supported_extensions = openmpt::module_impl::get_supported_extensions();
for ( std::vector<std::string>::iterator i = supported_extensions.begin(); i != supported_extensions.end(); ++i ) {
if ( first ) {
first = false;
} else {
retval += ";";
}
retval += *i;
}
return openmpt::strdup( retval.c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__ );
}
return NULL;
}
int openmpt_is_extension_supported( const char * extension ) {
try {
if ( !extension ) {
return 0;
}
return openmpt::module_impl::is_extension_supported( extension ) ? 1 : 0;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__ );
}
return 0;
}
void openmpt_log_func_default( const char * message, void * /*user*/ ) {
fprintf( stderr, "openmpt: %s\n", message );
fflush( stderr );
}
void openmpt_log_func_silent( const char * /*message*/ , void * /*user*/ ) {
return;
}
int openmpt_error_is_transient( int error ) {
int result = 0;
switch ( error ) {
case OPENMPT_ERROR_OUT_OF_MEMORY:
result = 1;
break;
default:
result = 0;
break;
}
return result;
}
const char * openmpt_error_string( int error ) {
const char * text = "unknown error";
switch ( error ) {
case OPENMPT_ERROR_OK:
text = "";
break;
case OPENMPT_ERROR_UNKNOWN:
text = "unknown internal error";
break;
case OPENMPT_ERROR_EXCEPTION:
text = "unknown exception ";
break;
case OPENMPT_ERROR_OUT_OF_MEMORY:
text = "out of memory";
break;
case OPENMPT_ERROR_RUNTIME:
text = "runtime error";
break;
case OPENMPT_ERROR_RANGE:
text = "range error";
break;
case OPENMPT_ERROR_OVERFLOW:
text = "arithmetic overflow";
break;
case OPENMPT_ERROR_UNDERFLOW:
text = "arithmetic underflow";
break;
case OPENMPT_ERROR_LOGIC:
text = "logic error";
break;
case OPENMPT_ERROR_DOMAIN:
text = "value domain error";
break;
case OPENMPT_ERROR_LENGTH:
text = "maximum supported size exceeded";
break;
case OPENMPT_ERROR_OUT_OF_RANGE:
text = "argument out of range";
break;
case OPENMPT_ERROR_INVALID_ARGUMENT:
text = "invalid argument";
break;
case OPENMPT_ERROR_GENERAL:
text = "libopenmpt error";
break;
}
return openmpt::strdup( text );
}
int openmpt_error_func_default( int error, void * /* user */ ) {
(void)error;
return OPENMPT_ERROR_FUNC_RESULT_DEFAULT;
}
int openmpt_error_func_log( int error, void * /* user */ ) {
(void)error;
return OPENMPT_ERROR_FUNC_RESULT_LOG;
}
int openmpt_error_func_store( int error, void * /* user */ ) {
(void)error;
return OPENMPT_ERROR_FUNC_RESULT_STORE;
}
int openmpt_error_func_ignore( int error, void * /* user */ ) {
(void)error;
return OPENMPT_ERROR_FUNC_RESULT_NONE;
}
int openmpt_error_func_errno( int error, void * user ) {
int * e = (int *)user;
if ( !e ) {
return OPENMPT_ERROR_FUNC_RESULT_DEFAULT;
}
*e = error;
return OPENMPT_ERROR_FUNC_RESULT_NONE;
}
void * openmpt_error_func_errno_userdata( int * error ) {
return (void *)error;
}
double openmpt_could_open_probability( openmpt_stream_callbacks stream_callbacks, void * stream, double effort, openmpt_log_func logfunc, void * loguser ) {
return openmpt_could_open_probability2( stream_callbacks, stream, effort, logfunc, loguser, NULL, NULL, NULL, NULL );
}
double openmpt_could_open_propability( openmpt_stream_callbacks stream_callbacks, void * stream, double effort, openmpt_log_func logfunc, void * loguser ) {
return openmpt_could_open_probability2( stream_callbacks, stream, effort, logfunc, loguser, NULL, NULL, NULL, NULL );
}
double openmpt_could_open_probability2( openmpt_stream_callbacks stream_callbacks, void * stream, double effort, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message ) {
try {
openmpt::callback_stream_wrapper istream = { stream, stream_callbacks.read, stream_callbacks.seek, stream_callbacks.tell };
return openmpt::module_impl::could_open_probability( istream, effort, openmpt::helper::make_unique<openmpt::logfunc_logger>( logfunc ? logfunc : openmpt_log_func_default, loguser ) );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, logfunc, loguser, errfunc, erruser, error, error_message );
}
return 0.0;
}
size_t openmpt_probe_file_header_get_recommended_size(void) {
try {
return openmpt::module_impl::probe_file_header_get_recommended_size();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__ );
}
return 0;
}
int openmpt_probe_file_header( uint64_t flags, const void * data, size_t size, uint64_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message ) {
try {
return openmpt::module_impl::probe_file_header( flags, data, size, filesize );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, logfunc, loguser, errfunc, erruser, error, error_message );
}
return OPENMPT_PROBE_FILE_HEADER_RESULT_ERROR;
}
int openmpt_probe_file_header_without_filesize( uint64_t flags, const void * data, size_t size, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message ) {
try {
return openmpt::module_impl::probe_file_header( flags, data, size );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, logfunc, loguser, errfunc, erruser, error, error_message );
}
return OPENMPT_PROBE_FILE_HEADER_RESULT_ERROR;
}
int openmpt_probe_file_header_from_stream( uint64_t flags, openmpt_stream_callbacks stream_callbacks, void * stream, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message ) {
try {
openmpt::callback_stream_wrapper istream = { stream, stream_callbacks.read, stream_callbacks.seek, stream_callbacks.tell };
return openmpt::module_impl::probe_file_header( flags, istream );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, logfunc, loguser, errfunc, erruser, error, error_message );
}
return OPENMPT_PROBE_FILE_HEADER_RESULT_ERROR;
}
openmpt_module * openmpt_module_create( openmpt_stream_callbacks stream_callbacks, void * stream, openmpt_log_func logfunc, void * user, const openmpt_module_initial_ctl * ctls ) {
return openmpt_module_create2( stream_callbacks, stream, logfunc, user, NULL, NULL, NULL, NULL, ctls );
}
openmpt_module * openmpt_module_create2( openmpt_stream_callbacks stream_callbacks, void * stream, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls ) {
try {
openmpt_module * mod = (openmpt_module*)std::calloc( 1, sizeof( openmpt_module ) );
if ( !mod ) {
throw std::bad_alloc();
}
std::memset( mod, 0, sizeof( openmpt_module ) );
mod->logfunc = logfunc ? logfunc : openmpt_log_func_default;
mod->loguser = loguser;
mod->errfunc = errfunc ? errfunc : NULL;
mod->erruser = erruser;
mod->error = OPENMPT_ERROR_OK;
mod->error_message = NULL;
mod->impl = 0;
try {
std::map< std::string, std::string > ctls_map;
if ( ctls ) {
for ( const openmpt_module_initial_ctl * it = ctls; it->ctl; ++it ) {
if ( it->value ) {
ctls_map[ it->ctl ] = it->value;
} else {
ctls_map.erase( it->ctl );
}
}
}
openmpt::callback_stream_wrapper istream = { stream, stream_callbacks.read, stream_callbacks.seek, stream_callbacks.tell };
mod->impl = new openmpt::module_impl( istream, openmpt::helper::make_unique<openmpt::logfunc_logger>( mod->logfunc, mod->loguser ), ctls_map );
return mod;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod, error, error_message );
}
delete mod->impl;
mod->impl = 0;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
std::free( (void*)mod );
mod = NULL;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, 0, error, error_message );
}
return NULL;
}
openmpt_module * openmpt_module_create_from_memory( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * user, const openmpt_module_initial_ctl * ctls ) {
return openmpt_module_create_from_memory2( filedata, filesize, logfunc, user, NULL, NULL, NULL, NULL, ctls );
}
openmpt_module * openmpt_module_create_from_memory2( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls ) {
try {
openmpt_module * mod = (openmpt_module*)std::calloc( 1, sizeof( openmpt_module ) );
if ( !mod ) {
throw std::bad_alloc();
}
std::memset( mod, 0, sizeof( openmpt_module ) );
mod->logfunc = logfunc ? logfunc : openmpt_log_func_default;
mod->loguser = loguser;
mod->errfunc = errfunc ? errfunc : NULL;
mod->erruser = erruser;
mod->error = OPENMPT_ERROR_OK;
mod->error_message = NULL;
mod->impl = 0;
try {
std::map< std::string, std::string > ctls_map;
if ( ctls ) {
for ( const openmpt_module_initial_ctl * it = ctls; it->ctl; ++it ) {
if ( it->value ) {
ctls_map[ it->ctl ] = it->value;
} else {
ctls_map.erase( it->ctl );
}
}
}
mod->impl = new openmpt::module_impl( filedata, filesize, openmpt::helper::make_unique<openmpt::logfunc_logger>( mod->logfunc, mod->loguser ), ctls_map );
return mod;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod, error, error_message );
}
delete mod->impl;
mod->impl = 0;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
std::free( (void*)mod );
mod = NULL;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, 0, error, error_message );
}
return NULL;
}
void openmpt_module_destroy( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
delete mod->impl;
mod->impl = 0;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
std::free( (void*)mod );
mod = NULL;
return;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return;
}
void openmpt_module_set_log_func( openmpt_module * mod, openmpt_log_func logfunc, void * loguser ) {
try {
openmpt::interface::check_soundfile( mod );
mod->logfunc = logfunc ? logfunc : openmpt_log_func_default;
mod->loguser = loguser;
return;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return;
}
void openmpt_module_set_error_func( openmpt_module * mod, openmpt_error_func errfunc, void * erruser ) {
try {
openmpt::interface::check_soundfile( mod );
mod->errfunc = errfunc ? errfunc : NULL;
mod->erruser = erruser;
mod->error = OPENMPT_ERROR_OK;
return;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return;
}
int openmpt_module_error_get_last( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->error;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return -1;
}
const char * openmpt_module_error_get_last_message( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->error_message ? openmpt::strdup( mod->error_message ) : openmpt::strdup( "" );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
void openmpt_module_error_set_last( openmpt_module * mod, int error ) {
try {
openmpt::interface::check_soundfile( mod );
mod->error = error;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
return;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return;
}
void openmpt_module_error_clear( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
mod->error = OPENMPT_ERROR_OK;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
return;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return;
}
int openmpt_module_select_subsong( openmpt_module * mod, int32_t subsong ) {
try {
openmpt::interface::check_soundfile( mod );
mod->impl->select_subsong( subsong );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
int32_t openmpt_module_get_selected_subsong( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_selected_subsong();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return -1;
}
int openmpt_module_set_repeat_count( openmpt_module * mod, int32_t repeat_count ) {
try {
openmpt::interface::check_soundfile( mod );
mod->impl->set_repeat_count( repeat_count );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
int32_t openmpt_module_get_repeat_count( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_repeat_count();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
double openmpt_module_get_duration_seconds( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_duration_seconds();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
double openmpt_module_set_position_seconds( openmpt_module * mod, double seconds ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->set_position_seconds( seconds );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
double openmpt_module_get_position_seconds( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_position_seconds();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
double openmpt_module_set_position_order_row( openmpt_module * mod, int32_t order, int32_t row ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->set_position_order_row( order, row );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
int openmpt_module_get_render_param( openmpt_module * mod, int param, int32_t * value ) {
try {
openmpt::interface::check_soundfile( mod );
openmpt::interface::check_pointer( value );
*value = mod->impl->get_render_param( (openmpt::module::render_param)param );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
int openmpt_module_set_render_param( openmpt_module * mod, int param, int32_t value ) {
try {
openmpt::interface::check_soundfile( mod );
mod->impl->set_render_param( (openmpt::module::render_param)param, value );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_mono( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * mono ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read( samplerate, count, mono );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_stereo( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * left, int16_t * right ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read( samplerate, count, left, right );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_quad( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * left, int16_t * right, int16_t * rear_left, int16_t * rear_right ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read( samplerate, count, left, right, rear_left, rear_right );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_float_mono( openmpt_module * mod, int32_t samplerate, size_t count, float * mono ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read( samplerate, count, mono );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_float_stereo( openmpt_module * mod, int32_t samplerate, size_t count, float * left, float * right ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read( samplerate, count, left, right );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_float_quad( openmpt_module * mod, int32_t samplerate, size_t count, float * left, float * right, float * rear_left, float * rear_right ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read( samplerate, count, left, right, rear_left, rear_right );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_interleaved_stereo( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_stereo ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read_interleaved_stereo( samplerate, count, interleaved_stereo );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_interleaved_quad( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_quad ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read_interleaved_quad( samplerate, count, interleaved_quad );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_interleaved_float_stereo( openmpt_module * mod, int32_t samplerate, size_t count, float * interleaved_stereo ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read_interleaved_stereo( samplerate, count, interleaved_stereo );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
size_t openmpt_module_read_interleaved_float_quad( openmpt_module * mod, int32_t samplerate, size_t count, float * interleaved_quad ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->read_interleaved_quad( samplerate, count, interleaved_quad );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
const char * openmpt_module_get_metadata_keys( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
std::string retval;
bool first = true;
std::vector<std::string> metadata_keys = mod->impl->get_metadata_keys();
for ( std::vector<std::string>::iterator i = metadata_keys.begin(); i != metadata_keys.end(); ++i ) {
if ( first ) {
first = false;
} else {
retval += ";";
}
retval += *i;
}
return openmpt::strdup( retval.c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
const char * openmpt_module_get_metadata( openmpt_module * mod, const char * key ) {
try {
openmpt::interface::check_soundfile( mod );
openmpt::interface::check_pointer( key );
return openmpt::strdup( mod->impl->get_metadata( key ).c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
LIBOPENMPT_API int32_t openmpt_module_get_current_speed( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_speed();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_current_tempo( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_tempo();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_current_order( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_order();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_current_pattern( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_pattern();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_current_row( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_row();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_current_playing_channels( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_playing_channels();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API float openmpt_module_get_current_channel_vu_mono( openmpt_module * mod, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_channel_vu_mono( channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
LIBOPENMPT_API float openmpt_module_get_current_channel_vu_left( openmpt_module * mod, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_channel_vu_left( channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
LIBOPENMPT_API float openmpt_module_get_current_channel_vu_right( openmpt_module * mod, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_channel_vu_right( channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
LIBOPENMPT_API float openmpt_module_get_current_channel_vu_rear_left( openmpt_module * mod, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_channel_vu_rear_left( channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
LIBOPENMPT_API float openmpt_module_get_current_channel_vu_rear_right( openmpt_module * mod, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_current_channel_vu_rear_right( channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0.0;
}
LIBOPENMPT_API int32_t openmpt_module_get_num_subsongs( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_num_subsongs();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_num_channels( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_num_channels();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_num_orders( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_num_orders();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_num_patterns( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_num_patterns();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_num_instruments( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_num_instruments();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_num_samples( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_num_samples();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API const char * openmpt_module_get_subsong_name( openmpt_module * mod, int32_t index ) {
try {
openmpt::interface::check_soundfile( mod );
std::vector<std::string> names = mod->impl->get_subsong_names();
if ( names.size() >= (std::size_t)std::numeric_limits<int32_t>::max() ) {
throw std::runtime_error("too many names");
}
if ( index < 0 || index >= (int32_t)names.size() ) {
return openmpt::strdup( "" );
}
return openmpt::strdup( names[index].c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
LIBOPENMPT_API const char * openmpt_module_get_channel_name( openmpt_module * mod, int32_t index ) {
try {
openmpt::interface::check_soundfile( mod );
std::vector<std::string> names = mod->impl->get_channel_names();
if ( names.size() >= (std::size_t)std::numeric_limits<int32_t>::max() ) {
throw std::runtime_error("too many names");
}
if ( index < 0 || index >= (int32_t)names.size() ) {
return openmpt::strdup( "" );
}
return openmpt::strdup( names[index].c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
LIBOPENMPT_API const char * openmpt_module_get_order_name( openmpt_module * mod, int32_t index ) {
try {
openmpt::interface::check_soundfile( mod );
std::vector<std::string> names = mod->impl->get_order_names();
if ( names.size() >= (std::size_t)std::numeric_limits<int32_t>::max() ) {
throw std::runtime_error("too many names");
}
if ( index < 0 || index >= (int32_t)names.size() ) {
return openmpt::strdup( "" );
}
return openmpt::strdup( names[index].c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
LIBOPENMPT_API const char * openmpt_module_get_pattern_name( openmpt_module * mod, int32_t index ) {
try {
openmpt::interface::check_soundfile( mod );
std::vector<std::string> names = mod->impl->get_pattern_names();
if ( names.size() >= (std::size_t)std::numeric_limits<int32_t>::max() ) {
throw std::runtime_error("too many names");
}
if ( index < 0 || index >= (int32_t)names.size() ) {
return openmpt::strdup( "" );
}
return openmpt::strdup( names[index].c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
LIBOPENMPT_API const char * openmpt_module_get_instrument_name( openmpt_module * mod, int32_t index ) {
try {
openmpt::interface::check_soundfile( mod );
std::vector<std::string> names = mod->impl->get_instrument_names();
if ( names.size() >= (std::size_t)std::numeric_limits<int32_t>::max() ) {
throw std::runtime_error("too many names");
}
if ( index < 0 || index >= (int32_t)names.size() ) {
return openmpt::strdup( "" );
}
return openmpt::strdup( names[index].c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
LIBOPENMPT_API const char * openmpt_module_get_sample_name( openmpt_module * mod, int32_t index ) {
try {
openmpt::interface::check_soundfile( mod );
std::vector<std::string> names = mod->impl->get_sample_names();
if ( names.size() >= (std::size_t)std::numeric_limits<int32_t>::max() ) {
throw std::runtime_error("too many names");
}
if ( index < 0 || index >= (int32_t)names.size() ) {
return openmpt::strdup( "" );
}
return openmpt::strdup( names[index].c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
LIBOPENMPT_API int32_t openmpt_module_get_order_pattern( openmpt_module * mod, int32_t order ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_order_pattern( order );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API int32_t openmpt_module_get_pattern_num_rows( openmpt_module * mod, int32_t pattern ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_pattern_num_rows( pattern );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API uint8_t openmpt_module_get_pattern_row_channel_command( openmpt_module * mod, int32_t pattern, int32_t row, int32_t channel, int command ) {
try {
openmpt::interface::check_soundfile( mod );
return mod->impl->get_pattern_row_channel_command( pattern, row, channel, command );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API const char * openmpt_module_format_pattern_row_channel_command( openmpt_module * mod, int32_t pattern, int32_t row, int32_t channel, int command ) {
try {
openmpt::interface::check_soundfile( mod );
return openmpt::strdup( mod->impl->format_pattern_row_channel_command( pattern, row, channel, command ).c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API const char * openmpt_module_highlight_pattern_row_channel_command( openmpt_module * mod, int32_t pattern, int32_t row, int32_t channel, int command ) {
try {
openmpt::interface::check_soundfile( mod );
return openmpt::strdup( mod->impl->highlight_pattern_row_channel_command( pattern, row, channel, command ).c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API const char * openmpt_module_format_pattern_row_channel( openmpt_module * mod, int32_t pattern, int32_t row, int32_t channel, size_t width, int pad ) {
try {
openmpt::interface::check_soundfile( mod );
return openmpt::strdup( mod->impl->format_pattern_row_channel( pattern, row, channel, width, pad ? true : false ).c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
LIBOPENMPT_API const char * openmpt_module_highlight_pattern_row_channel( openmpt_module * mod, int32_t pattern, int32_t row, int32_t channel, size_t width, int pad ) {
try {
openmpt::interface::check_soundfile( mod );
return openmpt::strdup( mod->impl->highlight_pattern_row_channel( pattern, row, channel, width, pad ? true : false ).c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
const char * openmpt_module_get_ctls( openmpt_module * mod ) {
try {
openmpt::interface::check_soundfile( mod );
std::string retval;
bool first = true;
std::vector<std::string> ctls = mod->impl->get_ctls();
for ( std::vector<std::string>::iterator i = ctls.begin(); i != ctls.end(); ++i ) {
if ( first ) {
first = false;
} else {
retval += ";";
}
retval += *i;
}
return openmpt::strdup( retval.c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
const char * openmpt_module_ctl_get( openmpt_module * mod, const char * ctl ) {
try {
openmpt::interface::check_soundfile( mod );
openmpt::interface::check_pointer( ctl );
return openmpt::strdup( mod->impl->ctl_get( ctl ).c_str() );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return NULL;
}
int openmpt_module_ctl_set( openmpt_module * mod, const char * ctl, const char * value ) {
try {
openmpt::interface::check_soundfile( mod );
openmpt::interface::check_pointer( ctl );
openmpt::interface::check_pointer( value );
mod->impl->ctl_set( ctl, value );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod );
}
return 0;
}
openmpt_module_ext * openmpt_module_ext_create( openmpt_stream_callbacks stream_callbacks, void * stream, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls ) {
try {
openmpt_module_ext * mod_ext = (openmpt_module_ext*)std::calloc( 1, sizeof( openmpt_module_ext ) );
if ( !mod_ext ) {
throw std::bad_alloc();
}
std::memset( mod_ext, 0, sizeof( openmpt_module_ext ) );
openmpt_module * mod = &mod_ext->mod;
std::memset( mod, 0, sizeof( openmpt_module ) );
mod_ext->impl = 0;
mod->logfunc = logfunc ? logfunc : openmpt_log_func_default;
mod->loguser = loguser;
mod->errfunc = errfunc ? errfunc : NULL;
mod->erruser = erruser;
mod->error = OPENMPT_ERROR_OK;
mod->error_message = NULL;
mod->impl = 0;
try {
std::map< std::string, std::string > ctls_map;
if ( ctls ) {
for ( const openmpt_module_initial_ctl * it = ctls; it->ctl; ++it ) {
if ( it->value ) {
ctls_map[ it->ctl ] = it->value;
} else {
ctls_map.erase( it->ctl );
}
}
}
openmpt::callback_stream_wrapper istream = { stream, stream_callbacks.read, stream_callbacks.seek, stream_callbacks.tell };
mod_ext->impl = new openmpt::module_ext_impl( istream, openmpt::helper::make_unique<openmpt::logfunc_logger>( mod->logfunc, mod->loguser ), ctls_map );
mod->impl = mod_ext->impl;
return mod_ext;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod, error, error_message );
}
delete mod_ext->impl;
mod_ext->impl = 0;
mod->impl = 0;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
std::free( (void*)mod_ext );
mod_ext = NULL;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, 0, error, error_message );
}
return NULL;
}
openmpt_module_ext * openmpt_module_ext_create_from_memory( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls ) {
try {
openmpt_module_ext * mod_ext = (openmpt_module_ext*)std::calloc( 1, sizeof( openmpt_module_ext ) );
if ( !mod_ext ) {
throw std::bad_alloc();
}
std::memset( mod_ext, 0, sizeof( openmpt_module_ext ) );
openmpt_module * mod = &mod_ext->mod;
std::memset( mod, 0, sizeof( openmpt_module ) );
mod_ext->impl = 0;
mod->logfunc = logfunc ? logfunc : openmpt_log_func_default;
mod->loguser = loguser;
mod->errfunc = errfunc ? errfunc : NULL;
mod->erruser = erruser;
mod->error = OPENMPT_ERROR_OK;
mod->error_message = NULL;
mod->impl = 0;
try {
std::map< std::string, std::string > ctls_map;
if ( ctls ) {
for ( const openmpt_module_initial_ctl * it = ctls; it->ctl; ++it ) {
if ( it->value ) {
ctls_map[ it->ctl ] = it->value;
} else {
ctls_map.erase( it->ctl );
}
}
}
mod_ext->impl = new openmpt::module_ext_impl( filedata, filesize, openmpt::helper::make_unique<openmpt::logfunc_logger>( mod->logfunc, mod->loguser ), ctls_map );
mod->impl = mod_ext->impl;
return mod_ext;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod, error, error_message );
}
delete mod_ext->impl;
mod_ext->impl = 0;
mod->impl = 0;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
std::free( (void*)mod_ext );
mod_ext = NULL;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, 0, error, error_message );
}
return NULL;
}
void openmpt_module_ext_destroy( openmpt_module_ext * mod_ext ) {
try {
openmpt::interface::check_soundfile( mod_ext );
openmpt_module * mod = &mod_ext->mod;
mod->impl = 0;
delete mod_ext->impl;
mod_ext->impl = 0;
if ( mod->error_message ) {
openmpt_free_string( mod->error_message );
mod->error_message = NULL;
}
std::free( (void*)mod_ext );
mod_ext = NULL;
return;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return;
}
openmpt_module * openmpt_module_ext_get_module( openmpt_module_ext * mod_ext ) {
try {
openmpt::interface::check_soundfile( mod_ext );
openmpt_module * mod = &mod_ext->mod;
return mod;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return NULL;
}
static int get_pattern_row_channel_volume_effect_type( openmpt_module_ext * mod_ext, int32_t pattern, int32_t row, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_pattern_row_channel_volume_effect_type( pattern, row, channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return -1;
}
static int get_pattern_row_channel_effect_type( openmpt_module_ext * mod_ext, int32_t pattern, int32_t row, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_pattern_row_channel_effect_type( pattern, row, channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return -1;
}
int set_current_speed( openmpt_module_ext * mod_ext, int32_t speed ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_current_speed( speed );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
int set_current_tempo( openmpt_module_ext * mod_ext, int32_t tempo ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_current_tempo( tempo );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
int set_tempo_factor( openmpt_module_ext * mod_ext, double factor ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_tempo_factor( factor );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
double get_tempo_factor( openmpt_module_ext * mod_ext ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_tempo_factor();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0.0;
}
int set_pitch_factor( openmpt_module_ext * mod_ext, double factor ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_pitch_factor( factor );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
double get_pitch_factor( openmpt_module_ext * mod_ext ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_pitch_factor();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0.0;
}
int set_global_volume( openmpt_module_ext * mod_ext, double volume ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_global_volume( volume );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
double get_global_volume( openmpt_module_ext * mod_ext ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_global_volume();
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0.0;
}
int set_channel_volume( openmpt_module_ext * mod_ext, int32_t channel, double volume ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_channel_volume( channel, volume );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
double get_channel_volume( openmpt_module_ext * mod_ext, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_channel_volume( channel );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0.0;
}
int set_channel_mute_status( openmpt_module_ext * mod_ext, int32_t channel, int mute ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_channel_mute_status( channel, mute ? true : false );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
int get_channel_mute_status( openmpt_module_ext * mod_ext, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_channel_mute_status( channel ) ? 1 : 0;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return -1;
}
int set_instrument_mute_status( openmpt_module_ext * mod_ext, int32_t instrument, int mute ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->set_instrument_mute_status( instrument, mute ? true : false );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
int get_instrument_mute_status( openmpt_module_ext * mod_ext, int32_t instrument ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->get_instrument_mute_status( instrument ) ? 1 : 0;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return -1;
}
int32_t play_note( openmpt_module_ext * mod_ext, int32_t instrument, int32_t note, double volume, double panning ) {
try {
openmpt::interface::check_soundfile( mod_ext );
return mod_ext->impl->play_note( instrument, note, volume, panning );
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return -1;
}
int stop_note( openmpt_module_ext * mod_ext, int32_t channel ) {
try {
openmpt::interface::check_soundfile( mod_ext );
mod_ext->impl->stop_note( channel );
return 1;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
/* add stuff here */
int openmpt_module_ext_get_interface( openmpt_module_ext * mod_ext, const char * interface_id, void * interface, size_t interface_size ) {
try {
openmpt::interface::check_soundfile( mod_ext );
openmpt::interface::check_pointer( interface_id );
openmpt::interface::check_pointer( interface );
std::memset( interface, 0, interface_size );
int result = 0;
if ( !strcmp( interface_id, "" ) ) {
result = 0;
} else if ( !strcmp( interface_id, LIBOPENMPT_EXT_C_INTERFACE_PATTERN_VIS ) && ( interface_size == sizeof( openmpt_module_ext_interface_pattern_vis ) ) ) {
openmpt_module_ext_interface_pattern_vis * i = static_cast< openmpt_module_ext_interface_pattern_vis * >( interface );
i->get_pattern_row_channel_volume_effect_type = &get_pattern_row_channel_volume_effect_type;
i->get_pattern_row_channel_effect_type = &get_pattern_row_channel_effect_type;
result = 1;
} else if ( !strcmp( interface_id, LIBOPENMPT_EXT_C_INTERFACE_INTERACTIVE ) && ( interface_size == sizeof( openmpt_module_ext_interface_interactive ) ) ) {
openmpt_module_ext_interface_interactive * i = static_cast< openmpt_module_ext_interface_interactive * >( interface );
i->set_current_speed = &set_current_speed;
i->set_current_tempo = &set_current_tempo;
i->set_tempo_factor = &set_tempo_factor;
i->get_tempo_factor = &get_tempo_factor;
i->set_pitch_factor = &set_pitch_factor;
i->get_pitch_factor = &get_pitch_factor;
i->set_global_volume = &set_global_volume;
i->get_global_volume = &get_global_volume;
i->set_channel_volume = &set_channel_volume;
i->get_channel_volume = &get_channel_volume;
i->set_channel_mute_status = &set_channel_mute_status;
i->get_channel_mute_status = &get_channel_mute_status;
i->set_instrument_mute_status = &set_instrument_mute_status;
i->get_instrument_mute_status = &get_instrument_mute_status;
i->play_note = &play_note;
i->stop_note = &stop_note;
result = 1;
/* add stuff here */
} else {
result = 0;
}
return result;
} catch ( ... ) {
openmpt::report_exception( __FUNCTION__, mod_ext ? &mod_ext->mod : NULL );
}
return 0;
}
} // extern "C"
#endif // NO_LIBOPENMPT_C
|