GNU Info

Info Node: (mikmod.info)More Sound Effects

(mikmod.info)More Sound Effects


Prev: Playing Sound Effects Up: Tutorial
Enter node , (file) or (file)node

More Sound Effects
==================

   Sound effects have some attributes that can be affected to control
the playback.  These are speed, panning, and volume. Given a voice
number, you can affect these attributes with the `Voice_SetFrequency',
`Voice_SetPanning' and `Voice_SetVolume' functions.

   In the previous example, we'll replace the actual sound code,
located between the calls to `MikMod_EnableOutput' and
`MikMod_DisableOutput', with the following code:

         Sample_Play(sfx1, 0, 0);
         for(i = 0; i < 5; i++) {
             MikMod_Update();
             usleep(100000);
         }
         v2 = Sample_Play(sfx2, 0, SFX_CRITICAL);
         i = 0;
         do {
             MikMod_Update();
             usleep(100000);
             v1 = Sample_Play(sfx1, 0, 0);
             Voice_SetVolume(v1, 160);
             Voice_SetFrequency(v1, (sfx1->speed * (100 + i)) / 100);
             Voice_SetPanning(v2, (i++ & 1) ? PAN_LEFT : PAN_RIGHT);
         } while (!Voice_Stopped(v2));

   The first thing you'll notice, is the `SFX_CRITICAL' flag used to
play the second sample. Since the `do' loop will add another sample
every 100 milliseconds, and we reserved only two voices, the oldest
voice will be cut each time this is necessary. Doing this would cut the
second sample in the second iteration of the loop. However, since we
flagged this sound as "critical", it won't be cut until it is finished
or we stop it with a `Voice_Stop' call. So the second sample will play
fine, whereas the first sample will be stopped every loop iteration.

   Then, we choose to play the first sample a bit lower, with
`Voice_SetVolume'. Volume voices range from 0 (silence) to 256. In this
case we play the sample at 160. To make the sound look weird, we also
change its frequency with `Voice_SetFrequency'. The computation in the
example code makes the frequency more and more high (starting from the
sample frequency and then increasing from 1% each iteration).

   And to demonstrate the `Voice_SetPanning' function, we change the
panning of the second sample at each iteration from the left to the
right. The argument can be one of the standard panning `PAN_LEFT',
`PAN_RIGHT', `PAN_CENTER' and `PAN_SURROUND'(1), or a numeric value
between 0 (`PAN_LEFT') and 255 (`PAN_RIGHT').

   ---------- Footnotes ----------

   (1) `PAN_SURROUND' will be mapped to `PAN_CENTER' if the library is
initialized without surround sound, that is, if the variable `md_mode'
doesn't have the bit `DMODE_SURROUND' set.


automatically generated by info2www version 1.2.2.9