为什么这个量平均值AutoHotKey程序会产生不合理的结果?

时间:2015-04-02 19:59:54

标签: math audio autohotkey volume date-arithmetic

我编写了以下AutoHotKey程序来测量计算机音频在一段时间内的“平均音量”。按下b键时窗口开始,按下e键时窗口结束。目的是使用该程序客观地比较一首歌曲的音量与另一首歌曲的音量(即在Youtube或Soundcloud上)。我还添加了代码来跟踪窗口内每分钟的平均音量(最后的“分钟”是一分钟的剩余部分)。

问题是只有第一分钟的结果是明智的。如果整个窗口大于一分钟,那么它也会产生不连贯的结果。在运行程序超过两分钟后,下面是试运行的输出。

总左数量:1.354168 总体右卷:1.359160

第1节左:0.528259 第1节右:0.529830

第2节左:1.097612 第2节右:1.101604

第3节左:63.397783 第3节右:63.656189

每个值应始终介于0和1之间,因为卷编号是这样存储的。我的逻辑可能在哪里出错了?

; These two lines increase script speed
#NoEnv
SetBatchLines, -1

; Include the Vista Audio library
#Include VA.ahk

audioMeter := VA_GetAudioMeter()
VA_IAudioMeterInformation_GetMeteringChannelCount(audioMeter, channelCount)
VA_GetDevicePeriod("capture", devicePeriod)

lSamples := []
rSamples := []
lSamplesTotal := 0
rSamplesTotal := 0
lSamplesMinuteAverageList := []
rSamplesMinuteAverageList := []
boolIsOn := False

~e::boolIsOn := False

while boolIsOn == False
{
    ~b::
    boolIsOn := True

    while boolIsOn == True
    {
        lSamplesMinuteTotal := 0
        rSamplesMinuteTotal := 0
        numLSamples := 0
        numRSamples := 0 ; These two variables track the number of entries in lSamples and rSamples generated during the current minute

        minuteStart := A_TickCount

        while (A_TickCount - minuteStart) < 60000 && boolIsOn == True
        {
            VA_IAudioMeterInformation_GetPeakValue(audioMeter, peakValue)    
            VarSetCapacity(peakValues, channelCount * 4)
            VA_IAudioMeterInformation_GetChannelsPeakValues(audioMeter, channelCount, &peakValues)

            lValue := NumGet(peakValues, 0, "float")
            rValue := NumGet(peakValues, 4, "float")

            lSamples.Insert(lValue)
            numLSamples++

            rSamples.Insert(rValue)
            numRSamples++

            Sleep, %devicePeriod%
        }

        Loop, % lSamples.MaxIndex()
        {
            lSamplesMinuteTotal += lSamples[A_Index]
            lSamplesTotal += lSamples[A_Index]
        }

        lSamplesMinuteAverage := lSamplesMinuteTotal / numLSamples

        Loop, % rSamples.MaxIndex()
        {
            rSamplesMinuteTotal += rSamples[A_Index]
            rSamplesTotal += rSamples[A_Index]
        }

        rSamplesMinuteAverage := rSamplesMinuteTotal / numRSamples

        lSamplesMinuteAverageList.Insert(lSamplesMinuteAverage)
        rSamplesMinuteAverageList.Insert(rSamplesMinuteAverage)
        ToolTip, % lSamplesMinuteAverage "`n" rSamplesMinuteAverage
    }

    lSamplesAverage := lSamplesTotal / lSamples.MaxIndex()
    rSamplesAverage := rSamplesTotal / rSamples.MaxIndex()

    messageBoxText := "Overall Left Volume:  " lSamplesAverage "`nOverall Right Volume:  " rSamplesAverage "`n"

    while A_Index <= lSamplesMinuteAverageList.MaxIndex() || A_Index <= rSamplesMinuteAverageList.MaxIndex()
    {
        messageBoxText .= "`n`nSection " A_Index " Left:  " lSamplesMinuteAverageList[A_Index] "`nSection " A_Index " Right:  " rSamplesMinuteAverageList[A_Index]
    }

    MsgBox, % messageBoxText

    lSamples := []
    rSamples := []
    lSamplesTotal := 0
    rSamplesTotal := 0
    lSamplesMinuteAverageList := []
    rSamplesMinuteAverageList := []

    return
}

return

0 个答案:

没有答案