Advertisement
Guest User

Untitled

a guest
Jan 17th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  MAAudioManager.m
  3. //  Minimal Noise
  4. //
  5. //  Created by Carlo Caione on 1/14/12.
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "MAAudioManager.h"
  10.  
  11. #define BUFFER_SIZE 16384
  12. #define BUFFER_COUNT 3
  13.  
  14. @implementation MAAudioManager
  15.  
  16. - (id)initWithBufferCount:(UInt32)bCount
  17.                    ofSize:(UInt32)bSize
  18. {
  19.     if (self = [super init])
  20.     {
  21.         deviceFormat.mSampleRate = 44100;
  22.         deviceFormat.mFormatID = kAudioFormatLinearPCM;
  23.         deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
  24.         deviceFormat.mBytesPerPacket = 4;
  25.         deviceFormat.mFramesPerPacket = 1;
  26.         deviceFormat.mBytesPerFrame = 4;
  27.         deviceFormat.mChannelsPerFrame = 2;
  28.         deviceFormat.mBitsPerChannel = 16;
  29.         deviceFormat.mReserved = 0;
  30.        
  31.         bufferCount = bCount;
  32.         bufferSize = bSize;
  33.        
  34.         isPlaying = NO;
  35.     }
  36.     return self;
  37. }
  38.  
  39. - (id)init
  40. {    
  41.     return [self initWithBufferCount:BUFFER_COUNT ofSize:BUFFER_SIZE];
  42. }
  43.  
  44. - (void)start
  45. {
  46.     OSStatus err;
  47.  
  48.     if(isPlaying == NO)
  49.     {
  50.         err = AudioQueueNewOutput(&deviceFormat,
  51.                                   AudioQueueCallback,
  52.                                   (__bridge void *) self,
  53.                                   nil,
  54.                                   nil,
  55.                                   0,
  56.                                   &audioQueue);
  57.         if(err != noErr)
  58.             NSLog(@"AudioQueueNewOutput() error: %d", err);
  59.        
  60.         for (int i = 0; i < bufferCount; ++i) {
  61.             AudioQueueBufferRef mBuffer;
  62.             err = AudioQueueAllocateBuffer(audioQueue,
  63.                                            bufferSize,
  64.                                            &mBuffer);
  65.             if (err != noErr){
  66.                 NSLog(@"AudioQueueAllocateBuffer() error: %d", err);
  67.                 break;
  68.             }
  69.             [noiseGenerator generateNoise:mBuffer];
  70.             err = AudioQueueEnqueueBuffer(audioQueue, mBuffer, 0, NULL);  
  71.             if(err != noErr){
  72.                 NSLog(@"AudioQueueEnqueueBuffer() error: %d", err);
  73.             }
  74.         }
  75.            
  76.         err = AudioQueueStart(audioQueue, NULL);
  77.         if (err == noErr){
  78.             isPlaying = YES;
  79.         } else {
  80.             isPlaying = NO;
  81.             NSLog(@"AudioQueueStart() error: %d", err);
  82.         }
  83.     } else {
  84.         NSLog(@"Error: audio is already playing back.");
  85.     }
  86.    
  87. }
  88.  
  89. - (void)stop
  90. {
  91.     OSStatus err;
  92.  
  93.     err = AudioQueueStop(audioQueue, true);
  94.     if(err != noErr) {
  95.         NSLog(@"Error: it is not possible to stop audio.");
  96.     } else {
  97.         isPlaying = NO;
  98.     }
  99. }
  100.  
  101. - (void)processOutputBuffer:(AudioQueueBufferRef)buffer
  102. {
  103.     OSStatus err;
  104.  
  105.     if(isPlaying == YES)
  106.     {
  107.         [outputLock lock];
  108.         [noiseGenerator generateNoise:buffer];
  109.         err = AudioQueueEnqueueBuffer(audioQueue, buffer, 0, NULL);
  110.         if(err != noErr){
  111.             NSLog(@"AudioQueueEnqueueBuffer() error: %d", err);
  112.             isPlaying = NO;
  113.         }
  114.         [outputLock unlock];
  115.     } else {
  116.         [self stop];
  117.     }
  118. }
  119.  
  120. - (void)registerNoiseGenerator:(MANoise *)noiseGen
  121. {
  122.     noiseGenerator = noiseGen;
  123. }
  124.  
  125. @end
  126.  
  127. void AudioQueueCallback(void* inUserData,
  128.                         AudioQueueRef inAQ,
  129.                         AudioQueueBufferRef inBuffer)
  130. {
  131.     MAAudioManager *audioMngr = (__bridge MAAudioManager *) inUserData;
  132.     [audioMngr processOutputBuffer:inBuffer];
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement