Hello,

I'm trying to set up a GUI with QT using PortAudio to capture sound. For contious recording i use two buffer which I switch, when one is filled with Data. Now I want to use a new thread to process the full Buffer (save data). That's where the Problem lies... I have created that thread and tryed to interface it via a signal. But the signal is emitted from within the callback thread and not the main thread. For some reason this is not working.

My savethread class:
Qt Code:
  1. class SaveBuffer : public QThread
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. SaveBuffer(QObject *parent = 0);
  7. ~SaveBuffer();
  8.  
  9. void process();
  10.  
  11. protected:
  12. void run();
  13.  
  14. protected:
  15. bool finish;
  16.  
  17. private:
  18.  
  19. QMutex mutex;
  20. QWaitCondition condition;
  21. SDataStruct sData;
  22. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "SaveBuffer.h"
  2.  
  3. SaveBuffer::SaveBuffer(QObject *parent)
  4. : QThread(parent)
  5. {
  6. sData.file.setFileName("recorded.raw");
  7. sData.file.open(QIODevice::Append);
  8. }
  9.  
  10. SaveBuffer::~SaveBuffer()
  11. {
  12. //mutex.lock();
  13. condition.wakeOne();
  14. //mutex.unlock();
  15.  
  16. sData.file.close();
  17. wait();
  18. }
  19.  
  20. void SaveBuffer::run()
  21. {
  22. forever
  23. {
  24. QMutexLocker locker(&mutex);
  25.  
  26. QFile *file = &sData.file;
  27. int frameIndex = *sData.FrameIndex;
  28. SAMPLE *buffer = sData.buffer;
  29.  
  30. QDataStream out(file);
  31. out.setByteOrder(QDataStream::LittleEndian);
  32. for(int k=0;k<frameIndex;k++)
  33. {
  34. out << (qreal)buffer[k];
  35. }
  36. condition.wait(&mutex);//put thread to sleep
  37. }
  38. };
  39.  
  40. void SaveBuffer::process(DataStruct* incData)
  41. {
  42. QMutexLocker locker(&mutex);
  43.  
  44. if (!isRunning()) {
  45. start(LowPriority);
  46. } else {
  47. condition.wakeOne();
  48. }
  49. }
To copy to clipboard, switch view to plain text mode 

My window class:
Qt Code:
  1. class EEGacquisition : public QMainWindow
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. EEGacquisition(QWidget *parent = 0, Qt::WFlags flags = 0);
  7. ~EEGacquisition();
  8. void processDataEmitter(void*);
  9.  
  10. private:
  11. Ui::EEGacquisitionClass ui;
  12. SaveBuffer sthread;
  13.  
  14.  
  15. signals:
  16. void processData();
  17.  
  18. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. int recordCallback( const void *inputBuffer, void *outputBuffer,
  2. unsigned long framesPerBuffer,
  3. const PaStreamCallbackTimeInfo* timeInfo,
  4. PaStreamCallbackFlags statusFlags,
  5. void *userData,
  6. void *ui);
  7.  
  8. EEGacquisition::EEGacquisition(QWidget *parent, Qt::WFlags flags)
  9. : QMainWindow(parent, flags)
  10. {
  11. ui.setupUi(this);
  12.  
  13. //connects
  14. connect(ui.pushButton_measure,SIGNAL(clicked()),this,SLOT(pushButton_measure_clicked()),Qt::DirectConnection);
  15.  
  16. /*---portaudio setup---*/
  17. [...]
  18. }
  19. }
  20.  
  21.  
  22. void EEGacquisition::processDataEmitter(void *user_int)
  23. {
  24. // problem here if i emit a signal I am still in record callback
  25. // I cannot connet this signal
  26. }
  27.  
  28. /////
  29. //callback for recording audio data
  30. /////
  31. static int recordCallback( const void *inputBuffer, void *outputBuffer,
  32. unsigned long framesPerBuffer,
  33. const PaStreamCallbackTimeInfo* timeInfo,
  34. PaStreamCallbackFlags statusFlags,
  35. void *userData,
  36. void *user_int)
  37. {
  38. [...]
  39.  
  40. //at some point emit save signal!!!
  41. ui->processDataEmitter();
  42.  
  43. [...]
  44.  
  45. return paContinue;//0
  46. }
To copy to clipboard, switch view to plain text mode 

I also tried directly calling the process() function in sthread, but doing so led to an exeption, because the call wasnt from the main window thread.

Can someone please explain some way to start the save thread?

Thanks