Fmod Quick Start

Source: Internet
Author: User
Tags usleep

Audio is always an indispensable part of audio preparation, whether it is the background audio of an operator or the sound of an atmosphere, when there is no sound, there will be no sense of playing audio. Maybe even a lot of attention may be placed on audio, for example, the speaker can be used to determine the location of the speaker.

From the perspective of the producer, there are at least the following requirements for the speaker:

    1. Audio Acquisition/audio effect cases. Of course, audio and video resources may not be stored on CD audio files, or even stored on the Web Using Streaming (rarely seen, but it is not impossible ).
    2. Background playback. In addition to releasing audio streams, you must make sure the audio streams can be played simultaneously.
    3. Sound mixing. For the hardware, only one speaker can be played at a time. Some audio mixing functions in the hardware, so you can play multiple audio streams at the same time, but there is still a limit on the number of audio streams. When the limit is exceeded, You need to mix all the audio streams being played and then send them to the audio effect card.
      (On Windows, it seems that you can mix audio on your own before sending it to the sound card. So this is not a big problem for Windows developers ?)
    4. Various types of audio effects, such as setting the position of the audio source to the same direction as the distance, or set the speed to model the Dopplar effect, or even various kinds of DSP processing.

What I want to renew today is the fmod sound effect function, which has all the above functions. In addition, it also supports cross-platform failover, in addition to Windows.
MacOS, Linux, and even all next-generation hosts, such as PS3, Xbox 360, and Wii. In addition, it is an ideal sound effect solution for non-commercial purposes.

Let's talk about fmod.

SYSTEM INTRODUCTION

Fmod has three main objects: system, sound, and channel. I only spent one night, so the contents like channelgroup, DSP, and geometry won't go deep...

    • system management is related to the system, for example, you can extract audio albums from the ICP filing system and set the audio card (fmod can control multiple audio cards at the same time), set the DSP buffer size, or merge into other plug-in.
    • sound is a sample, which has been sent to the audio card for broadcasting. There may be three internal sound formats: no audio, audio, or streaming:

      • no memory loss
        : the simplest memory format, directly store all the resources in the memory. during playback, the CPU resources are not required, but the memory is also used. When the length is short, the video will be repeatedly played.
      • strong margin
        : if audio is stored in MP3 or Ogg formats, You can import the information into the memory in a way that has not been resolved, when playing the video, the video is instantly restored. This requires a little more CPU resources, but it can reduce the memory usage and the long-lasting audio in the memory.
      • streaming
        : In streaming mode, the playback and playback operations are performed at the same time, while the recording body only keeps a small part of the same version. Therefore, you almost never take the recording body, however, the missing point is that playback cannot be re-played. You must re-open the case or re-upload the video to play the video again. Combine a long background audio or a neural network audio.
    • A Channel indicates the entity in which the audio is being played. For a channel, you can set its volume, playback time, or position and speed in a 3D space. A channel can only play one sound, but the same sound can be shared by different channels and played at the same time.

A simple audio player is coming soon.

Play audio

At the beginning, we must first generate a system object, which can be converted:

 Fmod_system  *  Sys  ;
Fmod_result R = Fmod_create_system ( & Sys ) ;
If ( R ! = Fmod_ OK ){
Cerr < " Unable to create fmod system: "
< Fmod_errorstring ( R ) < Endl ;
Exit ( 1 ) ;
}


All fmod functions will return fmod_result as a result. We can check whether it is due to fmod_ OK to determine whether there is any issue. As a result, I will omit these keywords in the program below. (You can save it when you start to renew your subscription !)

Next we initialize the system and merge the MP3 audio into it:

 Fmod_system_init  (  Sys  ,  32  , Fmod_init_normal  ,  0  )  ;
Fmod_sound * Sound ;
Fmod_system_createsound ( Sys , " Testbench " , Fmod_default , 0 ,& Sound ) ;


In fmod_system_init, the first parameter is the system object generated previously, and the second 32 indicates that the system can have a maximum of 32 channels, that is, up to 32 audio streams can be played at the same time. The numbers 3 and 4 can be used to set some input selections. In this example, you can use the preset values.

Fmod_system_createsound produces a sound object. The first example is system, and the second example is the audio register name that we want to import. fmod will determine the encoding format and parse it. If you want to set a third parameter number to be used for failover or streaming, we will first use the parameter value.

The last step is to release the audio:

Fmod_channel*Channel;
Fmod_system_playsound(Sys,Fmod_channel_free,Sound,0,&Channel);


Fmod_system_playsound enables a channel to start playing audio. The second channel can be used to specify which channel we want to use for playback. If fmod_channel_free is used, fmod finds an available channel for playback.

However, this is not the end of the program. Do not forget that the audio is played on the background. If this is the end of the program, audio will also be stopped immediately. Because this is only a simple playback program, you only need to enter a loop and other audio streams to finish broadcasting:

 Fmod_bool     Playing  =  1  ;
While ( Playing ){
# Ifdef Win32
Sleep ( 10 ) ;
# Else
Usleep ( 10000 ) ;
# Endif
Fmod_channel_isplaying ( Channel ,& Playing ) ;
Fmod_system_update ( Sys ) ;
}


I use usleep on UNIX, so the program running in the circle will record every 0.01 seconds. Sleep is used on Windows.

Fmod_channel_isplaying can be used to query the channel.
If you have already completed all the audio streams, you can jump out of the loop after the audio is played. Fmod_system_update indicates updating the system, including calling callback.
Or set the 3D location of the audio. In this example, it may not be important. However, if other functions are used, update MUST be called.

After playing the video, use fmod_sound_release and fmod_system_release to release the system resource:

Fmod_sound_release(SND);
Fmod_system_release(Sys);


In this case, this simple playback program is complete!

Add 3D Effects

Next, let's add some 3D effects. First, we need to specify 3D functions when building sound:

   fmod_system_createsound   (  
sys ,
" testbench " ,
fmod_loop_off | fmod_3d | fmod_hardware ,
0 ,
& sound
) ;


This seems to be a bit confusing, but in fact, it only specifies that the audio is not repeatedly played (fmod_loop_off), and adds 3D effects (fmod_3d) and use hardware acceleration (fmod_hardware ). The fmod_default used previously only changes 3D to 2D. The other options are identical.

Next, we can use fmod_channel_set3dattributes to set the channel position and speed in the blank space:

 Fmod_vector     Pos  ;
Pos . X = 0.0 F ;
Pos . Y = 0.0 F ;
Pos . Z = 2.0 F ;
Fmod_channel_set3dattributes ( Channel ,& Pos , Null ) ;


The second cursor is the location. The user configures the cursor to be (0, 0, 0). The cursor is positive Z, the left side is positive X, and the top side is positive y. The third parameter is speed. If you do not need to perform the optimization, you can set it to null.

The above program will place the audio source in two positions in front of the user. Of course, if the source cannot be moved, it will not be interesting:

 While  (  Playing  ){  
# Ifdef Win32
Sleep ( 10 ) ;
# Else
Usleep ( 10000 ) ;
# Endif
Fmod_channel_isplaying ( Channel ,& Playing ) ;

Unsigned Int MSEC ;
Fmod_channel_getposition ( Channel ,& MSEC , Fmod_timeunit_ms ) ;

Float Angle = 3.1415926 F * MSEC / 4000.0 F ;
Pos . X = 2.0 F * Sin ( Angle ) ;
Pos . Z = 2.0 F * Cos ( Angle ) ;
Fmod_channel_set3dattributes ( Channel ,& Pos , Null ) ;
Fmod_system_update ( Sys ) ;
}


In this case, we will allow the video source to draw a circle of user circles. Using fmod_channel_getposition, we can get the current playback time point. This time takes the upper speed (one circle in eight seconds, that is, the radians π triggered by the four-second period), you can get the animation angle, and then set sin/COs to get a new location.

The complete program is stored here: sample. cpp
. This program adds some handler processing and uses the command column metadata for playback. During the preparation, you must wait for a period of time (because the non-linear method is used), and then you should be able to hear the audio in the closed circle of the user.

Conclusion

Fmod
In terms of usage, it is still easy to use. The solution, sound mixing, and hardware control of audio mixing are all started, therefore, developers do not need to take extra effort to handle the problem. The only non-renewal is its c ++
Interface Design is poor, and it does not have the power of C ++ at all. Why do I use C interface?
Cause: the two are not significantly different.

When this feature is enabled, fmod functions are optimized and cross-platform and free-of-charge, which is exactly the same. Of course, many businesses or free-of-charge marketing staff used fmod to play audio.

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.