2012-04-30 14 views
6

Sto riproducendo suoni per il mio gioco con OpenAL e ho alcuni problemi che a volte un piccolo problema viene riprodotto durante il looping. Anche senza loop ottengo un piccolo pop ... a volte ma non tutti.OpenAL che fa il glitch durante il looping del suono

Penso che abbia qualcosa a che fare con il buffer che è un po 'troppo lungo, quindi alla fine ci sono alcuni dati non definiti. Non riesco a capire come cambiarlo. Sto caricamento di un file caf con questa funzione:

void* MyGetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei *outSampleRate, ALdouble *duration) { 
OSStatus      err = noErr;  
SInt64       theFileLengthInFrames = 0; 
AudioStreamBasicDescription  theFileFormat; 
UInt32       thePropertySize = sizeof(theFileFormat); 
ExtAudioFileRef     extRef = NULL; 
void*       theData = NULL; 
AudioStreamBasicDescription  theOutputFormat; 

// Open a file with ExtAudioFileOpen() 
err = ExtAudioFileOpenURL(inFileURL, &extRef); 
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld\n", err); goto Exit; } 

// Get the audio data format 
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat); 
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld\n", err); goto Exit; } 
if (theFileFormat.mChannelsPerFrame > 2) { printf("MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo\n"); goto Exit;} 

// Set the client format to 16 bit signed integer (native-endian) data 
// Maintain the channel count and sample rate of the original source format 
theOutputFormat.mSampleRate = theFileFormat.mSampleRate; 
theOutputFormat.mChannelsPerFrame = theFileFormat.mChannelsPerFrame; 

theOutputFormat.mFormatID = kAudioFormatLinearPCM; 
theOutputFormat.mBytesPerPacket = 2 * theOutputFormat.mChannelsPerFrame; 
theOutputFormat.mFramesPerPacket = 1; 
theOutputFormat.mBytesPerFrame = 2 * theOutputFormat.mChannelsPerFrame; 
theOutputFormat.mBitsPerChannel = 16; 
theOutputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger; 

// Set the desired client (output) data format 
err = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(theOutputFormat), &theOutputFormat); 
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld\n", err); goto Exit; } 

// Get the total frame count 
thePropertySize = sizeof(theFileLengthInFrames); 
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames); 
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld\n", err); goto Exit; } 

// Read all the data into memory 
UInt32  dataSize = theFileLengthInFrames * theOutputFormat.mBytesPerFrame;; 
theData = malloc(dataSize); 
if (theData) 
{ 
    AudioBufferList  theDataBuffer; 
    theDataBuffer.mNumberBuffers = 1; 
    theDataBuffer.mBuffers[0].mDataByteSize = dataSize; 
    theDataBuffer.mBuffers[0].mNumberChannels = theOutputFormat.mChannelsPerFrame; 
    theDataBuffer.mBuffers[0].mData = theData; 

    // Read the data into an AudioBufferList 
    err = ExtAudioFileRead(extRef, (UInt32*)&theFileLengthInFrames, &theDataBuffer); 
    if(err == noErr) 
    { 
     // success 
     *outDataSize = (ALsizei)dataSize; 
     *outDataFormat = (theOutputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; 
     *outSampleRate = (ALsizei)theOutputFormat.mSampleRate; 
    } 
    else 
    { 
     // failure 
     free (theData); 
     theData = NULL; // make sure to return NULL 
     printf("MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld\n", err); goto Exit; 
    } 
} 

// Alex(Colombiamug): get the file duration... 
// first, get the audioID for the file... 
AudioFileID audioID; 
UInt32 audioIDSize = sizeof(audioID); 
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_AudioFile, &audioIDSize, &audioID); 
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_AudioFile) FAILED, Error = %ld\n", err); goto Exit; } 

//now the duration... 
double soundDuration; 
UInt32 durationSize = sizeof(soundDuration); 
err = AudioFileGetProperty(audioID, kAudioFilePropertyEstimatedDuration, &durationSize, &soundDuration); 
if(err) { printf("MyGetOpenALAudioData: AudioFileGetProperty(kAudioFilePropertyEstimatedDuration) FAILED, Error = %ld\n", err); goto Exit; } 

*duration = soundDuration; 
//printf("Audio duration:%f secs.\n", soundDuration); 

Exit: // Smaltire l'ExtAudioFileRef, che non è più necessario se (extRef) ExtAudioFileDispose (extRef); restituisce i dati; }

Fa parte di questo SoundEngine: SoundEngine

ho cercato di mettere il mio file di caf direttamente nel codice di esempio ed è lo stesso piccolo problema. (Questo file caf stava facendo bene con il vecchio Mela SoundEngine.cpp ma ho avuto altri problemi con che così ho deciso di cambiare)

+0

I pop possono verificarsi a causa di una discontinuità nel loop. Stai svanendo il loop? – learnvst

+0

Sì. Ho creato una dissolvenza incrociata quando ho modificato il file audio. Si sta collegando perfettamente nel mio soundeditor, quindi so che non è il mio file caf – broch

risposta

4

Rispondendo alla mia domanda;)

Per pura fortuna devo ammettere che ho provato a rimuovere il flag kAudioFormatFlagIsPacked da questa linea:

theOutputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger; 

e quello fissato esso.

Se qualcuno può dirmi perché potrebbe essere bello sapere ... o se ci sono alcuni problemi nella rimozione di quella bandiera, vorrei anche sentirne parlare.

+0

Stesso problema. Questo non mi ha aiutato. Qualche altra soluzione? –

+0

Ho finito per utilizzare un altro soundengine, ovvero questo: https://github.com/alexrestrepo/SoundEngine Sembra funzionare molto meglio – broch

Problemi correlati