Recent Posts
None | 37 sec ago
None | 38 sec ago
None | 2 min ago
None | 3 min ago
None | 3 min ago
None | 3 min ago
None | 4 min ago
None | 5 min ago
None | 5 min ago
CSS | 6 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 10th of Feb 2010 12:50:30 AM Download | Raw | Embed | Report
  1.  
  2. #import <AudioUnit/AudioUnit.h>
  3. #import <math.h>
  4.  
  5.  
  6. #import "Singer.h"
  7.  
  8. #define kOutputBus 0
  9. #define kSampleRate 44100
  10. //44100.0f
  11. #define kWaveform (M_PI * 2.0f / kSampleRate)
  12.  
  13.  
  14. @implementation Singer
  15.  
  16.  
  17. OSStatus playbackCallback(void *inRefCon,
  18.                                                   AudioUnitRenderActionFlags *ioActionFlags,
  19.                                                   const AudioTimeStamp *inTimeStamp,
  20.                                                   UInt32 inBusNumber,
  21.                                                   UInt32 inNumberFrames,
  22.                                                   AudioBufferList *ioData) {   
  23.        
  24.         Singer *me = (Singer *)inRefCon;
  25.        
  26.         static int phase = 0;
  27.        
  28.         for(UInt32 i = 0; i < ioData->mNumberBuffers; i++) {
  29.                
  30.                 int samples = ioData->mBuffers[i].mDataByteSize / sizeof(SInt16);
  31.                
  32.                 SInt16 values[samples];
  33.                
  34.                 float waves;
  35.                
  36.                 for(int j = 0; j < samples; j++) {
  37.                        
  38.                        
  39.                         waves = 0;
  40.                        
  41.                        
  42.                         waves += sin(kWaveform * 440.0f * phase);
  43.                         waves += sin(kWaveform * 659.3f * phase);
  44.                         waves += sin(kWaveform * 1760.3f * phase);
  45.                         waves += sin(kWaveform * 880.0f * phase);                        
  46.                        
  47.                         waves *= 32500/4;// / 4; // <--------- make sure to divide by how many waves you're stacking
  48.                        
  49.                         values[j] = (SInt16)waves;
  50.                         values[j] += values[j]<<16;
  51.                        
  52.                         phase++;
  53.                        
  54.                 }
  55.                
  56.                 memcpy(ioData->mBuffers[i].mData, values, samples * sizeof(SInt16));
  57.        
  58.         }
  59.        
  60.        
  61.         return noErr;
  62.        
  63. }
  64.  
  65.  
  66. -(id)init {
  67.        
  68.         if(self = [super init]) {
  69.                                                
  70.                 [self initAudio];
  71.                
  72.         }
  73.        
  74.         return self;
  75.        
  76. }
  77.  
  78. -(void)initAudio {
  79.        
  80.         OSStatus status;
  81.        
  82.         AudioComponentDescription desc;
  83.         desc.componentType = kAudioUnitType_Output;
  84.         desc.componentSubType = kAudioUnitSubType_RemoteIO;
  85.         desc.componentFlags = 0;
  86.         desc.componentFlagsMask = 0;
  87.         desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  88.        
  89.         AudioComponent outputComponent = AudioComponentFindNext(NULL, &desc);
  90.        
  91.         status = AudioComponentInstanceNew(outputComponent, &audioUnit);
  92.        
  93.         UInt32 flag = 1;
  94.         status = AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag));
  95.        
  96.         AudioStreamBasicDescription audioFormat;
  97.         audioFormat.mSampleRate = kSampleRate;
  98.         audioFormat.mFormatID = kAudioFormatLinearPCM;
  99.         audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
  100.         audioFormat.mFramesPerPacket = 1;
  101.         audioFormat.mChannelsPerFrame = 1;
  102.         audioFormat.mBitsPerChannel = 16;
  103.         audioFormat.mBytesPerPacket = 2;
  104.         audioFormat.mBytesPerFrame = 2;
  105.        
  106.         status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat));
  107.        
  108.         AURenderCallbackStruct callbackStruct;
  109.         callbackStruct.inputProc = playbackCallback;
  110.         callbackStruct.inputProcRefCon = self;
  111.        
  112.         status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &callbackStruct, sizeof(callbackStruct));
  113.        
  114.         status = AudioUnitInitialize(audioUnit);
  115.        
  116. }
  117.  
  118. -(void)start {
  119.        
  120.         OSStatus status;
  121.        
  122.         status = AudioOutputUnitStart(audioUnit);
  123.        
  124. }
  125.  
  126. -(void)stop {
  127.        
  128.         OSStatus status;
  129.        
  130.         status = AudioOutputUnitStop(audioUnit);
  131.        
  132. }
  133.  
  134. -(void)dealloc {
  135.        
  136.         AudioUnitUninitialize(audioUnit);
  137.        
  138.         [super dealloc];
  139.        
  140. }
  141.  
  142. @end
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: