2017-11-20 1 views
0

정확한 메트로놈을 화합하려고합니다. 이미 몇 가지 질문이 있지만 내 문제에 대한 해결책을 찾을 수는 없습니다. 첫 번째 구현에는 Play() 또는 PlayOneShot()이 사용되었습니다. 그러나 불행히도 그것은 정확하지 않았습니다. 그래서 나는 PlayScheduled()와 버퍼 시간으로 구현할 수 있다고 생각했다. 이 예와 같이 : http://www.schmid.dk/talks/2014-05-21-Nordic_Game_Jam/Schmid-2014-05-21-NGJ-140.pdf 그러나 이것도 작동하지 않으며 전혀 소리가 들리지 않거나 소리가 시작되지 않는 것처럼 소리가 끊기는 경우가 있습니다. 예약 된 사운드가 재생되지 않는 이유는 무엇입니까? 누군가가 나를 도울 수 있다면Unity : PlayScheduled()가있는 정확한 메트로놈이 작동하지 않습니다.

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; 
     } 
    } 

} 

그것은 환상적인 것 :

이 지금까지 내 코드입니다. 감사합니다.

답변

0

에서 직접 사용 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++; 
      } 
     } 
    } 
+0

답장을 보내 주셔서 감사합니다. 당신이 말한 것처럼 내 자신의 사운드를 연주하려했지만 "재생은 메인 스레드에서만 호출 할 수 있습니다."라는 오류 메시지가 나타납니다. 그렇다면 어떻게 OnAudioFilterRead에서 내 사운드를 구현할 수 있습니까? –

관련 문제