Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Answer 44100 on the microphone as the speaker does

Jacky1544f46b4638
diff --git a/Research/Guest/virtio_sound_microphone.md b/Research/Guest/virtio_sound_microphone.md
index a16c4083..c1606fac 100644
--- a/Research/Guest/virtio_sound_microphone.md
+++ b/Research/Guest/virtio_sound_microphone.md
@@ -429,12 +429,59 @@ back as NULL/zero`.
* **Stream formats**: a format set that `ASDStream` refuses becomes the
device's rate change when the device supports the rate (§4), on both
kinds of stream.
-* **Device**: `canBeDefaultInputDevice`, `inputSafetyOffset` 100, wire rate
- only (the 44100 twin is the speaker's; captured frames are not resampled),
+* **Device**: `canBeDefaultInputDevice`, `inputSafetyOffset` 100, the wire
+ rate and the speaker's 44100 twin (captured frames are converted as they
+ are read; "Reading at 44100" below),
`willDoReadInputBlock` answering for the device's own direction, and the
macOS plugin's input control set: data source `'imic'` "Microphone", mute
and volume in the input scope, built the way the speaker's are.
+### Reading at 44100
+
+The speaker's device answers 44100 beside the wire's 48000, and a session
+that plays and records sets the rate it wants on both streams of its
+aggregate. With the microphone answering 48000 only, a session that asked for
+44100 did not fail; it was given 48000. Measured on `avtest-ipad`
+(iPad16,1 / 26.6.2) with a test app (`playAndRecord`, preferred rate 44100,
+an `AVAudioEngine` playing a 440 Hz tone and tapping its input):
+
+```
+VirtualAudio_Device.cpp:3135 Client request to set nominal sample rate to 44100.000000 on VAD: '[vdef]'.
+HALS_AHPPlugIn.cpp:126 HALS_AHPPlugIn::ObjectSetPropertyData: got an error from the plug-in routine …, Error: 560226676
+-CMVAEndptMgr- vaemSetSampleRateForDevice: FAILED to set default sample rate. Giving up.
+-CMSessionMgr- cmsSetDeviceSampleRateAndBufferSize: Unable to set sample rate. But let's keep going without exiting this routine.
+```
+
+The app then ran at 48000 and captured 47,600 frames a second. A device with
+a codec honours 44100, and an app that builds its formats on the rate it
+asked for does not expect otherwise, so the microphone now answers it too.
+
+The virtio stream stays at the wire rate, as the speaker's does. The input
+stream carries a 44100 physical format beside its own, the device lists both
+rates, and when the device runs at the other one the read block converts:
+`VPVirtIOSoundConverter` (`VPVirtIOSoundConverter.[ch]`, tested on the host)
+is asked how many wire frames the read takes, those come from the reader as
+before, and each frame handed to the HAL is the straight line between the two
+wire frames around it. The phase is kept in whole units of 1/44100 of a wire
+frame, so the count is exact and the ratio does not drift. It is the
+speaker path's conversion turned around, and it filters nothing first: what
+lies between 22,050 and 24,000 Hz folds back, which a microphone has next to
+none of. Only 32-bit float with one or two channels is converted, which is
+what the host sends; any other format keeps the wire rate alone.
+
+The same app after the change, both rates in one launch:
+
+| Asked | Session | App captured | Plugin, microphone | Plugin, speaker |
+| --- | --- | --- | --- | --- |
+| 48000 | 48000 | 571,200 frames in 12 s | `141 reads, in 577536 frames (47937/s), out 568960 frames, 8960 silent, 0 bytes skipped` | `142 writes, 0 starved, … (47971/s, hal 48000)` |
+| 44100 | 44100 | 524,790 frames in 12 s; 44,100 a second after the first | `device rate 48000 -> 44100`, `141 reads, in 577536 frames (47862/s), out 568425 frames, 10031 silent, 0 bytes skipped` | `142 writes, 0 starved, … (44046/s, hal 44100)` |
+
+`out` on the microphone line counts wire frames whatever rate the HAL reads
+at. The pitch says the conversion runs the right way: with the guest's tone
+coming out of the Mac's speakers into its microphone, the capture's power at
+440 Hz was 44–45 dB at both rates, against 9–18 dB at 404 Hz and 479 Hz,
+where a rate taken the wrong way round would have put it.
+
## 6. Reveal and validation
Measured on `mictest-ipad` (iPad16,1, iPadOS 26.6.2), recording in Voice
@@ -518,11 +565,9 @@ Not done:
* Whether the kernel returns reads still out at RELEASE is not known. The
plugin does not depend on it in the normal stop; the one-second fallback
does, and was not reached.
-* A session at 44100 that both plays and records sets each stream's format
- in the aggregate, and the microphone device answers 48000 only, so that
- set is refused and the route would fail as it did before §4's change.
- Voice Memos records at 48000. If an app needs it, the reader needs the
- speaker path's resampler, turned around.
+* The capture conversion to 44100 is a straight line between wire frames,
+ with nothing filtered first (§5, "Reading at 44100"). What that costs a
+ recording has not been listened to or measured beyond the tone's pitch.
* `ssrc` on the input device (§4) is refused. Nothing depended on it.
* The 1.9 s pause at the first capture (§6) is the host's. If it matters,
the lead queued on the speaker side is what would cover it.
diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefile
index 4a37810a..e4580d56 100644
--- a/VPhoneGuestComponents/Makefile
+++ b/VPhoneGuestComponents/Makefile
@@ -115,7 +115,7 @@ test-injection-environment:
@$(OUT)/InjectionEnvironmentTests
# Host proof harness for the virtio-snd format choice, the rings, the input
-# reader and the clock.
+# reader, the clock and the capture rate converter.
test-virtiosound:
@mkdir -p $(OUT)
xcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -IVirtIOSound \
@@ -126,6 +126,10 @@ test-virtiosound:
-o $(OUT)/VirtIOSoundClockTests \
Tests/VirtIOSoundClockTests.c VirtIOSound/VPVirtIOSoundClock.c
@$(OUT)/VirtIOSoundClockTests
+ xcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -IVirtIOSound \
+ -o $(OUT)/VirtIOSoundConverterTests \
+ Tests/VirtIOSoundConverterTests.c VirtIOSound/VPVirtIOSoundConverter.c
+ @$(OUT)/VirtIOSoundConverterTests
# Host proof harness for the camera data plane the guest hooks share.
test-vcam-dataplane:
diff --git a/VPhoneGuestComponents/Tests/VirtIOSoundConverterTests.c b/VPhoneGuestComponents/Tests/VirtIOSoundConverterTests.c
new file mode 100644
index 00000000..82dec702
--- /dev/null
+++ b/VPhoneGuestComponents/Tests/VirtIOSoundConverterTests.c
@@ -0,0 +1,123 @@
+// VirtIOSoundConverterTests.c — host checks for the capture rate converter:
+// how many wire frames a read takes, and what the frames it produces are.
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "VPVirtIOSoundConverter.h"
+
+static int failures;
+
+#define CHECK(condition) \
+ do { \
+ if (!(condition)) { \
+ fprintf(stderr, "%s:%d: CHECK(%s) failed\n", __FILE__, __LINE__, \
+ #condition); \
+ failures++; \
+ } \
+ } while (0)
+
+/// The wire signal: a ramp, so a frame interpolated at position p is p on
+/// the first channel and -p on the second.
+static void fillRamp(float *frames, uint32_t first, uint32_t count, uint32_t channels) {
+ for (uint32_t i = 0; i < count; i++) {
+ frames[i * channels] = (float)(first + i);
+ if (channels > 1) {
+ frames[i * channels + 1] = -(float)(first + i);
+ }
+ }
+}
+
+/// Reads `total` frames in blocks of the sizes given, cycling through them,
+/// and returns the wire frames taken. Every frame is checked against the
+/// ramp's value where it falls.
+static uint64_t readRamp(VPVirtIOSoundConverter *converter, uint32_t channels,
+ const uint32_t *blocks, uint32_t blockCount, uint64_t total) {
+ static float input[4096 * 2];
+ static float output[2048 * 2];
+ const double step = (double)converter->inputRate / converter->outputRate;
+ uint64_t taken = 0;
+ uint64_t produced = 0;
+ for (uint32_t turn = 0; produced < total; turn++) {
+ uint32_t block = blocks[turn % blockCount];
+ uint32_t needed = VPVirtIOSoundConverterInputFrames(converter, block);
+ CHECK(needed <= 4096);
+ fillRamp(input, (uint32_t)taken, needed, channels);
+ VPVirtIOSoundConverterProcess(converter, input, output, block);
+ for (uint32_t i = 0; i < block; i++) {
+ double expected = (double)(produced + i) * step;
+ // At the far end of the ramp a float is good to 1/32, and a frame
+ // taken from the wrong place would be off by a whole one; the
+ // interpolation is checked to a tenth.
+ CHECK(fabs(output[i * channels] - expected) < 0.1);
+ if (channels > 1) {
+ CHECK(fabs(output[i * channels + 1] + expected) < 0.1);
+ }
+ }
+ taken += needed;
+ produced += block;
+ }
+ return taken;
+}
+
+static void testFirstFrameIsTheFirstWireFrame(void) {
+ VPVirtIOSoundConverter converter;
+ VPVirtIOSoundConverterReset(&converter, 48000, 44100, 1);
+ CHECK(VPVirtIOSoundConverterInputFrames(&converter, 0) == 0);
+ // One frame out needs both of its neighbours.
+ CHECK(VPVirtIOSoundConverterInputFrames(&converter, 1) == 2);
+ float input[2] = {7, 9};
+ float output[1] = {0};
+ VPVirtIOSoundConverterProcess(&converter, input, output, 1);
+ CHECK(output[0] == 7);
+ // The next lies 48000/44100 of a wire frame on, past the second.
+ CHECK(VPVirtIOSoundConverterInputFrames(&converter, 1) == 1);
+}
+
+static void testTakesWireFramesAtTheRatioOfTheRates(void) {
+ VPVirtIOSoundConverter converter;
+ VPVirtIOSoundConverterReset(&converter, 48000, 44100, 2);
+ const uint32_t blocks[] = {512, 1, 441, 1024, 37};
+ // Ten seconds: the two frames the start takes ahead, then 48000 wire
+ // frames for every 44100 read, give or take the one in progress.
+ uint64_t taken = readRamp(&converter, 2, blocks, 5, 441000);
+ CHECK(taken >= 480000 && taken <= 480000 + 1200);
+ uint64_t again = readRamp(&converter, 2, blocks, 5, 0);
+ CHECK(again == 0);
+}
+
+static void testSameFramesWhateverTheBlockSize(void) {
+ VPVirtIOSoundConverter whole, pieces;
+ VPVirtIOSoundConverterReset(&whole, 48000, 44100, 1);
+ VPVirtIOSoundConverterReset(&pieces, 48000, 44100, 1);
+ const uint32_t one[] = {1470};
+ const uint32_t odd[] = {1, 7, 333, 2, 1127};
+ uint64_t takenWhole = readRamp(&whole, 1, one, 1, 1470);
+ uint64_t takenPieces = readRamp(&pieces, 1, odd, 5, 1470);
+ CHECK(takenWhole == takenPieces);
+ CHECK(whole.phase == pieces.phase);
+ CHECK(whole.current[0] == pieces.current[0]);
+}
+
+static void testGoesUpAsWellAsDown(void) {
+ VPVirtIOSoundConverter converter;
+ VPVirtIOSoundConverterReset(&converter, 44100, 48000, 1);
+ const uint32_t blocks[] = {480, 33};
+ uint64_t taken = readRamp(&converter, 1, blocks, 2, 48000);
+ CHECK(taken >= 44100 && taken <= 44100 + 520);
+}
+
+int main(void) {
+ testFirstFrameIsTheFirstWireFrame();
+ testTakesWireFramesAtTheRatioOfTheRates();
+ testSameFramesWhateverTheBlockSize();
+ testGoesUpAsWellAsDown();
+ if (failures) {
+ fprintf(stderr, "VirtIOSoundConverterTests: %d failure(s)\n", failures);
+ return 1;
+ }
+ printf("VirtIOSoundConverterTests: all passed\n");
+ return 0;
+}
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundConverter.c b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundConverter.c
new file mode 100644
index 00000000..ed0314e7
--- /dev/null
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundConverter.c
@@ -0,0 +1,56 @@
+// VPVirtIOSoundConverter.c — see the header.
+
+#include "VPVirtIOSoundConverter.h"
+
+void VPVirtIOSoundConverterReset(VPVirtIOSoundConverter *converter,
+ uint32_t inputRate, uint32_t outputRate, uint32_t channels) {
+ converter->inputRate = inputRate;
+ converter->outputRate = outputRate;
+ converter->channels = channels;
+ for (uint32_t channel = 0; channel < kVPVirtIOSoundConverterMaximumChannels; channel++) {
+ converter->last[channel] = 0;
+ converter->current[channel] = 0;
+ }
+ // Two frames past the empty pair: the first output frame needs both of
+ // its neighbours, and with them in place it is the first of them.
+ converter->phase = 2 * (uint64_t)outputRate;
+}
+
+uint32_t VPVirtIOSoundConverterInputFrames(const VPVirtIOSoundConverter *converter, uint32_t outputFrames) {
+ if (outputFrames == 0) {
+ return 0;
+ }
+ // The pair moves on once for every whole wire frame the last output
+ // frame's position has passed.
+ return (uint32_t)((converter->phase + (uint64_t)(outputFrames - 1) * converter->inputRate)
+ / converter->outputRate);
+}
+
+void VPVirtIOSoundConverterProcess(VPVirtIOSoundConverter *converter,
+ const float *input, float *output, uint32_t outputFrames) {
+ const uint32_t channels = converter->channels;
+ const uint64_t inputRate = converter->inputRate;
+ const uint64_t outputRate = converter->outputRate;
+ uint64_t phase = converter->phase;
+ uint32_t produced = 0;
+ while (produced < outputFrames) {
+ if (phase >= outputRate) {
+ phase -= outputRate;
+ for (uint32_t channel = 0; channel < channels; channel++) {
+ converter->last[channel] = converter->current[channel];
+ converter->current[channel] = input[channel];
+ }
+ input += channels;
+ continue;
+ }
+ const float fraction = (float)phase / (float)outputRate;
+ for (uint32_t channel = 0; channel < channels; channel++) {
+ output[channel] = converter->last[channel]
+ + (converter->current[channel] - converter->last[channel]) * fraction;
+ }
+ output += channels;
+ produced++;
+ phase += inputRate;
+ }
+ converter->phase = phase;
+}
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundConverter.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundConverter.h
new file mode 100644
index 00000000..4cf49893
--- /dev/null
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundConverter.h
@@ -0,0 +1,49 @@
+// VPVirtIOSoundConverter.h — captured frames at another rate than the wire's.
+//
+// The virtio stream captures at one rate, the wire rate. A session that plays
+// and records at the speaker's other rate reads the microphone at that rate
+// too, so what the ring holds is converted as it is read: the frames a read
+// asks for are interpolated, in a straight line, from the wire frames around
+// them. That is the speaker path's conversion turned around. It filters
+// nothing first: going down from 48000 to 44100 folds what lies above
+// 22050 Hz back, which a microphone has next to none of.
+//
+// A pull converter: the caller asks how many wire frames the next read
+// takes, fetches exactly those, and hands them over. The phase is kept in
+// whole units of 1/outputRate of a wire frame, so the count is exact and the
+// long-run ratio is the ratio of the rates.
+
+#ifndef VPVirtIOSoundConverter_h
+#define VPVirtIOSoundConverter_h
+
+#include <stdint.h>
+
+enum { kVPVirtIOSoundConverterMaximumChannels = 2 };
+
+typedef struct {
+ uint32_t inputRate;
+ uint32_t outputRate;
+ uint32_t channels;
+ /// The two wire frames the next output frame lies between.
+ float last[kVPVirtIOSoundConverterMaximumChannels];
+ float current[kVPVirtIOSoundConverterMaximumChannels];
+ /// Where it lies between them, in 1/outputRate of a wire frame. At or
+ /// past outputRate the pair moves on before anything is produced.
+ uint64_t phase;
+} VPVirtIOSoundConverter;
+
+/// Start over: the first output frame is the first wire frame handed over.
+/// `channels` is 1 or 2, interleaved 32-bit float.
+void VPVirtIOSoundConverterReset(VPVirtIOSoundConverter *converter,
+ uint32_t inputRate, uint32_t outputRate, uint32_t channels);
+
+/// The wire frames the next `outputFrames` frames take.
+uint32_t VPVirtIOSoundConverterInputFrames(const VPVirtIOSoundConverter *converter, uint32_t outputFrames);
+
+/// Produce `outputFrames` frames from `input`, which holds exactly
+/// `VPVirtIOSoundConverterInputFrames(converter, outputFrames)` wire frames.
+/// Real-time safe.
+void VPVirtIOSoundConverterProcess(VPVirtIOSoundConverter *converter,
+ const float *input, float *output, uint32_t outputFrames);
+
+#endif /* VPVirtIOSoundConverter_h */
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
index 3014ac5d..99fcbc75 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
@@ -51,6 +51,7 @@
#include <unistd.h>
#include "VPVirtIOSoundClock.h"
+#include "VPVirtIOSoundConverter.h"
#include "VPVirtIOSoundProtocol.h"
#include "VPVirtIOSoundRing.h"
@@ -581,6 +582,9 @@ typedef struct {
@property (nonatomic, readonly) UInt32 queuedLatencyFrames;
/// The device the stream belongs to, which is where its rate is changed.
@property (weak, nonatomic) ASDAudioDevice *device;
+/// Whether the stream answers `kVPAlternateRate` beside the wire rate,
+/// converting between the two itself.
+@property (nonatomic, readonly) BOOL offersAlternateRate;
/// What the device's volume and mute controls come to, 0 to 1.
- (void)setGain:(float)gain;
@end
@@ -639,6 +643,10 @@ typedef struct {
return format;
}
+- (BOOL)offersAlternateRate {
+ return NO;
+}
+
- (void)logFormatWithLeadPeriods:(uint32_t)leadPeriods {
const char *direction = _direction == ASDStreamDirectionInput ? "input" : "output";
os_log(VPLog(), "stream %u: %{public}s, %.0f Hz, %u channels, %u-bit %{public}s, period %u bytes",
@@ -910,6 +918,10 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
atomic_store_explicit(&_mix.gainBits, VPGainBits(gain), memory_order_relaxed);
}
+- (BOOL)offersAlternateRate {
+ return _format.sampleRate == 48000.0;
+}
+
// MARK: Rate changes
- (void)deviceChangedToSamplingRate:(double)rate {
@@ -1190,9 +1202,64 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
[stream readCompletedAtOffset:offset result:result argument:(uint64_t)(uintptr_t)argument];
}
+/// Frames per converter pass; 512 out at 48000→44100 take at most 559 in,
+/// so a 1024-frame scratch bounds the allocation.
+static const uint32_t kCaptureChunkFrames = 512;
+static const uint32_t kCaptureScratchFrames = 1024;
+
+/// What `readInputBlock` captures instead of the stream object, as the mix
+/// block does (`VPMixState`).
+typedef struct {
+ VPVirtIOSoundInputReader *reader;
+ uint32_t bytesPerFrame;
+ uint32_t wireRate;
+ /// The rate CoreAudio reads the stream at, which is the wire rate until
+ /// a route that plays and records at 44100 switches the device. The
+ /// read block reads it, the rate change writes it.
+ _Atomic uint32_t halRate;
+ /// The conversion between the two, the I/O thread's own. `scratch` is
+ /// NULL for a format the converter does not take; the stream then
+ /// offers the wire rate only.
+ VPVirtIOSoundConverter converter;
+ float *scratch;
+ /// Set when the stream starts: the converter starts over, as the reader
+ /// does.
+ _Atomic bool restart;
+} VPCaptureState;
+
+/// Fills `buffer` with `frameCount` captured frames at the rate CoreAudio
+/// reads at, or with silence where the reader has none.
+static void VPCaptureRead(VPCaptureState *capture, void *buffer, UInt32 frameCount) {
+ uint32_t halRate = atomic_load_explicit(&capture->halRate, memory_order_relaxed);
+ bool restart = atomic_exchange_explicit(&capture->restart, false, memory_order_acq_rel);
+ if (halRate == capture->wireRate || capture->scratch == NULL) {
+ VPVirtIOSoundInputReaderRead(capture->reader, buffer, frameCount);
+ return;
+ }
+ VPVirtIOSoundConverter *converter = &capture->converter;
+ if (restart || converter->outputRate != halRate) {
+ VPVirtIOSoundConverterReset(converter, capture->wireRate, halRate, converter->channels);
+ }
+ UInt32 done = 0;
+ while (done < frameCount) {
+ uint32_t chunk = MIN(frameCount - done, kCaptureChunkFrames);
+ float *output = (float *)((uint8_t *)buffer + done * capture->bytesPerFrame);
+ uint32_t needed = VPVirtIOSoundConverterInputFrames(converter, chunk);
+ if (needed > kCaptureScratchFrames) {
+ // No pair of rates the device offers comes near this.
+ memset(output, 0, chunk * capture->bytesPerFrame);
+ } else {
+ VPVirtIOSoundInputReaderRead(capture->reader, capture->scratch, needed);
+ VPVirtIOSoundConverterProcess(converter, capture->scratch, output, chunk);
+ }
+ done += chunk;
+ }
+}
+
@implementation VPVirtIOSoundInputStream {
VPVirtIOSoundInputRing _ring;
VPVirtIOSoundInputReader _reader;
+ VPCaptureState _capture;
/// Counts stops, so a drain deadline knows the stop it was set for.
uint64_t _stops;
/// CoreAudio started the stream while it was `Releasing`.
@@ -1209,7 +1276,7 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
/// ASDStream releases this block at every stop, as it does the output
/// stream's (`installMixBlock`), so it goes back in before every start.
- (void)installReadBlock {
- VPVirtIOSoundInputReader *reader = &_reader;
+ VPCaptureState *capture = &_capture;
_Atomic bool *muted = &_muted;
uint32_t bytesPerFrame = _format.bytesPerFrame;
self.readInputBlock = ^int(UInt32 frameCount, const AudioServerPlugInIOCycleInfo *cycleInfo,
@@ -1218,7 +1285,7 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
(void)secondaryBuffer;
(void)clientID;
// Read even when muted, so the ring keeps moving.
- VPVirtIOSoundInputReaderRead(reader, mainBuffer, frameCount);
+ VPCaptureRead(capture, mainBuffer, frameCount);
if (atomic_load_explicit(muted, memory_order_relaxed)) {
memset(mainBuffer, 0, frameCount * bytesPerFrame);
}
@@ -1249,15 +1316,60 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
.leadBytes = kInputLeadPeriods * _periodBytes,
.maximumBacklogBytes = kInputMaximumBacklogPeriods * _periodBytes,
};
+ _capture = (VPCaptureState){
+ .reader = &_reader,
+ .bytesPerFrame = format.bytesPerFrame,
+ .wireRate = (uint32_t)format.sampleRate,
+ };
+ atomic_init(&_capture.halRate, (uint32_t)format.sampleRate);
+ // The 44100 twin, as the speaker's stream has it: a route that plays and
+ // records at that rate sets it on both streams of its aggregate, and
+ // fails when one refuses. The virtio stream stays at the wire rate;
+ // captured frames are converted as they are read. Only for what the
+ // converter takes, which is what the host sends.
+ if (format.sampleRate == 48000.0 && format.isFloat && format.bitsPerChannel == 32
+ && format.channels <= kVPVirtIOSoundConverterMaximumChannels) {
+ _capture.scratch = malloc(kCaptureScratchFrames * format.bytesPerFrame);
+ }
+ if (_capture.scratch) {
+ VPVirtIOSoundConverterReset(&_capture.converter, _capture.wireRate, (uint32_t)kVPAlternateRate,
+ format.channels);
+ ASDStreamFormat *reduced = [self physicalFormatAtRate:kVPAlternateRate];
+ self.physicalFormats = @[self.physicalFormat, reduced];
+ self.physicalFormatSettable = YES;
+ if (VPNominalRateOverride() == kVPAlternateRate) {
+ self.physicalFormat = reduced;
+ atomic_store(&_capture.halRate, (uint32_t)kVPAlternateRate);
+ }
+ }
[self installReadBlock];
[self logFormatWithLeadPeriods:kInputLeadPeriods];
return self;
}
- (void)dealloc {
+ free(_capture.scratch);
VPVirtIOSoundInputRingDestroy(&_ring);
}
+- (BOOL)offersAlternateRate {
+ return _capture.scratch != NULL;
+}
+
+// MARK: Rate changes
+
+- (void)deviceChangedToSamplingRate:(double)rate {
+ [super deviceChangedToSamplingRate:rate];
+ uint32_t previous = atomic_load(&_capture.halRate);
+ if (rate > 0 && (uint32_t)rate != previous) {
+ atomic_store(&_capture.halRate, (uint32_t)rate);
+ os_log(VPLog(), "stream %u: device rate %.0f -> %.0f, physical format %.0f Hz",
+ _streamID, (double)previous, rate, self.physicalFormat.sampleRate);
+ VPLogToFile("stream %u: device rate %.0f -> %.0f, physical format %.0f Hz",
+ _streamID, (double)previous, rate, self.physicalFormat.sampleRate);
+ }
+}
+
/// What the reader keeps between the host's capture and the guest's read.
- (UInt32)queuedLatencyFrames {
return kInputLeadPeriods * (_periodBytes / _format.bytesPerFrame);
@@ -1412,6 +1524,7 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
self->_startedAt = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
// What an earlier run left in the ring is old by now.
atomic_store(&self->_reader.restart, true);
+ atomic_store(&self->_capture.restart, true);
switch (self->_state) {
case VPStreamStateIdle:
[self startDevice];
@@ -1626,6 +1739,7 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
double sampleRate = 0;
UInt32 latencyFrames = 0;
+ BOOL alternate = YES;
for (uint32_t streamID = 0; streamID < connection.streamCount; streamID++) {
VPVirtIOSoundPCMInfo info;
if (![connection getInfo:&info forStream:streamID]) {
@@ -1666,6 +1780,7 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
[_streams addObject:stream];
sampleRate = format.sampleRate;
latencyFrames = stream.queuedLatencyFrames;
+ alternate = alternate && stream.offersAlternateRate;
if (input) {
[self addInputStream:stream];
} else {
@@ -1677,9 +1792,9 @@ static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
}
_wireRate = sampleRate;
- // Only the mix converts between rates; captured frames reach CoreAudio
- // at the wire rate.
- _alternateRate = !input && sampleRate == 48000.0 ? kVPAlternateRate : 0;
+ // The streams convert between the two rates themselves, the mix as it
+ // is written and captured frames as they are read.
+ _alternateRate = alternate ? kVPAlternateRate : 0;
// The nominal rate the device answers at boot: the wire rate unless the
// override names the alternate (testing VirtualAudio's aggregate-member
// selection, which may compare the current nominal rate, not the list).