mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 03:44:02 +02:00
add methods to AudioInjector to prepare for procedural injection
This commit is contained in:
parent
d4b75670a9
commit
47766d6c6b
3 changed files with 47 additions and 17 deletions
|
@ -74,7 +74,6 @@ Agent::Agent(const Agent &otherAgent) :
|
|||
_localSocket = NULL;
|
||||
}
|
||||
|
||||
|
||||
if (otherAgent._activeSocket == otherAgent._publicSocket) {
|
||||
_activeSocket = _publicSocket;
|
||||
} else if (otherAgent._activeSocket == otherAgent._localSocket) {
|
||||
|
|
|
@ -21,30 +21,41 @@ const float SAMPLE_RATE = 22050.0f;
|
|||
const float BUFFER_SEND_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES / SAMPLE_RATE) * 1000000;
|
||||
|
||||
AudioInjector::AudioInjector(const char* filename) :
|
||||
_numTotalBytesAudio(0),
|
||||
_position(),
|
||||
_bearing(0),
|
||||
_attenuationModifier(255) {
|
||||
|
||||
_attenuationModifier(255),
|
||||
_indexOfNextSlot(0)
|
||||
{
|
||||
std::fstream sourceFile;
|
||||
|
||||
sourceFile.open(filename, std::ios::in | std::ios::binary);
|
||||
sourceFile.seekg(0, std::ios::end);
|
||||
|
||||
_numTotalBytesAudio = sourceFile.tellg();
|
||||
if (_numTotalBytesAudio == -1) {
|
||||
int totalBytes = sourceFile.tellg();
|
||||
if (totalBytes == -1) {
|
||||
printf("Error reading audio data from file %s\n", filename);
|
||||
_audioSampleArray = NULL;
|
||||
} else {
|
||||
printf("Read %d bytes from audio file\n", _numTotalBytesAudio);
|
||||
printf("Read %d bytes from audio file\n", totalBytes);
|
||||
sourceFile.seekg(0, std::ios::beg);
|
||||
long sizeOfShortArray = _numTotalBytesAudio / 2;
|
||||
_audioSampleArray = new int16_t[sizeOfShortArray];
|
||||
_numTotalSamples = totalBytes / 2;
|
||||
_audioSampleArray = new int16_t[_numTotalSamples];
|
||||
|
||||
sourceFile.read((char *)_audioSampleArray, _numTotalBytesAudio);
|
||||
sourceFile.read((char *)_audioSampleArray, _numTotalSamples);
|
||||
}
|
||||
}
|
||||
|
||||
AudioInjector::AudioInjector(int maxNumSamples) :
|
||||
_numTotalSamples(maxNumSamples),
|
||||
_position(),
|
||||
_bearing(0),
|
||||
_attenuationModifier(255),
|
||||
_indexOfNextSlot(0)
|
||||
{
|
||||
_audioSampleArray = new int16_t[maxNumSamples];
|
||||
memset(_audioSampleArray, 0, _numTotalSamples * sizeof(int16_t));
|
||||
}
|
||||
|
||||
AudioInjector::~AudioInjector() {
|
||||
delete[] _audioSampleArray;
|
||||
}
|
||||
|
@ -55,6 +66,21 @@ void AudioInjector::setPosition(float* position) {
|
|||
_position[2] = position[2];
|
||||
}
|
||||
|
||||
void AudioInjector::addSample(const int16_t sample) {
|
||||
if (_indexOfNextSlot != _numTotalSamples) {
|
||||
// only add this sample if we actually have space for it
|
||||
_audioSampleArray[_indexOfNextSlot++] = sample;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioInjector::addSamples(int16_t* sampleBuffer, int numSamples) {
|
||||
if (_audioSampleArray + _indexOfNextSlot + numSamples <= _audioSampleArray + (_numTotalSamples / sizeof(int16_t))) {
|
||||
// only copy the audio from the sample buffer if there's space
|
||||
memcpy(_audioSampleArray + _indexOfNextSlot, sampleBuffer, numSamples * sizeof(int16_t));
|
||||
_indexOfNextSlot += numSamples;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destinationSocket) const {
|
||||
if (_audioSampleArray != NULL) {
|
||||
timeval startTime;
|
||||
|
@ -77,17 +103,17 @@ void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destination
|
|||
memcpy(currentPacketPtr, &_bearing, sizeof(float));
|
||||
currentPacketPtr += sizeof(float);
|
||||
|
||||
for (int i = 0; i < _numTotalBytesAudio; i += BUFFER_LENGTH_BYTES) {
|
||||
for (int i = 0; i < _numTotalSamples; i += BUFFER_LENGTH_SAMPLES) {
|
||||
gettimeofday(&startTime, NULL);
|
||||
|
||||
int numBytesToCopy = BUFFER_LENGTH_BYTES;
|
||||
int numSamplesToCopy = BUFFER_LENGTH_SAMPLES;
|
||||
|
||||
if (_numTotalBytesAudio - i < BUFFER_LENGTH_BYTES) {
|
||||
numBytesToCopy = _numTotalBytesAudio - i;
|
||||
memset(currentPacketPtr + numBytesToCopy, 0, BUFFER_LENGTH_BYTES - numBytesToCopy);
|
||||
if (_numTotalSamples - i < BUFFER_LENGTH_SAMPLES) {
|
||||
numSamplesToCopy = _numTotalSamples - i;
|
||||
memset(currentPacketPtr + numSamplesToCopy, 0, BUFFER_LENGTH_BYTES - (numSamplesToCopy * sizeof(int16_t)));
|
||||
}
|
||||
|
||||
memcpy(currentPacketPtr, _audioSampleArray + (i / 2), numBytesToCopy);
|
||||
memcpy(currentPacketPtr, _audioSampleArray + i, numSamplesToCopy * sizeof(int16_t));
|
||||
|
||||
injectorSocket->send(destinationSocket, dataPacket, sizeof(dataPacket));
|
||||
|
||||
|
|
|
@ -17,19 +17,24 @@
|
|||
class AudioInjector {
|
||||
public:
|
||||
AudioInjector(const char* filename);
|
||||
AudioInjector(int maxNumSamples);
|
||||
~AudioInjector();
|
||||
|
||||
void setPosition(float* position);
|
||||
void setBearing(float bearing) { _bearing = bearing; }
|
||||
void setAttenuationModifier(unsigned char attenuationModifier) { _attenuationModifier = attenuationModifier; }
|
||||
|
||||
void addSample(const int16_t sample);
|
||||
void addSamples(int16_t* sampleBuffer, int numSamples);
|
||||
|
||||
void injectAudio(UDPSocket* injectorSocket, sockaddr* destinationSocket) const;
|
||||
private:
|
||||
int16_t* _audioSampleArray;
|
||||
int _numTotalBytesAudio;
|
||||
int _numTotalSamples;
|
||||
float _position[3];
|
||||
float _bearing;
|
||||
unsigned char _attenuationModifier;
|
||||
int _indexOfNextSlot;
|
||||
};
|
||||
|
||||
#endif /* defined(__hifi__AudioInjector__) */
|
||||
|
|
Loading…
Reference in a new issue