mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 12:12:32 +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
|
// AudioInjector.cpp
|
||||||
// hifi
|
// 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.
|
// 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 <NodeList.h>
|
||||||
#include <PacketHeaders.h>
|
#include <PacketHeaders.h>
|
||||||
#include <SharedUtil.h>
|
#include <SharedUtil.h>
|
||||||
|
@ -24,13 +18,12 @@
|
||||||
|
|
||||||
int abstractAudioPointerMeta = qRegisterMetaType<AbstractAudioInterface*>("AbstractAudioInterface*");
|
int abstractAudioPointerMeta = qRegisterMetaType<AbstractAudioInterface*>("AbstractAudioInterface*");
|
||||||
|
|
||||||
AudioInjector::AudioInjector(const QUrl& sampleURL) :
|
AudioInjector::AudioInjector(Sound* sound) :
|
||||||
_currentSendPosition(0),
|
_sound(sound),
|
||||||
_sourceURL(sampleURL),
|
|
||||||
_position(0,0,0),
|
|
||||||
_orientation(),
|
|
||||||
_volume(1.0f),
|
_volume(1.0f),
|
||||||
_shouldLoopback(true)
|
_shouldLoopback(true),
|
||||||
|
_position(0.0f, 0.0f, 0.0f),
|
||||||
|
_orientation()
|
||||||
{
|
{
|
||||||
// we want to live on our own thread
|
// we want to live on our own thread
|
||||||
moveToThread(&_thread);
|
moveToThread(&_thread);
|
||||||
|
@ -38,45 +31,30 @@ AudioInjector::AudioInjector(const QUrl& sampleURL) :
|
||||||
_thread.start();
|
_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) {
|
void AudioInjector::injectViaThread(AbstractAudioInterface* localAudioInterface) {
|
||||||
// use Qt::AutoConnection so that this is called on our thread, if appropriate
|
// use Qt::AutoConnection so that this is called on our thread, if appropriate
|
||||||
QMetaObject::invokeMethod(this, "injectAudio", Qt::AutoConnection, Q_ARG(AbstractAudioInterface*, localAudioInterface));
|
QMetaObject::invokeMethod(this, "injectAudio", Qt::AutoConnection, Q_ARG(AbstractAudioInterface*, localAudioInterface));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uchar MAX_INJECTOR_VOLUME = 0xFF;
|
||||||
|
|
||||||
void AudioInjector::injectAudio(AbstractAudioInterface* localAudioInterface) {
|
void AudioInjector::injectAudio(AbstractAudioInterface* localAudioInterface) {
|
||||||
|
|
||||||
|
QByteArray soundByteArray = _sound->getByteArray();
|
||||||
|
|
||||||
// make sure we actually have samples downloaded to inject
|
// 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
|
// give our sample byte array to the local audio interface, if we have it, so it can be handled locally
|
||||||
if (localAudioInterface) {
|
if (localAudioInterface) {
|
||||||
// assume that localAudioInterface could be on a separate thread, use Qt::AutoConnection to handle properly
|
// assume that localAudioInterface could be on a separate thread, use Qt::AutoConnection to handle properly
|
||||||
QMetaObject::invokeMethod(localAudioInterface, "handleAudioByteArray",
|
QMetaObject::invokeMethod(localAudioInterface, "handleAudioByteArray",
|
||||||
Qt::AutoConnection,
|
Qt::AutoConnection,
|
||||||
Q_ARG(QByteArray, _sampleByteArray));
|
Q_ARG(QByteArray, soundByteArray));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeList* nodeList = NodeList::getInstance();
|
NodeList* nodeList = NodeList::getInstance();
|
||||||
|
|
||||||
// reset the current send position to the beginning
|
|
||||||
_currentSendPosition = 0;
|
|
||||||
|
|
||||||
// setup the packet for injected audio
|
// setup the packet for injected audio
|
||||||
unsigned char injectedAudioPacket[MAX_PACKET_SIZE];
|
unsigned char injectedAudioPacket[MAX_PACKET_SIZE];
|
||||||
unsigned char* currentPacketPosition = injectedAudioPacket;
|
unsigned char* currentPacketPosition = injectedAudioPacket;
|
||||||
|
@ -121,14 +99,16 @@ void AudioInjector::injectAudio(AbstractAudioInterface* localAudioInterface) {
|
||||||
gettimeofday(&startTime, NULL);
|
gettimeofday(&startTime, NULL);
|
||||||
int nextFrame = 0;
|
int nextFrame = 0;
|
||||||
|
|
||||||
|
int currentSendPosition = 0;
|
||||||
|
|
||||||
// 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
|
||||||
while (_currentSendPosition < _sampleByteArray.size()) {
|
while (currentSendPosition < soundByteArray.size()) {
|
||||||
|
|
||||||
int bytesToCopy = std::min(NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL,
|
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
|
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
|
||||||
memcpy(currentPacketPosition, _sampleByteArray.data() + _currentSendPosition,
|
memcpy(currentPacketPosition, soundByteArray.data() + currentSendPosition,
|
||||||
bytesToCopy);
|
bytesToCopy);
|
||||||
|
|
||||||
|
|
||||||
|
@ -143,11 +123,11 @@ void AudioInjector::injectAudio(AbstractAudioInterface* localAudioInterface) {
|
||||||
audioMixer->getActiveSocket()->getPort());
|
audioMixer->getActiveSocket()->getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
_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 < _sampleByteArray.size()) {
|
if (currentSendPosition != bytesToCopy && currentSendPosition < soundByteArray.size()) {
|
||||||
// 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 = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
int usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// AudioInjector.h
|
// AudioInjector.h
|
||||||
// hifi
|
// 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.
|
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
@ -11,22 +11,15 @@
|
||||||
|
|
||||||
#include <QtCore/QObject>
|
#include <QtCore/QObject>
|
||||||
#include <QtCore/QThread>
|
#include <QtCore/QThread>
|
||||||
#include <QtCore/QUrl>
|
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtx/quaternion.hpp>
|
#include <glm/gtx/quaternion.hpp>
|
||||||
|
|
||||||
class AbstractAudioInterface;
|
#include "Sound.h"
|
||||||
class QNetworkReply;
|
|
||||||
|
|
||||||
const uchar MAX_INJECTOR_VOLUME = 0xFF;
|
|
||||||
|
|
||||||
class AudioInjector : public QObject {
|
class AudioInjector : public QObject {
|
||||||
Q_OBJECT
|
|
||||||
public:
|
public:
|
||||||
AudioInjector(const QUrl& sampleURL);
|
AudioInjector(Sound* sound);
|
||||||
|
|
||||||
int size() const { return _sampleByteArray.size(); }
|
|
||||||
|
|
||||||
void setPosition(const glm::vec3& position) { _position = position; }
|
void setPosition(const glm::vec3& position) { _position = position; }
|
||||||
void setOrientation(const glm::quat& orientation) { _orientation = orientation; }
|
void setOrientation(const glm::quat& orientation) { _orientation = orientation; }
|
||||||
|
@ -34,20 +27,14 @@ public:
|
||||||
void setShouldLoopback(bool shouldLoopback) { _shouldLoopback = shouldLoopback; }
|
void setShouldLoopback(bool shouldLoopback) { _shouldLoopback = shouldLoopback; }
|
||||||
public slots:
|
public slots:
|
||||||
void injectViaThread(AbstractAudioInterface* localAudioInterface = NULL);
|
void injectViaThread(AbstractAudioInterface* localAudioInterface = NULL);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QByteArray _sampleByteArray;
|
|
||||||
int _currentSendPosition;
|
|
||||||
QThread _thread;
|
QThread _thread;
|
||||||
QUrl _sourceURL;
|
Sound* _sound;
|
||||||
glm::vec3 _position;
|
|
||||||
glm::quat _orientation;
|
|
||||||
float _volume;
|
float _volume;
|
||||||
uchar _shouldLoopback;
|
uchar _shouldLoopback;
|
||||||
|
glm::vec3 _position;
|
||||||
|
glm::quat _orientation;
|
||||||
private slots:
|
private slots:
|
||||||
void startDownload();
|
|
||||||
void replyFinished(QNetworkReply* reply);
|
|
||||||
void injectAudio(AbstractAudioInterface* localAudioInterface);
|
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