1 2Libxmp 4.5 API documentation 3============================ 4 5.. contents:: `Contents` 6 :depth: 3 7 8.. raw:: pdf 9 10 PageBreak 11 12Introduction 13------------ 14 15Libxmp is a module player library supporting many mainstream and obscure 16module formats including Protracker MOD, Scream Tracker III S3M and 17Impulse Tracker IT. Libxmp loads the module and renders the sound as 18linear PCM samples in a buffer at rate and format specified by the user, 19one frame at a time (standard modules usually play at 50 frames per second). 20 21Possible applications for libxmp include stand-alone module players, module 22player plugins for other players, module information extractors, background 23music replayers for games and other applications, module-to-mp3 renderers, etc. 24 25 26Concepts 27~~~~~~~~ 28 29* **Player context:** 30 Most libxmp functions require a handle that identifies the module player 31 context. Each context is independent and multiple contexts can be defined 32 simultaneously. 33 34* **Sequence:** 35 Each group of positions in the order list that loops over itself, also 36 known as "subsong". Most modules have only one sequence, but some modules, 37 especially modules used in games can have multiple sequences. "Hidden 38 patterns" outside the main song are also listed as extra sequences, certain 39 module authors such as Skaven commonly place extra patterns at the end of 40 the module. 41 42* **State:** *[Added in libxmp 4.2]* 43 The player can be in one of three possible states: *unloaded*, *loaded*, 44 or *playing*. The player is in unloaded state after context creation, 45 changing to other states when a module is loaded or played. 46 47* **External sample mixer:** *[Added in libxmp 4.2]* 48 Special sound channels can be reserved using `xmp_start_smix()` 49 to play module instruments or external samples. This is useful when 50 libxmp is used to provide background music to games or other applications 51 where sound effects can be played in response to events or user actions 52 53* **Amiga mixer:** *[Added in libxmp 4.4]* 54 Certain formats may use special mixers modeled after the original hardware 55 used to play the format, providing more realistic sound at the expense of 56 CPU usage. Currently Amiga formats such as Protracker can use a mixer 57 modeled after the Amiga 500, with or without the led filter. 58 59A simple example 60~~~~~~~~~~~~~~~~ 61 62This example loads a module, plays it at 44.1kHz and writes it to a raw 63sound file:: 64 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <xmp.h> 68 69 int main(int argc, char **argv) 70 { 71 xmp_context c; 72 struct xmp_frame_info mi; 73 FILE *f; 74 75 /* The output raw file */ 76 f = fopen("out.raw", "wb"); 77 if (f == NULL) { 78 fprintf(stderr, "can't open output file\n"); 79 exit(EXIT_FAILURE); 80 } 81 82 /* Create the player context */ 83 c = xmp_create_context(); 84 85 /* Load our module */ 86 if (xmp_load_module(c, argv[1]) != 0) { 87 fprintf(stderr, "can't load module\n"); 88 exit(EXIT_FAILURE); 89 } 90 91 /* Play the module */ 92 xmp_start_player(c, 44100, 0); 93 while (xmp_play_frame(c) == 0) { 94 xmp_get_frame_info(c, &mi); 95 96 if (mi.loop_count > 0) /* exit before looping */ 97 break; 98 99 fwrite(mi.buffer, mi.buffer_size, 1, f); /* write audio data */ 100 } 101 xmp_end_player(c); 102 xmp_release_module(c); /* unload module */ 103 xmp_free_context(c); /* destroy the player context */ 104 105 fclose(f); 106 107 exit(EXIT_SUCCESS); 108 } 109 110 111A player context can load and play a single module at a time. Multiple 112contexts can be defined if needed. 113 114Use `xmp_test_module()`_ to check if the file is a valid module and 115retrieve the module name and type. Use `xmp_load_module()`_ to load 116the module to memory. These two calls return 0 on success or <0 in case of 117error. Error codes are:: 118 119 -XMP_ERROR_INTERNAL /* Internal error */ 120 -XMP_ERROR_FORMAT /* Unsupported module format */ 121 -XMP_ERROR_LOAD /* Error loading file */ 122 -XMP_ERROR_DEPACK /* Error depacking file */ 123 -XMP_ERROR_SYSTEM /* System error */ 124 -XMP_ERROR_STATE /* Incorrect player state */ 125 126If a system error occurs, the specific error is set in ``errno``. 127 128Parameters to `xmp_start_player()`_ are the sampling 129rate (up to 48kHz) and a bitmapped integer holding one or more of the 130following mixer flags:: 131 132 XMP_MIX_8BIT /* Mix to 8-bit instead of 16 */ 133 XMP_MIX_UNSIGNED /* Mix to unsigned samples */ 134 XMP_MIX_MONO /* Mix to mono instead of stereo */ 135 XMP_MIX_NEAREST /* Mix using nearest neighbor interpolation */ 136 XMP_MIX_NOFILTER /* Disable lowpass filter */ 137 138After `xmp_start_player()`_ is called, each call to `xmp_play_frame()`_ 139will render an audio frame. Call `xmp_get_frame_info()`_ to retrieve the 140buffer address and size. `xmp_play_frame()`_ returns 0 on success or -1 141if replay should stop. 142 143Use `xmp_end_player()`_, `xmp_release_module()`_ and 144`xmp_free_context()`_ to release memory and end replay. 145 146 147SDL example 148~~~~~~~~~~~ 149 150To use libxmp with SDL, just provide a callback function that renders module 151data. The module will play when ``SDL_PauseAudio(0)`` is called:: 152 153 #include <SDL/SDL.h> 154 #include <xmp.h> 155 156 static void fill_audio(void *udata, unsigned char *stream, int len) 157 { 158 xmp_play_buffer(udata, stream, len, 0); 159 } 160 161 int sound_init(xmp_context ctx, int sampling_rate, int channels) 162 { 163 SDL_AudioSpec a; 164 165 a.freq = sampling_rate; 166 a.format = (AUDIO_S16); 167 a.channels = channels; 168 a.samples = 2048; 169 a.callback = fill_audio; 170 a.userdata = ctx; 171 172 if (SDL_OpenAudio(&a, NULL) < 0) { 173 fprintf(stderr, "%s\n", SDL_GetError()); 174 return -1; 175 } 176 } 177 178 int main(int argc, char **argv) 179 { 180 xmp_context ctx; 181 182 if ((ctx = xmp_create_context()) == NULL) 183 return 1; 184 185 sound_init(ctx, 44100, 2); 186 xmp_load_module(ctx, argv[1]); 187 xmp_start_player(ctx, 44100, 0); 188 189 SDL_PauseAudio(0); 190 191 sleep(10); /* Do something important here */ 192 193 SDL_PauseAudio(1); 194 195 xmp_end_player(ctx); 196 xmp_release_module(ctx); 197 xmp_free_context(ctx); 198 199 SDL_CloseAudio(); 200 return 0; 201 } 202 203SDL callbacks run in a separate thread, so don't forget to protect sections 204that manipulate module data with ``SDL_LockAudio()`` and ``SDL_UnlockAudio()``. 205 206 207.. raw:: pdf 208 209 PageBreak 210 211API reference 212------------- 213 214Version and player information 215~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 216 217.. _xmp_version: 218 219const char \*xmp_version 220```````````````````````` 221 222 A string containing the library version, such as "4.0.0". 223 224.. _xmp_vercode: 225 226const unsigned int xmp_vercode 227`````````````````````````````` 228 229 The library version encoded in a integer value. Bits 23-16 contain the 230 major version number, bits 15-8 contain the minor version number, and 231 bits 7-0 contain the release number. 232 233 234.. _xmp_syserrno(): 235 236int xmp_syserrno() 237`````````````````` 238 239 *[Added in libxmp 4.5]* Use to retrieve errno if you received 240 ``-XMP_ERROR_SYSTEM`` from an xmp function call. Useful if either 241 libxmp or its client is statically linked to libc. 242 243 **Returns:** System errno. 244 245 246.. _xmp_get_format_list(): 247 248const char \*const \*xmp_get_format_list() 249`````````````````````````````````````````` 250 251 Query the list of supported module formats. 252 253 **Returns:** 254 a NULL-terminated read-only array of strings containing the names 255 of all supported module formats. 256 257 258Context creation 259~~~~~~~~~~~~~~~~ 260 261.. _xmp_create_context(): 262 263xmp_context xmp_create_context() 264```````````````````````````````` 265 266 Create a new player context and return an opaque handle to be used in 267 subsequent accesses to this context. 268 269 **Returns:** 270 the player context handle. 271 272.. _xmp_free_context(): 273 274void xmp_free_context(xmp_context c) 275```````````````````````````````````` 276 277 Destroy a player context previously created using `xmp_create_context()`_. 278 279 **Parameters:** 280 :c: 281 the player context handle. 282 283 284Module loading 285~~~~~~~~~~~~~~ 286 287.. _xmp_test_module(): 288 289int xmp_test_module(char \*path, struct xmp_test_info \*test_info) 290`````````````````````````````````````````````````````````````````` 291 292 Test if a file is a valid module. Testing a file does not affect the 293 current player context or any currently loaded module. 294 295 **Parameters:** 296 :path: pathname of the module to test. 297 298 :test_info: NULL, or a pointer to a structure used to retrieve the 299 module title and format if the file is a valid module. 300 ``struct xmp_test_info`` is defined as:: 301 302 struct xmp_test_info { 303 char name[XMP_NAME_SIZE]; /* Module title */ 304 char type[XMP_NAME_SIZE]; /* Module format */ 305 }; 306 307 **Returns:** 308 0 if the file is a valid module, or a negative error code 309 in case of error. Error codes can be ``-XMP_ERROR_FORMAT`` in case of an 310 unrecognized file format, ``-XMP_ERROR_DEPACK`` if the file is compressed 311 and uncompression failed, or ``-XMP_ERROR_SYSTEM`` in case of system error 312 (the system error code is set in ``errno``). 313 314.. xmp_test_module_from_memory(): 315 316int xmp_test_module_from_memory(const void \*mem, long size, struct xmp_test_info \*test_info) 317`````````````````````````````````````````````````````````````````````````````````````````````` 318 319 *[Added in libxmp 4.5]* Test if a memory buffer is a valid module. Testing 320 memory does not affect the current player context or any currently loaded 321 module. 322 323 **Parameters:** 324 :mem: a pointer to the module file image in memory. Multi-file modules 325 or compressed modules can't be tested in memory. 326 327 :size: the size of the module. 328 329 :test_info: NULL, or a pointer to a structure used to retrieve the 330 module title and format if the memory buffer is a valid module. 331 ``struct xmp_test_info`` is defined as:: 332 333 struct xmp_test_info { 334 char name[XMP_NAME_SIZE]; /* Module title */ 335 char type[XMP_NAME_SIZE]; /* Module format */ 336 }; 337 338 **Returns:** 339 0 if the memory buffer is a valid module, or a negative error code 340 in case of error. Error codes can be ``-XMP_ERROR_FORMAT`` in case of an 341 unrecognized file format or ``-XMP_ERROR_SYSTEM`` in case of system error 342 (the system error code is set in ``errno``). 343 344.. xmp_test_module_from_file(): 345 346int xmp_test_module_from_file(FILE \*f, struct xmp_test_info \*test_info) 347````````````````````````````````````````````````````````````````````````` 348 349 *[Added in libxmp 4.5]* Test if a module from a stream is a valid module. 350 Testing streams does not affect the current player context or any 351 currently loaded module. 352 353 **Parameters:** 354 :f: the file stream. Compressed modules that need an external depacker 355 can't be tested from a file stream. On return, the stream position is 356 undefined. Caller is responsible for closing the file stream. 357 358 :test_info: NULL, or a pointer to a structure used to retrieve the 359 module title and format if the memory buffer is a valid module. 360 ``struct xmp_test_info`` is defined as:: 361 362 struct xmp_test_info { 363 char name[XMP_NAME_SIZE]; /* Module title */ 364 char type[XMP_NAME_SIZE]; /* Module format */ 365 }; 366 367 **Returns:** 368 0 if the stream is a valid module, or a negative error code 369 in case of error. Error codes can be ``-XMP_ERROR_FORMAT`` in case of an 370 unrecognized file format, ``-XMP_ERROR_DEPACK`` if the stream is compressed 371 and uncompression failed, or ``-XMP_ERROR_SYSTEM`` in case of system error 372 (the system error code is set in ``errno``). 373 374.. _xmp_test_module_from_callbacks(): 375 376int xmp_test_module_from_callbacks(void \*priv, struct xmp_callbacks callbacks, struct xmp_test_info \*test_info) 377````````````````````````````````````````````````````````````````````````````````````````````````````````````````` 378 379 *[Added in libxmp 4.5]* Test if a module from a custom stream is a valid 380 module. Testing custom streams does not affect the current player context 381 or any currently loaded module. 382 383 **Parameters:** 384 :priv: pointer to the custom stream. Multi-file modules 385 or compressed modules can't be tested using this function. 386 This should not be NULL. 387 388 :callbacks: struct specifying stream callbacks for the custom stream. 389 These callbacks should behave as close to ``fread``/``fseek``/``ftell``/``fclose`` 390 as possible, and ``seek_func`` must be capable of seeking to ``SEEK_END``. 391 The ``close_func`` is optional, but all other functions must be provided. 392 If a ``close_func`` is provided, the stream will be closed once testing 393 has finished or upon returning an error code. 394 ``struct xmp_callbacks`` is defined as:: 395 396 struct xmp_callbacks { 397 unsigned long (*read_func)(void *dest, unsigned long len, 398 unsigned long nmemb, void *priv); 399 int (*seek_func)(void *priv, long offset, int whence); 400 long (*tell_func)(void *priv); 401 int (*close_func)(void *priv); 402 }; 403 404 :test_info: NULL, or a pointer to a structure used to retrieve the 405 module title and format if the memory buffer is a valid module. 406 407 ``struct xmp_test_info`` is defined as:: 408 409 struct xmp_test_info { 410 char name[XMP_NAME_SIZE]; /* Module title */ 411 char type[XMP_NAME_SIZE]; /* Module format */ 412 }; 413 414 **Returns:** 415 0 if the custom stream is a valid module, or a negative error code 416 in case of error. Error codes can be ``-XMP_ERROR_FORMAT`` in case of an 417 unrecognized file format or ``-XMP_ERROR_SYSTEM`` in case of system error 418 (the system error code is set in ``errno``). 419 420.. _xmp_load_module(): 421 422int xmp_load_module(xmp_context c, char \*path) 423``````````````````````````````````````````````` 424 425 Load a module into the specified player context. (Certain player flags, 426 such as ``XMP_PLAYER_SMPCTL`` and ``XMP_PLAYER_DEFPAN``, must be set 427 before loading the module, see `xmp_set_player()`_ for more information.) 428 429 **Parameters:** 430 :c: the player context handle. 431 432 :path: pathname of the module to load. 433 434 **Returns:** 435 0 if successful, or a negative error code in case of error. 436 Error codes can be ``-XMP_ERROR_FORMAT`` in case of an unrecognized file 437 format, ``-XMP_ERROR_DEPACK`` if the file is compressed and uncompression 438 failed, ``-XMP_ERROR_LOAD`` if the file format was recognized but the 439 file loading failed, or ``-XMP_ERROR_SYSTEM`` in case of system error 440 (the system error code is set in ``errno``). 441 442.. _xmp_load_module_from_memory(): 443 444int xmp_load_module_from_memory(xmp_context c, const void \*mem, long size) 445``````````````````````````````````````````````````````````````````````````` 446 447 *[Added in libxmp 4.2]* Load a module from memory into the specified 448 player context. 449 450 **Parameters:** 451 :c: the player context handle. 452 453 :mem: a pointer to the module file image in memory. Multi-file modules 454 or compressed modules can't be loaded from memory. 455 456 :size: the size of the module. 457 458 **Returns:** 459 0 if successful, or a negative error code in case of error. 460 Error codes can be ``-XMP_ERROR_FORMAT`` in case of an unrecognized file 461 format, ``-XMP_ERROR_LOAD`` if the file format was recognized but the 462 file loading failed, or ``-XMP_ERROR_SYSTEM`` in case of system error 463 (the system error code is set in ``errno``). 464 465.. _xmp_load_module_from_file(): 466 467int xmp_load_module_from_file(xmp_context c, FILE \*f, long size) 468````````````````````````````````````````````````````````````````` 469 470 *[Added in libxmp 4.3]* Load a module from a stream into the specified 471 player context. 472 473 **Parameters:** 474 :c: the player context handle. 475 476 :f: the file stream. On return, the stream position is undefined. 477 Caller is responsible for closing the file stream. 478 479 :size: the size of the module (ignored.) 480 481 **Returns:** 482 0 if successful, or a negative error code in case of error. 483 Error codes can be ``-XMP_ERROR_FORMAT`` in case of an unrecognized file 484 format, ``-XMP_ERROR_LOAD`` if the file format was recognized but the 485 file loading failed, or ``-XMP_ERROR_SYSTEM`` in case of system error 486 (the system error code is set in ``errno``). 487 488.. _xmp_load_module_from_callbacks(): 489 490int xmp_load_module_from_callbacks(xmp_context c, void \*priv, struct xmp_callbacks callbacks) 491`````````````````````````````````````````````````````````````````````````````````````````````` 492 493 *[Added in libxmp 4.5]* Load a module from a custom stream into the specified 494 player context. 495 496 **Parameters:** 497 :c: the player context handle. 498 499 :priv: pointer to the custom stream. Multi-file modules 500 or compressed modules can't be loaded using this function. 501 This should not be NULL. 502 503 :callbacks: struct specifying stream callbacks for the custom stream. 504 These callbacks should behave as close to ``fread``/``fseek``/``ftell``/``fclose`` 505 as possible, and ``seek_func`` must be capable of seeking to ``SEEK_END``. 506 The ``close_func`` is optional, but all other functions must be provided. 507 If a ``close_func`` is provided, the stream will be closed once loading 508 has finished or upon returning an error code. 509 ``struct xmp_callbacks`` is defined as:: 510 511 struct xmp_callbacks { 512 unsigned long (*read_func)(void *dest, unsigned long len, 513 unsigned long nmemb, void *priv); 514 int (*seek_func)(void *priv, long offset, int whence); 515 long (*tell_func)(void *priv); 516 int (*close_func)(void *priv); 517 }; 518 519 **Returns:** 520 0 if successful, or a negative error code in case of error. 521 Error codes can be ``-XMP_ERROR_FORMAT`` in case of an unrecognized file 522 format, ``-XMP_ERROR_LOAD`` if the file format was recognized but the 523 file loading failed, or ``-XMP_ERROR_SYSTEM`` in case of system error 524 (the system error code is set in ``errno``). 525 526.. _xmp_release_module(): 527 528void xmp_release_module(xmp_context c) 529`````````````````````````````````````` 530 531 Release memory allocated by a module from the specified player context. 532 533 **Parameters:** 534 :c: the player context handle. 535 536.. _xmp_scan_module(): 537 538void xmp_scan_module(xmp_context c) 539``````````````````````````````````` 540 541 Scan the loaded module for sequences and timing. Scanning is automatically 542 performed by `xmp_load_module()`_ and this function should be called only 543 if `xmp_set_player()`_ is used to change player timing (with parameter 544 ``XMP_PLAYER_VBLANK``) in libxmp 4.0.2 or older. 545 546 **Parameters:** 547 :c: the player context handle. 548 549.. _xmp_get_module_info(): 550 551void xmp_get_module_info(xmp_context c, struct xmp_module_info \*info) 552`````````````````````````````````````````````````````````````````````` 553 554 Retrieve current module data. 555 556 **Parameters:** 557 :c: the player context handle. 558 559 :info: pointer to structure containing the module data. 560 ``struct xmp_module_info`` is defined as follows:: 561 562 struct xmp_module_info { 563 unsigned char md5[16]; /* MD5 message digest */ 564 int vol_base; /* Volume scale */ 565 struct xmp_module *mod; /* Pointer to module data */ 566 char *comment; /* Comment text, if any */ 567 int num_sequences; /* Number of valid sequences */ 568 struct xmp_sequence *seq_data; /* Pointer to sequence data */ 569 }; 570 571 Detailed module data is exposed in the ``mod`` field:: 572 573 struct xmp_module { 574 char name[XMP_NAME_SIZE]; /* Module title */ 575 char type[XMP_NAME_SIZE]; /* Module format */ 576 int pat; /* Number of patterns */ 577 int trk; /* Number of tracks */ 578 int chn; /* Tracks per pattern */ 579 int ins; /* Number of instruments */ 580 int smp; /* Number of samples */ 581 int spd; /* Initial speed */ 582 int bpm; /* Initial BPM */ 583 int len; /* Module length in patterns */ 584 int rst; /* Restart position */ 585 int gvl; /* Global volume */ 586 587 struct xmp_pattern **xxp; /* Patterns */ 588 struct xmp_track **xxt; /* Tracks */ 589 struct xmp_instrument *xxi; /* Instruments */ 590 struct xmp_sample *xxs; /* Samples */ 591 struct xmp_channel xxc[64]; /* Channel info */ 592 unsigned char xxo[XMP_MAX_MOD_LENGTH]; /* Orders */ 593 }; 594 595 See the header file for more information about pattern and instrument 596 data. 597 598 599Module playing 600~~~~~~~~~~~~~~ 601 602.. _xmp_start_player(): 603 604int xmp_start_player(xmp_context c, int rate, int format) 605````````````````````````````````````````````````````````` 606 607 Start playing the currently loaded module. 608 609 **Parameters:** 610 :c: the player context handle. 611 612 :rate: the sampling rate to use, in Hz (typically 44100). Valid values 613 range from 8kHz to 48kHz. 614 615 :flags: bitmapped configurable player flags, one or more of the 616 following:: 617 618 XMP_FORMAT_8BIT /* Mix to 8-bit instead of 16 */ 619 XMP_FORMAT_UNSIGNED /* Mix to unsigned samples */ 620 XMP_FORMAT_MONO /* Mix to mono instead of stereo */ 621 622 **Returns:** 623 0 if successful, or a negative error code in case of error. 624 Error codes can be ``-XMP_ERROR_INTERNAL`` in case of a internal player 625 error, ``-XMP_ERROR_INVALID`` if the sampling rate is invalid, or 626 ``-XMP_ERROR_SYSTEM`` in case of system error (the system error 627 code is set in ``errno``). 628 629.. _xmp_play_frame(): 630 631int xmp_play_frame(xmp_context c) 632````````````````````````````````` 633 634 Play one frame of the module. Modules usually play at 50 frames per second. 635 Use `xmp_get_frame_info()`_ to retrieve the buffer containing audio data. 636 637 **Parameters:** 638 :c: the player context handle. 639 640 **Returns:** 641 0 if successful, ``-XMP_END`` if the module ended or was stopped, or 642 ``-XMP_ERROR_STATE`` if the player is not in playing state. 643 644.. _xmp_play_buffer(): 645 646int xmp_play_buffer(xmp_context c, void \*buffer, int size, int loop) 647````````````````````````````````````````````````````````````````````` 648 649 *[Added in libxmp 4.1]* Fill the buffer with PCM data up to the specified 650 size. This is a convenience function that calls `xmp_play_frame()`_ 651 internally to fill the user-supplied buffer. **Don't call both 652 xmp_play_frame() and xmp_play_buffer() in the same replay loop.** 653 If you don't need equally sized data chunks, `xmp_play_frame()`_ 654 may result in better performance. Also note that silence is added 655 at the end of a buffer if the module ends and no loop is to be performed. 656 657 **Parameters:** 658 :c: the player context handle. 659 660 :buffer: the buffer to fill with PCM data, or NULL to reset the 661 internal state. 662 663 :size: the buffer size in bytes. 664 665 :loop: stop replay when the loop counter reaches the specified 666 value, or 0 to disable loop checking. 667 668 **Returns:** 669 0 if successful, ``-XMP_END`` if module was stopped or the loop counter 670 was reached, or ``-XMP_ERROR_STATE`` if the player is not in playing 671 state. 672 673.. _xmp_get_frame_info(): 674 675void xmp_get_frame_info(xmp_context c, struct xmp_frame_info \*info) 676```````````````````````````````````````````````````````````````````` 677 678 Retrieve the current frame data. 679 680 **Parameters:** 681 :c: the player context handle. 682 683 :info: pointer to structure containing current frame data. 684 ``struct xmp_frame_info`` is defined as follows:: 685 686 struct xmp_frame_info { /* Current frame information */ 687 int pos; /* Current position */ 688 int pattern; /* Current pattern */ 689 int row; /* Current row in pattern */ 690 int num_rows; /* Number of rows in current pattern */ 691 int frame; /* Current frame */ 692 int speed; /* Current replay speed */ 693 int bpm; /* Current bpm */ 694 int time; /* Current module time in ms */ 695 int total_time; /* Estimated replay time in ms*/ 696 int frame_time; /* Frame replay time in us */ 697 void *buffer; /* Pointer to sound buffer */ 698 int buffer_size; /* Used buffer size */ 699 int total_size; /* Total buffer size */ 700 int volume; /* Current master volume */ 701 int loop_count; /* Loop counter */ 702 int virt_channels; /* Number of virtual channels */ 703 int virt_used; /* Used virtual channels */ 704 int sequence; /* Current sequence */ 705 706 struct xmp_channel_info { /* Current channel information */ 707 unsigned int period; /* Sample period */ 708 unsigned int position; /* Sample position */ 709 short pitchbend; /* Linear bend from base note*/ 710 unsigned char note; /* Current base note number */ 711 unsigned char instrument; /* Current instrument number */ 712 unsigned char sample; /* Current sample number */ 713 unsigned char volume; /* Current volume */ 714 unsigned char pan; /* Current stereo pan */ 715 unsigned char reserved; /* Reserved */ 716 struct xmp_event event; /* Current track event */ 717 } channel_info[XMP_MAX_CHANNELS]; 718 }; 719 720 This function should be used to retrieve sound buffer data after 721 `xmp_play_frame()`_ is called. Fields ``buffer`` and ``buffer_size`` 722 contain the pointer to the sound buffer PCM data and its size. The 723 buffer size will be no larger than ``XMP_MAX_FRAMESIZE``. 724 725.. _xmp_end_player(): 726 727void xmp_end_player(xmp_context c) 728`````````````````````````````````` 729 730 End module replay and release player memory. 731 732 **Parameters:** 733 :c: the player context handle. 734 735.. raw:: pdf 736 737 PageBreak 738 739Player control 740~~~~~~~~~~~~~~ 741 742.. _xmp_next_position(): 743 744int xmp_next_position(xmp_context c) 745```````````````````````````````````` 746 747 Skip replay to the start of the next position. 748 749 **Parameters:** 750 :c: the player context handle. 751 752 **Returns:** 753 The new position index, or ``-XMP_ERROR_STATE`` if the player is not 754 in playing state. 755 756.. _xmp_prev_position(): 757 758int xmp_prev_position(xmp_context c) 759```````````````````````````````````` 760 761 Skip replay to the start of the previous position. 762 763 **Parameters:** 764 :c: the player context handle. 765 766 **Returns:** 767 The new position index, or ``-XMP_ERROR_STATE`` if the player is not 768 in playing state. 769 770.. _xmp_set_position(): 771 772int xmp_set_position(xmp_context c, int pos) 773```````````````````````````````````````````` 774 775 Skip replay to the start of the given position. 776 777 **Parameters:** 778 :c: the player context handle. 779 780 :pos: the position index to set. 781 782 **Returns:** 783 The new position index, ``-XMP_ERROR_INVALID`` of the new position is 784 invalid or ``-XMP_ERROR_STATE`` if the player is not in playing state. 785 786.. _xmp_set_row(): 787 788int xmp_set_row(xmp_context c, int row) 789``````````````````````````````````````` 790 791 *[Added in libxmp 4.5]* Skip replay to the given row. 792 793 **Parameters:** 794 :c: the player context handle. 795 796 :row: the row to set. 797 798 **Returns:** 799 The new row, ``-XMP_ERROR_INVALID`` if the new row is invalid or 800 ``-XMP_ERROR_STATE`` if the player is not in playing state. 801 802.. _xmp_set_tempo_factor(): 803 804int xmp_set_tempo_factor(xmp_context c, double val) 805``````````````````````````````````````````````````` 806 807 *[Added in libxmp 4.5]* Modify the replay tempo multiplier. 808 809 **Parameters:** 810 :c: the player context handle. 811 812 :val: the new multiplier. 813 814 **Returns:** 815 0 on success, or -1 if value is invalid. 816 817.. _xmp_stop_module(): 818 819void xmp_stop_module(xmp_context c) 820``````````````````````````````````` 821 822 Stop the currently playing module. 823 824 **Parameters:** 825 :c: the player context handle. 826 827.. _xmp_restart_module(): 828 829void xmp_restart_module(xmp_context c) 830`````````````````````````````````````` 831 832 Restart the currently playing module. 833 834 **Parameters:** 835 :c: the player context handle. 836 837.. _xmp_seek_time(): 838 839int xmp_seek_time(xmp_context c, int time) 840`````````````````````````````````````````` 841 842 Skip replay to the specified time. 843 844 **Parameters:** 845 :c: the player context handle. 846 847 :time: time to seek in milliseconds. 848 849 **Returns:** 850 The new position index, or ``-XMP_ERROR_STATE`` if the player is not 851 in playing state. 852 853.. _xmp_channel_mute(): 854 855int xmp_channel_mute(xmp_context c, int chn, int status) 856```````````````````````````````````````````````````````````` 857 858 Mute or unmute the specified channel. 859 860 **Parameters:** 861 :c: the player context handle. 862 863 :chn: the channel to mute or unmute. 864 865 :status: 0 to mute channel, 1 to unmute or -1 to query the 866 current channel status. 867 868 **Returns:** 869 The previous channel status, or ``-XMP_ERROR_STATE`` if the player is not 870 in playing state. 871 872.. _xmp_channel_vol(): 873 874int xmp_channel_vol(xmp_context c, int chn, int vol) 875```````````````````````````````````````````````````````` 876 877 Set or retrieve the volume of the specified channel. 878 879 **Parameters:** 880 :c: the player context handle. 881 882 :chn: the channel to set or get volume. 883 884 :vol: a value from 0-100 to set the channel volume, or -1 to retrieve 885 the current volume. 886 887 **Returns:** 888 The previous channel volume, or ``-XMP_ERROR_STATE`` if the player is not 889 in playing state. 890 891 892.. _xmp_inject_event(): 893 894void xmp_inject_event(xmp_context c, int chn, struct xmp_event \*event) 895``````````````````````````````````````````````````````````````````````````` 896 897 Dynamically insert a new event into a playing module. 898 899 **Parameters:** 900 :c: the player context handle. 901 902 :chn: the channel to insert the new event. 903 904 :event: the event to insert. 905 ``struct xmp_event`` is defined as:: 906 907 struct xmp_event { 908 unsigned char note; /* Note number (0 means no note) */ 909 unsigned char ins; /* Patch number */ 910 unsigned char vol; /* Volume (0 to basevol) */ 911 unsigned char fxt; /* Effect type */ 912 unsigned char fxp; /* Effect parameter */ 913 unsigned char f2t; /* Secondary effect type */ 914 unsigned char f2p; /* Secondary effect parameter */ 915 unsigned char _flag; /* Internal (reserved) flags */ 916 }; 917 918 919.. raw:: pdf 920 921 PageBreak 922 923Player parameter setting 924~~~~~~~~~~~~~~~~~~~~~~~~ 925 926.. _xmp_set_instrument_path(): 927 928int xmp_set_instrument_path(xmp_context c, char \*path) 929``````````````````````````````````````````````````````` 930 931 Set the path to retrieve external instruments or samples. Used by some 932 formats (such as MED2) to read sample files from a different directory 933 in the filesystem. 934 935 **Parameters:** 936 :c: the player context handle. 937 938 :path: the path to retrieve instrument files. 939 940 **Returns:** 941 0 if the instrument path was correctly set, or ``-XMP_ERROR_SYSTEM`` 942 in case of error (the system error code is set in ``errno``). 943 944.. _xmp_get_player(): 945 946int xmp_get_player(xmp_context c, int param) 947```````````````````````````````````````````` 948 949 Retrieve current value of the specified player parameter. 950 951 **Parameters:** 952 :c: the player context handle. 953 954 :param: player parameter to get. 955 Valid parameters are:: 956 957 XMP_PLAYER_AMP /* Amplification factor */ 958 XMP_PLAYER_MIX /* Stereo mixing */ 959 XMP_PLAYER_INTERP /* Interpolation type */ 960 XMP_PLAYER_DSP /* DSP effect flags */ 961 XMP_PLAYER_FLAGS /* Player flags */ 962 XMP_PLAYER_CFLAGS /* Player flags for current module*/ 963 XMP_PLAYER_SMPCTL /* Control sample loading */ 964 XMP_PLAYER_VOLUME /* Player master volume */ 965 XMP_PLAYER_STATE /* Current player state (read only) */ 966 XMP_PLAYER_SMIX_VOLUME /* SMIX Volume */ 967 XMP_PLAYER_DEFPAN /* Default pan separation */ 968 XMP_PLAYER_MODE /* Player personality */ 969 XMP_PLAYER_MIXER_TYPE /* Current mixer (read only) */ 970 XMP_PLAYER_VOICES /* Maximum number of mixer voices */ 971 972 Valid states are:: 973 974 XMP_STATE_UNLOADED /* Context created */ 975 XMP_STATE_LOADED /* Module loaded */ 976 XMP_STATE_PLAYING /* Module playing */ 977 978 Valid mixer types are:: 979 980 XMP_MIXER_STANDARD /* Standard mixer */ 981 XMP_MIXER_A500 /* Amiga 500 */ 982 XMP_MIXER_A500F /* Amiga 500 with led filter */ 983 984 See ``xmp_set_player`` for the rest of valid values for each parameter. 985 986 **Returns:** 987 The parameter value, or ``-XMP_ERROR_STATE`` if the parameter is not 988 ``XMP_PLAYER_STATE`` and the player is not in playing state. 989 990.. raw:: pdf 991 992 PageBreak 993 994.. _xmp_set_player(): 995 996int xmp_set_player(xmp_context c, int param, int val) 997````````````````````````````````````````````````````` 998 999 Set player parameter with the specified value. 1000 1001 **Parameters:** 1002 :param: player parameter to set. 1003 Valid parameters are:: 1004 1005 XMP_PLAYER_AMP /* Amplification factor */ 1006 XMP_PLAYER_MIX /* Stereo mixing */ 1007 XMP_PLAYER_INTERP /* Interpolation type */ 1008 XMP_PLAYER_DSP /* DSP effect flags */ 1009 XMP_PLAYER_FLAGS /* Player flags */ 1010 XMP_PLAYER_CFLAGS /* Player flags for current module*/ 1011 XMP_PLAYER_SMPCTL /* Control sample loading */ 1012 XMP_PLAYER_VOLUME /* Player master volume */ 1013 XMP_PLAYER_SMIX_VOLUME /* SMIX Volume */ 1014 XMP_PLAYER_DEFPAN /* Default pan separation */ 1015 XMP_PLAYER_MODE /* Player personality */ 1016 XMP_PLAYER_VOICES /* Maximum number of mixer voices */ 1017 1018 :val: the value to set. Valid values depend on the parameter being set. 1019 1020 **Valid values:** 1021 1022 * Amplification factor: ranges from 0 to 3. Default value is 1. 1023 1024 * Stereo mixing: percentual left/right channel separation. Default is 70. 1025 1026 * Interpolation type: can be one of the following values:: 1027 1028 XMP_INTERP_NEAREST /* Nearest neighbor */ 1029 XMP_INTERP_LINEAR /* Linear (default) */ 1030 XMP_INTERP_SPLINE /* Cubic spline */ 1031 1032 * DSP effects flags: enable or disable DSP effects. Valid effects are:: 1033 1034 XMP_DSP_LOWPASS /* Lowpass filter effect */ 1035 XMP_DSP_ALL /* All effects */ 1036 1037 * Player flags: tweakable player parameters. Valid flags are:: 1038 1039 XMP_FLAGS_VBLANK /* Use vblank timing */ 1040 XMP_FLAGS_FX9BUG /* Emulate Protracker 2.x FX9 bug */ 1041 XMP_FLAGS_FIXLOOP /* Make sample loop value / 2 */ 1042 XMP_FLAGS_A500 /* Use Paula mixer in Amiga modules */ 1043 1044 * *[Added in libxmp 4.1]* Player flags for current module: same flags 1045 as above but after applying module-specific quirks (if any). 1046 1047 * *[Added in libxmp 4.1]* Sample control: Valid values are:: 1048 1049 XMP_SMPCTL_SKIP /* Don't load samples */ 1050 1051 * Disabling sample loading when loading a module allows allows 1052 computation of module duration without decompressing and 1053 loading large sample data, and is useful when duration information 1054 is needed for a module that won't be played immediately. 1055 1056 * *[Added in libxmp 4.2]* Player volumes: Set the player master volume 1057 or the external sample mixer master volume. Valid values are 0 to 100. 1058 1059 * *[Added in libxmp 4.3]* Default pan separation: percentual left/right 1060 pan separation in formats with only left and right channels. Default 1061 is 100%. 1062 1063.. raw:: pdf 1064 1065 PageBreak 1066 1067.. 1068 1069 * *[Added in libxmp 4.4]* Player personality: The player can be forced to 1070 emulate a specific tracker in cases where the module relies on a format 1071 quirk and tracker detection fails. Valid modes are:: 1072 1073 XMP_MODE_AUTO /* Autodetect mode (default) */ 1074 XMP_MODE_MOD /* Play as a generic MOD player */ 1075 XMP_MODE_NOISETRACKER /* Play using Noisetracker quirks */ 1076 XMP_MODE_PROTRACKER /* Play using Protracker 1/2 quirks */ 1077 XMP_MODE_S3M /* Play as a generic S3M player */ 1078 XMP_MODE_ST3 /* Play using ST3 bug emulation */ 1079 XMP_MODE_ST3GUS /* Play using ST3+GUS quirks */ 1080 XMP_MODE_XM /* Play as a generic XM player */ 1081 XMP_MODE_FT2 /* Play using FT2 bug emulation */ 1082 XMP_MODE_IT /* Play using IT quirks */ 1083 XMP_MODE_ITSMP /* Play using IT sample mode quirks */ 1084 1085 By default, formats similar to S3M such as PTM or IMF will use S3M 1086 replayer (without Scream Tracker 3 quirks/bug emulation), and formats 1087 similar to XM such as RTM and MDL will use the XM replayer (without FT2 quirks/bug emulation). 1088 1089 Multichannel MOD files will use the XM replayer, and Scream Tracker 3 1090 MOD files will use S3M replayer with ST3 quirks. S3M files will use 1091 the most appropriate replayer according to the tracker used to create 1092 the file, and enable Scream Tracker 3 quirks and bugs only if created 1093 using ST3. XM files will be played with FT2 bugs and quirks only if 1094 created using Fast Tracker II. 1095 1096 Modules created with OpenMPT will be played with all bugs and quirks 1097 of the original trackers. 1098 1099 * *[Added in libxmp 4.4]* Maximum number of mixer voices: the maximum 1100 number of virtual channels that can be used to play the module. If 1101 set too high, modules with voice leaks can cause excessive CPU usage. 1102 Default is 128. 1103 1104 **Returns:** 1105 0 if parameter was correctly set, ``-XMP_ERROR_INVALID`` if 1106 parameter or values are out of the valid ranges, or ``-XMP_ERROR_STATE`` 1107 if the player is not in playing state. 1108 1109 1110.. raw:: pdf 1111 1112 PageBreak 1113 1114External sample mixer API 1115------------------------- 1116 1117Libxmp 4.2 includes a mini-API that can be used to add sound effects to 1118games and similar applications, provided that you have a low latency sound 1119system. It allows module instruments or external sample files in WAV format 1120to be played in response to arbitrary events. 1121 1122Example 1123~~~~~~~ 1124 1125This example using SDL loads a module and a sound sample, plays the module 1126as background music, and plays the sample when a key is pressed:: 1127 1128 #include <SDL/SDL.h> 1129 #include <xmp.h> 1130 1131 static void fill_audio(void *udata, unsigned char *stream, int len) 1132 { 1133 xmp_play_buffer(udata, stream, len, 0); 1134 } 1135 1136 int sound_init(xmp_context ctx, int sampling_rate, int channels) 1137 { 1138 SDL_AudioSpec a; 1139 1140 a.freq = sampling_rate; 1141 a.format = (AUDIO_S16); 1142 a.channels = channels; 1143 a.samples = 2048; 1144 a.callback = fill_audio; 1145 a.userdata = ctx; 1146 1147 if (SDL_OpenAudio(&a, NULL) < 0) { 1148 fprintf(stderr, "%s\n", SDL_GetError()); 1149 return -1; 1150 } 1151 } 1152 1153 int video_init() 1154 { 1155 if (SDL_Init(SDL_INIT_VIDEO) < 0) { 1156 fprintf(stderr, "%s\n", SDL_GetError()); 1157 return -1; 1158 } 1159 if (SDL_SetVideoMode(640, 480, 8, 0) == NULL) { 1160 fprintf(stderr, "%s\n", SDL_GetError()); 1161 return -1; 1162 } 1163 atexit(SDL_Quit); 1164 } 1165 1166 int main(int argc, char **argv) 1167 { 1168 SDL_Event event; 1169 xmp_context ctx; 1170 1171 if ((ctx = xmp_create_context()) == NULL) 1172 return 1; 1173 1174 video_init(); 1175 sound_init(ctx, 44100, 2); 1176 1177 xmp_start_smix(ctx, 1, 1); 1178 xmp_smix_load_sample(ctx, 0, "blip.wav"); 1179 1180 xmp_load_module(ctx, "music.mod"); 1181 xmp_start_player(ctx, 44100, 0); 1182 xmp_set_player(ctx, XMP_PLAYER_VOLUME, 40); 1183 1184 SDL_PauseAudio(0); 1185 1186 while (1) { 1187 if (SDL_WaitEvent(&event)) { 1188 if (event.type == SDL_KEYDOWN) { 1189 if (event.key.keysym.sym == SDLK_ESCAPE) 1190 break; 1191 xmp_smix_play_sample(ctx, 0, 60, 64, 0); 1192 } 1193 } 1194 } 1195 1196 SDL_PauseAudio(1); 1197 1198 xmp_end_player(ctx); 1199 xmp_release_module(ctx); 1200 xmp_end_smix(ctx); 1201 xmp_free_context(ctx); 1202 1203 SDL_CloseAudio(); 1204 return 0; 1205 } 1206 1207 1208SMIX API reference 1209~~~~~~~~~~~~~~~~~~ 1210 1211.. _xmp_start_smix(): 1212 1213int xmp_start_smix(xmp_context c, int nch, int nsmp) 1214```````````````````````````````````````````````````` 1215 1216 Initialize the external sample mixer subsystem with the given number of 1217 reserved channels and samples. 1218 1219 **Parameters:** 1220 :c: the player context handle. 1221 1222 :nch: number of reserved sound mixer channels (1 to 64). 1223 1224 :nsmp: number of external samples. 1225 1226 **Returns:** 1227 0 if the external sample mixer system was correctly initialized, 1228 ``-XMP_ERROR_INVALID`` in case of invalid parameters, ``-XMP_ERROR_STATE`` 1229 if the player is already in playing state, or ``-XMP_ERROR_SYSTEM`` in case 1230 of system error (the system error code is set in ``errno``). 1231 1232.. _xmp_smix_play_instrument(): 1233 1234int xmp_smix_play_instrument(xmp_context c, int ins, int note, int vol, int chn) 1235```````````````````````````````````````````````````````````````````````````````` 1236 1237 Play a note using an instrument from the currently loaded module in 1238 one of the reserved sound mixer channels. 1239 1240 **Parameters:** 1241 :c: the player context handle. 1242 1243 :ins: the instrument to play. 1244 1245 :note: the note number to play (60 = middle C). 1246 1247 :vol: the volume to use (range: 0 to the maximum volume value used by the 1248 current module). 1249 1250 :chn: the reserved channel to use to play the instrument. 1251 1252 **Returns:** 1253 0 if the instrument was correctly played, ``-XMP_ERROR_INVALID`` in 1254 case of invalid parameters, or ``-XMP_ERROR_STATE`` if the player is not 1255 in playing state. 1256 1257.. _xmp_smix_play_sample(): 1258 1259int xmp_smix_play_sample(xmp_context c, int ins, int vol, int chn) 1260`````````````````````````````````````````````````````````````````` 1261 1262 Play an external sample file in one of the reserved sound channels. 1263 The sample must have been previously loaded using 1264 `xmp_smix_load_sample()`_. 1265 1266 **Parameters:** 1267 :c: the player context handle. 1268 1269 :ins: the sample to play. 1270 1271 :vol: the volume to use (0 to the maximum volume value used by the 1272 current module. 1273 1274 :chn: the reserved channel to use to play the sample. 1275 1276 **Returns:** 1277 0 if the sample was correctly played, ``-XMP_ERROR_INVALID`` in 1278 case of invalid parameters, or ``-XMP_ERROR_STATE`` if the player is not 1279 in playing state. 1280 1281.. _xmp_smix_channel_pan(): 1282 1283int xmp_smix_channel_pan(xmp_context c, int chn, int pan) 1284````````````````````````````````````````````````````````` 1285 1286 Set the reserved channel pan value. 1287 1288 **Parameters:** 1289 :c: the player context handle. 1290 1291 :chn: the reserved channel number. 1292 1293 :pan: the pan value to set (0 to 255). 1294 1295 **Returns:** 1296 0 if the pan value was set, or ``-XMP_ERROR_INVALID`` if parameters 1297 are invalid. 1298 1299.. _xmp_smix_load_sample(): 1300 1301int xmp_smix_load_sample(xmp_context c, int num, char \*path) 1302````````````````````````````````````````````````````````````` 1303 1304 Load a sound sample from a file. Samples should be in mono WAV (RIFF) 1305 format. 1306 1307 **Parameters:** 1308 :c: the player context handle. 1309 1310 :num: the slot number of the external sample to load. 1311 1312 :path: pathname of the file to load. 1313 1314 **Returns:** 1315 0 if the sample was correctly loaded, ``-XMP_ERROR_INVALID`` if the 1316 sample slot number is invalid (not reserved using `xmp_start_smix()`_), 1317 ``-XMP_ERROR_FORMAT`` if the file format is unsupported, or 1318 ``-XMP_ERROR_SYSTEM`` in case of system error (the system error code is 1319 set in ``errno``). 1320 1321.. _xmp_smix_release_sample(): 1322 1323int xmp_smix_release_sample(xmp_context c, int num) 1324``````````````````````````````````````````````````` 1325 1326 Release memory allocated by an external sample in the specified player 1327 context. 1328 1329 **Parameters:** 1330 :c: the player context handle. 1331 1332 :num: the sample slot number to release. 1333 1334 **Returns:** 1335 0 if memory was correctly released, or ``-XMP_ERROR_INVALID`` if the 1336 sample slot number is invalid. 1337 1338.. _xmp_end_smix(): 1339 1340void xmp_end_smix(xmp_context c) 1341```````````````````````````````` 1342 1343 Deinitialize and resease memory used by the external sample mixer subsystem. 1344 1345 **Parameters:** 1346 :c: the player context handle. 1347 1348