Skip to content

Commit

Permalink
VBAN Throttle correction
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 21, 2023
1 parent e38edd7 commit 8089a7b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void setup() {
cfg.stream_name = "Stream1";
cfg.target_ip = IPAddress{192,168,1,37};
cfg.throttle_active = true;
cfg.throttle_correction_ms = -20;
//cfg.throttle_correction_us = 0;
if (!out.begin(cfg)) stop();

// setup player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void setup(void) {
cfg.stream_name = "Stream1";
cfg.target_ip = IPAddress{192,168,1,37};
cfg.throttle_active = true;
cfg.throttle_correction_ms = -4; // optimize overload and underrun
//cfg.throttle_correction_us = 0; // optimize overload and underrun
if (!out.begin(cfg)) stop();

// Setup sine wave
Expand Down
22 changes: 14 additions & 8 deletions src/AudioLibs/Communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ struct ThrottleConfig : public AudioInfo {
bits_per_sample = 16;
channels = 2;
}
int correction_ms = 0;
int correction_us = 0;
};

/**
Expand All @@ -767,27 +767,33 @@ class Throttle {
void begin(ThrottleConfig info) {
this->info = info;
bytesPerSample = info.bits_per_sample / 8 * info.channels;
startDelay();
}

// starts the timing
void startDelay() { start_time = millis(); }
void startDelay() {
start_time = micros();
sum_samples = 0;
}

// delay
void delayBytes(size_t bytes) { delaySamples(bytes / bytesPerSample); }

// delay
void delaySamples(size_t samples) {
int durationMsEff = millis() - start_time;
int durationToBe = (samples * 1000) / info.sample_rate;
int waitMs = durationToBe - durationMsEff + info.correction_ms;
LOGI("wait: %d", waitMs);
if (waitMs > 0) {
delay(waitMs);
sum_samples += samples;
int64_t durationUsEff = micros() - start_time;
int64_t durationUsToBe = (sum_samples * 1000000) / info.sample_rate;
int64_t waitUs = durationUsToBe - durationUsEff + info.correction_us;
LOGI("wait: %d", waitUs);
if (waitUs > 0) {
delayMicroseconds(waitUs);
}
}

protected:
unsigned long start_time;
uint64_t sum_samples = 0;
ThrottleConfig info;
int bytesPerSample;
};
Expand Down
5 changes: 2 additions & 3 deletions src/AudioLibs/VBANStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class VBANConfig : public AudioInfo {
// set to true if samples are generated faster then sample rate
bool throttle_active = false;
// when negative the number of ms that are subtracted from the calculated wait time to fine tune Overload and Underruns
int throttle_correction_ms = -2;
int throttle_correction_us = 0;
// defines the max write size
int max_write_size = DEFAULT_BUFFER_SIZE * 2; // just good enough for 44100 stereo
};
Expand Down Expand Up @@ -57,7 +57,7 @@ class VBANStream : public AudioStream {
AudioStream::setAudioInfo(info);
auto thc = throttle.defaultConfig();
thc.copyFrom(info);
thc.correction_ms = cfg.throttle_correction_ms;;
thc.correction_us = cfg.throttle_correction_us;;
throttle.begin(thc);
if (cfg.mode==TX_MODE){
configure_tx();
Expand Down Expand Up @@ -111,7 +111,6 @@ class VBANStream : public AudioStream {
tx_buffer.reset();
}
}
throttle.startDelay();
return byteCount;
}

Expand Down

0 comments on commit 8089a7b

Please sign in to comment.