1
This commit is contained in:
@@ -179,6 +179,11 @@ static const float kAudioSoftwareGain = 2.5f;
|
||||
}
|
||||
|
||||
// 获取 Int16 数据
|
||||
if (!outputBuffer.int16ChannelData) {
|
||||
NSLog(@"[AudioCaptureManager] Int16 channel data is null");
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t *samples = (int16_t *)outputBuffer.int16ChannelData[0];
|
||||
NSUInteger sampleCount = outputBuffer.frameLength;
|
||||
NSUInteger byteCount = sampleCount * sizeof(int16_t);
|
||||
@@ -189,13 +194,20 @@ static const float kAudioSoftwareGain = 2.5f;
|
||||
[self calculateAndReportRMS:samples sampleCount:sampleCount];
|
||||
[self logAudioStatsIfNeeded:samples sampleCount:sampleCount];
|
||||
|
||||
if (byteCount == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSData *pcmData = [NSData dataWithBytes:samples length:byteCount];
|
||||
|
||||
// 将数据添加到 ring buffer 并输出完整帧
|
||||
dispatch_async(self.audioQueue, ^{
|
||||
[self appendToRingBuffer:samples byteCount:byteCount];
|
||||
[self appendToRingBuffer:(const uint8_t *)pcmData.bytes
|
||||
byteCount:pcmData.length];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)appendToRingBuffer:(int16_t *)samples byteCount:(NSUInteger)byteCount {
|
||||
- (void)appendToRingBuffer:(const uint8_t *)bytes byteCount:(NSUInteger)byteCount {
|
||||
// 将新数据追加到 ring buffer
|
||||
uint8_t *ringBufferBytes = (uint8_t *)self.ringBuffer.mutableBytes;
|
||||
NSUInteger ringBufferLength = self.ringBuffer.length;
|
||||
@@ -208,7 +220,7 @@ static const float kAudioSoftwareGain = 2.5f;
|
||||
NSUInteger copySize = MIN(bytesToCopy, spaceAvailable);
|
||||
|
||||
memcpy(ringBufferBytes + self.ringBufferWriteIndex,
|
||||
(uint8_t *)samples + sourceOffset, copySize);
|
||||
bytes + sourceOffset, copySize);
|
||||
self.ringBufferWriteIndex += copySize;
|
||||
sourceOffset += copySize;
|
||||
bytesToCopy -= copySize;
|
||||
|
||||
Reference in New Issue
Block a user