将OpenGL与RtAudio(或端口音频)同步

时间:2016-02-16 04:42:12

标签: c++ opengl wxwidgets portaudio rtaudio

我需要使用Metronome将一些绘图与OpenGL同步。节拍器是用libPD构建的,并与RtAudio一起播放。 两件事情都运作良好(单独),但我需要用脉冲节拍器移动一个物体(三角形)。应用程序也必须发挥点击次数。两个动作必须并行完成(播放和绘图)。我也应该添加一个midi记录。我的应用程序是用C ++编写的。 我尝试用一​​个线程运行它,但它不起作用。 我试着按照这个解释:How to make my metronome play at the same time as recording in my program? gui库是WxWidgets。用这种方式用Poco :: Runnable完成线程:

class MyThread : public Poco::Runnable {
public:
    MyThread(BasicGLPane *pane, std::shared_ptr<SoundManager> man);

    virtual void run();
private:
    BasicGLPane *_pane;
    std::shared_ptr<SoundManager> _man;
};

MyThread::MyThread(BasicGLPane *pane, std::shared_ptr<SoundManager> man) {
    _pane = pane;
    _man = man;
}


void MyThread::run() {
    _man->play();
    _pane->startAnimation();    
}

BasicGLpane是一个wxGLCanvas。 Sound Manager类的播放功能如下:

void SoundManager::play() {
    // Init pd
    if(!lpd->init(0, 2, sampleRate)) {
        std::cerr << "Could not init pd" << std::endl;
        exit(1);
    }    
    // Receive messages from pd
    lpd->setReceiver(object.get());
    lpd->subscribe("metro-bang");
    lpd->subscribe("switch");

    // send DSP 1 message to pd
    lpd->computeAudio(true);

    // load the patch
    open_patch("metro-main.pd");
    std::cout << patch << std::endl;    

    // Use the RtAudio API to connect to the default audio device.
    if(audio->getDeviceCount()==0){
        std::cout << "There are no available sound devices." << std::endl;
        exit(1);
    }

    RtAudio::StreamParameters parameters;
    parameters.deviceId = audio->getDefaultOutputDevice();
    parameters.nChannels = 2;

    RtAudio::StreamOptions options;
    options.streamName = "Pd Metronome";
    options.flags = RTAUDIO_SCHEDULE_REALTIME;
    if ( audio->getCurrentApi() != RtAudio::MACOSX_CORE ) {
        options.flags |= RTAUDIO_MINIMIZE_LATENCY; // CoreAudio doesn't seem to like this
    }
    try {
        if(audio->isStreamOpen()) {
            audio->closeStream();
        }
        else {
            audio->openStream( &parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &audioCallback, lpd.get(), &options );
            audio->startStream();
        }
    }
    catch ( RtAudioError& e ) {
        std::cerr << e.getMessage() << std::endl;
        exit(1);
    }  
}

OpenGL绘图方法如下:

void BasicGLPane::startAnimation() {
    std::cout<<"Start Animation"<<std::endl;
    triangle_1(p1, p2, p3);    
    Refresh();
}

void BasicGLPane::triangle_1(std::shared_ptr<vertex2f> _p1, std::shared_ptr<vertex2f> _p2, std::shared_ptr<vertex2f> _p3) {
    CGLContextObj ctx = CGLGetCurrentContext(); //enable multithreading (only apple) 
    CGLError err = CGLEnable( ctx, kCGLCEMPEngine);
    if (err != kCGLNoError ) {
        glEnable(GL_MULTISAMPLE);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, getWidth(), getHeight(),0 , -1, 1);
        glShadeModel(GL_SMOOTH);
        glBegin(GL_POLYGON);                      // Drawing Using Triangles
        glColor3f (157.0/255.0, 44.0/255.0, 44.0/255.0);
        glVertex3f( p1->x, p1->y, 0.0f);              // Top left
        glVertex3f(p2->x,p2->y, 0.0f);              // Top Right
        glVertex3f( p3->x,p3->y, 0.0f);               //Bottom
        glEnd();
        glMatrixMode(GL_MODELVIEW);
        glEnable (GL_BLEND);
        glLoadIdentity();
        glDisable(GL_MULTISAMPLE);
    }
}

线程正在调用以下函数:

void BasicGLPane::startThread() {
    while (_object->getCounter()<10) { //this is only to test the functionality
        thread.start(work);
    }
    thread.join();
    manager->stop();
}

之后,这个功能在Reder中被调用:

void BasicGLPane::render( wxPaintEvent& evt ) {
    //some code here, not important....
    startThread();
    SwapBuffers();
}

也许我要改变这个对象,现在不重要,我的问题是同步。我认为RtAudio正在制造问题,因为我成为了一个EXC_BAD_Acces来获取DeviceCount()或者来自RtAudio的任何其他函数。这只发生在线程上下文中。 用Port Audio做这个会更好吗?很高兴知道我做错了什么,或者是否有其他方法来解决这个问题

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题在于wxwidgets主循环和openGL之间的交互。解决方案是以下列方式创建一个Idle事件:

//on wxApp
void MyApp::activateRenderLoop(bool on) {
    if(on && !render_loop_on) {
        Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(MyApp::onIdle));
        render_loop_on = true;
    }
    else if (!on && render_loop_on) {
        Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyApp::onIdle));
        render_loop_on = false;
    }
}

void MyApp::onIdle(wxIdleEvent &evt) {
    activateRenderLoop(glPane->render_on);
    if(render_loop_on) {
        std::cout<<"MyApp on Idle, render_loop_on"<<std::endl;
        glPane->paint_now();
        evt.RequestMore();
    }
}

//on event table:
EVT_PAINT(BasicGLPane::paint_rt)

void BasicGLPane::rightClick(wxMouseEvent& event) {
    render_on = true;
    manager->init();
    SLEEP(2000);
    manager->play();
    wxGetApp().activateRenderLoop(true);   
}

void BasicGLPane::paint_rt(wxPaintEvent &evt) {
    wxPaintDC dc(this);
    render_rt(dc);
}

void BasicGLPane::paint_now(){
    wxClientDC dc(this);
    std::cout<<"paint now() "<<std::endl;
    render_rt(dc);
}

void BasicGLPane::render_rt(wxDC &dc) {
    wxGLCanvas::SetCurrent(*m_context);
    if(_object->getCounter()>=10) {
        wxGetApp().activateRenderLoop(false);
        manager->stop();
        render_on = false;
    }
    else {
        ctx = CGLGetCurrentContext(); //OSx only
        err =  CGLEnable( ctx, kCGLCEMPEngine); //OSX only
        std::cout<<"render_rt CGLError: "<<err<<std::endl;
        if (err==0) {
            glTranslatef(p3->x, p3->y, 0);
            Refresh(false);
        }

    }
}

同步现在完美无缺。