Conflict fixing

This commit is contained in:
Leonardo Murillo 2015-06-08 15:00:06 -06:00
commit bd16072053
11 changed files with 358 additions and 304 deletions

23
examples/dialTone.js Normal file
View file

@ -0,0 +1,23 @@
//
// dialTone.js
// examples
//
// Created by Stephen Birarda on 06/08/15.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// setup the local sound we're going to use
var connectSound = SoundCache.getSound("file://" + Paths.resources + "sounds/short1.wav");
// setup the options needed for that sound
var connectSoundOptions = {
localOnly: true
}
// play the sound locally once we get the first audio packet from a mixer
Audio.receivedFirstPacket.connect(function(){
Audio.playSound(connectSound, connectSoundOptions);
});

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -153,6 +153,7 @@ void AudioClient::reset() {
} }
void AudioClient::audioMixerKilled() { void AudioClient::audioMixerKilled() {
_hasReceivedFirstPacket = false;
_outgoingAvatarAudioSequenceNumber = 0; _outgoingAvatarAudioSequenceNumber = 0;
_stats.reset(); _stats.reset();
} }
@ -481,6 +482,7 @@ void AudioClient::start() {
qCDebug(audioclient) << "Unable to set up audio input because of a problem with input format."; qCDebug(audioclient) << "Unable to set up audio input because of a problem with input format.";
qCDebug(audioclient) << "The closest format available is" << inputDeviceInfo.nearestFormat(_desiredInputFormat); qCDebug(audioclient) << "The closest format available is" << inputDeviceInfo.nearestFormat(_desiredInputFormat);
} }
if (!outputFormatSupported) { if (!outputFormatSupported) {
qCDebug(audioclient) << "Unable to set up audio output because of a problem with output format."; qCDebug(audioclient) << "Unable to set up audio output because of a problem with output format.";
qCDebug(audioclient) << "The closest format available is" << outputDeviceInfo.nearestFormat(_desiredOutputFormat); qCDebug(audioclient) << "The closest format available is" << outputDeviceInfo.nearestFormat(_desiredOutputFormat);
@ -489,6 +491,7 @@ void AudioClient::start() {
if (_audioInput) { if (_audioInput) {
_inputFrameBuffer.initialize( _inputFormat.channelCount(), _audioInput->bufferSize() * 8 ); _inputFrameBuffer.initialize( _inputFormat.channelCount(), _audioInput->bufferSize() * 8 );
} }
_inputGain.initialize(); _inputGain.initialize();
_sourceGain.initialize(); _sourceGain.initialize();
_noiseSource.initialize(); _noiseSource.initialize();
@ -926,6 +929,14 @@ void AudioClient::addReceivedAudioToStream(const QByteArray& audioByteArray) {
DependencyManager::get<NodeList>()->flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::ReceiveFirstAudioPacket); DependencyManager::get<NodeList>()->flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::ReceiveFirstAudioPacket);
if (_audioOutput) { if (_audioOutput) {
if (!_hasReceivedFirstPacket) {
_hasReceivedFirstPacket = true;
// have the audio scripting interface emit a signal to say we just connected to mixer
emit receivedFirstPacket();
}
// Audio output must exist and be correctly set up if we're going to process received audio // Audio output must exist and be correctly set up if we're going to process received audio
_receivedAudioStream.parseData(audioByteArray); _receivedAudioStream.parseData(audioByteArray);
} }

View file

@ -37,6 +37,7 @@
#include <MixedProcessedAudioStream.h> #include <MixedProcessedAudioStream.h>
#include <RingBufferHistory.h> #include <RingBufferHistory.h>
#include <SettingHandle.h> #include <SettingHandle.h>
#include <Sound.h>
#include <StDev.h> #include <StDev.h>
#include "AudioIOStats.h" #include "AudioIOStats.h"
@ -57,7 +58,7 @@ static const int NUM_AUDIO_CHANNELS = 2;
static const int DEFAULT_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES = 3; static const int DEFAULT_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES = 3;
static const int MIN_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES = 1; static const int MIN_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES = 1;
static const int MAX_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES = 20; static const int MAX_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES = 20;
#if defined(Q_OS_ANDROID) || defined(Q_OS_WIN) #if defined(Q_OS_ANDROID) || defined(Q_OS_WIN)
static const int DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_ENABLED = false; static const int DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_ENABLED = false;
#else #else
static const int DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_ENABLED = true; static const int DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_ENABLED = true;
@ -88,14 +89,14 @@ public:
void stop() { close(); } void stop() { close(); }
qint64 readData(char * data, qint64 maxSize); qint64 readData(char * data, qint64 maxSize);
qint64 writeData(const char * data, qint64 maxSize) { return 0; } qint64 writeData(const char * data, qint64 maxSize) { return 0; }
int getRecentUnfulfilledReads() { int unfulfilledReads = _unfulfilledReads; _unfulfilledReads = 0; return unfulfilledReads; } int getRecentUnfulfilledReads() { int unfulfilledReads = _unfulfilledReads; _unfulfilledReads = 0; return unfulfilledReads; }
private: private:
MixedProcessedAudioStream& _receivedAudioStream; MixedProcessedAudioStream& _receivedAudioStream;
AudioClient* _audio; AudioClient* _audio;
int _unfulfilledReads; int _unfulfilledReads;
}; };
const MixedProcessedAudioStream& getReceivedAudioStream() const { return _receivedAudioStream; } const MixedProcessedAudioStream& getReceivedAudioStream() const { return _receivedAudioStream; }
MixedProcessedAudioStream& getReceivedAudioStream() { return _receivedAudioStream; } MixedProcessedAudioStream& getReceivedAudioStream() { return _receivedAudioStream; }
@ -105,30 +106,30 @@ public:
float getAudioAverageInputLoudness() const { return _lastInputLoudness; } float getAudioAverageInputLoudness() const { return _lastInputLoudness; }
int getDesiredJitterBufferFrames() const { return _receivedAudioStream.getDesiredJitterBufferFrames(); } int getDesiredJitterBufferFrames() const { return _receivedAudioStream.getDesiredJitterBufferFrames(); }
bool isMuted() { return _muted; } bool isMuted() { return _muted; }
const AudioIOStats& getStats() const { return _stats; } const AudioIOStats& getStats() const { return _stats; }
float getInputRingBufferMsecsAvailable() const; float getInputRingBufferMsecsAvailable() const;
float getAudioOutputMsecsUnplayed() const; float getAudioOutputMsecsUnplayed() const;
int getOutputBufferSize() { return _outputBufferSizeFrames.get(); } int getOutputBufferSize() { return _outputBufferSizeFrames.get(); }
bool getOutputStarveDetectionEnabled() { return _outputStarveDetectionEnabled.get(); } bool getOutputStarveDetectionEnabled() { return _outputStarveDetectionEnabled.get(); }
void setOutputStarveDetectionEnabled(bool enabled) { _outputStarveDetectionEnabled.set(enabled); } void setOutputStarveDetectionEnabled(bool enabled) { _outputStarveDetectionEnabled.set(enabled); }
int getOutputStarveDetectionPeriod() { return _outputStarveDetectionPeriodMsec.get(); } int getOutputStarveDetectionPeriod() { return _outputStarveDetectionPeriodMsec.get(); }
void setOutputStarveDetectionPeriod(int msecs) { _outputStarveDetectionPeriodMsec.set(msecs); } void setOutputStarveDetectionPeriod(int msecs) { _outputStarveDetectionPeriodMsec.set(msecs); }
int getOutputStarveDetectionThreshold() { return _outputStarveDetectionThreshold.get(); } int getOutputStarveDetectionThreshold() { return _outputStarveDetectionThreshold.get(); }
void setOutputStarveDetectionThreshold(int threshold) { _outputStarveDetectionThreshold.set(threshold); } void setOutputStarveDetectionThreshold(int threshold) { _outputStarveDetectionThreshold.set(threshold); }
void setPositionGetter(AudioPositionGetter positionGetter) { _positionGetter = positionGetter; } void setPositionGetter(AudioPositionGetter positionGetter) { _positionGetter = positionGetter; }
void setOrientationGetter(AudioOrientationGetter orientationGetter) { _orientationGetter = orientationGetter; } void setOrientationGetter(AudioOrientationGetter orientationGetter) { _orientationGetter = orientationGetter; }
static const float CALLBACK_ACCELERATOR_RATIO; static const float CALLBACK_ACCELERATOR_RATIO;
public slots: public slots:
void start(); void start();
void stop(); void stop();
@ -140,7 +141,7 @@ public slots:
void reset(); void reset();
void audioMixerKilled(); void audioMixerKilled();
void toggleMute(); void toggleMute();
virtual void enableAudioSourceInject(bool enable); virtual void enableAudioSourceInject(bool enable);
virtual void selectAudioSourcePinkNoise(); virtual void selectAudioSourcePinkNoise();
virtual void selectAudioSourceSine440(); virtual void selectAudioSourceSine440();
@ -148,10 +149,10 @@ public slots:
virtual void setIsStereoInput(bool stereo); virtual void setIsStereoInput(bool stereo);
void toggleAudioNoiseReduction() { _isNoiseGateEnabled = !_isNoiseGateEnabled; } void toggleAudioNoiseReduction() { _isNoiseGateEnabled = !_isNoiseGateEnabled; }
void toggleLocalEcho() { _shouldEchoLocally = !_shouldEchoLocally; } void toggleLocalEcho() { _shouldEchoLocally = !_shouldEchoLocally; }
void toggleServerEcho() { _shouldEchoToServer = !_shouldEchoToServer; } void toggleServerEcho() { _shouldEchoToServer = !_shouldEchoToServer; }
void processReceivedSamples(const QByteArray& inputBuffer, QByteArray& outputBuffer); void processReceivedSamples(const QByteArray& inputBuffer, QByteArray& outputBuffer);
void sendMuteEnvironmentPacket(); void sendMuteEnvironmentPacket();
@ -172,10 +173,10 @@ public slots:
void setReverbOptions(const AudioEffectOptions* options); void setReverbOptions(const AudioEffectOptions* options);
void outputNotify(); void outputNotify();
void loadSettings(); void loadSettings();
void saveSettings(); void saveSettings();
signals: signals:
bool muteToggled(); bool muteToggled();
void inputReceived(const QByteArray& inputSamples); void inputReceived(const QByteArray& inputSamples);
@ -184,14 +185,16 @@ signals:
void deviceChanged(); void deviceChanged();
void receivedFirstPacket();
protected: protected:
AudioClient(); AudioClient();
~AudioClient(); ~AudioClient();
virtual void customDeleter() { virtual void customDeleter() {
deleteLater(); deleteLater();
} }
private: private:
void outputFormatChanged(); void outputFormatChanged();
@ -216,35 +219,35 @@ private:
QString _inputAudioDeviceName; QString _inputAudioDeviceName;
QString _outputAudioDeviceName; QString _outputAudioDeviceName;
quint64 _outputStarveDetectionStartTimeMsec; quint64 _outputStarveDetectionStartTimeMsec;
int _outputStarveDetectionCount; int _outputStarveDetectionCount;
Setting::Handle<int> _outputBufferSizeFrames; Setting::Handle<int> _outputBufferSizeFrames;
Setting::Handle<bool> _outputStarveDetectionEnabled; Setting::Handle<bool> _outputStarveDetectionEnabled;
Setting::Handle<int> _outputStarveDetectionPeriodMsec; Setting::Handle<int> _outputStarveDetectionPeriodMsec;
// Maximum number of starves per _outputStarveDetectionPeriod before increasing buffer size // Maximum number of starves per _outputStarveDetectionPeriod before increasing buffer size
Setting::Handle<int> _outputStarveDetectionThreshold; Setting::Handle<int> _outputStarveDetectionThreshold;
StDev _stdev; StDev _stdev;
QElapsedTimer _timeSinceLastReceived; QElapsedTimer _timeSinceLastReceived;
float _averagedLatency; float _averagedLatency;
float _lastInputLoudness; float _lastInputLoudness;
float _timeSinceLastClip; float _timeSinceLastClip;
int _totalInputAudioSamples; int _totalInputAudioSamples;
bool _muted; bool _muted;
bool _shouldEchoLocally; bool _shouldEchoLocally;
bool _shouldEchoToServer; bool _shouldEchoToServer;
bool _isNoiseGateEnabled; bool _isNoiseGateEnabled;
bool _audioSourceInjectEnabled; bool _audioSourceInjectEnabled;
bool _reverb; bool _reverb;
AudioEffectOptions _scriptReverbOptions; AudioEffectOptions _scriptReverbOptions;
AudioEffectOptions _zoneReverbOptions; AudioEffectOptions _zoneReverbOptions;
AudioEffectOptions* _reverbOptions; AudioEffectOptions* _reverbOptions;
ty_gverb* _gverb; ty_gverb* _gverb;
// possible soxr streams needed for resample // possible soxr streams needed for resample
soxr* _inputToNetworkResampler; soxr* _inputToNetworkResampler;
soxr* _networkToOutputResampler; soxr* _networkToOutputResampler;
@ -268,17 +271,17 @@ private:
// Input framebuffer // Input framebuffer
AudioBufferFloat32 _inputFrameBuffer; AudioBufferFloat32 _inputFrameBuffer;
// Input gain // Input gain
AudioGain _inputGain; AudioGain _inputGain;
// Post tone/pink noise generator gain // Post tone/pink noise generator gain
AudioGain _sourceGain; AudioGain _sourceGain;
// Pink noise source // Pink noise source
bool _noiseSourceEnabled; bool _noiseSourceEnabled;
AudioSourcePinkNoise _noiseSource; AudioSourcePinkNoise _noiseSource;
// Tone source // Tone source
bool _toneSourceEnabled; bool _toneSourceEnabled;
AudioSourceTone _toneSource; AudioSourceTone _toneSource;
@ -286,17 +289,19 @@ private:
quint16 _outgoingAvatarAudioSequenceNumber; quint16 _outgoingAvatarAudioSequenceNumber;
AudioOutputIODevice _audioOutputIODevice; AudioOutputIODevice _audioOutputIODevice;
AudioIOStats _stats; AudioIOStats _stats;
AudioNoiseGate _inputGate; AudioNoiseGate _inputGate;
AudioPositionGetter _positionGetter; AudioPositionGetter _positionGetter;
AudioOrientationGetter _orientationGetter; AudioOrientationGetter _orientationGetter;
QVector<QString> _inputDevices; QVector<QString> _inputDevices;
QVector<QString> _outputDevices; QVector<QString> _outputDevices;
void checkDevices(); void checkDevices();
bool _hasReceivedFirstPacket = false;
}; };

View file

@ -26,7 +26,7 @@
AudioInjector::AudioInjector(QObject* parent) : AudioInjector::AudioInjector(QObject* parent) :
QObject(parent) QObject(parent)
{ {
} }
AudioInjector::AudioInjector(Sound* sound, const AudioInjectorOptions& injectorOptions) : AudioInjector::AudioInjector(Sound* sound, const AudioInjectorOptions& injectorOptions) :
@ -39,24 +39,24 @@ AudioInjector::AudioInjector(const QByteArray& audioData, const AudioInjectorOpt
_audioData(audioData), _audioData(audioData),
_options(injectorOptions) _options(injectorOptions)
{ {
} }
void AudioInjector::setIsFinished(bool isFinished) { void AudioInjector::setIsFinished(bool isFinished) {
_isFinished = isFinished; _isFinished = isFinished;
if (_isFinished) { if (_isFinished) {
emit finished(); emit finished();
if (_localBuffer) { if (_localBuffer) {
_localBuffer->stop(); _localBuffer->stop();
_localBuffer->deleteLater(); _localBuffer->deleteLater();
_localBuffer = NULL; _localBuffer = NULL;
} }
_isStarted = false; _isStarted = false;
_shouldStop = false; _shouldStop = false;
if (_shouldDeleteAfterFinish) { if (_shouldDeleteAfterFinish) {
// we've been asked to delete after finishing, trigger a queued deleteLater here // we've been asked to delete after finishing, trigger a queued deleteLater here
qCDebug(audio) << "AudioInjector triggering delete from setIsFinished"; qCDebug(audio) << "AudioInjector triggering delete from setIsFinished";
@ -69,16 +69,16 @@ void AudioInjector::injectAudio() {
if (!_isStarted) { if (!_isStarted) {
// check if we need to offset the sound by some number of seconds // check if we need to offset the sound by some number of seconds
if (_options.secondOffset > 0.0f) { if (_options.secondOffset > 0.0f) {
// convert the offset into a number of bytes // convert the offset into a number of bytes
int byteOffset = (int) floorf(AudioConstants::SAMPLE_RATE * _options.secondOffset * (_options.stereo ? 2.0f : 1.0f)); int byteOffset = (int) floorf(AudioConstants::SAMPLE_RATE * _options.secondOffset * (_options.stereo ? 2.0f : 1.0f));
byteOffset *= sizeof(int16_t); byteOffset *= sizeof(int16_t);
_currentSendPosition = byteOffset; _currentSendPosition = byteOffset;
} else { } else {
_currentSendPosition = 0; _currentSendPosition = 0;
} }
if (_options.localOnly) { if (_options.localOnly) {
injectLocally(); injectLocally();
} else { } else {
@ -86,7 +86,7 @@ void AudioInjector::injectAudio() {
} }
} else { } else {
qCDebug(audio) << "AudioInjector::injectAudio called but already started."; qCDebug(audio) << "AudioInjector::injectAudio called but already started.";
} }
} }
void AudioInjector::restart() { void AudioInjector::restart() {
@ -100,37 +100,37 @@ void AudioInjector::injectLocally() {
bool success = false; bool success = false;
if (_localAudioInterface) { if (_localAudioInterface) {
if (_audioData.size() > 0) { if (_audioData.size() > 0) {
_localBuffer = new AudioInjectorLocalBuffer(_audioData, this); _localBuffer = new AudioInjectorLocalBuffer(_audioData, this);
_localBuffer->open(QIODevice::ReadOnly); _localBuffer->open(QIODevice::ReadOnly);
_localBuffer->setShouldLoop(_options.loop); _localBuffer->setShouldLoop(_options.loop);
_localBuffer->setVolume(_options.volume); _localBuffer->setVolume(_options.volume);
// give our current send position to the local buffer // give our current send position to the local buffer
_localBuffer->setCurrentOffset(_currentSendPosition); _localBuffer->setCurrentOffset(_currentSendPosition);
success = _localAudioInterface->outputLocalInjector(_options.stereo, this); success = _localAudioInterface->outputLocalInjector(_options.stereo, this);
// if we're not looping and the buffer tells us it is empty then emit finished // if we're not looping and the buffer tells us it is empty then emit finished
connect(_localBuffer, &AudioInjectorLocalBuffer::bufferEmpty, this, &AudioInjector::stop); connect(_localBuffer, &AudioInjectorLocalBuffer::bufferEmpty, this, &AudioInjector::stop);
if (!success) { if (!success) {
qCDebug(audio) << "AudioInjector::injectLocally could not output locally via _localAudioInterface"; qCDebug(audio) << "AudioInjector::injectLocally could not output locally via _localAudioInterface";
} }
} else { } else {
qCDebug(audio) << "AudioInjector::injectLocally called without any data in Sound QByteArray"; qCDebug(audio) << "AudioInjector::injectLocally called without any data in Sound QByteArray";
} }
} else { } else {
qCDebug(audio) << "AudioInjector::injectLocally cannot inject locally with no local audio interface present."; qCDebug(audio) << "AudioInjector::injectLocally cannot inject locally with no local audio interface present.";
} }
if (!success) { if (!success) {
// we never started so we are finished, call our stop method // we never started so we are finished, call our stop method
stop(); stop();
} }
} }
const uchar MAX_INJECTOR_VOLUME = 0xFF; const uchar MAX_INJECTOR_VOLUME = 0xFF;
@ -140,65 +140,65 @@ void AudioInjector::injectToMixer() {
_currentSendPosition >= _audioData.size()) { _currentSendPosition >= _audioData.size()) {
_currentSendPosition = 0; _currentSendPosition = 0;
} }
auto nodeList = DependencyManager::get<NodeList>(); auto nodeList = DependencyManager::get<NodeList>();
// make sure we actually have samples downloaded to inject // make sure we actually have samples downloaded to inject
if (_audioData.size()) { if (_audioData.size()) {
// setup the packet for injected audio // setup the packet for injected audio
QByteArray injectAudioPacket = nodeList->byteArrayWithPopulatedHeader(PacketTypeInjectAudio); QByteArray injectAudioPacket = nodeList->byteArrayWithPopulatedHeader(PacketTypeInjectAudio);
QDataStream packetStream(&injectAudioPacket, QIODevice::Append); QDataStream packetStream(&injectAudioPacket, QIODevice::Append);
// pack some placeholder sequence number for now // pack some placeholder sequence number for now
int numPreSequenceNumberBytes = injectAudioPacket.size(); int numPreSequenceNumberBytes = injectAudioPacket.size();
packetStream << (quint16)0; packetStream << (quint16)0;
// pack stream identifier (a generated UUID) // pack stream identifier (a generated UUID)
packetStream << QUuid::createUuid(); packetStream << QUuid::createUuid();
// pack the stereo/mono type of the stream // pack the stereo/mono type of the stream
packetStream << _options.stereo; packetStream << _options.stereo;
// pack the flag for loopback // pack the flag for loopback
uchar loopbackFlag = (uchar) true; uchar loopbackFlag = (uchar) true;
packetStream << loopbackFlag; packetStream << loopbackFlag;
// pack the position for injected audio // pack the position for injected audio
int positionOptionOffset = injectAudioPacket.size(); int positionOptionOffset = injectAudioPacket.size();
packetStream.writeRawData(reinterpret_cast<const char*>(&_options.position), packetStream.writeRawData(reinterpret_cast<const char*>(&_options.position),
sizeof(_options.position)); sizeof(_options.position));
// pack our orientation for injected audio // pack our orientation for injected audio
int orientationOptionOffset = injectAudioPacket.size(); int orientationOptionOffset = injectAudioPacket.size();
packetStream.writeRawData(reinterpret_cast<const char*>(&_options.orientation), packetStream.writeRawData(reinterpret_cast<const char*>(&_options.orientation),
sizeof(_options.orientation)); sizeof(_options.orientation));
// pack zero for radius // pack zero for radius
float radius = 0; float radius = 0;
packetStream << radius; packetStream << radius;
// pack 255 for attenuation byte // pack 255 for attenuation byte
int volumeOptionOffset = injectAudioPacket.size(); int volumeOptionOffset = injectAudioPacket.size();
quint8 volume = MAX_INJECTOR_VOLUME * _options.volume; quint8 volume = MAX_INJECTOR_VOLUME * _options.volume;
packetStream << volume; packetStream << volume;
packetStream << _options.ignorePenumbra; packetStream << _options.ignorePenumbra;
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
int nextFrame = 0; int nextFrame = 0;
int numPreAudioDataBytes = injectAudioPacket.size(); int numPreAudioDataBytes = injectAudioPacket.size();
bool shouldLoop = _options.loop; bool shouldLoop = _options.loop;
// loop to send off our audio in NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL byte chunks // loop to send off our audio in NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL byte chunks
quint16 outgoingInjectedAudioSequenceNumber = 0; quint16 outgoingInjectedAudioSequenceNumber = 0;
while (_currentSendPosition < _audioData.size() && !_shouldStop) { while (_currentSendPosition < _audioData.size() && !_shouldStop) {
int bytesToCopy = std::min(((_options.stereo) ? 2 : 1) * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL, int bytesToCopy = std::min(((_options.stereo) ? 2 : 1) * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL,
_audioData.size() - _currentSendPosition); _audioData.size() - _currentSendPosition);
// Measure the loudness of this frame // Measure the loudness of this frame
_loudness = 0.0f; _loudness = 0.0f;
for (int i = 0; i < bytesToCopy; i += sizeof(int16_t)) { for (int i = 0; i < bytesToCopy; i += sizeof(int16_t)) {
@ -215,45 +215,45 @@ void AudioInjector::injectToMixer() {
sizeof(_options.orientation)); sizeof(_options.orientation));
volume = MAX_INJECTOR_VOLUME * _options.volume; volume = MAX_INJECTOR_VOLUME * _options.volume;
memcpy(injectAudioPacket.data() + volumeOptionOffset, &volume, sizeof(volume)); memcpy(injectAudioPacket.data() + volumeOptionOffset, &volume, sizeof(volume));
// resize the QByteArray to the right size // resize the QByteArray to the right size
injectAudioPacket.resize(numPreAudioDataBytes + bytesToCopy); injectAudioPacket.resize(numPreAudioDataBytes + bytesToCopy);
// pack the sequence number // pack the sequence number
memcpy(injectAudioPacket.data() + numPreSequenceNumberBytes, memcpy(injectAudioPacket.data() + numPreSequenceNumberBytes,
&outgoingInjectedAudioSequenceNumber, sizeof(quint16)); &outgoingInjectedAudioSequenceNumber, sizeof(quint16));
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet // copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
memcpy(injectAudioPacket.data() + numPreAudioDataBytes, memcpy(injectAudioPacket.data() + numPreAudioDataBytes,
_audioData.data() + _currentSendPosition, bytesToCopy); _audioData.data() + _currentSendPosition, bytesToCopy);
// grab our audio mixer from the NodeList, if it exists // grab our audio mixer from the NodeList, if it exists
SharedNodePointer audioMixer = nodeList->soloNodeOfType(NodeType::AudioMixer); SharedNodePointer audioMixer = nodeList->soloNodeOfType(NodeType::AudioMixer);
// send off this audio packet // send off this audio packet
nodeList->writeDatagram(injectAudioPacket, audioMixer); nodeList->writeDatagram(injectAudioPacket, audioMixer);
outgoingInjectedAudioSequenceNumber++; outgoingInjectedAudioSequenceNumber++;
_currentSendPosition += bytesToCopy; _currentSendPosition += bytesToCopy;
// send two packets before the first sleep so the mixer can start playback right away // send two packets before the first sleep so the mixer can start playback right away
if (_currentSendPosition != bytesToCopy && _currentSendPosition < _audioData.size()) { if (_currentSendPosition != bytesToCopy && _currentSendPosition < _audioData.size()) {
// process events in case we have been told to stop and be deleted // process events in case we have been told to stop and be deleted
QCoreApplication::processEvents(); QCoreApplication::processEvents();
if (_shouldStop) { if (_shouldStop) {
break; break;
} }
// not the first packet and not done // not the first packet and not done
// sleep for the appropriate time // sleep for the appropriate time
int usecToSleep = (++nextFrame * AudioConstants::NETWORK_FRAME_USECS) - timer.nsecsElapsed() / 1000; int usecToSleep = (++nextFrame * AudioConstants::NETWORK_FRAME_USECS) - timer.nsecsElapsed() / 1000;
if (usecToSleep > 0) { if (usecToSleep > 0) {
usleep(usecToSleep); usleep(usecToSleep);
} }
} }
if (shouldLoop && _currentSendPosition >= _audioData.size()) { if (shouldLoop && _currentSendPosition >= _audioData.size()) {
@ -261,13 +261,13 @@ void AudioInjector::injectToMixer() {
} }
} }
} }
setIsFinished(true); setIsFinished(true);
} }
void AudioInjector::stop() { void AudioInjector::stop() {
_shouldStop = true; _shouldStop = true;
if (_options.localOnly) { if (_options.localOnly) {
// we're only a local injector, so we can say we are finished right away too // we're only a local injector, so we can say we are finished right away too
setIsFinished(true); setIsFinished(true);

View file

@ -53,21 +53,25 @@ Sound::Sound(const QUrl& url, bool isStereo) :
_isStereo(isStereo), _isStereo(isStereo),
_isReady(false) _isReady(false)
{ {
} }
void Sound::downloadFinished(QNetworkReply* reply) { void Sound::downloadFinished(QNetworkReply* reply) {
// replace our byte array with the downloaded data // replace our byte array with the downloaded data
QByteArray rawAudioByteArray = reply->readAll(); QByteArray rawAudioByteArray = reply->readAll();
QString fileName = reply->url().fileName();
if (reply->hasRawHeader("Content-Type")) { const QString WAV_EXTENSION = ".wav";
if (reply->hasRawHeader("Content-Type") || fileName.endsWith(WAV_EXTENSION)) {
QByteArray headerContentType = reply->rawHeader("Content-Type"); QByteArray headerContentType = reply->rawHeader("Content-Type");
// WAV audio file encountered // WAV audio file encountered
if (headerContentType == "audio/x-wav" if (headerContentType == "audio/x-wav"
|| headerContentType == "audio/wav" || headerContentType == "audio/wav"
|| headerContentType == "audio/wave") { || headerContentType == "audio/wave"
|| fileName.endsWith(WAV_EXTENSION)) {
QByteArray outputAudioByteArray; QByteArray outputAudioByteArray;
@ -80,7 +84,7 @@ void Sound::downloadFinished(QNetworkReply* reply) {
_isStereo = true; _isStereo = true;
qCDebug(audio) << "Processing sound of" << rawAudioByteArray.size() << "bytes from" << reply->url() << "as stereo audio file."; qCDebug(audio) << "Processing sound of" << rawAudioByteArray.size() << "bytes from" << reply->url() << "as stereo audio file.";
} }
// Process as RAW file // Process as RAW file
downSample(rawAudioByteArray); downSample(rawAudioByteArray);
} }
@ -88,7 +92,7 @@ void Sound::downloadFinished(QNetworkReply* reply) {
} else { } else {
qCDebug(audio) << "Network reply without 'Content-Type'."; qCDebug(audio) << "Network reply without 'Content-Type'.";
} }
_isReady = true; _isReady = true;
reply->deleteLater(); reply->deleteLater();
} }
@ -99,16 +103,16 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) {
// we want to convert it to the format that the audio-mixer wants // we want to convert it to the format that the audio-mixer wants
// which is signed, 16-bit, 24Khz // which is signed, 16-bit, 24Khz
int numSourceSamples = rawAudioByteArray.size() / sizeof(AudioConstants::AudioSample); int numSourceSamples = rawAudioByteArray.size() / sizeof(AudioConstants::AudioSample);
int numDestinationBytes = rawAudioByteArray.size() / sizeof(AudioConstants::AudioSample); int numDestinationBytes = rawAudioByteArray.size() / sizeof(AudioConstants::AudioSample);
if (_isStereo && numSourceSamples % 2 != 0) { if (_isStereo && numSourceSamples % 2 != 0) {
numDestinationBytes += sizeof(AudioConstants::AudioSample); numDestinationBytes += sizeof(AudioConstants::AudioSample);
} }
_byteArray.resize(numDestinationBytes); _byteArray.resize(numDestinationBytes);
int16_t* sourceSamples = (int16_t*) rawAudioByteArray.data(); int16_t* sourceSamples = (int16_t*) rawAudioByteArray.data();
int16_t* destinationSamples = (int16_t*) _byteArray.data(); int16_t* destinationSamples = (int16_t*) _byteArray.data();
@ -134,22 +138,22 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) {
} }
void Sound::trimFrames() { void Sound::trimFrames() {
const uint32_t inputFrameCount = _byteArray.size() / sizeof(int16_t); const uint32_t inputFrameCount = _byteArray.size() / sizeof(int16_t);
const uint32_t trimCount = 1024; // number of leading and trailing frames to trim const uint32_t trimCount = 1024; // number of leading and trailing frames to trim
if (inputFrameCount <= (2 * trimCount)) { if (inputFrameCount <= (2 * trimCount)) {
return; return;
} }
int16_t* inputFrameData = (int16_t*)_byteArray.data(); int16_t* inputFrameData = (int16_t*)_byteArray.data();
AudioEditBufferFloat32 editBuffer(1, inputFrameCount); AudioEditBufferFloat32 editBuffer(1, inputFrameCount);
editBuffer.copyFrames(1, inputFrameCount, inputFrameData, false /*copy in*/); editBuffer.copyFrames(1, inputFrameCount, inputFrameData, false /*copy in*/);
editBuffer.linearFade(0, trimCount, true); editBuffer.linearFade(0, trimCount, true);
editBuffer.linearFade(inputFrameCount - trimCount, inputFrameCount, false); editBuffer.linearFade(inputFrameCount - trimCount, inputFrameCount, false);
editBuffer.copyFrames(1, inputFrameCount, inputFrameData, true /*copy out*/); editBuffer.copyFrames(1, inputFrameCount, inputFrameData, true /*copy out*/);
} }
@ -238,7 +242,7 @@ void Sound::interpretAsWav(const QByteArray& inputAudioByteArray, QByteArray& ou
} else if (qFromLittleEndian<quint16>(fileHeader.wave.numChannels) > 2) { } else if (qFromLittleEndian<quint16>(fileHeader.wave.numChannels) > 2) {
qCDebug(audio) << "Currently not support audio files with more than 2 channels."; qCDebug(audio) << "Currently not support audio files with more than 2 channels.";
} }
if (qFromLittleEndian<quint16>(fileHeader.wave.bitsPerSample) != 16) { if (qFromLittleEndian<quint16>(fileHeader.wave.bitsPerSample) != 16) {
qCDebug(audio) << "Currently not supporting non 16bit audio files."; qCDebug(audio) << "Currently not supporting non 16bit audio files.";
return; return;

View file

@ -28,19 +28,19 @@ AudioScriptingInterface& AudioScriptingInterface::getInstance() {
AudioScriptingInterface::AudioScriptingInterface() : AudioScriptingInterface::AudioScriptingInterface() :
_localAudioInterface(NULL) _localAudioInterface(NULL)
{ {
} }
ScriptAudioInjector* AudioScriptingInterface::playSound(Sound* sound, const AudioInjectorOptions& injectorOptions) { ScriptAudioInjector* AudioScriptingInterface::playSound(Sound* sound, const AudioInjectorOptions& injectorOptions) {
if (QThread::currentThread() != thread()) { if (QThread::currentThread() != thread()) {
ScriptAudioInjector* injector = NULL; ScriptAudioInjector* injector = NULL;
QMetaObject::invokeMethod(this, "playSound", Qt::BlockingQueuedConnection, QMetaObject::invokeMethod(this, "playSound", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(ScriptAudioInjector*, injector), Q_RETURN_ARG(ScriptAudioInjector*, injector),
Q_ARG(Sound*, sound), Q_ARG(const AudioInjectorOptions&, injectorOptions)); Q_ARG(Sound*, sound), Q_ARG(const AudioInjectorOptions&, injectorOptions));
return injector; return injector;
} }
if (sound) { if (sound) {
// stereo option isn't set from script, this comes from sound metadata or filename // stereo option isn't set from script, this comes from sound metadata or filename
AudioInjectorOptions optionsCopy = injectorOptions; AudioInjectorOptions optionsCopy = injectorOptions;

View file

@ -22,22 +22,23 @@ class AudioScriptingInterface : public QObject {
Q_OBJECT Q_OBJECT
public: public:
static AudioScriptingInterface& getInstance(); static AudioScriptingInterface& getInstance();
void setLocalAudioInterface(AbstractAudioInterface* audioInterface) { _localAudioInterface = audioInterface; } void setLocalAudioInterface(AbstractAudioInterface* audioInterface) { _localAudioInterface = audioInterface; }
protected: protected:
// this method is protected to stop C++ callers from calling, but invokable from script // this method is protected to stop C++ callers from calling, but invokable from script
Q_INVOKABLE ScriptAudioInjector* playSound(Sound* sound, const AudioInjectorOptions& injectorOptions = AudioInjectorOptions()); Q_INVOKABLE ScriptAudioInjector* playSound(Sound* sound, const AudioInjectorOptions& injectorOptions = AudioInjectorOptions());
Q_INVOKABLE void injectGeneratedNoise(bool inject); Q_INVOKABLE void injectGeneratedNoise(bool inject);
Q_INVOKABLE void selectPinkNoise(); Q_INVOKABLE void selectPinkNoise();
Q_INVOKABLE void selectSine440(); Q_INVOKABLE void selectSine440();
Q_INVOKABLE void setStereoInput(bool stereo); Q_INVOKABLE void setStereoInput(bool stereo);
signals: signals:
void mutedByMixer(); void mutedByMixer();
void environmentMuted(); void environmentMuted();
void receivedFirstPacket();
private: private:
AudioScriptingInterface(); AudioScriptingInterface();

View file

@ -18,7 +18,7 @@
#include "PathUtils.h" #include "PathUtils.h"
QString& PathUtils::resourcesPath() { const QString& PathUtils::resourcesPath() {
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
static QString staticResourcePath = QCoreApplication::applicationDirPath() + "/../Resources/"; static QString staticResourcePath = QCoreApplication::applicationDirPath() + "/../Resources/";
#else #else

View file

@ -12,12 +12,17 @@
#ifndef hifi_PathUtils_h #ifndef hifi_PathUtils_h
#define hifi_PathUtils_h #define hifi_PathUtils_h
#include <QtCore/QObject>
#include <QString> #include "DependencyManager.h"
namespace PathUtils { class PathUtils : public QObject, public Dependency {
QString& resourcesPath(); Q_OBJECT
} SINGLETON_DEPENDENCY
Q_PROPERTY(QString resources READ resourcesPath)
public:
static const QString& resourcesPath();
};
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions); QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions);
QString findMostRecentFileExtension(const QString& originalFileName, QVector<QString> possibleExtensions); QString findMostRecentFileExtension(const QString& originalFileName, QVector<QString> possibleExtensions);