/* * This file is part of openauto project. * Copyright (C) 2018 f1x.studio (Michal Szwaj) * * openauto is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * openauto is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with openauto. If not, see . */ #include #include #include namespace f1x { namespace openauto { namespace autoapp { namespace projection { AudioOutput::AudioOutput(uint32_t channelCount, uint32_t sampleSize, uint32_t sampleRate) { audioFormat_.setChannelCount(channelCount); audioFormat_.setSampleRate(sampleRate); audioFormat_.setSampleSize(sampleSize); audioFormat_.setCodec("audio/pcm"); audioFormat_.setByteOrder(QAudioFormat::LittleEndian); audioFormat_.setSampleType(QAudioFormat::SignedInt); this->moveToThread(QApplication::instance()->thread()); connect(this, &AudioOutput::startPlayback, this, &AudioOutput::onStartPlayback); connect(this, &AudioOutput::stopPlayback, this, &AudioOutput::onStopPlayback); QMetaObject::invokeMethod(this, "createAudioOutput", Qt::BlockingQueuedConnection); } void AudioOutput::createAudioOutput() { OPENAUTO_LOG(debug) << "[AudioOutput] create."; audioOutput_ = std::make_unique(QAudioDeviceInfo::defaultOutputDevice(), audioFormat_); // Default volume level (max) produces crackles audioOutput_->setVolume(static_cast(0.90)); } bool AudioOutput::open() { return audioBuffer_.open(QIODevice::ReadWrite); } void AudioOutput::write(const aasdk::common::DataConstBuffer& buffer) { audioBuffer_.write(reinterpret_cast(buffer.cdata), buffer.size); } void AudioOutput::start() { emit startPlayback(); } void AudioOutput::stop() { emit stopPlayback(); } uint32_t AudioOutput::getSampleSize() const { return audioFormat_.sampleSize(); } uint32_t AudioOutput::getChannelCount() const { return audioFormat_.channelCount(); } uint32_t AudioOutput::getSampleRate() const { return audioFormat_.sampleRate(); } void AudioOutput::onStartPlayback() { audioOutput_->start(&audioBuffer_); } void AudioOutput::onStopPlayback() { audioOutput_->suspend(); } } } } }