- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试统一编写一个准确的节拍器。我知道,已经有一些问题了,但是我找不到解决方案。
我的第一个简单的实现使用了Play()或PlayOneShot()。但不幸的是,它不是很准确。所以我想,我可以用PlayScheduled()和一个缓冲时间来实现它。像这个例子一样:http://www.schmid.dk/talks/2014-05-21-Nordic_Game_Jam/Schmid-2014-05-21-NGJ-140.pdf
但这也不起作用,或者我什么都没听到,或者有时声音被切断了,好像没有播放声音的开头。
为什么预定的声音不播放?
到目前为止,这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class inaccurateMetronome : MonoBehaviour {
public double bpm = 140.0F;
public AudioClip sound0;
public AudioSource audio0;
bool running = false;
bool ticked = false;
public double buff = 0.2d;
double timePerTick;
double nextTick;
double dspTime;
void Start () {
double startTick = AudioSettings.dspTime;
nextTick = startTick + buff;
audio0 = GetComponent<AudioSource>();
audio0.clip = sound0;
}
public void toggleMetronome() {
if (running)
stopMetronome();
else
startMetronome();
}
public void startMetronome() {
if (running) {
Debug.LogError("Metronome: already running");
return;
} else {
running = true;
timePerTick = 60.0f / bpm;
nextTick = AudioSettings.dspTime + buff;
Debug.Log("Metronome started");
}
}
public void stopMetronome() {
if (!running) {
Debug.LogError("Metronome: not yet running");
return;
} else {
running = false;
Debug.Log("Metronome stopped");
}
}
public void setBpm(double bpm){
this.bpm = bpm;
this.timePerTick = 60.0f / bpm;
}
void FixedUpdate() {
dspTime = AudioSettings.dspTime;
if ( running && dspTime + buff >= nextTick) {
ticked = false;
nextTick += timePerTick;
}
else if ( running && !ticked && nextTick >= AudioSettings.dspTime ) {
audio0.PlayOneShot(sound0, 1);
Debug.Log("Tick");
ticked = true;
}
}
}
最佳答案
直接从Unity Documentation使用
如果要播放自己的声音,只需在Debug.Log()
之后播放即可。
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++;
}
}
}
关于c# - Unity:带有PlayScheduled()的准确节拍器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47393234/
我正在尝试使用 Typescript(和 Angular 2)构建一个节拍器。感谢 @Nitzan-Tomer ( Typescript Loop with Delay ),他帮助我完成了基础知识。
作为练习,我正在尝试使用 Thread.sleep 作为计时器并使用 JMF 作为声音来使用 Java 创建一个节拍器。它运行良好,但出于某种原因,JMF 似乎只能以每分钟最多 207 拍的速度播放声
我想用jquery制作一个节拍器,用单击声音并用一种颜色可视化速度。在这一点上,视觉部分工作正常,但声音有问题。 无法使其正常工作,它应每分钟发出哔哔声,其速度是所选速度的多少倍。 这是代码: $(f
我正在尝试通过实现苹果提供的示例代码来创建一个节拍器应用程序。一切正常,但我看到节拍视觉效果出现延迟,它与播放器时间不正确同步。这是苹果提供的示例代码 let secondsPerBeat = 60.
如标题中所述,我正在尝试创建一个基于 jQuery/JavaScript 的节拍器以及 HTML 标签来播放声音。 它工作“没问题”,但在我看来 setInterval方法不够准确。我在这里搜索了一些
我是一名优秀的程序员,十分优秀!