Real-time sound mixing using directsound Programming

Source: Internet
Author: User
You only need to call the following functions.

Playsound ("test.wav", null, snd_filename | snd_async );

This is simple. In fact, we can see that domestic games can be basically done using playsound. However, since it is simple, it will be limited in terms of functions. If you encounter complicated scenarios, you will not be able to use playsound. For example, in the scenario, there will be both the sound of opening the door and the sound of cutting people, if you use playsound, you won't be able to hear two types of sound at the same time. You can only hear one sound and then another. Then, you need to mix it.

In network video conferencing development, if different clients speak at the same time, how can we transmit the voice data from multiple endpoints to a certain endpoint through the network and output the data through the wave device of this endpoint, multiple voices can be heard at the same time to achieve multi-party voice conversations in the Local Area Network. This also requires sound mixing technology.

To achieve voice conversation on the network, with special emphasis on real-time performance, we should try to ensure smooth and continuous voice. Therefore, in order to ensure the continuity of voice data and reduce the delay caused by the storage of voice data, in the specific implementation, the recording and playback of voice are not in the form of files, and the recorded and played voice data are in the buffer zone. In Windows systems, generally, High-Level Wave interface functions cannot directly play the voice data in the buffer zone, but must be implemented using underlying functions. Commonly used is the wave function in Windows APIs. The waveoutwrite function is used to output wave data on the wave device. However, this function does not support simultaneous playback of multiple wave data. To achieve simultaneous playback of multiple wave data, perform necessary preprocessing on the multi-channel wave data in the buffer zone, and then submit it to the wave output device for playback. Implementation Principle 1 is shown.


Figure 1 implementation principle of multi-channel Wave Mixing


The effect of this mixing method is as follows:AlgorithmThere is a lot of relationship, but if we use directsound for sound mixing, it will be much simpler. We only need to pass the content we want to mix to it, and directsound will automatically mix internally. Next we will go to directsound sound mixing programming.

Before learning about how directsound is mixed, let's take a look at how directsound plays a piece of wave audio.

Here is just a brief introduction to the steps for playing a sound.

Step 1: create a device object.

In yourCodeYou can call the directsoundcreat8 function to create an object that supports the idirectsound8 interface. This object usually represents the default playing device. Of course, you can enumerate available devices and pass the guid of the devices to the directsoundcreat8 function.

Note that although directsound is based on COM, you do not need to initialize the com library. These directsound will help you. Of course, if you use DMOs stunt, you have to initialize the com library by yourself. Remember.

Step 2: Create a secondary buffer, also called a backup Buffer

You can use idirectsound8: createsoundbuffer to create a buffer object. This object is mainly used to obtain and process data. This buffer is called a secondary buffer, which is different from the primary buffer, direcsound mixes the sound in several buffer zones into the main buffer zone and then outputs the sound to the sound output device to achieve the sound mixing effect.

Step 3: Obtain PCM Data

Read data from WAV Files or other resources to the buffer.

Step 4: Read data to the buffer zone

You can use idirectsoundbuffer8: Lock. method to prepare a secondary buffer for write operations. Usually this method returns a memory address. For details, see Data copied from your private buffer to this address. Then, call idirectsoundbuffer8: unlock.

Step 5: Play data in the buffer

You can use the idirectsoundbuffer8: Play method to play the audio data in the buffer. You can use idirectsoundbuffer8: Stop to pause the playback data. You can repeatedly stop, play, and audio data, if you create several buffers at the same time, you can play the data at the same time, and the sound will be automatically mixed.
As you can see, directsound sound mixing is very simple. We only need to create several secondary buffers on a device, read the data to the buffer zone, and play the video at the same time, directsound is automatically mixed in the main buffer zone. When several secondary buffers can be played simultaneously, the performance of the hardware device is determined.

In WDM drive mode, audio mixing is performed by the core mixer. Different auxiliary buffers may have different WAV formats (for example, different sampling frequencies). When necessary, the format of the secondary buffer must be converted to the primary buffer or the format of the core mixer.

In VxD drive mode, if your auxiliary buffer uses the same audio format and the hardware audio format matches your audio format, the mixer does not need to be converted. Your applicationProgramYou can create a primary buffer and use idirectsoundbuffer8: setformat to set the hardware output format. Note that only your degree of collaboration must be priority cooperative level. And you must set the primary buffer before creating the secondary buffer. directsound will save your settings.

in WDM mode, primary buffer settings are ineffective because the format of the primary buffer is determined by the kernel mixer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.