返回介绍

AudioSettings.dspTime 运行用时

发布于 2019-12-18 15:37:23 字数 4761 浏览 1439 评论 0 收藏 0

JavaScript => public static var dspTime: double;
C# => public static double dspTime;

Description 描述

Returns the current time of the audio system.

返回音频系统的运行时间。

This is a value specified in seconds and based on the actual number of samples the audio system processes and is therefore much more precise than the time obtained via the Time.time property.

这个值以秒为单位,根据的音频系统中音频播放的的实际的值得出,因此比由Time.time属性所获得的时间更精确。

JavaScript:

// The code example shows how to implement a metronome that procedurally generates the click sounds via the OnAudioFilterRead callback.
//这段代码展示了,如何通过OnAudioFilterRead回调播放“咔嗒声”来实现一个节拍器。
// While the game is paused or the suspended, this time will not be updated and sounds playing will be paused. Therefore developers of music scheduling routines do not have to do any rescheduling after the app is unpaused 
//当这个游戏被暂停或着挂起时,这个时间点(PS:暂停的位置)不会被刷新并且声音播放将会暂停。因此在暂停终止时(再次开启)音乐开发者不需要做任何的事情来重新安排。
@script RequireComponent(AudioSource)
 
public var bpm : double = 140.0;
public var gain : float = 0.5f;
public var signatureHi : int = 4;
public var signatureLo : int = 4;
 
private var nextTick : double = 0.0;
private var amp : float = 0.0f;
private var phase : float = 0.0f;
private var sampleRate : double = 0.0;
private var accent : int;
private var running : boolean = false;
 
function Start ()
{
    accent = signatureHi;
    var startTick = AudioSettings.dspTime;
    sampleRate = AudioSettings.outputSampleRate;
    nextTick = startTick * sampleRate;
    running = true;
}
 
function OnAudioFilterRead(data:float[], channels:int)
{
    if(!running)
        return;
    var samplesPerTick = sampleRate * (60.0f / bpm) * (4.0 / signatureLo);
    var sample = AudioSettings.dspTime * sampleRate;
    var dataLen = data.length / channels;
    for(var n = 0; n < dataLen; n++)
    {
        var x : float = gain * amp * Mathf.Sin(phase);
        for(var i = 0; i < channels; i++)
            data[n * channels + i] += x;
        while (sample + n >= nextTick)
        {
            nextTick += samplesPerTick;
            amp = 1.0;
            if(++accent > signatureHi)
            {
                accent = 1;
                amp *= 2.0;
            }
            Debug.Log("Tick: " + accent + "/" + signatureHi);
        }
        phase += amp * 0.3;
        amp *= 0.993;
    }
}

C#:

using UnityEngine;
using System.Collections;
 
// The code example shows how to implement a metronome that procedurally generates the click sounds via the OnAudioFilterRead callback.
// While the game is paused or the suspended, this time will not be updated and sounds playing will be paused. Therefore developers of music scheduling routines do not have to do any rescheduling after the app is unpaused 
 
[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour
{
	public double bpm = 140.0F;
	public float gain = 0.5F;
	public int signatureHi = 4;
	public int signatureLo = 4;
	private double nextTick = 0.0F;
	private float amp = 0.0F;
	private float phase = 0.0F;
	private double sampleRate = 0.0F;
	private int accent;
	private bool running = false;
	void Start()
	{
		accent = signatureHi;
		double startTick = AudioSettings.dspTime;
		sampleRate = AudioSettings.outputSampleRate;
		nextTick = startTick * sampleRate;
		running = true;
	}
	void OnAudioFilterRead(float[] data, int channels)
	{
		if (!running)
			return;
 
		double samplesPerTick = sampleRate * 60.0F / bpm * 4.0F / signatureLo;
		double sample = AudioSettings.dspTime * sampleRate;
		int dataLen = data.Length / channels;
		int n = 0;
		while (n < dataLen)
		{
			float x = gain * amp * Mathf.Sin(phase);
			int i = 0;
			while (i < channels)
			{
				data[n * channels + i] += x;
				i++;
			}
			while (sample + n >= nextTick)
			{
				nextTick += samplesPerTick;
				amp = 1.0F;
				if (++accent > signatureHi)
				{
					accent = 1;
					amp *= 2.0F;
				}
				Debug.Log("Tick: " + accent + "/" + signatureHi);
			}
			phase += amp * 0.3F;
			amp *= 0.993F;
			n++;
		}
	}
}

AudioSettings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文