mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-07-23 09:54:24 +02:00
Stereo injectors now handled same as mono
Except of course no HRTF.
This commit is contained in:
parent
4f80c77b77
commit
c84ef5f626
2 changed files with 26 additions and 42 deletions
|
@ -788,24 +788,37 @@ void AudioClient::mixLocalAudioInjectors(int16_t* inputBuffer) {
|
||||||
|
|
||||||
memset(_hrtfBuffer, 0, sizeof(_hrtfBuffer));
|
memset(_hrtfBuffer, 0, sizeof(_hrtfBuffer));
|
||||||
QVector<AudioInjector*> injectorsToRemove;
|
QVector<AudioInjector*> injectorsToRemove;
|
||||||
|
static const float INT16_TO_FLOAT_SCALE_FACTOR = 1/32768.0f;
|
||||||
|
|
||||||
bool injectorsHaveData = false;
|
bool injectorsHaveData = false;
|
||||||
|
|
||||||
for (AudioInjector* injector : getActiveLocalAudioInjectors()) {
|
for (AudioInjector* injector : getActiveLocalAudioInjectors()) {
|
||||||
if (injector->getLocalBuffer()) {
|
if (injector->getLocalBuffer()) {
|
||||||
|
|
||||||
// get one (mono) frame from the injector
|
qint64 samplesToRead = injector->isStereo() ?
|
||||||
if (0 < injector->getLocalBuffer()->readData((char*)_scratchBuffer, AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL*sizeof(int16_t)) ) {
|
AudioConstants::NETWORK_FRAME_BYTES_STEREO :
|
||||||
|
AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL;
|
||||||
|
|
||||||
|
// get one frame from the injector (mono or stereo)
|
||||||
|
if (0 < injector->getLocalBuffer()->readData((char*)_scratchBuffer, samplesToRead)) {
|
||||||
|
|
||||||
injectorsHaveData = true;
|
injectorsHaveData = true;
|
||||||
|
|
||||||
// calculate gain and azimuth for hrtf
|
if (injector->isStereo() ) {
|
||||||
glm::vec3 relativePosition = injector->getPosition() - _positionGetter();
|
for(int i=0; i<AudioConstants::NETWORK_FRAME_SAMPLES_STEREO; i++) {
|
||||||
float gain = gainForSource(relativePosition, injector->getVolume());
|
_hrtfBuffer[i] += (float)(_scratchBuffer[i]) * INT16_TO_FLOAT_SCALE_FACTOR;
|
||||||
float azimuth = azimuthForSource(relativePosition);
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// calculate gain and azimuth for hrtf
|
||||||
|
glm::vec3 relativePosition = injector->getPosition() - _positionGetter();
|
||||||
|
float gain = gainForSource(relativePosition, injector->getVolume());
|
||||||
|
float azimuth = azimuthForSource(relativePosition);
|
||||||
|
|
||||||
|
|
||||||
injector->getLocalHRTF().render(_scratchBuffer, _hrtfBuffer, 1, azimuth, gain, AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL);
|
injector->getLocalHRTF().render(_scratchBuffer, _hrtfBuffer, 1, azimuth, gain, AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL);
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
@ -828,7 +841,7 @@ void AudioClient::mixLocalAudioInjectors(int16_t* inputBuffer) {
|
||||||
|
|
||||||
// mix network into the hrtfBuffer
|
// mix network into the hrtfBuffer
|
||||||
for(int i=0; i<AudioConstants::NETWORK_FRAME_SAMPLES_STEREO; i++) {
|
for(int i=0; i<AudioConstants::NETWORK_FRAME_SAMPLES_STEREO; i++) {
|
||||||
_hrtfBuffer[i] += (float)(inputBuffer[i]) * 1/32678.0f;
|
_hrtfBuffer[i] += (float)(inputBuffer[i]) * INT16_TO_FLOAT_SCALE_FACTOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now, use limiter to write back to the inputBuffer
|
// now, use limiter to write back to the inputBuffer
|
||||||
|
@ -920,42 +933,12 @@ void AudioClient::setIsStereoInput(bool isStereoInput) {
|
||||||
|
|
||||||
bool AudioClient::outputLocalInjector(bool isStereo, AudioInjector* injector) {
|
bool AudioClient::outputLocalInjector(bool isStereo, AudioInjector* injector) {
|
||||||
if (injector->getLocalBuffer() && _audioInput ) {
|
if (injector->getLocalBuffer() && _audioInput ) {
|
||||||
if(isStereo) {
|
|
||||||
QAudioFormat localFormat = _desiredOutputFormat;
|
|
||||||
localFormat.setChannelCount(isStereo ? 2 : 1);
|
|
||||||
|
|
||||||
QAudioOutput* localOutput = new QAudioOutput(getNamedAudioDeviceForMode(QAudio::AudioOutput, _outputAudioDeviceName),
|
|
||||||
localFormat,
|
|
||||||
injector->getLocalBuffer());
|
|
||||||
|
|
||||||
// move the localOutput to the same thread as the local injector buffer
|
|
||||||
localOutput->moveToThread(injector->getLocalBuffer()->thread());
|
|
||||||
|
|
||||||
// have it be stopped when that local buffer is about to close
|
|
||||||
// We don't want to stop this localOutput and injector whenever this AudioClient singleton goes idle,
|
|
||||||
// only when the localOutput does. But the connection is to localOutput, so that it happens on the right thread.
|
|
||||||
connect(localOutput, &QAudioOutput::stateChanged, localOutput, [=](QAudio::State state) {
|
|
||||||
if (state == QAudio::IdleState) {
|
|
||||||
localOutput->stop();
|
|
||||||
injector->stop();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(injector->getLocalBuffer(), &QIODevice::aboutToClose, localOutput, &QAudioOutput::stop);
|
|
||||||
|
|
||||||
qCDebug(audioclient) << "Starting QAudioOutput for local injector" << localOutput;
|
|
||||||
|
|
||||||
localOutput->start(injector->getLocalBuffer());
|
|
||||||
return localOutput->state() == QAudio::ActiveState;
|
|
||||||
} else {
|
|
||||||
// just add it to the vector of active local injectors
|
// just add it to the vector of active local injectors
|
||||||
// TODO: deal with concurrency perhaps? Maybe not
|
// TODO: deal with concurrency perhaps? Maybe not
|
||||||
qDebug() << "adding new injector!!!!!!!";
|
qDebug() << "adding new injector!!!!!!!";
|
||||||
|
|
||||||
_activeLocalAudioInjectors.append(injector);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
_activeLocalAudioInjectors.append(injector);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -59,7 +59,8 @@ public:
|
||||||
|
|
||||||
bool isLocalOnly() const { return _options.localOnly; }
|
bool isLocalOnly() const { return _options.localOnly; }
|
||||||
float getVolume() const { return _options.volume; }
|
float getVolume() const { return _options.volume; }
|
||||||
glm::vec3 getPosition() const { return _options.position; }
|
glm::vec3 getPosition() const { return _options.position; }
|
||||||
|
bool isStereo() const { return _options.stereo; }
|
||||||
void setLocalAudioInterface(AbstractAudioInterface* localAudioInterface) { _localAudioInterface = localAudioInterface; }
|
void setLocalAudioInterface(AbstractAudioInterface* localAudioInterface) { _localAudioInterface = localAudioInterface; }
|
||||||
|
|
||||||
static AudioInjector* playSoundAndDelete(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface);
|
static AudioInjector* playSoundAndDelete(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface);
|
||||||
|
|
Loading…
Reference in a new issue