0

我正在做一个项目,我需要获取音轨的电平,为此我正在使用BASS音频库(C++) 中的BASS_ChannelGetLevel方法。但问题是它总是为我返回一个 0,我不明白为什么!文档说, 当通道停止时,该方法将返回 0。

请在下面找到我正在使用的代码:

#include <iostream>
#include "bass.h"
using namespace std;

int main() {
    DWORD level, left, right;
    double pos;
    if (BASS_Init(-1, 44100, BASS_DEVICE_DEFAULT, 0, NULL)) {
        // create the stream
        int stream = BASS_StreamCreateFile(FALSE, "test.ogg", 0,
                BASS_STREAM_DECODE, BASS_SAMPLE_FLOAT);
        if (stream != 0) {
            BASS_ChannelPlay(stream, FALSE);

            level = BASS_ChannelGetLevel(stream);
        } else
            cout << "error 1: " << BASS_ErrorGetCode() << endl;


        left = LOWORD(level); // the left level
        right = HIWORD(level); // the right level
        cout << " low word : " << left << endl;
        cout << " high word : " << right << endl;

        cout << "error 3: " << BASS_ErrorGetCode() << endl;
        // free the stream
        BASS_StreamFree(stream);
        // free BASS
        BASS_Free();
    }

}
4

1 回答 1

1

经过多次研究,我找到了这个解决方案:

#include <cstddef>
#include <stdio.h>
#include <stdlib.h>
#include "bass.h"

int main(int argc, char **argv) {
    BASS_Init(0 /* "NO SOUND" device */, 44100, 0, 0, NULL);
    if (argc == 3) {
        int block = atoi(argv[2]); // take levels every argv[2] ms
        if (block < 20)
            block = 20;
        HSTREAM chan = BASS_StreamCreateFile(FALSE, argv[1], 0, 0,
        BASS_STREAM_DECODE);
        if (chan) {
            // BASS_ChannelGetLevel takes 20ms from the channel
            QWORD len = BASS_ChannelSeconds2Bytes(chan,
                    (float) block / (float) 1000 - (float) 0.02);
            char data[len];
            DWORD level, left, right;
            while (-1 != (level = BASS_ChannelGetLevel(chan))) // takes 20ms
            {
                left = LOWORD(level); // the left level
                right = HIWORD(level); // the right level
                printf("%i, %i\n", left, right);
                BASS_ChannelGetData(chan, data, len); // get data away from the channel
            }
            BASS_StreamFree(chan);
        }
    }

    BASS_Free();
    return 0;
}

此代码用于获取级别并将其输出到 STDOUT。

运行它:./levels 1.mp3 5000 >levels.txt

于 2014-05-06T23:14:49.493 回答