Skip to content
Snippets Groups Projects
Commit 177b288d authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Resolves #335 audio reset bug

parent 45412664
Branches
Tags
No related merge requests found
......@@ -4,6 +4,9 @@
#include <vector>
#include <cmath>
#define LOGURU_REPLACE_GLOG 1
#include <loguru.hpp>
namespace ftl {
namespace audio {
......@@ -25,7 +28,7 @@ class Buffer {
void setDelay(float d) {
req_delay_ = d * static_cast<float>(rate_);
// Big jumps should be instant
if (fabs(req_delay_ - cur_delay_) > 0.5f) {
if (fabs(req_delay_ - cur_delay_) > 0.5f*static_cast<float>(rate_)) {
//cur_delay_ = req_delay_;
reset();
}
......@@ -98,7 +101,8 @@ class FixedBuffer : public ftl::audio::Buffer<T> {
void reset() override {
Buffer<T>::reset();
write_position_ = 0;
write_position_ = 0; //int(this->cur_delay_);
LOG(INFO) << "RESET AUDIO: " << write_position_;
read_position_ = 0;
}
......@@ -133,6 +137,7 @@ void FixedBuffer<T,CHAN,FRAME,SIZE>::write(const std::vector<T> &in) {
const float d = 0.6f*clamp((this->req_delay_ - this->cur_delay_) / static_cast<float>(this->rate_), 0.5f);
i += 1.0f - d; // FIXME: Is this correct? Seems to function but perhaps not ideal
//LOG(INFO) << "D " << this->req_delay_ << " - " << this->cur_delay_;
/*if (d > 0.0f) { // Increase delay = oversample with increment < 1.0
//i += 1.0f * (1.0f - d);
......@@ -141,7 +146,7 @@ void FixedBuffer<T,CHAN,FRAME,SIZE>::write(const std::vector<T> &in) {
//i += 1.0f / (1.0f + d);
i += 1.0f - d;
}*/
this->cur_delay_ += d;
this->cur_delay_ += d; //* static_cast<float>(this->rate_);
offset_+= CHAN;
if (offset_ == CHAN*FRAME) {
......
......@@ -74,6 +74,9 @@ struct StreamPacket {
inline int frameNumber() const { return (version >= 4) ? frame_number : streamID; }
inline int frameSetID() const { return (version >= 4) ? streamID : 0; }
inline int64_t localTimestamp() const { return timestamp + originClockDelta; }
int64_t originClockDelta; // Not message packet / saved
MSGPACK_DEFINE(timestamp, streamID, frame_number, channel);
......
......@@ -218,8 +218,9 @@ Peer::Peer(SOCKET s, Universe *u, Dispatcher *d) : sock_(s), can_reconnect_(fals
});
bind("__ping__", [this]() {
auto now = std::chrono::high_resolution_clock::now();
return std::chrono::time_point_cast<std::chrono::milliseconds>(now).time_since_epoch().count();
//auto now = std::chrono::high_resolution_clock::now();
//return std::chrono::time_point_cast<std::chrono::milliseconds>(now).time_since_epoch().count();
return ftl::timer::get_time();
});
send("__handshake__", ftl::net::kMagic, ftl::net::kVersion, ftl::net::this_peer);
......@@ -293,8 +294,9 @@ Peer::Peer(const char *pUri, Universe *u, Dispatcher *d) : can_reconnect_(true),
});
bind("__ping__", [this]() {
auto now = std::chrono::high_resolution_clock::now();
return std::chrono::time_point_cast<std::chrono::milliseconds>(now).time_since_epoch().count();
//auto now = std::chrono::high_resolution_clock::now();
//return std::chrono::time_point_cast<std::chrono::milliseconds>(now).time_since_epoch().count();
return ftl::timer::get_time();
});
}
}
......
......@@ -46,7 +46,6 @@ bool Poser::apply(ftl::rgbd::FrameSet &in, ftl::rgbd::FrameSet &out, cudaStream_
idstr += std::to_string(in.id) + string("-") + std::to_string(i) + string("-") + std::to_string(t.id);
LOG(INFO) << "POSE ID: " << idstr;
auto pose = t.pose.cast<double>(); // f.getPose() *
auto p = pose_db__.find(idstr);
......@@ -55,6 +54,7 @@ bool Poser::apply(ftl::rgbd::FrameSet &in, ftl::rgbd::FrameSet &out, cudaStream_
ps.pose = pose;
ps.locked = false;
pose_db__.emplace(std::make_pair(idstr,ps));
LOG(INFO) << "POSE ID: " << idstr;
} else {
// TODO: Merge poses
if (!(*p).second.locked) (*p).second.pose = pose;
......
......@@ -153,6 +153,8 @@ bool Net::begin() {
StreamPacket spkt = spkt_raw;
// FIXME: see #335
//spkt.timestamp -= clock_adjust_;
spkt.originClockDelta = clock_adjust_;
//LOG(INFO) << "LATENCY: " << ftl::timer::get_time() - spkt.localTimestamp() << " : " << spkt.timestamp << " - " << clock_adjust_;
spkt.version = 4;
// Manage recuring requests
......@@ -236,8 +238,6 @@ bool Net::begin() {
peer_ = *p;
tally_ = 30*kTallyScale;
for (size_t i=0; i<reqtally_.size(); ++i) reqtally_[i] = 0;
LOG(INFO) << "SEND REQUESTS FOR: " << uri_;
// Initially send a colour request just to create the connection
_sendRequest(Channel::Colour, kAllFramesets, kAllFrames, 30, 0);
......@@ -281,7 +281,7 @@ bool Net::_sendRequest(Channel c, uint8_t frameset, uint8_t frames, uint8_t coun
net_->send(peer_, uri_, (short)0, spkt, pkt);
// FIXME: Find a way to use this for correct stream latency info
if (false) { // TODO: Not every time
if (false) { //if (c == Channel::Colour) { // TODO: Not every time
auto start = std::chrono::high_resolution_clock::now();
//int64_t mastertime;
......@@ -289,16 +289,16 @@ bool Net::_sendRequest(Channel c, uint8_t frameset, uint8_t frames, uint8_t coun
net_->asyncCall<int64_t>(peer_, "__ping__", [this, start](const int64_t &mastertime) {
auto elapsed = std::chrono::high_resolution_clock::now() - start;
int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
clock_adjust_ = mastertime - (ftl::timer::get_time() + (latency/2));
clock_adjust_ = ftl::timer::get_time() - mastertime + (latency/2);
if (clock_adjust_ > 0) {
LOG(INFO) << "Clock adjustment: " << clock_adjust_;
}
//if (clock_adjust_ > 0) {
// LOG(INFO) << "Clock adjustment: " << clock_adjust_;
//}
});
} catch (...) {
LOG(ERROR) << "Ping failed";
// Reset time peer and remove timer
time_peer_ = ftl::UUID(0);
//time_peer_ = ftl::UUID(0);
return false;
}
}
......
......@@ -176,6 +176,7 @@ void Receiver::_processAudio(const StreamPacket &spkt, const Packet &pkt) {
ftl::audio::FrameSet fs;
fs.id = 0;
fs.timestamp = frame.timestamp;
fs.originClockDelta;
fs.count = 1;
//fs.stale = false;
fs.clear(ftl::data::FSFlag::STALE);
......
......@@ -32,6 +32,7 @@ class FrameSet {
int id=0;
int64_t timestamp; // Millisecond timestamp of all frames
int64_t originClockDelta;
std::vector<FRAME> frames;
std::atomic<int> count; // Number of valid frames
std::atomic<unsigned int> mask; // Mask of all sources that contributed
......@@ -40,6 +41,8 @@ class FrameSet {
Eigen::Matrix4d pose; // Set to identity by default.
inline int64_t localTimestamp() const { return timestamp + originClockDelta; }
void set(FSFlag f) { flags_ |= (1 << static_cast<int>(f)); }
void clear(FSFlag f) { flags_ &= ~(1 << static_cast<int>(f)); }
bool test(FSFlag f) const { return flags_ & (1 << static_cast<int>(f)); }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment