基于libzplay 实现window下音乐频谱动态显示



  • 刚好用到了libzplay,那么就顺便写个博客实现给予音乐频谱数据动态显示吧
    先看看实现的效果图:
    0_1461500043731_upload-01e9b585-ab75-470b-994c-7bdca4fb5913
    0_1461500202955_upload-ea403aad-26fa-434d-aeda-f6d16f05efcd

    0_1461500332213_upload-72cecac9-a3bf-4703-8693-d45c2ffa15f8

    实现步骤:
    首先我们需要去libzplay下载这个库,因为我使用了它来采集音频频谱数据。
    然后配置pro文件
    0_1461506350205_upload-f1c88301-28be-4285-aca7-0516e9e872d4

    配置好了就开始写功能实现部分,直接上代码吧!

    我写了一个专门读取音频数据的类:

    AudioData.h

    #ifndef AUDIODATA_H
    #define AUDIODATA_H
    
    #include <QObject>
    //#include <QAudioDecoder>
    //#include <QAudioBuffer>
    #include <QDebug>
    //#include <QAudioDecoderControl>
    #include <QFile>
    #include "libzplay.h"
    #include <QTimer>
    
    #include <QJsonDocument>
    #include <QJsonArray>
    #include <QJsonObject>
    using namespace libZPlay;
    
    class AudioData : public QObject
    {
        Q_OBJECT
    public:
        explicit AudioData(QObject *parent = 0);
    
        Q_INVOKABLE void setSource(QString source);
        Q_INVOKABLE void playMusic();
        Q_INVOKABLE void stopMusic();
        Q_INVOKABLE void pauseMusic();
        Q_INVOKABLE void seekPosition(int ms);
        Q_PROPERTY(int amplitude READ amplitude NOTIFY amplitudeChanged)
        Q_PROPERTY(int position READ position NOTIFY positionChanged)
        Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged)
    
        //music pcm data
        Q_PROPERTY(QString pcmData READ pcmData NOTIFY pcmDataChanged)
    
        QString pcmData(){
            QJsonObject root;
            QJsonArray hn;
            for(int i =0;i<257;i++){
                hn.append (pnLeftAmplitude[i]);
            }
            root.insert ("data",hn);
            QJsonDocument doc;
            doc.setObject (root);
            return doc.toJson ();
        }
    
    signals:
        void amplitudeChanged();
        void positionChanged();
        void isPlayingChanged();
        void pcmDataChanged();
    
    public slots:
        void refreshDatas();
    private:
        ZPlay *player;
        QTimer timer;
        int  pnHarmonicNumber[512];
        int  pnHarmonicFreq[257];
        int  pnLeftAmplitude[257];
        int  pnRightAmplitude[257];
        int  pnLeftPhase[257];
        int  pnRightPhase[257];
        int mposition=0;
        bool isMusicPlaying;
    
        int amplitude(){
            return pnRightAmplitude[171];
        }
    
        int position(){
            return mposition;
        }
    
        bool isPlaying(){
            return isMusicPlaying;
        }
    };
    #endif // AUDIODATA_H
    

    AudioData.cpp

    #include "audiodata.h"
    
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <QImage>
    
    AudioData::AudioData(QObject *parent) : QObject(parent)
    {
        player=CreateZPlay();
    
        // set graph type to AREA, left channel on top
        player->SetFFTGraphParam(gpGraphType, gtAreaLeftOnTop);
    
        // set linear scale
        player->SetFFTGraphParam(gpHorizontalScale, gsLinear);
    
        timer.setInterval(1);
    
        connect(&timer,SIGNAL(timeout()),this,SLOT(refreshDatas()));
    
    }
    //music source
    void AudioData::setSource(QString source){
        const char* msource=source.toLatin1().data();
    
    
        // open file
        int result = player->OpenFile(msource, sfAutodetect);
        if(result == 0)
        {
            // display error message
            qDebug()<<"file open failed"<<player->GetError()<<endl;
            return;
        }
    
    }
    
    /**
     * @brief AudioData::playMusic
     */
    void AudioData::playMusic(){
        player->Play();
        timer.start();
        isMusicPlaying=true;
        isPlayingChanged();
    }
    /**
     * @brief AudioData::stopMusic
     */
    void AudioData::stopMusic(){
        player->Stop();
        timer.stop();
        isMusicPlaying=false;
        isPlayingChanged();
    }
    /**
     * @brief AudioData::pauseMusic
     */
    void AudioData::pauseMusic(){
        player->Pause();
        timer.stop();
        isMusicPlaying=false;
        isPlayingChanged();
    }
    /**
     * @brief AudioData::refreshDatas
     */
    void AudioData::refreshDatas(){
    
        // get song length
        TStreamInfo info;
        player->GetStreamInfo(&info);
    
        // check key press
        if(kbhit())
        {
            int a = getch();
            if(a == 'p' || a == 'P')
                player->Pause();
            else if(a=='q'||a=='Q'){
                player->Stop();
            }
        }
    
        // get stream status to check if song is still playing
        TStreamStatus status;
        player->GetStatus(&status);
        if(status.fPlay == 0)
            return;
    
        // get current position
        TStreamTime pos;
        player->GetPosition(&pos);
        this->mposition=pos.ms;
        positionChanged();
        int FFTPoints = player->GetFFTGraphParam(gpFFTPoints);
        player->GetFFTData(FFTPoints,fwTriangular,
                           pnHarmonicNumber,
                           pnHarmonicFreq,
                           pnLeftAmplitude,
                           pnRightAmplitude,
                           pnLeftPhase,
                           pnRightPhase);
        amplitudeChanged();
        pcmDataChanged();
        // draw FFT graph on desktop window
    
    //    player->DrawFFTGraphOnHWND(0, 0, 0, 300, 200);
    
    //    qDebug()<<pnLeftAmplitude[100]<<endl;
    
    }
    /**
     * @brief AudioData::seekPosition
     * @param ms
     */
    void AudioData::seekPosition(int ms){
        TStreamTime time;
        time.ms=ms;
        player->Seek(tfMillisecond,&time,smFromBeginning);
    }
    

    main.cpp

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <audiodata.h>
    #include <QtQml>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        AudioData ad;
        engine.rootContext()->setContextProperty("AudioData",&ad);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    

    main.qml

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Dialogs 1.2
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        property url soundUrl;
    //    property var pcmDataM:new Array();
        property var pcmDatastr:AudioData.pcmData
    
        ListModel{
            id:dataModel
        }
    
        onPcmDatastrChanged: {
            try{
                if(dataModel.count===0)
                    return;
                var jdata=JSON.parse(pcmDatastr).data;
                for(var a=0;a<jdata.length;a++){
                    dataModel.setProperty(a,"pcmdata",jdata[a]);
                }
    
            }
            catch(e){
                console.log(e);
            }
        }
    
        id:mainwin
        Component.onCompleted: {
            for(var a=0;a<257;a++){
                dataModel.append({"pcmdata":0});
            }
        }
    
        menuBar: MenuBar {
            Menu {
                title: qsTr("文件")
                MenuItem {
                    text: qsTr("&打开MP3文件")
                    onTriggered: {
                        mdialog.open();
                    }
    
                }
            }
        }
    
    
        FileDialog{
            id:mdialog
            folder: shortcuts.home
            nameFilters: [ "Sound files (*.mp3)" ]
            onAccepted:{
                soundUrl=mdialog.fileUrl;
                AudioData.setSource(soundUrl.toString().substring(8,soundUrl.toString().length));
                AudioData.playMusic();
            }
        }
    
        Row{
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            Repeater{
                model: dataModel
                delegate: Rectangle{
                    width: mainwin.width/dataModel.count
                    height: pcmdata*mainwin.height/255
                    color:Qt.rgba(index/257,Math.abs(257-index)/257,Math.abs(257-index)/257,1)
                    anchors.bottom: parent.bottom
    //                radius: width/2
    //                border.width:1
    //                border.color:"#ffff12"
                    Rectangle{
                        width:parent.width
                        height:1
                        color:"#454545"
                    }
    
                    Behavior on height{
                        PropertyAnimation{
                            properties: "height"
                            duration: 70
                        }
                    }
                }
            }
        }
    
    }
    

    代码就不解释了,没有过多的逻辑,libzplay那部分看不懂的就自行在libzplay官网看文档吧,我也是用了半天时间搞了一下下。



  • @tommego 这个可是独家技术哦,我可要好好学习,以后播放音乐的时候,加上频谱,那可多炫酷了!



  • @jiangcaiyang 哈哈,要是能找到qt自带的库来实现就好了,这样就能跨平台


Log in to reply
 

走马观花

最近的回复

  • 在Maya 2018安装了之后,还会出现很多的错误:比如说在脚本编辑器中会出现这个:

    file /usr/autodesk/maya2018/lib/python2.7/site-packages/maya/app/general/mayaMixin.py line 35: libpyside2.so.2.0: cannot open shared object file: No such file or directory, No module named PyQt4.QtCore

    这是因为Maya里面有一个文件叫做mayaMixin.py,判断系统是否有PySide或者是PySide2。不用担心一定要安装PySide。网上有人说遇到了No module named PyQt4.QtCore需要执行下面的操作:

    sudo apt install python-qt4

    但是我查找了mayaMixin.py的35行,发现它是先寻找是否有PySide2,如果没有PySide2才会去找PySide。其实Maya 2018默认带了PySide2,因此我们只需要编写一个运行Maya的脚本,设置好环境变量,就可以让这个问题消除了。所以我果断卸载了python-qt4。

    sudo apt remove python-qt4

    #!/bin/sh export PYTHONPATH=usr/autodesk/maya2018/lib/python2.7/site-packages:$PYTHONPATH export LD_LIBRARY_PATH=/usr/autodesk/maya2018/lib:$LD_LIBRARY_PATH /usr/autodesk/maya2018/bin/maya.bin

    把这个脚本放在桌面上吧。

    read more
  • 最近在研究怎样让USD支持Maya类似的约束功能。目前初步得到了成功。当一开始遇到这个问题的时候,我还是转向了github,问问官方人员有没有相关现成的方案,或者是有没有计划怎么做。不过我在这个issue中得知,官方并没有立即支持Maya的约束,目前只能用到的是ConstraintTarget特性。所以很遗憾地暂时没有办法使用官方的建议了,只能自己做USD的约束了。😞 😱

    不过好在USD的基础设施还比较好,我开始研究USD的约束,希望在USDView中得到类似Maya的效果。在上一篇文章中,我介绍了一种通过Schema生成cpp文件的方法,事实上,大部分基于USD的项目,都是用到了这个方便的方式快速拓展原型,并且在此基础上添加功能。

    不过USDView的功能,变得更加复杂了。我尝试了很多方案。首先参考的是USDView的通知机制。首先在USDView中打开interpreter,然后看到交互式Python界面(虽然还是有点丑):
    interpreter在USDView中

    然后输入下面的脚本,用来测试:

    stage = usdviewApi.stage pipe = stage.GetPrimAtPath( '/pPipe' ) UsdGeom.XformCommonAPI( pipe ).SetTranslate( ( 1, 3, 1 ) )

    这个脚本是用来让名为pPipe的物体移动到世界坐标1, 3, 1中的。但是如果你用Visual Studio 2017调试它,会发现一条比较严谨并且漫长的调用路线:
    XformCommonAPI -> UsdGeomXformOp -> UsdStage -> SdfLayer -> SdfChangeBlock -> Sdf_ChangeManager -> TfNotice -> UsdImagingDelegate

    这里有一个很重要的点,就是在UsdImagingDelegate在TfNotice中注册了一个处理函数_OnUsdObjectsChanged,所以一旦场景的属性更改了,都会触发_OnUsdObjectsChanged函数。所以我根据这个特性,创建了usdRigImaging模块,位置在USD\qtdream\usdImaging\lib\usdRigImaging中。这其中很重要的一个类是UsdRigImagingConstraintAdapter,并且在构造函数注册了TfNotice的回调函数。一旦场景发生变化了,我让约束本身感知被约束的物体,进行同步的更新。当然了,Maya本身是支持多个物体约束一个物体的,并且给出了一个权重的信息。其实约束的算法就是各个约束的加权平均数。因此要通过一个for循环对所有的约束物体计算平移变化,再加权平均,即可得到效果。核心的代码在这里:

    void UsdRigImagingConstraintAdapter::_OnUsdObjectsChanged( UsdNotice::ObjectsChanged const& notice ) { const UsdNotice::ObjectsChanged::PathRange& fields = notice.GetChangedInfoOnlyPaths( ); TF_FOR_ALL( it, _constraints ) { UsdRigConstraint constraint( *it ); std::vector<int> constrainedIndices; VtArray<SdfAssetPath> targets; constraint.GetTargetsAttr( ).Get( &targets ); for ( int i = 0; i < targets.size( ); ++i ) { SdfPath targetPath( targets[i].GetAssetPath( ) ); TF_FOR_ALL( field, fields ) { if ( targetPath == field->GetPrimPath( ) ) { constrainedIndices.push_back( i ); } } } TF_FOR_ALL( constrainedIndex, constrainedIndices ) { _ApplyConstrainEffect( constraint, *constrainedIndex ); } } } void UsdRigImagingConstraintAdapter::_ApplyConstrainEffect( const UsdRigConstraint& constraint, int constrainedIndex ) { const UsdPrim& constraintTarget = _GetConstraintTarget( constraint ); const UsdPrim& target = _GetTargetAtIndex( constraint, constrainedIndex ); UsdTimeCode time = UsdTimeCode::Default( ); UsdGeomXformCommonAPI ctAPI( constraintTarget ); GfVec3d translation( 0, 0, 0 ); target.GetAttribute( TfToken( "xformOp.translate" ) ).Get( &translation ); GfVec3d initialTranslation = _initialTranslationMap[target.GetPrimPath( )]; double weight = _GetWeightAtIndex( constraint, constrainedIndex ); GfVec3d finalTranslation = ( translation - initialTranslation ) * weight; ctAPI.SetTranslate( finalTranslation ); } void UsdRigImagingConstraintAdapter::_PopulateOnConstraint( const UsdPrim& prim ) { UsdStageWeakPtr& stage = prim.GetStage( ); _constraints.push_back( prim ); UsdRigConstraint constraint( prim ); VtArray<SdfAssetPath> targets; constraint.GetTargetsAttr( ).Get( &targets ); TF_FOR_ALL( tit, targets ) { SdfPath targetPath( tit->GetAssetPath( ) ); GfVec3d translation; const UsdPrim& target = stage->GetPrimAtPath( targetPath ); target.GetAttribute( TfToken( "xformOp:translate" ) ).Get( &translation ); std::pair<SdfPath, GfVec3d> translationPair = std::pair<SdfPath, GfVec3d>( targetPath, translation ); _initialTranslationMap.insert( translationPair ); } } UsdPrim UsdRigImagingConstraintAdapter::_GetConstraintTarget( const UsdRigConstraint& constraint ) { SdfAssetPath constraintTargetPath; constraint.GetTargetAttr( ).Get( &constraintTargetPath ); return _GetPrimAtPath( constraint, SdfPath( constraintTargetPath.GetAssetPath( ) ) ); } UsdPrim UsdRigImagingConstraintAdapter::_GetTargetAtIndex( const UsdRigConstraint& constraint, int constraintIndex ) { VtArray<SdfAssetPath> targets; constraint.GetTargetsAttr( ).Get( &targets ); return _GetPrimAtPath( constraint, SdfPath( targets[constraintIndex].GetAssetPath( ) ) ); } UsdPrim UsdRigImagingConstraintAdapter::_GetPrimAtPath( const UsdRigConstraint& constraint, const SdfPath& path ) { UsdStageWeakPtr& stage = constraint.GetPrim( ).GetStage( ); return stage->GetPrimAtPath( path ); } float UsdRigImagingConstraintAdapter::_GetWeightAtIndex( const UsdRigConstraint& constraint, int constraintIndex ) { VtArray<float> weights; constraint.GetWeightsAttr( ).Get( &weights ); float weight = weights[constraintIndex]; weight = weight / weights.size( ); return weight; }

    在内部的一个测试场景中,我们针对pPipe物体做出了平移变换,被约束的物体也同步地变换。

    对USD的二次开发感兴趣?或者是想要让你的三维软件添加USD的导入导出支持?可以找我们,我们研究USD很深入哦。
    上海USD研究小组

    read more

关注我们

微博
QQ群