Attaching Multiple Samples to an Instrument


Q: I tried attaching all the samples I load to one instrument (instead of allocating an instrument for each one), and then calling SetAudioItemInfo() with the AF_ATTF_NOAUTOSTART tag, and it fixed my problem. The only drawback is that now I cannot play multiple sound effects at the same time. There are a couple of places in the game where this happens.

A: Consider a compromise between the previous and your current techniques. Allocate three or four instruments, keep track of which ones are in use at any given moment, and when you want to play a sound attach the sample to the instrument and mark that instrument as in use. When the sample is done (CUE_AT_END), detach the sample from the instrument and mark it as free.

This sounds like a lot of work, but actually, it is all done for you. If you assign each sound effect as a MIDI note in a PIMap file (see Playing MIDI Scores), you can create and delete these sounds very efficiently with StartScoreNote() and StopScoreNote(). A setting in the PIMap file allows you to control the maximum number of allocated sample players, and they are automatically reused when a sample stops playing. See the example file tsc_soundfx.c.