Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ok, this is my implementation so far -
- AudioBuffer::AudioBuffer()
- {
- this->init();
- }
- AudioBuffer::~AudioBuffer()
- {
- delete this->buffer;
- }
- void AudioBuffer::init()
- {
- this->buffer = new char[this->MAXSIZE];
- this->writePosition = this->readPosition = this->currentBufferLength = 0;
- }
- // OVERRIDE
- qint64 AudioBuffer::writeData(const char *data, qint64 len)
- {
- if((this->writePosition + len) > this->MAXSIZE)
- {
- qint64 firstData = this->MAXSIZE - this->writePosition;
- memcpy(&this->buffer[this->writePosition], data, firstData);
- memcpy(&this->buffer[0], data+firstData, len - firstData);
- }
- else
- {
- memcpy(&this->buffer[writePosition], data, len);
- }
- this->writePosition = (this->writePosition + len) % this->MAXSIZE;
- this->currentBufferLength += len;
- return len;
- }
- // OVERRIDE
- qint64 AudioBuffer::readData(char *data, qint64 maxlen)
- {
- if(this->currentBufferLength <= 0)
- return 0;
- qint64 writeLen = qMin(maxlen, this->currentBufferLength);
- if( (writeLen + this->readPosition) > this->MAXSIZE)
- {
- qint64 firstData = this->MAXSIZE - this->readPosition;
- memcpy(data, &this->buffer[this->readPosition], firstData);
- memcpy(data+firstData, &this->buffer[0], writeLen - firstData);
- }
- else
- {
- memcpy(data, &this->buffer[this->readPosition], writeLen);
- }
- this->readPosition = (this->readPosition + writeLen) % this->MAXSIZE;
- this->currentBufferLength -= writeLen;
- return writeLen;
- }
- // and this is the code that's using this buffer -
- void MainWindow::on_btnStart_clicked()
- {
- QAudioFormat format = this->getFormat();
- this->audioInput = new QAudioInput(format, this);
- connect(this->audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(inputStateChanged(QAudio::State)));
- audioInput->start(this->buffer);
- this->audioOutput = new QAudioOutput(format, this);
- connect(this->audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(outputStateChanged(QAudio::State)));
- audioOutput->start(this->buffer);
- }
- QAudioFormat MainWindow::getFormat()
- {
- QAudioFormat format;
- format.setSampleRate(8000);
- format.setChannelCount(1);
- format.setSampleSize(8);
- format.setCodec("audio/pcm");
- format.setByteOrder(QAudioFormat::LittleEndian);
- format.setSampleType(QAudioFormat::UnSignedInt);
- return format;
- }
- void MainWindow::inputStateChanged(QAudio::State state)
- {
- QString message="";
- if(state==QAudio::ActiveState)
- {
- message = "Input state has gone to ACTIVE! ";
- }
- else if(state==QAudio::SuspendedState)
- {
- message = "Input state has gone to SUSPENDED! ";
- }
- else if(state==QAudio::StoppedState)
- {
- message = "Input state has gone to STOPPED! ";
- }
- else if(state==QAudio::IdleState)
- {
- message = "Input state has gone to IDLE! ";
- }
- // Check if any error has occured
- if(this->audioInput->error() == QAudio::OpenError)
- message.append("Input: Open Error has occured!");
- else if(this->audioInput->error() == QAudio::IOError)
- message.append("Input: IO Error has occured!");
- else if(this->audioInput->error() == QAudio::UnderrunError)
- message.append("Input: Underrun Error has occured!");
- else if(this->audioInput->error() == QAudio::FatalError)
- message.append("Input: Fatal Error has occured!");
- else
- message.append("Input: No error has occured!");
- this->logger.log(message);
- }
- void MainWindow::outputStateChanged(QAudio::State state)
- {
- QString message="";
- if(state==QAudio::ActiveState)
- {
- message = "Output state has gone to ACTIVE! ";
- }
- else if(state==QAudio::SuspendedState)
- {
- message = "Output state has gone to SUSPENDED! ";
- }
- else if(state==QAudio::StoppedState)
- {
- message = "Output state has gone to STOPPED! ";
- }
- else if(state==QAudio::IdleState)
- {
- message = "Output state has gone to IDLE! ";
- // Check if any error has occured
- if(this->audioOutput->error() == QAudio::OpenError)
- message.append("Output: Open Error has occured!");
- else if(this->audioOutput->error() == QAudio::IOError)
- message.append("Output: IO Error has occured!");
- else if(this->audioOutput->error() == QAudio::UnderrunError)
- message.append("Output: Underrun Error has occured!");
- else if(this->audioOutput->error() == QAudio::FatalError)
- message.append("Output: Fatal Error has occured!");
- else
- message.append("Output: No error has occured!");
- this->logger.log(message);
- this->audioOutput->start(this->buffer);
- }
- // Check if any error has occured
- if(this->audioOutput->error() == QAudio::OpenError)
- message.append("Output: Open Error has occured!");
- else if(this->audioOutput->error() == QAudio::IOError)
- message.append("Output: IO Error has occured!");
- else if(this->audioOutput->error() == QAudio::UnderrunError)
- message.append("Output: Underrun Error has occured!");
- else if(this->audioOutput->error() == QAudio::FatalError)
- message.append("Output: Fatal Error has occured!");
- else
- message.append("Output: No error has occured!");
- this->logger.log(message);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement