Domanda

i'm working on a project and i need to get the level of an audio track, for that I'm using the method BASS_ChannelGetLevel from BASS audio library (C++). But the problem is that it always return a 0 for me and I don't understand why! The documentation says that the method will return 0 when a channel is stalled.

Please find the code that i'm using below:

#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();
    }

}
È stato utile?

Soluzione

After many researches I found this solution:

#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;
}

This code to get levels and output it to STDOUT.

to run it : ./levels 1.mp3 5000 >levels.txt

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top