MixerHostAudio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. 1.纯播放器
  3. 2.纯录音器
  4. 3.即播又录器
  5. 4.即播又录,还即时合成,并支持声道
  6. 5.支持暂停
  7. 6.支持分路控制音量,麦克风音量,伴奏音量
  8. 7.支持切换左右声道,立体声输出
  9. 8.支持声效模式:回音,正常,变调,并即时保存
  10. 9.检查文件格式
  11. 10.支持定时
  12. 11.监测音量
  13. 12.对伴奏的音量调节记录进文件
  14. */
  15. #import <AudioToolbox/AudioToolbox.h>
  16. #import <AVFoundation/AVFoundation.h>
  17. #import <Accelerate/Accelerate.h> // for vdsp functions
  18. #import "lame.h"
  19. #define NUM_FILES 1 // number of audio files read in by old method
  20. #define kDelayBufferLength 1024 * 100 // measured in slices - a couple seconds worth at 44.1k
  21. #define MP3_SIZE 8192
  22. // Data structure for mono or stereo sound, to pass to the application's render callback function,
  23. // which gets invoked by a Mixer unit input bus when it needs more audio to play.
  24. // Note: this is used by the callbacks for playing looped files (old way)
  25. typedef struct {
  26. UInt32 writeNumber;
  27. BOOL isStereo; // set to true if there is data in the audioDataRight member
  28. UInt32 frameCount; // the total number of frames in the audio data
  29. UInt32 sampleNumber; // the next audio sample to play
  30. float sampleRate;
  31. AudioUnitSampleType *audioDataLeft; // the complete left (or mono) channel of audio data read from an audio file
  32. AudioUnitSampleType *audioDataRight; // the complete right channel of audio data read from an audio file
  33. ExtAudioFileRef audioFile;
  34. } soundStruct, *soundStructPtr;
  35. @interface MixerHostAudio : NSObject <AVAudioSessionDelegate> {
  36. BOOL isPlayer; //开启播放器
  37. BOOL isRecorder; //开启录音器
  38. BOOL isGenerator;//开启产生器
  39. BOOL isOutputer; //开启录音保存
  40. BOOL isEffecter; //开启效果器
  41. BOOL isMixSave; //开启混音保存
  42. BOOL isPaused; //是否暂停
  43. BOOL isPlayMic; //回放录音
  44. BOOL isHeadset; //接上耳塞
  45. BOOL isIPad1; //是否ipad1
  46. NSString* importAudioFile; //播放文件
  47. NSString* outputAudioFile; //输出文件
  48. int outputChanelIndex;//输出声道
  49. float volumeRecorder; //输入音量
  50. float volumePlayer; //输出音量
  51. NSTimeInterval currentTime; //当前时间
  52. NSTimeInterval timeLenRecord; //录音时长
  53. UInt32 recordSamples; //录音总sample
  54. FILE * _mp3;
  55. lame_t lame;
  56. unsigned char _mp3_buffer[MP3_SIZE];
  57. ExtAudioFileRef audioFile;
  58. Float64 graphSampleRate; // audio graph sample rate
  59. CFURLRef sourceURLArray[NUM_FILES]; // for handling loop files
  60. soundStruct soundStructArray[NUM_FILES]; // scope reference for loop file callback
  61. //float _oldVolPlayer;
  62. //float _oldVolRecorder;
  63. // Before using an AudioStreamBasicDescription struct you must initialize it to 0. However, because these ASBDs
  64. // are declared in external storage, they are automatically initialized to 0.
  65. AudioStreamBasicDescription stereoStreamFormat; // standard stereo 8.24 fixed point
  66. AudioStreamBasicDescription monoStreamFormat; // standard mono 8.24 fixed point
  67. AudioStreamBasicDescription SInt16StreamFormat; // signed 16 bit int sample format
  68. AudioStreamBasicDescription floatStreamFormat; // float sample format (for testing)
  69. AudioStreamBasicDescription auEffectStreamFormat; // audio unit Effect format
  70. AudioStreamBasicDescription stereoFileFormat; // standard stereo 8.24 fixed point
  71. AudioStreamBasicDescription monoFileFormat; // standard mono 8.24 fixed point
  72. AUGraph processingGraph; // the main audio graph
  73. BOOL playing; // indicates audiograph is running
  74. BOOL interruptedDuringPlayback; // indicates interruption happened while audiograph running
  75. // some of the audio units in this app
  76. AudioUnit ioUnit; // remote io unit
  77. AudioUnit mixerUnit; // multichannel mixer audio unit
  78. AUNode iONode; // node for I/O unit speaker
  79. AUNode mixerNode; // node for Multichannel Mixer unit
  80. FFTSetup fftSetup; // fft predefined structure required by vdsp fft functions
  81. COMPLEX_SPLIT fftA; // complex variable for fft
  82. int fftLog2n; // base 2 log of fft size
  83. int fftN; // fft size
  84. int fftNOver2; // half fft size
  85. size_t fftBufferCapacity; // fft buffer size (in samples)
  86. size_t fftIndex; // read index pointer in fft buffer
  87. // working buffers for sample data
  88. void *dataBuffer; // input buffer from mic/line
  89. float *outputBuffer; // fft conversion buffer
  90. float *analysisBuffer; // fft analysis buffer
  91. SInt16 *conversion16BufferLeft; // for data conversion from fixed point to integer
  92. SInt16 *conversion16BufferRight; // for data conversion from fixed point to integer
  93. SInt32 *conversion32BufferLeft; // for data conversion from fixed point to integer
  94. SInt32 *conversion32BufferRight; // for data conversion from fixed point to integer
  95. // convolution
  96. float *filterBuffer; // impusle response buffer
  97. int filterLength; // length of filterBuffer
  98. float *signalBuffer; // signal buffer
  99. int signalLength; // signal length
  100. float *resultBuffer; // result buffer
  101. int resultLength; // result length
  102. // new instance variables for UI display objects
  103. int displayInputFrequency; // frequency determined by analysis
  104. float displayInputLevelLeft; // average input level for meter left channel
  105. float displayInputLevelRight; // average input level for meter right channel
  106. int displayNumberOfInputChannels; // number of input channels detected on startup
  107. // for the synth callback - these are now instance variables so we can pass em back and forth to mic callback using self for inrefcon
  108. float sinFreq; // frequency of sine wave to generate
  109. float sinPhase; // current phase
  110. BOOL synthNoteOn; // determines whether note is playing
  111. // mic FX type selection
  112. int micFxType; // enumerated fx types:
  113. // 0: ring mod
  114. // 1: fft
  115. // 2: pitch shift
  116. // 3: echo (delay)
  117. // 4: low pass filter (moving average)
  118. // 5: low pass filter (convolution)
  119. BOOL micFxOn; // toggle for using mic fx
  120. float micFxControl; // multipurpose mix fx control slider
  121. BOOL inputDeviceIsAvailable; // indicates whether input device is available on ipod touch
  122. }
  123. // property declarations - corresponding to instance variables declared above
  124. @property(assign) BOOL isPlayer; //开启播放器
  125. @property(assign) BOOL isRecorder; //开启录音器
  126. @property(assign) BOOL isGenerator;//开启混音器
  127. @property(assign) BOOL isOutputer; //开启录音保存
  128. @property(assign) BOOL isEffecter; //开启效果器
  129. @property(assign) BOOL isMixSave; //开启混音保存
  130. @property(assign) BOOL isPlayMic; //回放录音
  131. @property(assign) BOOL isPaused; //是否暂停
  132. @property(assign) BOOL isHeadset; //接上耳塞
  133. @property(assign) BOOL isHeadsetTrue;//接上耳塞
  134. @property(assign) BOOL isIPad1; //是否ipad1
  135. @property(assign) BOOL isIOS5; //是否IOS5.x
  136. @property(assign) BOOL isIOS6; //是否IOS6.x
  137. @property(assign) BOOL isErroring;//是否发生错误
  138. @property(assign) BOOL isOutputMp3;//是否压缩成mp3
  139. @property(assign) BOOL isReadFileToMemory;//是否读文件至内存
  140. @property(assign) int outputChanelIndex;//输出声道
  141. @property(assign) ExtAudioFileRef audioFile; //保存的音频文件接口
  142. @property(assign) NSTimeInterval currentTime; //当前时间
  143. @property(assign) NSTimeInterval timeLenRecord; //录音时长
  144. @property(assign) UInt32 recordSamples; //录音总sample
  145. @property (nonatomic,assign) id delegate;//父对象
  146. @property(nonatomic,retain,setter=setImportAudioFile:, getter=importAudioFile) NSString* importAudioFile; //播放文件
  147. @property(nonatomic,retain,setter=setOutputAudioFile:, getter=outputAudioFile) NSString* outputAudioFile; //输出文件
  148. @property(assign,setter=setVolumeRecorder:, getter=volumeRecorder) float volumeRecorder; //输入音量
  149. @property(assign,setter=setVolumePlayer:, getter=volumePlayer)float volumePlayer; //输出音量
  150. @property (readwrite) AudioStreamBasicDescription stereoStreamFormat;
  151. @property (readwrite) AudioStreamBasicDescription monoStreamFormat;
  152. @property (readwrite) AudioStreamBasicDescription SInt16StreamFormat;
  153. @property (readwrite) AudioStreamBasicDescription floatStreamFormat;
  154. @property (readwrite) AudioStreamBasicDescription auEffectStreamFormat;
  155. @property (readwrite) Float64 graphSampleRate;
  156. @property (getter = isPlaying) BOOL playing;
  157. @property BOOL interruptedDuringPlayback;
  158. @property AudioUnit mixerUnit;
  159. @property AudioUnit ioUnit;
  160. @property AUNode iONode;
  161. @property AUNode mixerNode;
  162. @property FFTSetup fftSetup;
  163. @property COMPLEX_SPLIT fftA;
  164. @property int fftLog2n;
  165. @property int fftN;
  166. @property int fftNOver2;
  167. @property void *dataBuffer;
  168. @property float *outputBuffer;
  169. @property float *analysisBuffer;
  170. @property SInt16 *conversion16BufferLeft;
  171. @property SInt16 *conversion16BufferRight;
  172. @property SInt32 *conversion32BufferLeft;
  173. @property SInt32 *conversion32BufferRight;
  174. @property float *filterBuffer; // filter buffer
  175. @property int filterLength; // filter length
  176. @property float *signalBuffer; // signal buffer
  177. @property int signalLength; // signal length
  178. @property float *resultBuffer; // signal buffer
  179. @property int resultLength; // signal length
  180. @property size_t fftBufferCapacity;
  181. @property size_t fftIndex;
  182. @property (assign) int displayInputFrequency;
  183. @property (assign) float displayInputLevelLeft;
  184. @property (assign) float displayInputLevelRight;
  185. @property (assign) int displayNumberOfInputChannels;
  186. @property float sinFreq;
  187. @property float sinPhase;
  188. @property BOOL synthNoteOn;
  189. @property int micFxType;
  190. @property BOOL micFxOn;
  191. @property float micFxControl;
  192. @property BOOL inputDeviceIsAvailable;
  193. // function (method) declarations
  194. - (void) obtainSoundFileURLs;
  195. - (void) setupAudioSession;
  196. - (void) setupStereoStreamFormat;
  197. - (void) setupMonoStreamFormat;
  198. - (void) setupSInt16StreamFormat;
  199. - (void) setupFloatStreamFormat;
  200. - (void) setupStereoFileFormat;
  201. - (void) setupMonoFileFormat;
  202. - (void) setup;
  203. - (void) initBuffer;
  204. - (void) readAudioFilesIntoMemory;
  205. - (void) configureAndInitializeAudioProcessingGraph;
  206. - (void) setupAudioProcessingGraph;
  207. - (void) connectAudioProcessingGraph;
  208. - (void) start;
  209. - (void) start:(SInt64)n;
  210. - (BOOL) stop;
  211. - (void) pause;
  212. - (void) play;
  213. - (void) seek:(NSTimeInterval)n;
  214. - (BOOL) getHeadsetMode;
  215. - (void) delete;
  216. - (void) playSynthNote;
  217. - (void) stopSynthNote;
  218. - (void) enableMixerInput: (UInt32) inputBus isOn: (AudioUnitParameterValue) isONValue;
  219. - (void) setMixerInput: (UInt32) inputBus gain: (AudioUnitParameterValue) inputGain;
  220. - (void) setMixerOutputGain: (AudioUnitParameterValue) outputGain;
  221. - (void) setMixerFx: (AudioUnitParameterValue) isOnValue;
  222. - (void) setMixerBus5Fx: (AudioUnitParameterValue) isOnValue;
  223. - (void) printASBD: (AudioStreamBasicDescription) asbd;
  224. - (void) printErrorMessage: (NSString *) errorString withStatus: (OSStatus) result;
  225. - (void) convolutionSetup;
  226. - (void) FFTSetup;
  227. - (void) initDelayBuffer;
  228. - (Float32) getMixerOutputLevel;
  229. - (soundStructPtr) getSoundArray:(int)index;
  230. - (void)writeAudioFile:(int)totalFrames;
  231. - (BOOL) writeMp3Buffer:(void*)buffer_l nSamples:(int)nSamples;
  232. - (void) closeMp3File;
  233. - (void) createMp3File;
  234. @end