2017-04-04 86 views
0

我正在爲我的鼓課程製作應用程序並使其跨平臺我選擇了Urho.Sharp,因爲它具有低級別的聲音API以及豐富的圖形功能。這裏描述BufferedSoundStream無法播放wav文件

第一步我做一個節拍器應用程序,爲我與BufferedSoundStream工作在這裏加入音頻,然後在需要的沉默,:https://github.com/xamarin/urho-samples/blob/master/FeatureSamples/Core/29_SoundSynthesis/SoundSynthesis.cs

但由此產生的聲音是不是聲音就像隨機位進入緩衝流一樣。

這是我的代碼:

/// 
/// this code initialize sound subsystem 
/// 
void CreateSound() 
{ 
    // Sound source needs a node so that it is considered enabled 
    node = new Node(); 
    SoundSource source = node.CreateComponent<SoundSource>(); 

    soundStream = new BufferedSoundStream(); 
    // Set format: 44100 Hz, sixteen bit, stereo 
    soundStream.SetFormat(44100, true, true); 

    // Start playback. We don't have data in the stream yet, but the 
    SoundSource will wait until there is data, 
    // as the stream is by default in the "don't stop at end" mode 
    source.Play(soundStream); 
} 

/// 
/// this code preload all sound resources 
/// 
readonly Dictionary<PointSoundType, string> SoundsMapping = new Dictionary<PointSoundType, string> 
{ 
    {PointSoundType.beat, "wav/beat.wav"},    
    {PointSoundType.click, "wav/click.wav"}, 
    {PointSoundType.click_accent, "wav/click_accent.wav"}, 
    {PointSoundType.crash, "wav/crash.wav"}, 
    {PointSoundType.foot_hh, "wav/foot_hh.wav"}, 
    {PointSoundType.hh, "wav/hh.wav"}, 
    {PointSoundType.open_hh, "wav/open_hh.wav"}, 
    {PointSoundType.ride, "wav/ride.wav"}, 
    {PointSoundType.snare, "wav/snare.wav"}, 
    {PointSoundType.tom_1, "wav/tom_1.wav"}, 
    {PointSoundType.tom_2, "wav/tom_2.wav"}, 
}; 

Dictionary<PointSoundType, Sound> SoundCache = new Dictionary<PointSoundType, Sound>(); 

private void LoadSoundResources() 
{ 
    // preload all sounds 
    foreach (var s in SoundsMapping) 
    { 
     SoundCache[s.Key] = ResourceCache.GetSound(s.Value); 
     Debug.WriteLine("resource loaded: " + s.Value + ", length = " + SoundCache[s.Key].Length); 
    } 
} 

/// 
/// this code fill up the stream with audio 
/// 
private void UpdateSound() 
{ 
    // Try to keep 1/10 seconds of sound in the buffer, to avoid both dropouts and unnecessary latency 
    //float targetLength = 1.0f/10.0f; 

    // temporary increase buffer to 1s 
    float targetLength = 1.0f; 

    float requiredLength = targetLength - soundStream.BufferLength; 
    if (requiredLength < 0.0f) 
     return; 

    uint numSamples = (uint)(soundStream.Frequency * requiredLength); 

    // check if stream is still full 
    if (numSamples == 0) 
     return; 

    var silencePause = new short[44100]; 

    // iterate and play all sounds 
    SoundCache.All(s => 
    { 
     soundStream.AddData(s.Value.Handle, s.Value.DataSize); 

     // add silencio 
     soundStream.AddData(silencePause, 0, silencePause.Length); 

     return true; 
    }); 
} 

回答

0

確保您的WAV文件是在資源緩存。然後不要播放BufferedSoundStream,而是播放Urho.Audio.Sound聲音。這只是Urho.Audio.SoundSource.Play()方法的不同重寫,但它起作用。

int PlaySound(string sSound) 
    { 
     var cache = Application.Current.ResourceCache; 
     Urho.Audio.Sound sound = cache.GetSound(sSound); 
     if (sound != null) 
     { 
      Node soundNode = scene.CreateChild("Sound"); 
      Urho.Audio.SoundSource soundSource = soundNode.CreateComponent<Urho.Audio.SoundSource>(); 
      soundSource.Play(sound); 
      soundSource.Gain = 0.99f; 
      return 1; 
     } 
     return 0; 
    } 

由於您使用urhosamples,您可以從覆蓋更新這樣的事情開始每個鼓樣本:

public float fRun = 0.0f; 
public int iRet = 0;   // keep counting the played sounds 
public override void OnUpdate(float timeStep) 
{ 
    fRun = fRun + timeStep; 
    int iMS = (int)(10f * fRun); // tenth of seconds 
    if (iMS == 100) iRet = iRet + PlaySound("wav/hh.wav"); 
    if (iMS == 120) iRet = iRet + PlaySound("wav/hh.wav"); 
    if (iMS == 140) iRet = iRet + PlaySound("wav/hh.wav"); 
    if (iMS == 160) iRet = iRet + PlaySound("wav/open_hh.wav"); 
    if (iMS >= 160) fRun = 0.8f; 
} 

祝你好運!