繁体   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