You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.8 KiB
100 lines
2.8 KiB
/*
|
|
Copyright 2020-2021 Brad Parker
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
#ifndef NO_SOUND
|
|
#include <QAudioFormat>
|
|
#include <QAudioDeviceInfo>
|
|
#endif
|
|
|
|
#include <iostream>
|
|
#include "core.h"
|
|
#include "../common/video.h"
|
|
|
|
void Core::audioSample(int16_t left, int16_t right) {
|
|
// most cores just use the batch callback instead
|
|
if(m_isAudioStreamReady && !m_isMuted) {
|
|
emit Core::instance()->gotAudioSample(left, right);
|
|
}
|
|
}
|
|
|
|
size_t Core::audioSampleBatch(const int16_t *data, size_t frames) {
|
|
if(m_isAudioStreamReady && !m_isMuted) {
|
|
emit Core::instance()->gotAudioSampleBatch(data, frames);
|
|
}
|
|
|
|
return frames;
|
|
}
|
|
|
|
void Core::onAudioStreamReady() {
|
|
m_isAudioStreamReady = true;
|
|
}
|
|
|
|
void Core::setPaused(bool on) {
|
|
std::cout << "set core pause to " << on << std::endl;
|
|
m_isPaused = on;
|
|
|
|
if(m_isPaused) {
|
|
m_timer.stop();
|
|
}else{
|
|
m_timer.start();
|
|
}
|
|
}
|
|
|
|
bool Core::isPaused() const {
|
|
return m_isPaused;
|
|
}
|
|
|
|
void Core::setMuted(bool on) {
|
|
std::cout << "set core mute to " << on << std::endl;
|
|
m_isMuted = on;
|
|
}
|
|
|
|
bool Core::isMuted() const {
|
|
return m_isMuted;
|
|
}
|
|
|
|
void Core::setupAudio() {
|
|
#ifdef NO_SOUND
|
|
std::cout << "audio support not compiled in, no sound will play." << std::endl;
|
|
#else
|
|
bool audio = true;
|
|
|
|
struct retro_system_av_info *avInfo = VideoState::instance()->avInfo();
|
|
|
|
// cores only support S16LE raw audio with 2 channels
|
|
QAudioFormat audioFormat;
|
|
|
|
// We're assuming our QAudioOutput's backend natively supports the core's sample rate, but this may not always be the case.
|
|
// If it is not supported and/or your audio output mechanism cannot resample accordingly, you may need to implement this yourself (RetroArch does)
|
|
audioFormat.setSampleRate(avInfo->timing.sample_rate);
|
|
audioFormat.setChannelCount(2);
|
|
audioFormat.setSampleSize(16);
|
|
audioFormat.setCodec("audio/pcm");
|
|
audioFormat.setByteOrder(QAudioFormat::LittleEndian);
|
|
audioFormat.setSampleType(QAudioFormat::SignedInt);
|
|
|
|
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
|
|
|
|
if(!info.isFormatSupported(audioFormat)) {
|
|
std::cerr << "Core's audio format (S16LE stereo @ " << avInfo->timing.sample_rate << "Hz) not supported by backend, cannot play audio." << std::endl;
|
|
audio = false;
|
|
}
|
|
|
|
if(audio) {
|
|
emit audioReady(&audioFormat);
|
|
}
|
|
#endif
|
|
}
|
|
|