From 296ff3a8a31827e7cc37cc7a9dd183eb23b79565 Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Sun, 11 Nov 2007 22:54:37 +0000 Subject: [PATCH] - refactored pulseaudio port, cleanup left git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@700 6778bc44-b910-0410-a7a0-be141de4315d --- src/qprojectM-pulseaudio/CMakeLists.txt | 8 +- .../QPulseAudioThread.cpp | 74 +++++++++++++++ .../QPulseAudioThread.hpp | 33 +++++++ .../qprojectM-pulseaudio.cpp | 93 ++----------------- 4 files changed, 117 insertions(+), 91 deletions(-) create mode 100644 src/qprojectM-pulseaudio/QPulseAudioThread.cpp create mode 100644 src/qprojectM-pulseaudio/QPulseAudioThread.hpp diff --git a/src/qprojectM-pulseaudio/CMakeLists.txt b/src/qprojectM-pulseaudio/CMakeLists.txt index 6eb3d7020..c13fd6173 100644 --- a/src/qprojectM-pulseaudio/CMakeLists.txt +++ b/src/qprojectM-pulseaudio/CMakeLists.txt @@ -12,20 +12,18 @@ pkg_search_module(PROJECTM REQUIRED projectM qprojectM) #pkg_search_module(PULSE_AUDIO REQUIRED pulseaudio) - # the variable "qprojectM_SRCS" contains all .cpp files of this project set(qprojectM_pulseaudio_SRCS - qprojectM-pulseaudio.cpp ConfigFile.h ConfigFile.cpp + qprojectM-pulseaudio.cpp ConfigFile.h ConfigFile.cpp QPulseAudioThread.cpp ) set(qprojectM_pulseaudio_MOC_HDRS - + QPulseAudioThread.hpp ) # After this call, foo_MOC_SRCS = moc_Class1.cxx moc_Class2.cxx moc_Class3.cxx. -qt4_wrap_cpp(qprojectM_pulseaudio_MOC_SRCS ${qprojectM_MOC_HDRS}) - +qt4_wrap_cpp(qprojectM_pulseaudio_MOC_SRCS ${qprojectM_pulseaudio_MOC_HDRS}) ADD_DEFINITIONS(-DLINUX -DPROJECTM_PREFIX='"${PROJECTM_PREFIX}"') ADD_DEFINITIONS(${QT_DEFINITIONS}) diff --git a/src/qprojectM-pulseaudio/QPulseAudioThread.cpp b/src/qprojectM-pulseaudio/QPulseAudioThread.cpp new file mode 100644 index 000000000..a21005917 --- /dev/null +++ b/src/qprojectM-pulseaudio/QPulseAudioThread.cpp @@ -0,0 +1,74 @@ +#include "QPulseAudio.hpp" +#include +#include "libprojectM/projectM.hpp" + +#define BUFSIZE 1024 +PulseAudioThread::PulseAudioThread(int _argc, char **_argv, projectM * _projectM, QObject * parent) : QThread(parent), argc(_argc), argv(_argv), s(0) , m_projectM(_projectM) { + + + m_timer = new QTimer(this); +} + +void PulseAudioThread::cleanup() { + + if ( s ) + pa_simple_free ( s ); + s = 0; + + qDebug() << "pulse audio quit"; + return ; +} + +void PulseAudioThread::init() { + + ss.format = PA_SAMPLE_FLOAT32LE; + ss.rate = 44100; + ss.channels = 2; + int ret = 1; + int error; + + /* Create the recording stream */ + if ( ! ( s = pa_simple_new ( NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error ) ) ) + { + fprintf ( stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror ( error ) ); + return;//RETURthis->exit(error); + } + + +} + +void PulseAudioThread::updatePCM() { + //qDebug() << "pulse audio loop"; + float buf[BUFSIZE]; + ssize_t r; + int error; + qDebug() << "HERE"; + /* Record some data ... */ + if ( pa_simple_read ( s, buf, sizeof(buf), &error ) < 0 ) + { + + fprintf ( stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror ( error ) ); + return; //this->exit(error); + + } + qDebug() << "HERE"; + m_projectM->pcm->addPCMfloat(buf, BUFSIZE); +} + + +PulseAudioThread::~PulseAudioThread() +{ + cleanup(); +} + +void PulseAudioThread::run() +{ + + init(); + connect(m_timer, SIGNAL(timeout()), this, SLOT(updatePCM())); + + + m_timer->start(0); + + //exec(); +} diff --git a/src/qprojectM-pulseaudio/QPulseAudioThread.hpp b/src/qprojectM-pulseaudio/QPulseAudioThread.hpp new file mode 100644 index 000000000..613e459a3 --- /dev/null +++ b/src/qprojectM-pulseaudio/QPulseAudioThread.hpp @@ -0,0 +1,33 @@ +#ifndef PULSE_AUDIO_THREAD +#include +#include +#include +class projectM; + +#include +#include + +class PulseAudioThread : public QThread +{ + Q_OBJECT + public: + PulseAudioThread () {} + PulseAudioThread(int _argc, char **_argv, projectM * projectM, QObject *parent); + ~PulseAudioThread() ; + void run(); + void cleanup(); + + private: + void init(); + int argc; + char ** argv; + QTimer * m_timer; + pa_sample_spec ss ; + pa_simple * s ; + projectM * m_projectM; + + public slots: + void updatePCM(); + +}; +#endif diff --git a/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp b/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp index 41a491780..61cacb88f 100644 --- a/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp +++ b/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp @@ -1,3 +1,4 @@ + /** * projectM -- Milkdrop-esque visualisation SDK * Copyright (C)2003-2004 projectM Team @@ -55,7 +56,7 @@ #include #include - +#include #define CONFIG_FILE "/share/projectM/config.inp" std::string read_config(); @@ -68,14 +69,9 @@ std::string read_config(); #include #include -#include -#include //#include -#define BUFSIZE 1024 - - -projectM *globalPM = NULL; +#include "QPulseAudioThread.hpp" int dumpFrame = 0; int frameNumber = 0; @@ -100,72 +96,7 @@ static ssize_t loop_write ( const float * data, size_t size ) -class PulseAudioThread: public QThread -{ - public: - PulseAudioThread(int _argc, char **_argv, QProjectM_MainWindow * window) : QThread(window), argc(_argc), argv(_argv), m_window(window) ,notStopped(true){} - void run();; - private: - int argc; - char ** argv; - QProjectM_MainWindow * m_window; - bool notStopped; -}; - - -void PulseAudioThread::run() -{ - /* The sample type to use */ - static pa_sample_spec ss ; - //ss.format = PA_SAMPLE_FLOAT32; - ss.format = PA_SAMPLE_FLOAT32LE; - ss.rate = 44100; - ss.channels = 2; - pa_simple *s = NULL; - int ret = 1; - int error; - - qDebug() << "here!"; - /* Create the recording stream */ - if ( ! ( s = pa_simple_new ( NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error ) ) ) - { - fprintf ( stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror ( error ) ); - goto finish; - } - - while (notStopped) - { - //qDebug() << "pulse audio loop"; - float buf[BUFSIZE]; - ssize_t r; - - /* Record some data ... */ - if ( pa_simple_read ( s, buf, sizeof(buf), &error ) < 0 ) - { - - fprintf ( stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror ( error ) ); - goto finish; - } - - - globalPM->pcm->addPCMfloat(buf, BUFSIZE); - - } - - ret = 0; - -finish: - - if ( s ) - pa_simple_free ( s ); - - qDebug() << "pulse audio quit"; - return ; - - - -} int main ( int argc, char*argv[] ) { @@ -180,15 +111,13 @@ int main ( int argc, char*argv[] ) QProjectM_MainWindow * mainWindow = new QProjectM_MainWindow ( config_file ); - globalPM = mainWindow->getQProjectM(); - mainWindow->show(); - globalPM = mainWindow->getQProjectM(); - PulseAudioThread * pulseThread = new PulseAudioThread(argc, argv, mainWindow); + PulseAudioThread * pulseThread = new PulseAudioThread(argc, argv, mainWindow->getQProjectM(), mainWindow); + pulseThread->start(); - - return app.exec(); + qDebug() << "app exec"; + return app.exec(); } @@ -277,12 +206,4 @@ std::string read_config() abort(); } -int -process ( void *arg ) -{ - - // globalPM->pcm->addPCMfloat(in,nframes); -// printf("%x %f\n",nframes,in[128]); - return 0; -}