Skip to content

Commit a63cfca

Browse files
committed
更换变声器
Change-Id: I8a16fc8e1abd670efc3fa7406aefda13aeb80031
1 parent 384a05b commit a63cfca

File tree

8 files changed

+153
-65
lines changed

8 files changed

+153
-65
lines changed

Podfile.lock

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ PODS:
3131
- YYModel (= 1.0.4)
3232
- TIoTLinkKit_FLV (2.2.3)
3333
- TIoTLinkKit_IJKPlayer (2.0.8-beta.202212290234)
34-
- TIoTLinkKit_SoundTouch (0.0.4)
34+
- TIoTLinkKit_SoundTouch (0.0.5)
3535
- TIoTLinkKit_TPNS (1.0.1)
3636
- TIoTLinkKit_WechatOpenSDK (1.0.1)
3737
- TIoTLinkKit_XP2P (2.4.34-beta.202211150834)
3838
- TIoTLinkVideo (1.0.0):
3939
- CocoaAsyncSocket (= 7.6.5)
4040
- TIoTLinkKit_FLV (= 2.2.3)
41-
- TIoTLinkKit_SoundTouch (= 0.0.4)
41+
- TIoTLinkKit_SoundTouch
4242
- TIoTLinkKit_XP2P (= 2.4.34-beta.202211150834)
43+
- TPCircularBuffer (= 1.6.1)
44+
- TPCircularBuffer (1.6.1)
4345
- TrueTime (5.0.3)
4446
- TXLiteAVSDK_TRTC (8.0.9644)
4547
- TZImagePickerController (3.2.1)
@@ -93,6 +95,7 @@ SPEC REPOS:
9395
- TIoTLinkKit_TPNS
9496
- TIoTLinkKit_WechatOpenSDK
9597
- TIoTLinkKit_XP2P
98+
- TPCircularBuffer
9699
- TrueTime
97100
- TXLiteAVSDK_TRTC
98101
- TZImagePickerController
@@ -123,11 +126,12 @@ SPEC CHECKSUMS:
123126
TIoTLinkKit: 0386fb583c61765a02ad5cca810c7661d5a0bc53
124127
TIoTLinkKit_FLV: aec2b2ec9664990ec6c5b9f760fc1a9043d5a9a5
125128
TIoTLinkKit_IJKPlayer: 423fb51f6d04f59a2f2a0ba3c875592f8048735c
126-
TIoTLinkKit_SoundTouch: 3b558a8663d8c867305f4336960ca078a9ae9768
129+
TIoTLinkKit_SoundTouch: ad6b0009ee12d65959aca605073fabb92db87a91
127130
TIoTLinkKit_TPNS: 7cea4fc1d20ef6c4c11c4f0b66bbb10ecf4ed968
128131
TIoTLinkKit_WechatOpenSDK: 7822d68cc5c46edf3f6020422202e7b65ea87614
129132
TIoTLinkKit_XP2P: a5409ccbbaa2de3faf8aa82bf98b0c3df193541a
130-
TIoTLinkVideo: 1b22edc89b230a34dbaedc139f575291f2320979
133+
TIoTLinkVideo: 961719b41bfff549fd76a807ee753431fe60eab3
134+
TPCircularBuffer: c13243556527551c4d320709c7b14a6d20cdc30a
131135
TrueTime: b49551ffafb28a9dee04e51b226f42a416010842
132136
TXLiteAVSDK_TRTC: e3383a81565e8bb2aaaaab4bd099ad5239cd5b2d
133137
TZImagePickerController: bf4c57b98d8707fce41ea6be872414a71c7a8c9d

Source/LinkSDKDemo/Video/P2P/Controller/TIoTDemoPreviewDeviceVC.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ - (void)viewDidLoad {
108108
// [TIoTCoreXP2PBridge sharedInstance].logEnable = NO;
109109
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
110110

111-
self.qualityString = quality_high;
111+
self.qualityString = quality_standard;
112112
self.screenRect = [UIApplication sharedApplication].delegate.window.frame;
113113

114114
if (self.isNVR == NO) {
@@ -260,12 +260,12 @@ - (void)getDeviceStatusWithType:(NSString *)singleType qualityType:(NSString *)q
260260
// [[TIoTCoreXP2PBridge sharedInstance] sendVoiceToServer:weakSelf.deviceName?:@"" channel:channel];
261261
[[TIoTSessionManager sharedInstance] resumeRTCAudioSession];
262262

263-
static int tt_pitch = 0;
263+
static int tt_pitch = 3;
264264
TIoTCoreAudioConfig *audio_config = [TIoTCoreAudioConfig new];
265265
audio_config.refreshSession = NO;
266266
audio_config.sampleRate = TIoTAVCaptionFLVAudio_16;
267267
audio_config.channels = 1;
268-
audio_config.isEchoCancel = NO;
268+
audio_config.isEchoCancel = YES;
269269
audio_config.pitch = tt_pitch; // -6声音会变粗一点; 6声音会变细一点
270270

271271
TIoTCoreVideoConfig *video_config = [TIoTCoreVideoConfig new];

Source/SDK/LinkVideo/FLV/TIoTAVCaptionFLV.mm

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
#include <iostream>
1414
#import "TIoTPCMXEchoRecord.h"
15-
#import <SoundTouchiOS/ijksoundtouch_wrap.h>
15+
//#import <SoundTouchiOS/ijksoundtouch_wrap.h>
16+
#import <SoundTouchiOS/TRAESoundTouch.h>
1617

1718
__weak static TIoTAVCaptionFLV *tAVCaptionFLV = nil;
1819
static flv_muxer_t* flvMuxer = nullptr;
1920
dispatch_queue_t muxerQueue;
2021
//NSFileHandle *_fileHandle;
21-
22+
//NSFileHandle *_originfileHandle;
2223
@interface TIoTAVCaptionFLV ()<AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureAudioDataOutputSampleBufferDelegate,H264EncoderDelegate,TIoTAACEncoderDelegate>
2324
// 负责输如何输出设备之间的数据传递
2425
@property (nonatomic, strong) AVCaptureSession *session;
@@ -349,35 +350,35 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CM
349350

350351
#pragma mark - PCM XEcho record_callback
351352
void *ijk_soundtouch_handle = NULL;
353+
//trae_voice_changer_t *trae_voice_handle = NULL;
352354
- (void)setPitch:(int)pitch {
353-
_pitch = ((pitch >= -12) && (pitch <= 12 ))?pitch:-6;
355+
_pitch = ((pitch >= 0) && (pitch <= 3 ))?pitch:2; //0 NONE 2萝莉,3 大叔
354356
NSLog(@"设置变调参数:%d<===>修正为:%d",pitch, _pitch);
355357
int tmpChannel = _channel;
356-
357-
if (ijk_soundtouch_handle != NULL) {
358-
ijk_soundtouch_destroy(ijk_soundtouch_handle);
359-
}
360-
ijk_soundtouch_handle = ijk_soundtouch_create(1.0, _pitch, tmpChannel, 16000);
358+
359+
[TRAESoundTouch voice_handle_open:_pitch channels:tmpChannel];
361360
}
362361

362+
static uint8_t trae_pcm_buffer[640];
363363
static void record_callback(uint8_t *buffer, int size, void *u)
364364
{
365-
// printf("pcm_size_callback: %d\n", size);
366-
int ret_len = size;
367-
// Sets pitch change in semi-tones compared to the original pitch
368-
// (-12 .. +12)
365+
// NSData *oridata = [NSData dataWithBytes:buffer length:size];
366+
// [_originfileHandle writeData:oridata];
367+
369368
TIoTAVCaptionFLV *vc = (__bridge TIoTAVCaptionFLV *)(u);
369+
memset(trae_pcm_buffer, 0, 640);
370+
UInt32 len = [vc.pcmRecord getData:trae_pcm_buffer :640];
371+
if (len < 640) {
372+
return;
373+
}
370374
if (vc.pitch != 0) {
371375
static int tmpChannel = vc.pcmRecord.pcmStreamDescription.mChannelsPerFrame;
372-
ret_len = ijk_soundtouch_translate(ijk_soundtouch_handle, (short *)buffer, size/2, 2, tmpChannel);
373-
if (ret_len<1) {
374-
return;
375-
}
376+
// int put_n_sample = (size/2) / tmpChannel;
377+
// [TRAESoundTouch voice_handle_process:(short *)buffer output:(short *)trae_pcm_buffer frames:320];
378+
[TRAESoundTouch voice_handle_process:(short *)trae_pcm_buffer output:(short *)trae_pcm_buffer frames:320];
376379
}
377-
// printf("pcm_size_callback_translate: %d\n", ret_len);
378-
379380

380-
NSData *data = [NSData dataWithBytes:buffer length:ret_len];
381+
NSData *data = [NSData dataWithBytes:trae_pcm_buffer length:640];
381382
// [_fileHandle writeData:data];
382383
[vc.aacEncoder encodePCMData:data];
383384
}
@@ -510,11 +511,17 @@ -(BOOL) startCapture {
510511
// NSFileManager *fileManager = [NSFileManager defaultManager];
511512
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
512513
// NSString *documentsDirectory = [paths firstObject];
513-
514-
// NSString *h264File = [documentsDirectory stringByAppendingPathComponent:@"lyh.pcm"];
514+
//
515+
// NSString *h264File = [documentsDirectory stringByAppendingPathComponent:@"out.pcm"];
515516
// [fileManager removeItemAtPath:h264File error:nil];
516517
// [fileManager createFileAtPath:h264File contents:nil attributes:nil];
517518
// _fileHandle = [NSFileHandle fileHandleForWritingAtPath:h264File];
519+
//
520+
//
521+
// NSString *originFile = [documentsDirectory stringByAppendingPathComponent:@"origin.pcm"];
522+
// [fileManager removeItemAtPath:originFile error:nil];
523+
// [fileManager createFileAtPath:originFile contents:nil attributes:nil];
524+
// _originfileHandle = [NSFileHandle fileHandleForWritingAtPath:originFile];
518525

519526
flv_init_load();
520527

Source/SDK/LinkVideo/FLV/TIoTPCMXEchoRecord.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#import <Foundation/Foundation.h>
33
#import <AudioUnit/AudioUnit.h>
4+
#import <TPCircularBuffer/TPCircularBuffer.h>
45

56
typedef void(*RecordCallback)(uint8_t *buffer, int size, void *u);
67

@@ -13,6 +14,9 @@ NS_ASSUME_NONNULL_BEGIN
1314
- (void)set_record_callback:(RecordCallback)c user:(void *)u;
1415
- (void)start_record;
1516
- (void)stop_record;
17+
18+
19+
-(UInt32)getData:(void *)buf_ :(UInt32)size_;
1620
@end
1721

1822
NS_ASSUME_NONNULL_END

Source/SDK/LinkVideo/FLV/TIoTPCMXEchoRecord.mm

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ - (instancetype)initWithChannel:(int)channel isEcho:(BOOL)isEcho
8282
static int pcm_buffer_size = 0;
8383
static uint8_t pcm_buffer[kTVURecoderPCMMaxBuffSize*4];
8484

85+
TPCircularBuffer pcm_circularBuffer;
86+
8587
static OSStatus record_callback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrame, AudioBufferList *__nullable ioData)
8688
{
8789
TIoTPCMXEchoRecord *r = (__bridge TIoTPCMXEchoRecord *)(inRefCon);
@@ -99,7 +101,12 @@ static OSStatus record_callback(void *inRefCon, AudioUnitRenderActionFlags *ioAc
99101

100102
UInt32 bufferSize = list.mBuffers[0].mDataByteSize;
101103
uint8_t *bufferData = (uint8_t *)list.mBuffers[0].mData;
102-
104+
// NSLog(@"record_callback__________size : %d", bufferSize);
105+
[r addData:bufferData :bufferSize];
106+
if (r->callback)
107+
r->callback(bufferData, bufferSize, r->user);
108+
return error;
109+
103110
// if (size > 0 && src)
104111
// {
105112
// char *dst = (char*)calloc(1, size);
@@ -130,6 +137,7 @@ OSStatus outputRender_cb(void *inRefCon, AudioUnitRenderActionFlags *ioActionFla
130137

131138
- (void)start_record
132139
{
140+
[self Init_buffer:4096];
133141
pcm_buffer_size = 0;
134142
AudioOutputUnitStart(audioUnit);
135143
}
@@ -138,6 +146,7 @@ - (void)stop_record
138146
{
139147
AudioOutputUnitStop(audioUnit);
140148
pcm_buffer_size = 0;
149+
[self Destory_buffer];
141150
}
142151

143152
- (void)set_record_callback:(RecordCallback)c user:(nonnull void *)u
@@ -153,4 +162,45 @@ - (void)dealloc
153162
[self stop_record];
154163
AudioComponentInstanceDispose(audioUnit);
155164
}
165+
166+
167+
168+
-(BOOL)Init_buffer:(UInt32)size_
169+
{
170+
return TPCircularBufferInit(&pcm_circularBuffer, size_ );
171+
}
172+
173+
-(void)Destory_buffer
174+
{
175+
TPCircularBufferCleanup(&pcm_circularBuffer );
176+
}
177+
178+
-(UInt32)addData:(void *)buf_ :(UInt32)size_
179+
{
180+
uint32_t availableBytes = 0;
181+
TPCircularBufferHead(&pcm_circularBuffer, &availableBytes);
182+
if (availableBytes <= 0)
183+
return 0;
184+
185+
UInt32 len = (availableBytes >= size_ ? size_ : availableBytes);
186+
TPCircularBufferProduceBytes(&pcm_circularBuffer, (void*)buf_, size_);
187+
return len;
188+
}
189+
190+
-(UInt32)getData:(void *)buf_ :(UInt32)size_
191+
{
192+
uint32_t availableBytes = 0;
193+
void *bufferTail = TPCircularBufferTail(&pcm_circularBuffer, &availableBytes);
194+
if (availableBytes >= size_)
195+
{
196+
UInt32 len = 0;
197+
len = (size_ > availableBytes ? availableBytes : size_);
198+
memcpy(buf_, bufferTail, len);
199+
TPCircularBufferConsume(&pcm_circularBuffer, len);
200+
// NSLog(@"ggggggggggg=====len = %ld", len);
201+
return len;
202+
}
203+
return 0;
204+
}
205+
156206
@end

Source/SDK/LinkVideo/TIoTCoreXP2PBridge.mm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,27 @@ - (void)sendVoiceToServer:(NSString *)dev_name channel:(NSString *)channel_numbe
331331
[self sendVoiceToServer:dev_name channel:channel_number audioConfig:audio_config videoConfig:video_config];
332332
}
333333

334+
335+
- (void)setupAVAudioSession:(TIoTCoreAudioConfig *)audio_config {
336+
AVAudioSession *avsession = [AVAudioSession sharedInstance];
337+
[avsession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
338+
[avsession setPreferredSampleRate:16000 error:nil];
339+
[avsession setPreferredInputNumberOfChannels:audio_config.channels error:nil];
340+
341+
//16khz * 1channel * notEcho = 640frame 设置为0.03
342+
NSTimeInterval duration = 0.015;
343+
if (audio_config.isEchoCancel) {
344+
duration = duration*2; //回音消除打开会减少采样
345+
}
346+
if (audio_config.channels == 2) {
347+
duration = duration/2;
348+
}
349+
[avsession setPreferredIOBufferDuration:duration error:nil];
350+
[avsession setActive:YES error:nil];
351+
}
352+
334353
- (void)sendVoiceToServer:(NSString *)dev_name channel:(NSString *)channel_number audioConfig:(TIoTCoreAudioConfig *)audio_config videoConfig:(TIoTCoreVideoConfig *)video_config {
354+
[self setupAVAudioSession:audio_config];
335355
// NSString *audioFile = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"testVideoStreamfile.flv"];
336356
// [[NSFileManager defaultManager] removeItemAtPath:audioFile error:nil];
337357
// [[NSFileManager defaultManager] createFileAtPath:audioFile contents:nil attributes:nil];

0 commit comments

Comments
 (0)