SoundTouchOperation.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. ReadMe.strings
  3. Created by chuliangliang on 15-1-14.
  4. Copyright (c) 2014年 aikaola. All rights reserved.
  5. */
  6. #import "SoundTouchOperation.h"
  7. #include "SoundTouch.h"
  8. #include "WaveHeader.h"
  9. #import "CWFlieManager.h"
  10. #import "CWRecordModel.h"
  11. @interface SoundTouchOperation ()
  12. @property (nonatomic, strong) NSData *voicefileData;
  13. @end
  14. @implementation SoundTouchOperation
  15. - (id)initWithTarget:(id)tar action:(SEL)ac SoundTouchConfig:(MySountTouchConfig)soundConfig soundFile:(NSData *)file
  16. {
  17. self = [super init];
  18. if (self) {
  19. target = tar;
  20. action = ac;
  21. MysoundConfig = soundConfig;
  22. self.voicefileData =file;
  23. }
  24. return self;
  25. }
  26. - (void)main {
  27. NSData *soundData = self.voicefileData;
  28. soundtouch::SoundTouch mSoundTouch;
  29. mSoundTouch.setSampleRate(MysoundConfig.sampleRate); //采样率
  30. mSoundTouch.setChannels(1); //设置声音的声道
  31. mSoundTouch.setTempoChange(MysoundConfig.tempoChange); //这个就是传说中的变速不变调
  32. mSoundTouch.setPitchSemiTones(MysoundConfig.pitch); //设置声音的pitch (集音高变化semi-tones相比原来的音调) //男: -8 女:8
  33. mSoundTouch.setRateChange(MysoundConfig.rate); //设置声音的速率
  34. mSoundTouch.setSetting(SETTING_SEQUENCE_MS, 40);
  35. mSoundTouch.setSetting(SETTING_SEEKWINDOW_MS, 15); //寻找帧长
  36. mSoundTouch.setSetting(SETTING_OVERLAP_MS, 6); //重叠帧长
  37. NSLog(@"tempoChangeValue:%d pitchSemiTones:%d rateChange:%d",MysoundConfig.tempoChange,MysoundConfig.pitch,MysoundConfig.rate);
  38. NSMutableData *soundTouchDatas = [[NSMutableData alloc] init];
  39. if (soundData != nil) {
  40. char *pcmData = (char *)soundData.bytes;
  41. NSUInteger pcmSize = soundData.length;
  42. NSUInteger nSamples = pcmSize / 2;
  43. mSoundTouch.putSamples((short *)pcmData, (unsigned int)nSamples);
  44. short *samples = new short[pcmSize];
  45. int numSamples = 0;
  46. do {
  47. memset(samples, 0, pcmSize);
  48. //short samples[nSamples];
  49. numSamples = mSoundTouch.receiveSamples(samples, (unsigned int)pcmSize);
  50. [soundTouchDatas appendBytes:samples length:numSamples*2];
  51. } while (numSamples > 0);
  52. delete [] samples;
  53. }
  54. NSMutableData *wavDatas = [[NSMutableData alloc] init];
  55. NSUInteger fileLength = soundTouchDatas.length;
  56. void *header = createWaveHeader((int)fileLength, 1, MysoundConfig.sampleRate, 16);
  57. [wavDatas appendBytes:header length:44];
  58. [wavDatas appendData:soundTouchDatas];
  59. // NSString *savewavfilepath = [self createSavePath];
  60. NSString *savewavfilepath = [CWFlieManager soundTouchSavePathWithFileName:[CWRecordModel shareInstance].path.lastPathComponent];
  61. // NSLog(ASLocalizedString(@"SoundTouch 保存路径 : %@ "),savewavfilepath);
  62. BOOL isSave = [wavDatas writeToFile:savewavfilepath atomically:YES];
  63. // [soundTouchDatas release];
  64. // [wavDatas release];
  65. if (isSave && !self.isCancelled) {
  66. [target performSelectorOnMainThread:action withObject:savewavfilepath waitUntilDone:NO];
  67. }
  68. }
  69. ////创建文件存储路径
  70. //- (NSString *)createSavePath {
  71. // //文件名使用 "voiceFile+当前时间的时间戳"
  72. // NSString *fileName = [self createFileName];
  73. //
  74. // NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  75. // NSString *wavfilepath = [NSString stringWithFormat:@"%@/SoundTouch",documentDir];
  76. //
  77. // NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.wav",wavfilepath, fileName];
  78. // BOOL isExist = [[NSFileManager defaultManager]fileExistsAtPath:writeFilePath];
  79. // if (isExist) {
  80. // //如果存在则移除 以防止 文件冲突
  81. // NSError *err = nil;
  82. // [[NSFileManager defaultManager]removeItemAtPath:writeFilePath error:&err];
  83. // }
  84. //
  85. // BOOL isExistDic = [[NSFileManager defaultManager]fileExistsAtPath:wavfilepath];
  86. // if (!isExistDic) {
  87. // [[NSFileManager defaultManager] createDirectoryAtPath:wavfilepath withIntermediateDirectories:YES attributes:nil error:nil];
  88. // }
  89. //
  90. // return writeFilePath;
  91. //}
  92. //
  93. //- (NSString *)createFileName {
  94. // NSString *fileName = [NSString stringWithFormat:@"voiceFile%lld",(long long)[NSDate timeIntervalSinceReferenceDate]];
  95. // return fileName;
  96. //}
  97. @end