Advertisement
Guest User

Audio I/O Implementation

a guest
Oct 18th, 2011
1,622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. // Ok, this is my implementation so far -
  2. AudioBuffer::AudioBuffer()
  3. {
  4.     this->init();
  5. }
  6.  
  7. AudioBuffer::~AudioBuffer()
  8. {
  9.     delete this->buffer;
  10. }
  11.  
  12. void AudioBuffer::init()
  13. {
  14.     this->buffer = new char[this->MAXSIZE];
  15.     this->writePosition = this->readPosition = this->currentBufferLength = 0;
  16. }
  17.  
  18. // OVERRIDE
  19. qint64 AudioBuffer::writeData(const char *data, qint64 len)
  20. {
  21.     if((this->writePosition + len) > this->MAXSIZE)
  22.     {
  23.         qint64 firstData = this->MAXSIZE - this->writePosition;
  24.         memcpy(&this->buffer[this->writePosition], data, firstData);
  25.         memcpy(&this->buffer[0], data+firstData, len - firstData);
  26.     }
  27.     else
  28.     {
  29.         memcpy(&this->buffer[writePosition], data, len);
  30.     }
  31.  
  32.     this->writePosition = (this->writePosition + len) % this->MAXSIZE;
  33.     this->currentBufferLength += len;
  34.  
  35.     return len;
  36. }
  37.  
  38. // OVERRIDE
  39. qint64 AudioBuffer::readData(char *data, qint64 maxlen)
  40. {
  41.     if(this->currentBufferLength <= 0)
  42.         return 0;
  43.  
  44.     qint64 writeLen = qMin(maxlen, this->currentBufferLength);
  45.     if( (writeLen + this->readPosition) > this->MAXSIZE)
  46.     {
  47.         qint64 firstData = this->MAXSIZE - this->readPosition;
  48.         memcpy(data, &this->buffer[this->readPosition], firstData);
  49.         memcpy(data+firstData, &this->buffer[0], writeLen - firstData);
  50.     }
  51.     else
  52.     {
  53.         memcpy(data, &this->buffer[this->readPosition], writeLen);        
  54.     }
  55.  
  56.     this->readPosition = (this->readPosition + writeLen) % this->MAXSIZE;
  57.     this->currentBufferLength -= writeLen;
  58.  
  59.     return writeLen;
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. // and this is the code that's using this buffer -
  67. void MainWindow::on_btnStart_clicked()
  68. {
  69.     QAudioFormat format = this->getFormat();
  70.  
  71.     this->audioInput = new QAudioInput(format, this);
  72.     connect(this->audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(inputStateChanged(QAudio::State)));
  73.     audioInput->start(this->buffer);
  74.  
  75.     this->audioOutput = new QAudioOutput(format, this);
  76.     connect(this->audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(outputStateChanged(QAudio::State)));
  77.     audioOutput->start(this->buffer);
  78. }
  79.  
  80. QAudioFormat MainWindow::getFormat()
  81. {
  82.     QAudioFormat format;
  83.     format.setSampleRate(8000);
  84.     format.setChannelCount(1);
  85.     format.setSampleSize(8);
  86.     format.setCodec("audio/pcm");
  87.     format.setByteOrder(QAudioFormat::LittleEndian);
  88.     format.setSampleType(QAudioFormat::UnSignedInt);
  89.  
  90.     return format;
  91. }
  92.  
  93. void MainWindow::inputStateChanged(QAudio::State state)
  94. {
  95.     QString message="";
  96.  
  97.     if(state==QAudio::ActiveState)
  98.     {
  99.         message = "Input state has gone to ACTIVE! ";
  100.     }
  101.     else if(state==QAudio::SuspendedState)
  102.     {
  103.         message = "Input state has gone to SUSPENDED! ";
  104.     }
  105.     else if(state==QAudio::StoppedState)
  106.     {
  107.         message = "Input state has gone to STOPPED! ";
  108.     }
  109.     else if(state==QAudio::IdleState)
  110.     {
  111.         message = "Input state has gone to IDLE! ";
  112.     }
  113.  
  114.     // Check if any error has occured
  115.     if(this->audioInput->error() == QAudio::OpenError)
  116.         message.append("Input: Open Error has occured!");
  117.     else if(this->audioInput->error() == QAudio::IOError)
  118.         message.append("Input: IO Error has occured!");
  119.     else if(this->audioInput->error() == QAudio::UnderrunError)
  120.         message.append("Input: Underrun Error has occured!");
  121.     else if(this->audioInput->error() == QAudio::FatalError)
  122.         message.append("Input: Fatal Error has occured!");
  123.     else
  124.         message.append("Input: No error has occured!");
  125.  
  126.     this->logger.log(message);
  127. }
  128.  
  129. void MainWindow::outputStateChanged(QAudio::State state)
  130. {
  131.     QString message="";
  132.  
  133.     if(state==QAudio::ActiveState)
  134.     {
  135.         message = "Output state has gone to ACTIVE! ";
  136.     }
  137.     else if(state==QAudio::SuspendedState)
  138.     {
  139.         message = "Output state has gone to SUSPENDED! ";
  140.     }
  141.     else if(state==QAudio::StoppedState)
  142.     {
  143.         message = "Output state has gone to STOPPED! ";
  144.     }
  145.     else if(state==QAudio::IdleState)
  146.     {
  147.         message = "Output state has gone to IDLE! ";
  148.         // Check if any error has occured
  149.         if(this->audioOutput->error() == QAudio::OpenError)
  150.             message.append("Output: Open Error has occured!");
  151.         else if(this->audioOutput->error() == QAudio::IOError)
  152.             message.append("Output: IO Error has occured!");
  153.         else if(this->audioOutput->error() == QAudio::UnderrunError)
  154.             message.append("Output: Underrun Error has occured!");
  155.         else if(this->audioOutput->error() == QAudio::FatalError)
  156.             message.append("Output: Fatal Error has occured!");
  157.         else
  158.             message.append("Output: No error has occured!");
  159.  
  160.         this->logger.log(message);
  161.  
  162.         this->audioOutput->start(this->buffer);
  163.     }
  164.  
  165.     // Check if any error has occured
  166.     if(this->audioOutput->error() == QAudio::OpenError)
  167.         message.append("Output: Open Error has occured!");
  168.     else if(this->audioOutput->error() == QAudio::IOError)
  169.         message.append("Output: IO Error has occured!");
  170.     else if(this->audioOutput->error() == QAudio::UnderrunError)
  171.         message.append("Output: Underrun Error has occured!");
  172.     else if(this->audioOutput->error() == QAudio::FatalError)
  173.         message.append("Output: Fatal Error has occured!");
  174.     else
  175.         message.append("Output: No error has occured!");
  176.  
  177.     this->logger.log(message);
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement