簡體   English   中英

單步執行到單步執行時功能會有所不同

[英]Function behaves differently when stepping through to when stepping over

我對使用NAudio的PinkNoiseGenerator的函數進行單元測試時遇到問題。

逐步執行該程序時,createPinkNoiseGeneratorLists函數的行為似乎與預期的差不多—也就是說,ReceiverGeneratorList(請參見下文)填充了PinkNoiseGenerators列表,每個列表均包含一個示例列表,最重要的是,所有示例具有不同的值:

單步執行 單步執行

單步執行功能時會出現問題。 receiveGeneratorList仍填充有PinkNoiseGenerators列表,但樣本的值相同:

跨步 跨步

第二種行為是不需要的。 無論是在測試中還是在程序運行時,我都需要粉紅色噪聲發生器的值不同。 我將行為范圍縮小到以下嵌套的for循環(底部的完整代碼):

for (int j = 0; j < receiverList[i].AmplitudeCurveList.Count; j++)
{
    // Calculate duration of pink noise in seconds

    double time = receiverList[i].AmplitudeCurveList[j].Count / (double)sampleRate;

    // Instantiate new generator (list of pink noise samples) at given duration

    PinkNoiseGenerator generator = new PinkNoiseGenerator(time, sampleRate, outputFileName, numberOfChannels);

    // Add generator to generator list which will then be added to the list of lists of pink noise generators,

    generatorList.Add(generator);
}

我看不到代碼立即有任何問題-這可能是NAudio的計時問題嗎? 我用谷歌搜索了這個問題,這似乎很少見,但是經歷過類似情況的人有時會得到與多線程有關的響應-我本人並沒有使用任何多線程,但可以肯定的是,NAudio可能會在“幕后”進行。

我也通過側欄中的“相似問題”進行了研究,它們大多不相似。 這個問題的最高答案暗示着一個播種問題-我認為類似的事情可能是我的問題的原因,並且理解了這個概念,但是不確定如何將其應用於我的特定問題-我的問題可能與粉紅色的方式有關噪音播種了嗎? 我真的不確定如何調試這樣的錯誤。

我的系統正在運行:

Windows 7 64位

VS2013 v12.0.21005.1

NET v4.6.01055


下面是完整的測試/功能代碼,如果它可以幫助解決問題,請執行以下操作:

這是(節略的)測試類:

[TestMethod]
public void testPinkNoise()
{
    string outputFileName = @"C:\Users\Mick\Test\\Output\Test.wav";
    int numberOfChannels = 1;
    int sampleRate = 1000;

    // Act

    // Problem with identical PinkNoiseGenerators occurs here -> timing issue with NAudio?

    receiverGeneratorList = scene.createPinkNoiseGeneratorLists(sampleRate, outputFileName, numberOfChannels); 
}

這是函數本身(為了便於記錄,我知道變量名令人困惑,並且通常該函數迫切需要進行重構以提高清晰度/可讀性,這是曲線列表的列表之類的東西-尚未解決)到此為止):

public List<List<PinkNoiseGenerator>> createPinkNoiseGeneratorLists(int sampleRate, string outputFileName, int numberOfChannels)
{
    // Initialise list of lists of pink noise samples

    List<List<PinkNoiseGenerator>> receiverGeneratorList = new List<List<PinkNoiseGenerator>>();

    for (int i = 0; i < receiverList.Count; i++)
    {
        List<PinkNoiseGenerator> generatorList = new List<PinkNoiseGenerator>();

        // For every amplitude curve within the receiver, generate a corresponding pink noise curve

        for (int j = 0; j < receiverList[i].AmplitudeCurveList.Count; j++)
        {
            // Calculate duration of pink noise in seconds

            double time = receiverList[i].AmplitudeCurveList[j].Count / (double)sampleRate;

            // Instantiate new generator (list of pink noise samples) at given duration

            PinkNoiseGenerator generator = new PinkNoiseGenerator(time, sampleRate, outputFileName, numberOfChannels);

            // Add generator to generator list which will then be added to the list of lists of pink noise curves,

            generatorList.Add(generator);
        }

        // Add list of pink noise curves 

        receiverGeneratorList.Add(generatorList);
    }

    return receiverGeneratorList;
}

NAudio在內部生成一個隨機數生成器,將時間作為種子值。 new PinkNoiseGenerator()該函數時,兩個new PinkNoiseGenerator()調用發生得如此之快,以至於時間沒有改變,並且使用了相同的種子值。 要驗證這一點,請嘗試在new PinkNoiseGenerator()調用之后調用Thread.Sleep(1000) ,看看它是否new PinkNoiseGenerator()

如果確實存在問題,則可以考慮兩種選擇:

  1. 在代碼中放置故意的延遲,以便隨后的PinkNoiseGenerator構造函數調用發生在不同的時間戳記中
  2. 僅創建一個生成器(一個隨機種子),並從中讀取所有緩沖區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM