try another version of an 8bit audio loudness

This commit is contained in:
ZappoMan 2017-01-11 17:51:12 -08:00
parent 407ad633e0
commit 0661531e3a
4 changed files with 25 additions and 6 deletions

View file

@ -35,7 +35,6 @@
#include <UUID.h>
#include <shared/JSONHelpers.h>
#include <ShapeInfo.h>
#include <AudioHelpers.h>
#include "AvatarLogging.h"
@ -53,6 +52,7 @@ const QString AvatarData::FRAME_NAME = "com.highfidelity.recording.AvatarData";
static const int TRANSLATION_COMPRESSION_RADIX = 12;
static const int SENSOR_TO_WORLD_SCALE_RADIX = 10;
static const int AUDIO_LOUDNESS_RADIX = 2;
static const float AUDIO_LOUDNESS_SCALE = 10.0f;
//static const int MODEL_OFFSET_RADIX = 6;
#define ASSERT(COND) do { if (!(COND)) { abort(); } } while(0)
@ -336,8 +336,9 @@ QByteArray AvatarData::toByteArray(AvatarDataDetail dataDetail, quint64 lastSent
if (hasAudioLoudness) {
auto data = reinterpret_cast<AvatarDataPacket::AudioLoudness*>(destinationBuffer);
data->audioLoudness = packFloatGainToByte(_headData->getAudioLoudness());
destinationBuffer += sizeof(AvatarDataPacket::AudioLoudness);
auto audioLoudness = glm::min(_headData->getAudioLoudness(), MAX_AUDIO_LOUDNESS) / AUDIO_LOUDNESS_SCALE;
destinationBuffer += packFloatScalarToSignedOneByteFixed((uint8_t*)&data->audioLoudness, audioLoudness, AUDIO_LOUDNESS_RADIX);
}
if (hasSensorToWorldMatrix) {
@ -751,7 +752,9 @@ int AvatarData::parseDataFromBuffer(const QByteArray& buffer) {
PACKET_READ_CHECK(AudioLoudness, sizeof(AvatarDataPacket::AudioLoudness));
auto data = reinterpret_cast<const AvatarDataPacket::AudioLoudness*>(sourceBuffer);
float audioLoudness = unpackFloatGainFromByte(data->audioLoudness);
float audioLoudness;
sourceBuffer += unpackFloatScalarFromSignedOneByteFixed(&data->audioLoudness, &audioLoudness, AUDIO_LOUDNESS_RADIX);
audioLoudness *= AUDIO_LOUDNESS_SCALE;
if (isNaN(audioLoudness)) {
if (shouldLogError(now)) {
@ -760,7 +763,7 @@ int AvatarData::parseDataFromBuffer(const QByteArray& buffer) {
return buffer.size();
}
_headData->setAudioLoudness(audioLoudness);
sourceBuffer += sizeof(AvatarDataPacket::AudioLoudness);
qDebug() << "audioLoudness:" << audioLoudness;
int numBytesRead = sourceBuffer - startSection;
_audioLoudnessRate.increment(numBytesRead);
}

View file

@ -200,7 +200,7 @@ namespace AvatarDataPacket {
const size_t LOOK_AT_POSITION_SIZE = 12;
PACKED_BEGIN struct AudioLoudness {
uint8_t audioLoudness; // current loudness of microphone, compressed by packFloatGainToByte()
int8_t audioLoudness; // current loudness of microphone, compressed by packFloatGainToByte()
} PACKED_END;
const size_t AUDIO_LOUDNESS_SIZE = 1;

View file

@ -81,6 +81,18 @@ int unpackFloatScalarFromSignedTwoByteFixed(const int16_t* byteFixedPointer, flo
return sizeof(int16_t);
}
// Allows sending of fixed-point numbers: radix 1 makes 15.1 number, radix 8 makes 8.8 number, etc
int packFloatScalarToSignedOneByteFixed(unsigned char* buffer, float scalar, int radix) {
int8_t outVal = (int8_t)(scalar * (float)(1 << radix));
memcpy(buffer, &outVal, sizeof(uint16_t));
return sizeof(outVal);
}
int unpackFloatScalarFromSignedOneByteFixed(const int8_t* byteFixedPointer, float* destinationPointer, int radix) {
*destinationPointer = *byteFixedPointer / (float)(1 << radix);
return sizeof(int8_t);
}
int packFloatVec3ToSignedTwoByteFixed(unsigned char* destBuffer, const glm::vec3& srcVector, int radix) {
const unsigned char* startPosition = destBuffer;
destBuffer += packFloatScalarToSignedTwoByteFixed(destBuffer, srcVector.x, radix);

View file

@ -125,6 +125,10 @@ int unpackFloatFromByte(const unsigned char* buffer, float& value, float scaleBy
int packFloatScalarToSignedTwoByteFixed(unsigned char* buffer, float scalar, int radix);
int unpackFloatScalarFromSignedTwoByteFixed(const int16_t* byteFixedPointer, float* destinationPointer, int radix);
// Allows sending of fixed-point numbers: radix 1 makes 7.1 number, radix 4 makes 4.4 number, etc
int unpackFloatScalarFromSignedOneByteFixed(const int8_t* byteFixedPointer, float* destinationPointer, int radix);
int packFloatScalarToSignedOneByteFixed(unsigned char* buffer, float scalar, int radix);
// A convenience for sending vec3's as fixed-point floats
int packFloatVec3ToSignedTwoByteFixed(unsigned char* destBuffer, const glm::vec3& srcVector, int radix);
int unpackFloatVec3FromSignedTwoByteFixed(const unsigned char* sourceBuffer, glm::vec3& destination, int radix);