This article is translated from orx tutorials sound and music)
,~ Dai Yi ~ Translation. For the latest version, see orx Chinese wiki.
. (Because the format is not easy to read here, it is recommended to go to the official wiki to view it) This article is transferred from ~ Dai Yi ~ Blog
. Original link in: http://blog.csdn.net/v_023/archive/2010/07/06/5717261.aspx
.
Sound and music
(Sound & music) Tutorial
Summary
See the basics of the previous tutorial.
, Object Creation
, Clock
,
Framework hierarchy
,
Animation
, View and camera
.
This tutorial demonstrates how to play sound (sample) and music (Stream ). Like other functions, most of the time, only one line of code is required, and everything is data-driven.
This tutorial also demonstrates how to use soldier images as visual feedback to display real-time changed sound settings.
When you press the up/down arrow, the sound changes accordingly. The soldiers also change.
By clicking the left and right keys, the music tone (audio) changes accordingly. The soldiers will rotate like the knob on the radio.
The left control key will play the music when the music is paused (the soldiers are activated at the same time) and pause the music (and stop the activity of soldiers) when the music is paused, press enter and space to play sound effects on soldiers.
Result.
The effect of triggering a sound by space is the same as that of pressing a carriage return. The only difference is that the volume and tone controlled by the Space key are randomly defined in the default configuration file.
The random frequency of this configuration control allows a simple step or a slightly different hit sound without redundant code. We can freely change the colors of soldiers to illustrate this. Sound effect, only added and displayed in the effective soldier Angle
Color.
If you want to display a sound effect without the support of objects, you can create music in the same way as in this tutorial. However, a sound expression on an object requires space sound location (not described in this tutorial ).
Many sound effects can be displayed on a single object at the same time.
The sound Configuration Attribute keepdataincache allows you to keep it in the memory, instead of reading the sound sample from the file every time. This is only applicable to non-streaming data (that is, it is not a music type ).
If it is set to false, the sample will be reloaded from the file, unless another sound of the same type is playing.
We also register a sound event to get the start or stop playing sound. These events are only triggered during actual playback.
Detailed description
Generally, we first load the config
File (configuration file), create a viewport, create a clock (clock), register the update (update) function, and finally create a primary object. Please refer to the previous tutorial
For more information.
Next, create a music object and play it.
Orxsound *
Pstmusic;
Pstmusic =
Orxsound_createfromconfig (
"Music"
)
;
Orxsound_play (
Pstmusic)
;
As we can see, both music and sound belong to the orxsound type. The main difference is that music is a stream, and sound is fully loaded into the memory.
Next, let me see their differences in the configuration files.
Last step of the initialization function: add the audio event response.
Orxevent_addhandler (orxevent_type_sound, eventhandler );
The corresponding code is as follows:
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
Orxsound_event_payload * pstpayload;
Pstpayload = (orxsound_event_payload *) _ pstevent-> pstpayload;
Switch
(_ Pstevent-> EID)
{
Case
Orxsound_event_start:
Orxlog ("sound <
% S
>@<
% S
> Has started! "
, Pstpayload-> zsoundname, orxobject_getname (orxobject (_ pstevent-> hrecipient )));
Break
;
Case
Orxsound_event_stop:
Orxlog ("sound <
% S
>@<
% S
> Has stoped! "
, Pstpayload-> zsoundname, orxobject_getname (orxobject (_ pstevent-> hrecipient )));
Break
;
}
Return
Orxstatus_success;
As you can see, nothing is new.
Now let's look at how to add an audio file to the soldier role.
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
If (orxinput_isactive ("randomsfx") & orxinput_hasnewstatus ("randomsfx "))
{
Orxobject_addsound (pstsoldier, "randombip ");
Orxobject_setcolor (pstsoldier, orxcolor_set (& stcolor, orxconfig_getvector ("randomcolor", & V), orxfloat_1 ));
}
If (orxinput_isactive ("defaultsfx") & orxinput_hasnewstatus ("defaultsfx "))
{
Orxobject_addsound (pstsoldier, "defaultbip ");
Orxobject_setcolor (pstsoldier, orxcolor_set (& stcolor, & orxvector_white, orxfloat_1 ));
}
What we can see is that adding an audio to a soldier role requires only one line of code, and more importantly, random and fixed audios do the same. We will introduce their differences in the configuration files later.
When we add a randombip audio, we randomly change the soldier color through the key-randomcolor defined in the configuration file. When playing defaultbip, we can
Simply change the color back to white.
Note: A sound will be played every time a corresponding input is input.
So far, we only care about whether an input is activated. Now, we need to perform some operations when the input is activated.
For this reason, we useorxInput_HasNewStatus()
Function, which will be returned when the input state changesorxTRUE
. (Ratio
From inactive to inactive)
Combined with orxinput_isactive (), we can ensure that when we only play the sound, the obtained input is from inactive to active.
Now, let's demonstrate it together.
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
If
(Orxinput_isactive ("togglemusic"
) & Orxinput_hasnewstatus ("togglemusic"
))
{
If
(Orxsound_getstatus (pstmusic )! = Orxsound_status_play)
{
Orxsound_play (pstmusic );
Orxobject_enable (pstsoldier, orxtrue );
}
Else
{
Orxsound_pause (pstmusic );
Orxobject_enable (pstsoldier, orxfalse );
}
}
With this simple code, we can see that when togglemusic input is activated, it starts playing music and activates soldiers in non-playing status. The player stops playing the music and does not activate it.
Soldiers.
Now, let's change the pitch.
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
If (orxinput_isactive ("pitchup "))
{
Orxsound_setpitch (pstmusic, orxsound_getpitch (pstmusic) + orx2f (0.01f ));
Orxobject_setrotation (pstsoldier, orxobject_getrotation (pstsoldier) + orx2f (4.0f) * _ pstclockinfo-> FDT );
}
Nothing special, so is the parameter changed to pitchdown.
Finally, let's change the volume.
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
If (orxinput_isactive ("volumedown "))
{
Orxsound_setvolume (pstmusic, orxsound_getvolume (pstmusic)-orx2f (0.05f ));
Orxobject_setscale (pstsoldier, orxvector_mulf (& V, orxobject_getscale (pstsoldier, & V), orx2f (0.98f )));
}
Similar to changing pitch, there is nothing special about it.
Note: we can see that only the rotation time of our object is consistent (see the clock tutorial
(Clock tutorial)
).
The pitch and volume of music, including the object scaling, will all be frame-related (framerate-dependent), which is a bad thing.
To solve this problem, we only need to use the clock's DT 1)
Confirm the parameters. 2)
We already know the code section. Now let's look at the data section.
First, define the music.
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
[Music]
Music =
.../../Data/sound/gbloop.ogg
Loop =
True
Easy! If we do not explicitly define loop = true, the music will not be played cyclically.
Now let's take a look at defaultbip.
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
[Defaultbip]
Sound =
../Data/sound/bip.wav
Keepincache =
True;
Pitch =
1.0
Volume =
1.0
As before, the keepincache attribute ensures that the audio will never be automatically detached from the memory.
The sound height and volume are clearly defined as not the actual default values.
Finally, let's take a look at our randombip.
E:/myprogram/clipboardhighlighterversion0.2/untitled.html
[Randombip @ defaultbip]
Pitch =
0.1 ~ 3.0
Volume =
0.5 ~ 3.0
We can see that randombip inherits from defaultbip. This means that if we change the defaultbip sample, it may also change the randombip.
We only need to change the pitch (frequency) and random volume value. This means that each playback
Randombip, which has different frequencies and quantities, and does not need to change the code. You only need to change the configuration!
Source code: 06_sound.c
Configuration File: 06_sound.ini
2)
Note: change it to time-related