Copyright (C) 2000-2012 |
GNU Info (mikmod.info)Playing Sound EffectsPlaying Sound Effects ===================== MikMod is not limited to playing modules, it can also play sound effects, that is, module samples. It's a bit more complex than playing a module, because the module player does a lot of things for us, but here we'll get more control over what is actually played by the program. Let's look at an example: /* MikMod Sound Library example program: sound effects */ #include <unistd.h> #include <mikmod.h> main() { int i; /* sound effects */ SAMPLE *sfx1, *sfx2; /* voices */ int v1, v2; /* register all the drivers */ MikMod_RegisterAllDrivers(); /* initialize the library */ md_mode |= DMODE_SOFT_SNDFX; if (MikMod_Init("")) { fprintf(stderr, "Could not initialize sound, reason: %s\n", MikMod_strerror(MikMod_errno)); return; } /* load samples */ sfx1 = Sample_Load("first.wav"); if (!sfx1) { MikMod_Exit(); fprintf(stderr, "Could not load the first sound, reason: %s\n", MikMod_strerror(MikMod_errno)); return; } sfx2 = Sample_Load("second.wav"); if (!sfx2) { Sample_Free(sfx1); MikMod_Exit(); fprintf(stderr, "Could not load the second sound, reason: %s\n", MikMod_strerror(MikMod_errno)); return; } /* reserve 2 voices for sound effects */ MikMod_SetNumVoices(-1, 2); /* get ready to play */ MikMod_EnableOutput(); /* play first sample */ v1 = Sample_Play(sfx1, 0, 0); for(i = 0; i < 5; i++) { MikMod_Update(); usleep(100000); } /* half a second later, play second sample */ v2 = Sample_Play(sfx2, 0, 0); do { MikMod_Update(); usleep(100000); } while (!Voice_Stopped(v2)); MikMod_DisableOutput(); Sample_Free(sfx2); Sample_Free(sfx1); MikMod_Exit(); } As in the previous example, we begin by registering the sound drivers and initializing the library. We also ask for software mixing by modifying the variable `md_mode'. It's time to load our files, with the `Sample_Load' function. Don't forget to test the return value -- it looks ugly here on such a small example, but it's a good practice.... Since we want to play two samples, we have to use at least two voices for this, so we reserve them with a `MikMod_SetNumVoices' call. The first parameter sets the number of module voices, and the second parameter the number of sound effect voices. We don't want to set the number of module voices here (it's part of the module player's duty), so we use the value `-1' to keep the current value, and we reserve two sound effect voices. Now we're ready to play, so we call `MikMod_EnableOutput' to make the driver ready. Sound effects are played by the `Sample_Play' function. You just have to specify which sample you want to play, the offset from which you want to start, and the playback flags. More on this later. The function returns the number of the voice associated to the sample. We play the first sample for half a second, then we start to play the second sample. Since we've reserved two channels, both samples play simultaneously. We use the `Voice_Stopped' function to stop the playback: it returns the current status of the voice argument, which is zero when the sample plays and nonzero when it has finished. So the `do' loop will stop exactly when the second sample is finished, regardless of the length of the first sample. To finish, we get rid of the samples with `Sample_Free'. automatically generated by info2www version 1.2.2.9 |