CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
Level meter

Description of the sample

Overview

This sample script obtains playback volume in real time.
It then visualizes that volume by changing the size of the cube.

cri4u_samples_criatom_script05_main.png

Operations

No operations are available.

Scene information


Middleware CRI ADX (CRI Atom)
Sample Script sample: Level meter
Location /CRIWARE/SDK/unity/samples/UnityProject/Assets/Scenes/criatom/script/Unity3Project/Assets/ScriptSample05_LevelMeter
Scene file ScriptSample05_scene_LevelMeter.unity
Original ADX data Data: Simple cross-fading of music


Description of the program

This sample script obtains the sound playback volume in real time.
The sizes of the two cubes are changed based on the volume of the two stereo channels.
The music that is played is added on the "LevelMeterCube L" cube as a CriAtomSource. Since the "Play On Start" option is selected, playback is started immediately when the scene is executed.

Volume acquisition script

You can get the volume of each DSP bus output of ADX instead of the volume of each Cue (sound).
This sample obtains the volume of DSP bus 0.
DSP bus 0 is the master bus, from which all sounds are finally output.
The Debug.Log function is also used to output the volume value to the console at all times.

Script file Scripts/LevelMeter.cs
using System.Collections;
public class LevelMeter : MonoBehaviour {
public int monitoredChannelId = 0;
private int analyzedDspBusId = 0;
private float objScaleBaseVal = 2.0f;
/* This "Start()" method is called before "Update()".*/
void Start () {
CriAtom.AttachDspBusSetting("DspBusSetting_0");
/* Set Bus Analayzer to use "BusAnalyzeInfo" */
CriAtom.SetBusAnalyzer(true);
}
/* Update the local scale value of GameObject */
void Update(){
/* Get BusAnalyzerInfo from a DSP Buss verifyed by mDspBusId*/
CriAtomExAsr.BusAnalyzerInfo lBusInfo = CriAtom.GetBusAnalyzerInfo(analyzedDspBusId);
/* calculate new value of GameObject scale */
float lObjScaleNewVal = 0.1f + objScaleBaseVal * lBusInfo.peakLevels[monitoredChannelId];
Debug.Log("Channel_" + monitoredChannelId + " : " + lBusInfo.peakLevels[monitoredChannelId]);
/* update local scale of 'this' game object */
this.transform.localScale = new Vector3(lObjScaleNewVal, lObjScaleNewVal, lObjScaleNewVal);
}
}

For a project where a comprehensive management script, such as SoundManager is available, you need to configure CriAtom.AttachDspBusSetting and CriAtom.SetBusAnalyzer in the Start function of the script only once.
To get the volume, the CriAtom.GetBusAnalyzerInfo function is used to first obtain the bus information.
Its argument is the number of the DSP bus. Here the master bus, 0, is specified.
The volume is stored in the peakLevels[] array in the bus information, CriAtomExAsr.BusAnalyzerInfo.
peakLevels[0] is the left stereo channel, while peakLevels[1] is the right stereo channel.

Bus volume value

There are several types of level values that can be obtained from the bus information, such as the RMS level, peak level, and peak hold level.
The values stored in peakLevels[] are floating point values greater or equal to 0.
The audio volume may be 0.1 or less, which is not very large.
To provide a level meter in a game, or to use the volume value as a variable, scale the value up or down based on the volume of the sound to use.
[Note]
Each level value is the scaling factor for the amplitude of the waveform data (the unit is not decibels).
You can use the following formula to convert the value into decibels.
dB = 10.0f * log10f(level);


DSP bus number

The number for the DSP bus output specified in CriAtom.GetBusAnalyzerInfo corresponds to the bus number in the DSP bus setting information within the CRI Atom Craft sound tool.

cri4u_samples_criatom_script05_dsp_setting.png

DSP bus 0 is the master bus, which is the final output stage. Usually, all the sounds are output here.

To obtain the volume of a specific Cue

The volume of DSP bus 0 is the volume of the sound where music, sound effects, and voice lines are all mixed.
To obtain the volume of the music or the voice lines, configure the DSP settings so that this data is output to other DSP buses than bus 0.
For example, configure CRI Atom Craft so that the Cues for which you want to know the volume are output to both bus sends 0 and 5. Then, check the volume of DSP bus 5 in the game.

cri4u_samples_criatom_script05_dsp_5.png

The change in volume of the DSP bus can be checked in the DSP bus setting window even during playback in CRI Atom Craft.

cri4u_samples_criatom_script05_dsp_level.png

If you cannot get the volume other than from DSP bus 0

If you cannot get the volume other than from DSP bus 0 in your application, check that the DSP bus setting and the bus analysis setting are configured in the script.
Check the following code. In this sample, it is run in the Start function in LevelMeter.cs.
The name of the DSP bus setting, "DspBusSetting_0", can also be set in the tool. Check that the names are the same on both sides (tool and script).

CriAtom.AttachDspBusSetting("DspBusSetting_0");
CriAtom.SetBusAnalyzer(true);