Skip to content

Commit c38ce7f

Browse files
authored
Change audio renderer output format (#149)
Instead of converting to Float, output original Int data without conversion. Output the raw format and convert when required.
1 parent 0ae5688 commit c38ce7f

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

sdk/objc/api/RTCAudioRendererAdapter.mm

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ void OnData(const void *audio_data, int bits_per_sample, int sample_rate,
3939
AudioStreamBasicDescription sd = {
4040
.mSampleRate = static_cast<Float64>(sample_rate),
4141
.mFormatID = kAudioFormatLinearPCM,
42-
.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked,
43-
.mBytesPerPacket = static_cast<UInt32>(number_of_channels * 4),
42+
.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
43+
.mBytesPerPacket = static_cast<UInt32>(number_of_channels * 2),
4444
.mFramesPerPacket = 1,
45-
.mBytesPerFrame = static_cast<UInt32>(number_of_channels * 4),
45+
.mBytesPerFrame = static_cast<UInt32>(number_of_channels * 2),
4646
.mChannelsPerFrame = static_cast<UInt32>(number_of_channels),
47-
.mBitsPerChannel = 32,
47+
.mBitsPerChannel = 16,
4848
.mReserved = 0};
4949

5050
CMFormatDescriptionRef formatDescription = nullptr;
@@ -69,16 +69,25 @@ void OnData(const void *audio_data, int bits_per_sample, int sample_rate,
6969
}
7070

7171
pcmBuffer.frameLength = frameCount;
72+
73+
// Handle both mono and stereo
7274
const int16_t *inputData = static_cast<const int16_t *>(audio_data);
73-
const float scale = 1.0f / 32768.0f;
74-
75-
dispatch_apply(number_of_channels, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
76-
^(size_t channel) {
77-
vDSP_vflt16(inputData + channel * number_of_frames, 1,
78-
pcmBuffer.floatChannelData[channel], 1, frameCount);
79-
vDSP_vsmul(pcmBuffer.floatChannelData[channel], 1, &scale,
80-
pcmBuffer.floatChannelData[channel], 1, frameCount);
81-
});
75+
if (number_of_channels == 1) {
76+
// Mono: straight copy
77+
memcpy(pcmBuffer.int16ChannelData[0], inputData, number_of_frames * sizeof(int16_t));
78+
} else if (number_of_channels == 2) {
79+
// Stereo: manual deinterleave
80+
int16_t *leftChannel = pcmBuffer.int16ChannelData[0];
81+
int16_t *rightChannel = pcmBuffer.int16ChannelData[1];
82+
83+
for (size_t i = 0; i < number_of_frames; i++) {
84+
leftChannel[i] = inputData[i * 2];
85+
rightChannel[i] = inputData[i * 2 + 1];
86+
}
87+
} else {
88+
NSLog(@"Unsupported number of channels: %zu", number_of_channels);
89+
return;
90+
}
8291

8392
[adapter_.audioRenderer renderPCMBuffer:pcmBuffer];
8493
}

0 commit comments

Comments
 (0)