mirror of
https://github.com/overte-org/overte.git
synced 2025-04-22 04:44:08 +02:00
audio API changes to allow multiple injections of single sound
This commit is contained in:
parent
591876eaf3
commit
3123b83d0b
4 changed files with 81 additions and 58 deletions
|
@ -2,16 +2,10 @@
|
|||
// AudioInjector.cpp
|
||||
// hifi
|
||||
//
|
||||
// Created by Stephen Birarda on 12/19/2013.
|
||||
// Created by Stephen Birarda on 1/2/2014.
|
||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <QtNetwork/QNetworkAccessManager>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
#include <QtNetwork/QNetworkRequest>
|
||||
|
||||
#include <NodeList.h>
|
||||
#include <PacketHeaders.h>
|
||||
#include <SharedUtil.h>
|
||||
|
@ -24,13 +18,12 @@
|
|||
|
||||
int abstractAudioPointerMeta = qRegisterMetaType<AbstractAudioInterface*>("AbstractAudioInterface*");
|
||||
|
||||
AudioInjector::AudioInjector(const QUrl& sampleURL) :
|
||||
_currentSendPosition(0),
|
||||
_sourceURL(sampleURL),
|
||||
_position(0,0,0),
|
||||
_orientation(),
|
||||
AudioInjector::AudioInjector(Sound* sound) :
|
||||
_sound(sound),
|
||||
_volume(1.0f),
|
||||
_shouldLoopback(true)
|
||||
_shouldLoopback(true),
|
||||
_position(0.0f, 0.0f, 0.0f),
|
||||
_orientation()
|
||||
{
|
||||
// we want to live on our own thread
|
||||
moveToThread(&_thread);
|
||||
|
@ -38,45 +31,30 @@ AudioInjector::AudioInjector(const QUrl& sampleURL) :
|
|||
_thread.start();
|
||||
}
|
||||
|
||||
void AudioInjector::startDownload() {
|
||||
// assume we have a QApplication or QCoreApplication instance and use the
|
||||
// QNetworkAccess manager to grab the raw audio file at the given URL
|
||||
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
||||
connect(manager, SIGNAL(finished(QNetworkReply*)),
|
||||
this, SLOT(replyFinished(QNetworkReply*)));
|
||||
|
||||
manager->get(QNetworkRequest(_sourceURL));
|
||||
}
|
||||
|
||||
void AudioInjector::replyFinished(QNetworkReply* reply) {
|
||||
// replace our samples array with the downloaded data
|
||||
_sampleByteArray = reply->readAll();
|
||||
}
|
||||
|
||||
void AudioInjector::injectViaThread(AbstractAudioInterface* localAudioInterface) {
|
||||
// use Qt::AutoConnection so that this is called on our thread, if appropriate
|
||||
QMetaObject::invokeMethod(this, "injectAudio", Qt::AutoConnection, Q_ARG(AbstractAudioInterface*, localAudioInterface));
|
||||
}
|
||||
|
||||
const uchar MAX_INJECTOR_VOLUME = 0xFF;
|
||||
|
||||
void AudioInjector::injectAudio(AbstractAudioInterface* localAudioInterface) {
|
||||
|
||||
QByteArray soundByteArray = _sound->getByteArray();
|
||||
|
||||
// make sure we actually have samples downloaded to inject
|
||||
if (_sampleByteArray.size()) {
|
||||
if (soundByteArray.size()) {
|
||||
// give our sample byte array to the local audio interface, if we have it, so it can be handled locally
|
||||
if (localAudioInterface) {
|
||||
// assume that localAudioInterface could be on a separate thread, use Qt::AutoConnection to handle properly
|
||||
QMetaObject::invokeMethod(localAudioInterface, "handleAudioByteArray",
|
||||
Qt::AutoConnection,
|
||||
Q_ARG(QByteArray, _sampleByteArray));
|
||||
Q_ARG(QByteArray, soundByteArray));
|
||||
|
||||
}
|
||||
|
||||
NodeList* nodeList = NodeList::getInstance();
|
||||
|
||||
// reset the current send position to the beginning
|
||||
_currentSendPosition = 0;
|
||||
|
||||
// setup the packet for injected audio
|
||||
unsigned char injectedAudioPacket[MAX_PACKET_SIZE];
|
||||
unsigned char* currentPacketPosition = injectedAudioPacket;
|
||||
|
@ -121,14 +99,16 @@ void AudioInjector::injectAudio(AbstractAudioInterface* localAudioInterface) {
|
|||
gettimeofday(&startTime, NULL);
|
||||
int nextFrame = 0;
|
||||
|
||||
int currentSendPosition = 0;
|
||||
|
||||
// loop to send off our audio in NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL byte chunks
|
||||
while (_currentSendPosition < _sampleByteArray.size()) {
|
||||
while (currentSendPosition < soundByteArray.size()) {
|
||||
|
||||
int bytesToCopy = std::min(NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL,
|
||||
_sampleByteArray.size() - _currentSendPosition);
|
||||
soundByteArray.size() - currentSendPosition);
|
||||
|
||||
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
|
||||
memcpy(currentPacketPosition, _sampleByteArray.data() + _currentSendPosition,
|
||||
memcpy(currentPacketPosition, soundByteArray.data() + currentSendPosition,
|
||||
bytesToCopy);
|
||||
|
||||
|
||||
|
@ -143,11 +123,11 @@ void AudioInjector::injectAudio(AbstractAudioInterface* localAudioInterface) {
|
|||
audioMixer->getActiveSocket()->getPort());
|
||||
}
|
||||
|
||||
_currentSendPosition += bytesToCopy;
|
||||
currentSendPosition += bytesToCopy;
|
||||
|
||||
// send two packets before the first sleep so the mixer can start playback right away
|
||||
|
||||
if (_currentSendPosition != bytesToCopy && _currentSendPosition < _sampleByteArray.size()) {
|
||||
if (currentSendPosition != bytesToCopy && currentSendPosition < soundByteArray.size()) {
|
||||
// not the first packet and not done
|
||||
// sleep for the appropriate time
|
||||
int usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
// AudioInjector.h
|
||||
// hifi
|
||||
//
|
||||
// Created by Stephen Birarda on 12/19/2013.
|
||||
// Created by Stephen Birarda on 1/2/2014.
|
||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
|
@ -11,22 +11,15 @@
|
|||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QThread>
|
||||
#include <QtCore/QUrl>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
class AbstractAudioInterface;
|
||||
class QNetworkReply;
|
||||
|
||||
const uchar MAX_INJECTOR_VOLUME = 0xFF;
|
||||
#include "Sound.h"
|
||||
|
||||
class AudioInjector : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
AudioInjector(const QUrl& sampleURL);
|
||||
|
||||
int size() const { return _sampleByteArray.size(); }
|
||||
AudioInjector(Sound* sound);
|
||||
|
||||
void setPosition(const glm::vec3& position) { _position = position; }
|
||||
void setOrientation(const glm::quat& orientation) { _orientation = orientation; }
|
||||
|
@ -34,20 +27,14 @@ public:
|
|||
void setShouldLoopback(bool shouldLoopback) { _shouldLoopback = shouldLoopback; }
|
||||
public slots:
|
||||
void injectViaThread(AbstractAudioInterface* localAudioInterface = NULL);
|
||||
|
||||
private:
|
||||
QByteArray _sampleByteArray;
|
||||
int _currentSendPosition;
|
||||
QThread _thread;
|
||||
QUrl _sourceURL;
|
||||
glm::vec3 _position;
|
||||
glm::quat _orientation;
|
||||
Sound* _sound;
|
||||
float _volume;
|
||||
uchar _shouldLoopback;
|
||||
|
||||
glm::vec3 _position;
|
||||
glm::quat _orientation;
|
||||
private slots:
|
||||
void startDownload();
|
||||
void replyFinished(QNetworkReply* reply);
|
||||
void injectAudio(AbstractAudioInterface* localAudioInterface);
|
||||
};
|
||||
|
||||
|
|
29
libraries/audio/src/Sound.cpp
Normal file
29
libraries/audio/src/Sound.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
//
|
||||
// Sound.cpp
|
||||
// hifi
|
||||
//
|
||||
// Created by Stephen Birarda on 1/2/2014.
|
||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#include <QtNetwork/QNetworkAccessManager>
|
||||
#include <QtNetwork/QNetworkRequest>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
|
||||
#include "Sound.h"
|
||||
|
||||
Sound::Sound(const QUrl& sampleURL) {
|
||||
// assume we have a QApplication or QCoreApplication instance and use the
|
||||
// QNetworkAccess manager to grab the raw audio file at the given URL
|
||||
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
||||
connect(manager, SIGNAL(finished(QNetworkReply*)),
|
||||
this, SLOT(replyFinished(QNetworkReply*)));
|
||||
|
||||
manager->get(QNetworkRequest(sampleURL));
|
||||
}
|
||||
|
||||
void Sound::replyFinished(QNetworkReply* reply) {
|
||||
// replace our samples array with the downloaded data
|
||||
_sampleByteArray = reply->readAll();
|
||||
}
|
27
libraries/audio/src/Sound.h
Normal file
27
libraries/audio/src/Sound.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
//
|
||||
// Sound.h
|
||||
// hifi
|
||||
//
|
||||
// Created by Stephen Birarda on 1/2/2014.
|
||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef __hifi__Sound__
|
||||
#define __hifi__Sound__
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
class QNetworkReply;
|
||||
|
||||
class Sound : public QObject {
|
||||
public:
|
||||
Sound(const QUrl& sampleURL);
|
||||
|
||||
const QByteArray& getByteArray() { return _byteArray; }
|
||||
private:
|
||||
QByteArray _byteArray;
|
||||
private slots:
|
||||
void replyFinished(QNetworkReply* reply);
|
||||
};
|
||||
|
||||
#endif /* defined(__hifi__Sound__) */
|
Loading…
Reference in a new issue