VB.NET implementation DirectSound9 (8) sound controller

xiaoxiao2021-03-06  20

Keywords: DirectSound 9 VB.NET DirectX sound engine sound control Author: Dong Jun containing

Reprint, please indicate from http://blog.9cbs.net/a11s

OBJECTIVE: To make a sound controller, reduce memory saving, encapsulation DirectSound, low CPU consumption, and meet the actual needs of people, and ease of use.

I didn't plan to continue, I have studied the relevant issues in the past few days. Discuss that too much sound effects on the machine, especially memory, and release. It is necessary to make a controller specially managed.

Since it is different from DENGHE, it is realized. He has to realize the GameObject interface. For games, this is feasible. But I am doing a controller's purpose is not just used for games. But more like It is a service that provides a sound. I still don't dare to call it as a sound engine. After all, the features that can be implemented are still relatively small.

Problem

1 When you intend to play the sound effect (that is, WAV), you can get buffer (filename, ....), we all know. But after New, the entire WAV will be loaded into memory. We call it static Into. When you attack the enemy, such as the sound effect of the bullet or the sound of the sword. Many are repeated. At this time, you can use the buffer you just came out directly to PLAY.

2 If I am so simple, I don't have to write this article. When a voice is not played, another repetitive sound is loud, it is obvious that I can't directly play, then what do you do? First Stop it then re-start Play? Obviously, no. So, then there is one more. Of course, you can use two buffer to play simultaneously unless you do your own mix (requires a lot of acoustic knowledge, and mathematics). .

3 seems to solve problems. We continue to promote problems to multiple sound effects (WAV), as well as a lot of repeated playback. Solution to problem 2 New large amount of buffer will have problems. Not only is it very complicated, And he will consume horrible memory. And your system resource overhead is very considerable. It seems that this phenomenon is easy to appear. So it is urgent to manage a management method to deal with this phenomenon.

Different from me, Denghe provides a SoundPool program, which is the scripting engine that relying on the game. Manage the load and uninstall of the WAV file. Each valid WAV creates multiple backups when you load each scene, you can To establish a number of buffer buffers for each WAV. For example, set up one, then automatically point the next one. Then there is four cycles. For short sound effects, you can fully meet the needs of human hearing. So The game can fundamentally solve the situation of unlimited waste of memory, and each sound is not interfered with each other. As long as there is a reliable script engine to load and release it. The total space required is the total occupancy volume of the file. The number of Buffers of each file * The size of the file is actually interacting with the user, so there is no way to expect the actual frequency of certain sound effects. Do the worst plan, the actual memory = number of WAV files * Buffer Number. For programs that are smaller in the required number of sound values, it can meet the requirements. But some larger scenes may pay the consideration. But this program is a better solution for the game.

4 My solution:

4.1 Establishing Layer: In my opinion, the sound can be hierarchically. For example, a fighting game. Player1 is said, can be placed in Layer1. PLAYER says, can be placed in Layer2, and the sound is effective - the sound of those 噼 啦Placed in Layer3, "Ready Go" when the scene begins, and "ko" can be placed in Layer4

4.2 Establishing a Layer Buffer: After classification, you will find that if the mutual impact between Layer is not suitable, but the inside of Layer, when people talk, if there is two sentences, if there is two sentences, it is a bit unnatural ?? Or when fighting, the sounds of those are too common, and it is relatively short. When people are hot, they will not care if they have already played. The factors that have been placed in one of the same Layer is in line with practical needs. of.

4.3 Establishing steps:

In order to accommodate the speed, it is not possible to consider the replay, load from the hard disk. So it will be loaded into memory. When playing, only one buffer is only a buffer, so you need to create a copy of the copy. Copy. Management can be handed over to the controller. For example, the Play method .play (SoundKey, Targetlayer) performance analysis: Mainly memory usage. The required memory = the number of each file and Layer * Number of buffers in each Layer

These are controllable. Of course, the bigger the buffer, the better the effect, and Layer is often determined according to the actual needs, so the memory usage can be stable in a value, which is fortunate is that this value is controllable. , Can even change the amount of buffer according to the computer's memory capacity (huh, generally not thinking about it)

5 Key Technical Issues: It seems that the above ideas are good, but to implement, the following issues need to consider

5.1 How to Manage All Buffer? We may have rarely consider this problem for people who are used to object-oriented, you can use any advanced means, even think of Dataset .... is not unable to be, but I am more classic, using a buffer The two-dimensional array is already enough. I have wanted to use Arrylist, but I can't think of how much the actual advantage would be brought to me than the array.

5.2 How to determine which buffer play? Since Buffer is an array or other form, according to the division of Layer, it is from Layer. I only for two-dimensional array, buffer [tarlayer] [tarbuffer] just fill two parameters of the two-dimensional array .

5.3 Since it is a recycling, how to specify the right buffer?

This problem is more complicated, I encapsulated inside the controller, specify a suitable buffer with an enumeration method, and it is the step.

Function Enumbuffer (TARLAYER AS INTEGER) AS INTEGER passes the use of the Layer, return to the appropriate bufferid

a. Cycle Check whether there is an idle buffer if there is a Buffer for Nothing or Null, there is no doubt that he will be the most suitable buffer, so directly exit the loop back to his ID.

b. If you don't satisfy A, then see if there is DISPOSED or the buffer that has already stood, they are no longer played. It is also suitable. After finding one, there is no need to continue loop, directly return to this ID.

c. If you don't satisfy B, it is traversed by the current Layer's entire buffer, find the longer play time. Playing time means the longest time. We can ask him to rest, return this ID.

d. If there is multiple of C, then follow the first returns, or the last one returns.

5.4 What should we pay attention to when playing?

In order to save space, certain, you have to empty the buffer just enumerated, and then create a copy of a Wavbuffer list to play.

Since it is not possible to get information from the hard disk, you can only consider the method of using buffer.clone (), or directly New Buffer (using Wavbuffer Stream)

6 specific structural description

Class: XSoundList is a FileStream's HashTable, providing a keyword, a filename, creating a filestream .. Getting the role of the memory, set the original information here, convenient to read

Class: Xbgmlist is similar to XSoundList, but here is used to store larger files, do not use static loading, provide a variety of formats. Used for background music (do you plan to load CD tracks or a few minutes of WAV to memory? Too crazy )

Category: XSoundManex controller. Which one and internal buffers are played in a specific tube, the volume of volume, etc. is also a package class for DirectSound, using it to manage SoundBuffer in Layer's ideology

Class: XSoundman standard controller, because XSoundManex uses too many DirectSound features, does not meet the idea of ​​OOP package, so this will be used to replace XSoundManex, unless you intend to re-encapsulate the original EX.

Class: XSoundSimp Simple Controller. As long as there is a Layer enough, the super simple controller, the first step is loaded, the second step is gone, have rewritten destructor, do not worry about leak, even new XSoundSIMP each time It is also more than you save memory directly, and you don't have to perform DirectSound initialization 7 additional features

The adjustment of sound volume, background music and SoundBuffler separation process, dynamically load WAV (although it is not recommended to do this). Loop playback. Wait, if not enough can continue to encapsulate from XSoundmanex

8 Know the deficiencies of existence and the plan of the next version.

The volume between Layer is not independent, this is contrary to the mutual interference between Layer.

BGM did not distinguish Layer. Due to design, it was decided that BGM is not significant. So set BGM only one layer. The middle use is also competitive. Although it can be used in this regard (no relationship, dynamic Loading, memory consumption is not very large). However, for users who don't understand the mechanisms, it will feel incredible. And Layer is competitive, once the Buffer maximum can affect the background music. For example, some sound effects are too large. The programmer adds it as BGM, which seems to save, but there is a hidden danger. Unless you can ensure that the enumeration condition B is met.

9 Program Demo and Source Code Introduction

The first simple application, pressing the keyboard button will make a sound effect, you can be like playing the piano. (Do you want to do soft wave table?)

The second demo is the use of text-driven sounds, and "recording"

The third demo is the frequency of repetition of the frequency and the impact of the CPU (test performance), including the background music play, and the use of Layer

The fourth is hidden by me, I originally made a game. Time is not a lot, no completion, interested, can continue to improve

10 Download the address is temporarily unable to provide, because I can't find a place for a long time to store my code, about 800K, and 9CBS blog temporarily does not provide RAR upload. If necessary, please add QQ group: 589626 C # game development, group There is RAR download in sharing

============== End =================

转载请注明原文地址:https://www.9cbs.com/read-49878.html

New Post(0)