0

大家,我在使用 OpenAL Libaray 时遇到了一个关于 API 的问题——alSourceUnqueueBuffers。

我的问题如下:

1.我通过流媒体机制播放 pcm 音乐。

2.应用程序可以使用 alSourceQueueBuffers 将一个或多个缓冲区名称排队。

  1. 当缓冲区被处理时。我想在我的函数中填充新的音频数据: getSourceState 。但是当我使用 OpenAL alSourceUnqueueBuffers 的 API 时。它返回一个错误 --- AL_INVALID_OPERATION 。我这样做是作为关于 OpenAL 的文档。所以我测试了一种方法来解决这个问题。我在 api alSourceUnqueueBuffers 之前使用 alSourceStop(source),在我通过 alBufferData 和 alSourceQueueBuffers 填充新数据之后使用 alSourcePlay(source)。但这很糟糕。因为它破坏了音乐。

谁能帮我找到这个问题?

在哪里可以找到有关 openAL 的更多信息和方法?

我在等你的帮助。谢谢大家。

所以我的代码如下:

。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];
}
4

1 回答 1

2

我找到了问题的原因。

我打开循环的原因:alSourcei(source, AL_LOOPING, AL_TRUE);

如果你设置了这个,当源处理一个缓冲区时,你想填充新数据或从源中删除缓冲区。你会得到错误。

于 2012-05-15T05:02:09.003 回答