Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-14-2010, 05:38 AM   #1 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default Poltergeist in OpenAL

Hi there!

I'm adding sound to my app using OpenAL and I've found some really strange troubles whit it... First of all, seems that it doesn't work in 3.1.2 OS, and seems to work better on 3.0 OS. I'll show my OpenAL's class code and I'll explain what is happening each time:

// SingletonSoundManager class:
Code:
- (void) loadSoundWithKey:(NSString*)theSoundKey 
				 fileName:(NSString*)theFileName 
				  fileExt:(NSString*)theFileExt 
				frequency:(NSUInteger)theFrequency {
	
	// Get the full path of the audio file
	NSString *filePath = [[NSBundle mainBundle] pathForResource:theFileName ofType:theFileExt];
	
	// Now we need to open the file
	AudioFileID fileID = [self openAudioFile:filePath];
	
	// Find out how big the actual audio data is
	UInt32 fileSize = [self audioFileSize:fileID];
	
	// Create a location for the audio data to live temporarily
	unsigned char *outData = malloc(fileSize);
	
	// Load the byte data from the file into the data buffer
	OSStatus result = noErr;
	result = AudioFileReadBytes(fileID, FALSE, 0, &fileSize, outData);
	AudioFileClose(fileID);
	
	if(result != 0) {
		NSLog(@"ERROR SoundEngine: Cannot load sound: %@", theFileName);
		return;
	}
	
	NSUInteger bufferID;
	
	// Generate a buffer within OpenAL for this sound
	alGenBuffers(1, &bufferID);
	
	// Place the audio data into the new buffer
	alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, theFrequency);
	
	// Save the buffer to be used later
	[soundLibrary setObject:[NSNumber numberWithUnsignedInt:bufferID] forKey:theSoundKey];
	
	// Clean the buffer
	if(outData) {
		free(outData);
		outData = NULL;
	}
}

- (NSUInteger) playSoundWithKey:(NSString*)theSoundKey gain:(ALfloat)theGain pitch:(ALfloat)thePitch location:(Vector2f)theLocation shouldLoop:(BOOL)theShouldLoop {
	
	ALenum err = alGetError(); // clear the error code
	
	// Find the buffer linked to the key which has been passed in
	NSNumber *numVal = [soundLibrary objectForKey:theSoundKey];
	if(numVal == nil) return 0;
	NSUInteger bufferID = [numVal unsignedIntValue];
	
	// Find an available source i.e. it is currently not playing anything
	NSUInteger sourceID = [self nextAvailableSource];
	
	// Make sure that the source is clean by resetting the buffer assigned to the source
	// to 0
	alSourcei(sourceID, AL_BUFFER, 0);
	//Attach the buffer we have looked up to the source we have just found
	alSourcei(sourceID, AL_BUFFER, bufferID);
	
	// Set the pitch and gain of the source
	alSourcef(sourceID, AL_PITCH, thePitch);
	alSourcef(sourceID, AL_GAIN, theGain);
	
	// Set the looping value
	if(theShouldLoop) {
		alSourcei(sourceID, AL_LOOPING, AL_TRUE);
	} else {
		alSourcei(sourceID, AL_LOOPING, AL_FALSE);
	}
	
	// Check to see if there were any errors
	err = alGetError();
	if(err != 0) {
		NSLog(@"ERROR SoundManager: %d", err);
		return 0;
	}
	
	NSNumber *num = [NSNumber numberWithUnsignedInt:sourceID];
	
	[sourceLibrary setObject:num forKey:theSoundKey];
	
	//NSLog(@"PLAY1: Source %@:%u",theSoundKey,sourceID);
	//NSLog(@"PLAY2: Source %@:%u",theSoundKey,[[sourceLibrary objectForKey:theSoundKey] unsignedIntValue]);
	
	// Now play the sound
	alSourcePlay(sourceID);
	
	// Return the source ID so that loops can be stopped etc
	return sourceID;
}
// My EAGLView's method to load an play sounds:
Code:
sharedSoundManager = [SingletonSoundManager sharedSoundManager];
[sharedSoundManager loadSoundWithKey:@"morGrossa" fileName:@"XofGrossa" fileExt:@"caf" frequency:44100];
	[sharedSoundManager loadSoundWithKey:@"morBorinot" fileName:@"XofGrossa" fileExt:@"caf" frequency:44100];
	[sharedSoundManager loadSoundWithKey:@"flit" fileName:@"Spray" fileExt:@"caf" frequency:44100];

(...)

[sharedSoundManager playSoundWithKey:@"morBorinot" gain:1.0f pitch:1.0f location:Vector2fZero shouldLoop:NO];

(...)
Now, let's see what happens (always talking about devices, not simulator):

- 3.0 OS: Sounds are loaded well but when some sounds are played game suddenly freezes for about 2 or 3 seconds, then screen turns black and the Apple's logo is shown. After 10 or more seconds the device shows the locked screen like if nothing have happened and no crash log is generated. Really really strange, because it only occurs with some sounds...

- 3.1.2 OS: Even stranger maybe. Sound loading isn't done as it should do and XCode's console shows the following:

Code:
...[2589:207] Funcio carregaAudioInterficie
...[2589:207] ERROR SoundEngine: Cannot open file: /var/mobile/Applications/2CDC0469-1031-4339-B226-6F90DBC36836/myGame.app/Xof2.caf
...[2589:207] ERROR: cannot file file size
...[2589:207] ERROR SoundEngine: Cannot load sound: Xof2
Of course files are stored in the same directory as the project, are imported to the project, and they are loaded well in 3.0 OS... When game is running some sounds doesn't sound right (only noise), but some others sound well.

Have anyone any clue about this? Maybe I should call Dr. Holmes, or an exorcist? Any help will be really appreciated because I have no idea of what can be happening...

Thanks in advance!
Johanovski is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
9 members and 320 guests
2Apps1Day, akacaj, Domele, michelle, NSString, SLIC, soohyun, Techgirl-52, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,886
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:01 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0