GNU Info

Info Node: (mikmod.info)Playing Modules

(mikmod.info)Playing Modules


Next: Playing Sound Effects Prev: A Skeleton Program Up: Tutorial
Enter node , (file) or (file)node

Playing Modules
===============

   Our program is not really useful if it doesn't produce sound. Let's
suppose you've got this good old module, "Beyond music", in the file
`beyond music.mod'. How about playing it ?

   To do this, we'll use the following code:

     /* MikMod Sound Library example program: a simple module player */
     
     #include <unistd.h>
     #include <mikmod.h>
     
     main()
     {
         MODULE *module;
     
         /* register all the drivers */
         MikMod_RegisterAllDrivers();
     
         /* register all the module loaders */
         MikMod_RegisterAllLoaders();
     
         /* initialize the library */
         md_mode |= DMODE_SOFT_MUSIC;
         if (MikMod_Init("")) {
             fprintf(stderr, "Could not initialize sound, reason: %s\n",
                     MikMod_strerror(MikMod_errno));
             return;
         }
     
         /* load module */
         module = Player_Load("beyond music.mod", 64, 0);
         if (module) {
             /* start module */
             Player_Start(module);
     
             while (Player_Active()) {
                 /* we're playing */
                 usleep(10000);
                 MikMod_Update();
             }
     
             Player_Stop();
             Player_Free(module);
         } else
             fprintf(stderr, "Could not load module, reason: %s\n",
                     MikMod_strerror(MikMod_errno));
     
         /* give up */
         MikMod_Exit();
     }

   What's new here ? First, we've not only registered MikMod's device
driver, but also the module loaders. MikMod comes with a large choice
of module loaders, each one for a different module type. Since _every_
loader is called to determine the type of the module when we try to
load them, you may want to register only a few of them to save time. In
our case, we don't matter, so we happily register every module loader.

   Then, there's an extra line before calling `MikMod_Init'. We change
the value of MikMod's variable `md_mode' to tell the library that we
want the module to be processed by the software. If you're the happy
owner of a GUS-type card, you could use the specific hardware driver
for this card, but in this case you should not set the
`DMODE_SOFT_MUSIC' flag.

   We'll ensure that `MikMod_Init' was successful. Note that, in case of
error, MikMod provides the variable `MikMod_errno', an equivalent of
the C library `errno' for MikMod errors, and the function
`MikMod_strerror', an equivalent to `strerror'.

   Now onto serious business ! The module is loaded with the
`Player_Load' function, which takes the name of the module file, and
the number of voices afforded to the module. In this case, the module
has only 4 channels, so 4 voices, but complex Impulse Tracker modules
can have a lot of voices (as they can have as many as 256 virtual
channels with so-called "new note actions").  Since empty voices don't
cost time to be processed, it is safe to use a big value, such as 64 or
128. The third parameter is the "curiosity" of the loader: if nonzero,
the loader will search for hidden parts in the module.  However, only a
few module formats can embed hidden or non played parts, so we'll use 0
here.

   Now that the module is ready to play, let's play it. We inform the
player that the current module is `module' with `Player_Start'.
Playback starts, but we have to update it on a regular basis. So
there's a loop on the result of the `Player_Active' function, which
will tell us if the module has finished. To update the sound, we simply
call `MikMod_Update'.

   After the module has finished, we tell the player its job is done
with `Player_Stop', and we free the module with `Player_Free'.


automatically generated by info2www version 1.2.2.9