Playing a sound from Cocoa and setting the balance

The NSSound class is great for playing simple sounds, yet it lacks control over a more flexible way to play sounds. MacTrek plays sounds louder when they happen closer to the players position and balances them left or right when a target is actually left or right of our position.

The code is quite simple but not easy to find:


-(void) playWithVolume:(float)vol balance:(float)bal {

QTMovie *sound = [self sound];
if (sound != nil) {
// assume bal is between -1 and 1
// convert to -128 to 127
short balance = 127 * bal; // sort of..

// QTMovie * is actually a void pntr to the raw QT data media handler

MediaSetSoundBalance((void*)sound, balance);
[sound setVolume:vol];
//LLLog(@"SoundEffect.playWithVolume %f balance %f (%d) sound %@", vol, bal, balance, name);
[sound play];
}
}

Advertisements
Tagged with: ,
Posted in Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories
%d bloggers like this: