TerminateByTime

A number of options are available for handling multiple voices in hardware or software devices. Many sound cards maintain their own secondary buffers and handle 3-D effects and mixing for them. These hardware buffers, as they are called, usually reside in system memory rather than on the card itself, but because they are mixed by the device, they make far smaller demands on the system processor than do software buffers. It is therefore most efficient to have as many buffers as possible allocated to hardware, especially 3-D buffers.

By default, Microsoft DirectSound allocates buffers to hardware whenever it can. However, it can create only as many hardware buffers as the device can play at one time. The number of hardware buffers is therefore limited by the hardware voices available. DirectSound allocates a hardware voice when a buffer is created and releases it only when the buffer is destroyed; the voice is not free even when the buffer is not playing. If an application creates many buffers, chances are that some of them will end up being in software that is, they will be managed and mixed by the CPU rather than the sound card.

To defer the allocation of resources for hardware mixing and 3-D effects to the moment when the buffer is played, set the DeferLocation property in the BufferDescription object passed to the Buffer(BufferDescription,Device) buffer creation method. When you call SecondaryBuffer.Play or SecondaryBuffer.AcquireResources on the buffer, DirectSound places the buffer in hardware if possible, and in software otherwise.

When calling Play, you can attempt to free a hardware voice for the buffer by passing one of the following BufferPlayFlags, or one of the first two flags in combination with the third. DirectSound then searches for a playing buffer that is suitable for termination as specified by the flags.

Flag Action
TerminateByTime Select the buffer that has been playing longer than any other candidate buffers.
TerminateByDistance Select the 3-D candidate buffer farthest from the listener.
TerminateByPriority Select the buffer that has the lowest priority of candidate buffers, as set in the call to Play. If this is combined with one of the other two flags, the other flag is used only to resolve ties.

If it finds a suitable buffer, DirectSound stops it and allocates the voice it was using to the newly played buffer. If no buffer is suitable for termination, the newly played buffer is played in software, unless the LocateInHardware flag was specified, in which case the call to Play fails.

For more information on how deferred buffers are dealt with when played, see Buffer.Play.

Note: Do not use the StaticBuffer property in combination with DeferLocation when creating buffers. When StaticBuffer is specified, buffers are placed in on-card memory when this is available, as is the case for some Peripheral Component Interconnect (PCI) cards. If you create such a buffer with the DeferLocation property, the buffer memory is not copied to the sound card until the buffer is played for the first time, which can result in unacceptable delays. For more information, see Hardware Acceleration on ISA and PCI Cards.