Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix: carry AAC downsample remainder across packets
超超超超超级喜欢你的达妮娅93e439e8fad4
diff --git a/electron/native/wgc-capture/src/audio_sample_utils.cpp b/electron/native/wgc-capture/src/audio_sample_utils.cppindex ec6ecdbd7..96847ee7d 100644--- a/electron/native/wgc-capture/src/audio_sample_utils.cpp+++ b/electron/native/wgc-capture/src/audio_sample_utils.cpp@@ -5,6 +5,7 @@#include <algorithm>#include <chrono>#include <cmath>+#include <cstddef>#include <cstring>#include <limits>@@ -189,6 +190,19 @@ void convertAudioWithGain(const AudioInputFormat& targetFormat,double gain,std::vector<BYTE>& destination) {+ std::vector<BYTE> discardedRemainder;+ convertAudioWithGain(+ source, byteCount, sourceFormat, targetFormat, gain, destination, discardedRemainder);+}++void convertAudioWithGain(+ const BYTE* source,+ DWORD byteCount,+ const AudioInputFormat& sourceFormat,+ const AudioInputFormat& targetFormat,+ double gain,+ std::vector<BYTE>& destination,+ std::vector<BYTE>& remainder) {if (!source || byteCount == 0 || sourceFormat.blockAlign == 0 || targetFormat.blockAlign == 0 ||sourceFormat.sampleRate == 0 || targetFormat.sampleRate == 0 || sourceFormat.channels == 0 ||targetFormat.channels == 0) {@@ -201,8 +215,8 @@ void convertAudioWithGain(return;}- const size_t sourceFrames = byteCount / sourceFormat.blockAlign;- if (sourceFrames == 0) {+ const size_t packetFrames = byteCount / sourceFormat.blockAlign;+ if (packetFrames == 0) {destination.clear();return;}@@ -210,37 +224,56 @@ void convertAudioWithGain(// Integer-factor downsample (96 kHz / 192 kHz -> 48 kHz): average each// group of source frames instead of picking one. Nearest-neighbour// decimation aliases content above the new Nyquist into the recording.+ // Incomplete groups stay in remainder so the next packet can finish them.if (sourceFormat.sampleRate > targetFormat.sampleRate &&sourceFormat.sampleRate % targetFormat.sampleRate == 0) {const UINT32 factor = sourceFormat.sampleRate / targetFormat.sampleRate;- const size_t targetFrames = sourceFrames / factor;- if (targetFrames > 0) {- destination.assign(targetFrames * targetFormat.blockAlign, 0);- for (size_t targetFrame = 0; targetFrame < targetFrames; ++targetFrame) {- for (UINT32 channel = 0; channel < targetFormat.channels; ++channel) {- double sum = 0.0;- for (UINT32 tap = 0; tap < factor; ++tap) {- sum += readMappedChannel(- source,- sourceFormat,- targetFrame * factor + tap,- channel,- targetFormat.channels);- }- writeSampleFromDouble(- destination.data(),- targetFormat,- targetFrame,+ if (remainder.size() % sourceFormat.blockAlign != 0) {+ remainder.clear();+ }+ std::vector<BYTE> combined;+ combined.reserve(remainder.size() + byteCount);+ combined.insert(combined.end(), remainder.begin(), remainder.end());+ combined.insert(combined.end(), source, source + byteCount);+ const size_t totalFrames = combined.size() / sourceFormat.blockAlign;+ const size_t targetFrames = totalFrames / factor;+ const size_t consumedFrames = targetFrames * factor;+ const size_t leftoverBytes = (totalFrames - consumedFrames) * sourceFormat.blockAlign;+ if (targetFrames == 0) {+ destination.clear();+ remainder.swap(combined);+ return;+ }+ destination.assign(targetFrames * targetFormat.blockAlign, 0);+ for (size_t targetFrame = 0; targetFrame < targetFrames; ++targetFrame) {+ for (UINT32 channel = 0; channel < targetFormat.channels; ++channel) {+ double sum = 0.0;+ for (UINT32 tap = 0; tap < factor; ++tap) {+ sum += readMappedChannel(+ combined.data(),+ sourceFormat,+ targetFrame * factor + tap,channel,- (sum / static_cast<double>(factor)) * gain);+ targetFormat.channels);}+ writeSampleFromDouble(+ destination.data(),+ targetFormat,+ targetFrame,+ channel,+ (sum / static_cast<double>(factor)) * gain);}- return;}- // Too few source frames for one averaged output frame (tiny WASAPI- // packets). Fall through to interpolation instead of dropping them.+ remainder.assign(+ combined.begin() + static_cast<std::ptrdiff_t>(consumedFrames * sourceFormat.blockAlign),+ combined.end());+ if (remainder.size() != leftoverBytes) {+ remainder.resize(leftoverBytes);+ }+ return;}+ const size_t sourceFrames = packetFrames;const double rateRatio = static_cast<double>(targetFormat.sampleRate) /static_cast<double>(sourceFormat.sampleRate);const size_t targetFrames = std::max<size_t>(1, static_cast<size_t>(std::llround(sourceFrames * rateRatio)));@@ -338,6 +371,8 @@ bool AudioMixer::start() {emittedFrames_ = 0;timelineStarted_ = false;paused_ = false;+ systemResampleRemainder_.clear();+ microphoneResampleRemainder_.clear();thread_ = std::thread([this] {mixLoop();});@@ -349,6 +384,8 @@ void AudioMixer::beginTimeline() {std::scoped_lock lock(mutex_);systemQueue_.clear();microphoneQueue_.clear();+ systemResampleRemainder_.clear();+ microphoneResampleRemainder_.clear();emittedFrames_ = 0;timelineStarted_ = true;}@@ -362,6 +399,8 @@ void AudioMixer::setPaused(bool paused) {if (paused_) {systemQueue_.clear();microphoneQueue_.clear();+ systemResampleRemainder_.clear();+ microphoneResampleRemainder_.clear();}}cv_.notify_all();@@ -385,7 +424,7 @@ void AudioMixer::pushSystem(const BYTE* data, DWORD byteCount) {if (paused_) {return;}- append(systemQueue_, data, byteCount, systemFormat_, 1.0);+ append(systemQueue_, data, byteCount, systemFormat_, 1.0, systemResampleRemainder_);}cv_.notify_all();}@@ -400,7 +439,13 @@ void AudioMixer::pushMicrophone(const BYTE* data, DWORD byteCount) {if (paused_) {return;}- append(microphoneQueue_, data, byteCount, microphoneFormat_, microphoneGain_);+ append(+ microphoneQueue_,+ data,+ byteCount,+ microphoneFormat_,+ microphoneGain_,+ microphoneResampleRemainder_);}cv_.notify_all();}@@ -410,12 +455,13 @@ void AudioMixer::append(const BYTE* data,DWORD byteCount,const AudioInputFormat& sourceFormat,- double gain) {+ double gain,+ std::vector<BYTE>& remainder) {if (!data || byteCount == 0) {return;}- convertAudioWithGain(data, byteCount, sourceFormat, format_, gain, gainBuffer_);+ convertAudioWithGain(data, byteCount, sourceFormat, format_, gain, gainBuffer_, remainder);queue.insert(queue.end(), gainBuffer_.begin(), gainBuffer_.end());}diff --git a/electron/native/wgc-capture/src/audio_sample_utils.h b/electron/native/wgc-capture/src/audio_sample_utils.hindex 0bdbc0809..0f8e6b69f 100644--- a/electron/native/wgc-capture/src/audio_sample_utils.h+++ b/electron/native/wgc-capture/src/audio_sample_utils.h@@ -27,6 +27,14 @@ void convertAudioWithGain(const AudioInputFormat& targetFormat,double gain,std::vector<BYTE>& destination);+void convertAudioWithGain(+ const BYTE* source,+ DWORD byteCount,+ const AudioInputFormat& sourceFormat,+ const AudioInputFormat& targetFormat,+ double gain,+ std::vector<BYTE>& destination,+ std::vector<BYTE>& remainder);void mixAudioInPlace(std::vector<BYTE>& destination,const BYTE* source,@@ -63,7 +71,8 @@ private:const BYTE* data,DWORD byteCount,const AudioInputFormat& sourceFormat,- double gain);+ double gain,+ std::vector<BYTE>& remainder);bool pop(std::vector<BYTE>& queue, std::vector<BYTE>& chunk, size_t byteCount);void mixLoop();@@ -78,6 +87,8 @@ private:std::condition_variable cv_;std::vector<BYTE> systemQueue_;std::vector<BYTE> microphoneQueue_;+ std::vector<BYTE> systemResampleRemainder_;+ std::vector<BYTE> microphoneResampleRemainder_;std::vector<BYTE> gainBuffer_;std::thread thread_;std::atomic<bool> stopRequested_ = false;diff --git a/electron/native/wgc-capture/src/audio_sample_utils_test.cpp b/electron/native/wgc-capture/src/audio_sample_utils_test.cppindex b261a7d28..8b74b9144 100644--- a/electron/native/wgc-capture/src/audio_sample_utils_test.cpp+++ b/electron/native/wgc-capture/src/audio_sample_utils_test.cpp@@ -276,10 +276,21 @@ int main() {}expect("resample-96k-nyquist-box", folded, "frames=" + std::to_string(downFrames));+ auto fillStereoFrame = [](std::vector<BYTE>& packet, int16_t left, int16_t right) {+ auto* samples = reinterpret_cast<int16_t*>(packet.data());+ samples[0] = left;+ samples[1] = right;+ };+ const auto destFrames = [&](const std::vector<BYTE>& out) -> size_t {+ return target48k.blockAlign == 0 ? 0 : out.size() / target48k.blockAlign;+ };+ const auto remainderFrames = [&](const std::vector<BYTE>& rem) -> size_t {+ return source96k.blockAlign == 0 ? 0 : rem.size() / source96k.blockAlign;+ };++ std::vector<BYTE> remainder;std::vector<BYTE> shortPkt(source96k.blockAlign, 0);- auto* shortSamples = reinterpret_cast<int16_t*>(shortPkt.data());- shortSamples[0] = 12345;- shortSamples[1] = -12345;+ fillStereoFrame(shortPkt, 12345, -12345);std::vector<BYTE> shortOut;convertAudioWithGain(shortPkt.data(),@@ -287,13 +298,61 @@ int main() {source96k,target48k,1.0,- shortOut);- const bool shortOk = !shortOut.empty() && target48k.blockAlign != 0 &&- (shortOut.size() % target48k.blockAlign == 0);+ shortOut,+ remainder);expect("resample-96k-short-packet",- shortOk,- "bytes=" + std::to_string(shortOut.size()));+ shortOut.empty() && remainderFrames(remainder) == 1,+ "dest=" + std::to_string(shortOut.size()) + " rem=" + std::to_string(remainder.size()));++ remainder.clear();+ std::vector<BYTE> oneA(source96k.blockAlign, 0);+ std::vector<BYTE> oneB(source96k.blockAlign, 0);+ fillStereoFrame(oneA, 1000, 2000);+ fillStereoFrame(oneB, 3000, 4000);+ std::vector<BYTE> outA;+ std::vector<BYTE> outB;+ convertAudioWithGain(oneA.data(), static_cast<DWORD>(oneA.size()), source96k, target48k, 1.0, outA, remainder);+ convertAudioWithGain(oneB.data(), static_cast<DWORD>(oneB.size()), source96k, target48k, 1.0, outB, remainder);+ expect(+ "resample-96k-one-frame-packets",+ destFrames(outA) == 0 && destFrames(outB) == 1 && remainder.empty(),+ "a=" + std::to_string(destFrames(outA)) + " b=" + std::to_string(destFrames(outB)) ++ " rem=" + std::to_string(remainderFrames(remainder)));++ remainder.clear();+ std::vector<BYTE> threePkt(3 * source96k.blockAlign, 0);+ std::vector<BYTE> onePkt(source96k.blockAlign, 0);+ fillStereoFrame(onePkt, 5000, 6000);+ std::vector<BYTE> threeOut;+ std::vector<BYTE> oneOut;+ convertAudioWithGain(+ threePkt.data(), static_cast<DWORD>(threePkt.size()), source96k, target48k, 1.0, threeOut, remainder);+ convertAudioWithGain(+ onePkt.data(), static_cast<DWORD>(onePkt.size()), source96k, target48k, 1.0, oneOut, remainder);+ expect(+ "resample-96k-remainder-three-then-one",+ destFrames(threeOut) == 1 && destFrames(oneOut) == 1 && remainder.empty(),+ "three=" + std::to_string(destFrames(threeOut)) + " one=" + std::to_string(destFrames(oneOut)) ++ " rem=" + std::to_string(remainderFrames(remainder)));++ remainder.clear();+ std::vector<BYTE> remainderFullOut;+ convertAudioWithGain(+ source.data(),+ static_cast<DWORD>(source.size()),+ source96k,+ target48k,+ 1.0,+ remainderFullOut,+ remainder);+ const size_t remainderFullFrames = destFrames(remainderFullOut);+ const bool remainderFullOk =+ remainderFullFrames == 48000 || remainderFullFrames == 47999 || remainderFullFrames == 48001;+ expect(+ "resample-96k-remainder-full",+ remainderFullOk && remainder.empty(),+ "frames=" + std::to_string(remainderFullFrames) + " rem=" + std::to_string(remainder.size()));HRESULT mfHr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);if (FAILED(mfHr) && mfHr != RPC_E_CHANGED_MODE) {