-->

OpenAL alSourceUnqueueBuffers & alSourceUnqueueBuf

2019-08-24 03:23发布

问题:

everyone , I have a problem about the API-- alSourceUnqueueBuffers when I use the OpenAL Libaray.

My problem as follows:

1.I play a pcm-music though streaming mechanism.

2.The application can queue up one or multiple buffer names using alSourceQueueBuffers.

  1. 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 & alSourceQueueBuffers. but it is bad. because It breaks down the music.

who can help me to find this problem ?

and where i can find more information and method about openAL?

I am waiting for your help . thanks , everyone.

so my code as follows:

.h:

    @interface myPlayback : NSObject
    {
    ALuint                  source;
    ALuint              *   buffers;
    ALCcontext*             context;
    ALCdevice*              device;
    unsigned long long      offset;
    ALenum                  m_format;
    ALsizei                 m_freq;
    void*                   data;
    }

@end

.m

    - (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, &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, &size, &format, &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, &queued);
    alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
    alGetSourcei(source, AL_SOURCE_STATE, &state);
    NSLog(@"%d", queued);
    NSLog(@"%d", processed);

    NSLog(@"===================================");

    while (processed > 0) {
        for (int i = 0; i < processed; ++i) {
            ALuint buf;
            alGetError();
    //            alSourceStop(source);
            ALenum y = alGetError();
            NSLog(@"%d", y);
            alSourceUnqueueBuffers(source, 1, &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, &buf);
            ALenum k = alGetError();
            NSLog(@"%d", k);
            offset += READ_SIZE;
//            alSourcePlay(source);
        }
    }

//    [self getSourceState];
}

回答1:

I found the reason about the problem.

the reason I turn Looping ON : alSourcei(source, AL_LOOPING, AL_TRUE);

if you set this , when the source processed a buffer, you want to fill new data or delete the buffer from the source. you will get the error.