Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(macos): read unsigned integer PCM around its midpoint

Etienne Lescotd392cf8641e2
diff --git a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
index d188949f8..47d62951e 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
@@ -566,7 +566,8 @@ public final class AudioTrackMixer {
// Where the significant bits sit when they do not fill the container.
// Irrelevant to packed formats, where the two coincide.
padBits: bytesPerSample * 8 - bitsPerChannel,
- isAlignedHigh: asbd.mFormatFlags & kAudioFormatFlagIsAlignedHigh != 0
+ isAlignedHigh: asbd.mFormatFlags & kAudioFormatFlagIsAlignedHigh != 0,
+ isSigned: asbd.mFormatFlags & kAudioFormatFlagIsSignedInteger != 0
)
)
}
@@ -710,6 +711,9 @@ public final class AudioTrackMixer {
let isFloat: Bool
let padBits: Int
let isAlignedHigh: Bool
+ /// Unsigned PCM (offset binary, the usual 8-bit layout) centres on the midpoint, not on
+ /// zero; read as signed it would come out as full-scale noise rather than be rejected.
+ let isSigned: Bool
func value(at frame: Int) -> Float {
let index = start + frame * stride
@@ -732,6 +736,10 @@ public final class AudioTrackMixer {
// Low-aligned: the padding is on top, so shift it out to put the sign bit on 31.
word <<= UInt32(padBits)
}
+ if !isSigned {
+ // With the value left-justified, subtracting the midpoint is flipping bit 31.
+ word ^= 0x8000_0000
+ }
return Float(Int32(bitPattern: word)) / 2_147_483_648
}
}
diff --git a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
index 56bef6942..64492a7ce 100644
--- a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
+++ b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
@@ -462,12 +462,14 @@ final class AudioTrackMixerTests: XCTestCase {
/// for it (issue #709). Every integer container CoreAudio describes must decode, at the right
/// level and with the right sign.
func testIntegerMicrophoneFormatsAreHeardAtTheirLevel() {
- let layouts: [(bits: Int, bytes: Int, alignedHigh: Bool, label: String)] = [
- (24, 3, false, "24-bit packed (the reported format)"),
- (24, 4, false, "24-bit in 4 bytes, low-aligned"),
- (24, 4, true, "24-bit in 4 bytes, high-aligned"),
- (16, 2, false, "16-bit"),
- (32, 4, false, "32-bit"),
+ let layouts: [(bits: Int, bytes: Int, alignedHigh: Bool, signed: Bool, label: String)] = [
+ (24, 3, false, true, "24-bit packed (the reported format)"),
+ (24, 4, false, true, "24-bit in 4 bytes, low-aligned"),
+ (24, 4, true, true, "24-bit in 4 bytes, high-aligned"),
+ (16, 2, false, true, "16-bit"),
+ (32, 4, false, true, "32-bit"),
+ (16, 2, false, false, "16-bit unsigned"),
+ (24, 3, false, false, "24-bit unsigned"),
]
for layout in layouts {
let sink = RecordingSink()
@@ -477,7 +479,8 @@ final class AudioTrackMixerTests: XCTestCase {
mixer.ingest(
makeIntegerMonoBuffer(
alternating: 0.5, frames: sampleRate / 5, at: clock.now,
- bits: layout.bits, bytes: layout.bytes, alignedHigh: layout.alignedHigh
+ bits: layout.bits, bytes: layout.bytes, alignedHigh: layout.alignedHigh,
+ signed: layout.signed
),
from: .microphone
)
@@ -637,12 +640,13 @@ final class AudioTrackMixerTests: XCTestCase {
at presentationTime: CMTime,
bits: Int,
bytes: Int,
- alignedHigh: Bool
+ alignedHigh: Bool,
+ signed: Bool = true
) -> CMSampleBuffer {
var asbd = AudioStreamBasicDescription(
mSampleRate: Float64(sampleRate),
mFormatID: kAudioFormatLinearPCM,
- mFormatFlags: kAudioFormatFlagIsSignedInteger
+ mFormatFlags: (signed ? kAudioFormatFlagIsSignedInteger : 0)
| (bits == bytes * 8 ? kAudioFormatFlagIsPacked : 0)
| (alignedHigh ? kAudioFormatFlagIsAlignedHigh : 0),
mBytesPerPacket: UInt32(bytes),
@@ -680,6 +684,10 @@ final class AudioTrackMixerTests: XCTestCase {
let sample = Double(frame % 2 == 0 ? amplitude : -amplitude)
var value = Int64((sample * fullScale).rounded())
value = min(max(value, -Int64(fullScale)), Int64(fullScale) - 1)
+ if !signed {
+ // Offset binary: the midpoint is silence.
+ value += Int64(fullScale)
+ }
// High-aligned puts the padding below the value; low-aligned sign-extends above it.
let word = UInt32(truncatingIfNeeded: alignedHigh ? value << Int64(bytes * 8 - bits) : value)
for byte in 0..<bytes {