Skip to content

Commit d996591

Browse files
author
eagleychen
committed
兼容高版本系统采样
1 parent a44762b commit d996591

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

Source/SDK/LinkVideo/FLV/TIoTAACEncoder.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ - (instancetype)initWithAudioDescription:(AudioStreamBasicDescription)inAudioDes
4646
if ( self = [super init]) {
4747
encodeQueue = dispatch_queue_create("com.audio.encode", DISPATCH_QUEUE_SERIAL);
4848
inAudioStreamBasicDescription = inAudioDes;
49+
inAudioStreamBasicDescription.mSampleRate = 16000;
4950
}
5051
return self;
5152
}

Source/SDK/LinkVideo/FLV/TIoTAVCaptionFLV.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ -(BOOL) startCapture {
564564
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
565565
// NSString *documentsDirectory = [paths firstObject];
566566
//
567-
// NSString *h264File = [documentsDirectory stringByAppendingPathComponent:@"test.pcm"];
567+
// NSString *h264File = [documentsDirectory stringByAppendingPathComponent:@"test.aac"];
568568
// [fileManager removeItemAtPath:h264File error:nil];
569569
// [fileManager createFileAtPath:h264File contents:nil attributes:nil];
570570
// _fileHandle = [NSFileHandle fileHandleForWritingAtPath:h264File];

Source/SDK/LinkVideo/FLV/TIoTPCMXEchoRecord.mm

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ - (instancetype)initWithChannel:(int)channel isEcho:(BOOL)isEcho
3232
}else {
3333
des.componentSubType = kAudioUnitSubType_RemoteIO;
3434
}
35-
if (@available(iOS 26.0, *)) {
36-
des.componentSubType = kAudioUnitSubType_RemoteIO;
37-
}
3835

3936
AudioComponent audioComponent;
4037
audioComponent = AudioComponentFindNext(NULL, &des);
@@ -43,9 +40,9 @@ - (instancetype)initWithChannel:(int)channel isEcho:(BOOL)isEcho
4340
return nil;
4441

4542
AudioStreamBasicDescription outStreamDes;
46-
outStreamDes.mSampleRate = 16000;
43+
outStreamDes.mSampleRate = 48000;
4744
outStreamDes.mFormatID = kAudioFormatLinearPCM;
48-
outStreamDes.mFormatFlags = kAudioFormatFlagIsSignedInteger;
45+
outStreamDes.mFormatFlags = kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;
4946
outStreamDes.mFramesPerPacket = 1;
5047
outStreamDes.mChannelsPerFrame = channel;
5148
outStreamDes.mBitsPerChannel = 16;
@@ -83,6 +80,34 @@ - (instancetype)initWithChannel:(int)channel isEcho:(BOOL)isEcho
8380

8481
TPCircularBuffer pcm_circularBuffer;
8582

83+
/**
84+
* 线性插值法重采样 48000Hz -> 16000Hz
85+
* 转换比例 3:1,适合实时处理
86+
*/
87+
uint8_t bufferData16[8192];
88+
- (void)resample48000To16000_Linear:(int16_t *)input
89+
output:(int16_t *)output
90+
inputFrames:(UInt32)inputFrames
91+
outputFrames:(UInt32 *)outputFrames {
92+
93+
float ratio = 48000.0f / 16000.0f; // 3.0
94+
*outputFrames = (UInt32)(inputFrames / ratio);
95+
96+
for (UInt32 i = 0; i < *outputFrames; i++) {
97+
float inputIndex = i * ratio;
98+
UInt32 index1 = (UInt32)inputIndex;
99+
UInt32 index2 = MIN(index1 + 1, inputFrames - 1);
100+
float fraction = inputIndex - index1;
101+
102+
// 线性插值
103+
float sample1 = input[index1];
104+
float sample2 = input[index2];
105+
float interpolated = sample1 + (sample2 - sample1) * fraction;
106+
107+
output[i] = (int16_t)interpolated;
108+
}
109+
}
110+
86111
static OSStatus record_callback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrame, AudioBufferList *__nullable ioData)
87112
{
88113
TIoTPCMXEchoRecord *r = (__bridge TIoTPCMXEchoRecord *)(inRefCon);
@@ -101,9 +126,17 @@ static OSStatus record_callback(void *inRefCon, AudioUnitRenderActionFlags *ioAc
101126
UInt32 bufferSize = list.mBuffers[0].mDataByteSize;
102127
uint8_t *bufferData = (uint8_t *)list.mBuffers[0].mData;
103128
// NSLog(@"record_callback__________size : %d", bufferSize);
104-
[r addData:&pcm_circularBuffer :bufferData :bufferSize];
129+
130+
131+
UInt32 bufferData16Size = 0;
132+
[r resample48000To16000_Linear:(int16_t *)bufferData output:(int16_t *)bufferData16 inputFrames:bufferSize outputFrames:&bufferData16Size];
133+
[r addData:&pcm_circularBuffer :bufferData16 :bufferData16Size];
105134
if (r->callback)
106-
r->callback(bufferData, bufferSize, r->user);
135+
r->callback(bufferData16, bufferData16Size, r->user);
136+
137+
// [r addData:&pcm_circularBuffer :bufferData :bufferSize];
138+
// if (r->callback)
139+
// r->callback(bufferData, bufferSize, r->user);
107140
return error;
108141
}
109142

0 commit comments

Comments
 (0)