Skip to content

Commit

Permalink
Change audio renderer output format (#149)
Browse files Browse the repository at this point in the history
Instead of converting to Float, output original Int data without
conversion.
Output the raw format and convert when required.
  • Loading branch information
hiroshihorie authored Oct 19, 2024
1 parent 0ae5688 commit c38ce7f
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions sdk/objc/api/RTCAudioRendererAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ void OnData(const void *audio_data, int bits_per_sample, int sample_rate,
AudioStreamBasicDescription sd = {
.mSampleRate = static_cast<Float64>(sample_rate),
.mFormatID = kAudioFormatLinearPCM,
.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked,
.mBytesPerPacket = static_cast<UInt32>(number_of_channels * 4),
.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
.mBytesPerPacket = static_cast<UInt32>(number_of_channels * 2),
.mFramesPerPacket = 1,
.mBytesPerFrame = static_cast<UInt32>(number_of_channels * 4),
.mBytesPerFrame = static_cast<UInt32>(number_of_channels * 2),
.mChannelsPerFrame = static_cast<UInt32>(number_of_channels),
.mBitsPerChannel = 32,
.mBitsPerChannel = 16,
.mReserved = 0};

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

pcmBuffer.frameLength = frameCount;

// Handle both mono and stereo
const int16_t *inputData = static_cast<const int16_t *>(audio_data);
const float scale = 1.0f / 32768.0f;

dispatch_apply(number_of_channels, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
^(size_t channel) {
vDSP_vflt16(inputData + channel * number_of_frames, 1,
pcmBuffer.floatChannelData[channel], 1, frameCount);
vDSP_vsmul(pcmBuffer.floatChannelData[channel], 1, &scale,
pcmBuffer.floatChannelData[channel], 1, frameCount);
});
if (number_of_channels == 1) {
// Mono: straight copy
memcpy(pcmBuffer.int16ChannelData[0], inputData, number_of_frames * sizeof(int16_t));
} else if (number_of_channels == 2) {
// Stereo: manual deinterleave
int16_t *leftChannel = pcmBuffer.int16ChannelData[0];
int16_t *rightChannel = pcmBuffer.int16ChannelData[1];

for (size_t i = 0; i < number_of_frames; i++) {
leftChannel[i] = inputData[i * 2];
rightChannel[i] = inputData[i * 2 + 1];
}
} else {
NSLog(@"Unsupported number of channels: %zu", number_of_channels);
return;
}

[adapter_.audioRenderer renderPCMBuffer:pcmBuffer];
}
Expand Down

0 comments on commit c38ce7f

Please sign in to comment.