title: OpenAL alSourceUnqueueBuffers & alSourceUnqueueBuffers
body: <p>everyone , I have a problem about the API-- alSourceUnqueueBuffers when I use the OpenAL Libaray.</p>
  
  <p>My  problem as follows:</p>
  
  <p>1.I play a pcm-music though streaming mechanism. </p>
  
  <p>2.The application can queue up one or multiple buffer names using alSourceQueueBuffers. </p>
  
  <ol>
  <li>when a buffer has been processed. I want to fill new audio data in my function:              getSourceState . but when I use the API of OpenAL alSourceUnqueueBuffers. it returns an error
  --- AL_INVALID_OPERATION  . I do this as the document about the OpenAL. 
  so I test a way to solve this problem. I use alSourceStop(source) before the api alSourceUnqueueBuffers, an use alSourcePlay(source) after i filled new data though 
  alBufferData &amp; alSourceQueueBuffers. but it is bad. because It breaks down the music.</li>
  </ol>
  
  <p>who can help me to find this problem ?</p>
  
  <p>and where i can find more information and method about openAL?</p>
  
  <p>I am waiting for your help . thanks , everyone.</p>
  
  <p>so my code as follows:</p>
  
  <p>.h:</p>
  
  <pre><code>    @interface myPlayback : NSObject
      {
      ALuint                  source;
      ALuint              *   buffers;
      ALCcontext*             context;
      ALCdevice*              device;
      unsigned long long      offset;
      ALenum                  m_format;
      ALsizei                 m_freq;
      void*                   data;
      }
  
  @end
  </code></pre>
  
  <p>.m</p>
  
  <pre><code>    - (void)initOpenAL
      {
      ALenum          error;
  
      // Create a new OpenAL Device
      // Pass NULL to specify the system’s default output device
      device = alcOpenDevice(NULL);
      if (device != NULL)
      {
          // Create a new OpenAL Context
          // The new context will render to the OpenAL Device just created 
          context = alcCreateContext(device, 0);
          if (context != NULL)
          {
              // Make the new context the Current OpenAL Context
              alcMakeContextCurrent(context);
  
              // Create some OpenAL Buffer Objects
  
              buffers = (ALuint*)malloc(sizeof(ALuint) * 5);
              alGenBuffers(5, buffers);
              if((error = alGetError()) != AL_NO_ERROR) {
                  NSLog(@"Error Generating Buffers: %x", error);
                  exit(1);
              }
  
              // Create some OpenAL Source Objects
              alGenSources(1, &amp;source);
              if(alGetError() != AL_NO_ERROR) 
              {
                  NSLog(@"Error generating sources! %x\n", error);
                  exit(1);
              }
  
          }
      }
      // clear any errors
      alGetError();
  
      [self initBuffer];  
          [self initSource];
  }
  
      - (void) initBuffer
      {
      ALenum  error = AL_NO_ERROR;
      ALenum  format;
      ALsizei size;
      ALsizei freq;
  
      NSBundle*               bundle = [NSBundle mainBundle];
  
      // get some audio data from a wave file
      CFURLRef fileURL = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:@"4" ofType:@"caf"]] retain];
  
      if (fileURL)
      {   
          data = MyGetOpenALAudioData(fileURL, &amp;size, &amp;format, &amp;freq);
          CFRelease(fileURL);
          m_freq = freq;
          m_format = format;
          if((error = alGetError()) != AL_NO_ERROR) {
              NSLog(@"error loading sound: %x\n", error);
              exit(1);
          }
  
          alBufferData(buffers[0], format, data, READ_SIZE , freq);
          offset += READ_SIZE;
          alBufferData(buffers[1], format, data + offset, READ_SIZE, freq);
          offset += READ_SIZE;
          alBufferData(buffers[2], format, data + offset, READ_SIZE, freq);
          offset += READ_SIZE;
          alBufferData(buffers[3], format, data + offset, READ_SIZE, freq);
          offset += READ_SIZE;
          alBufferData(buffers[4], format, data + offset, READ_SIZE, freq);
          offset += READ_SIZE;
  
          if((error = alGetError()) != AL_NO_ERROR) {
              NSLog(@"error attaching audio to buffer: %x\n", error);
          }       
      }
      else
          NSLog(@"Could not find file!\n");
  }
  
      - (void) initSource
      {
      ALenum error = AL_NO_ERROR;
      alGetError(); // Clear the error
  
      // Turn Looping ON
      alSourcei(source, AL_LOOPING, AL_TRUE);
  
      // Set Source Position
      float sourcePosAL[] = {sourcePos.x, kDefaultDistance, sourcePos.y};
      alSourcefv(source, AL_POSITION, sourcePosAL);
  
      // Set Source Reference Distance
      alSourcef(source, AL_REFERENCE_DISTANCE, 50.0f);
  
      alSourceQueueBuffers(source, 5, buffers);
  
      if((error = alGetError()) != AL_NO_ERROR) {
          NSLog(@"Error attaching buffer to source: %x\n", error);
          exit(1);
      }   
  }
  
  
  
  
  
      - (void)startSound
          {
          ALenum error;
  
          NSLog(@"Start!\n");
          // Begin playing our source file
          alSourcePlay(source);
          if((error = alGetError()) != AL_NO_ERROR) {
              NSLog(@"error starting source: %x\n", error);
          } else {
              // Mark our state as playing (the view looks at this)
              self.isPlaying = YES;
          }
  
          while (1) {
              [self getSourceState];
          }
  }
      -(void)getSourceState
      {
      int queued;
      int processed;
      int state;
      alGetSourcei(source, AL_BUFFERS_QUEUED, &amp;queued);
      alGetSourcei(source, AL_BUFFERS_PROCESSED, &amp;processed);
      alGetSourcei(source, AL_SOURCE_STATE, &amp;state);
      NSLog(@"%d", queued);
      NSLog(@"%d", processed);
  
      NSLog(@"===================================");
  
      while (processed &gt; 0) {
          for (int i = 0; i &lt; processed; ++i) {
              ALuint buf;
              alGetError();
      //            alSourceStop(source);
              ALenum y = alGetError();
              NSLog(@"%d", y);
              alSourceUnqueueBuffers(source, 1, &amp;buf);
              ALenum i = alGetError();
              NSLog(@"%d", i);
              processed --;
              alBufferData(buf, m_format, data + offset, READ_SIZE, m_freq);
              ALenum j = alGetError();
              NSLog(@"%d", j);
              alSourceQueueBuffers(source, 1, &amp;buf);
              ALenum k = alGetError();
              NSLog(@"%d", k);
              offset += READ_SIZE;
  //            alSourcePlay(source);
          }
      }
  
  //    [self getSourceState];
  }
  </code></pre>