Sound/SoundEffect.m

00001 //
00002 //  SoundEffect.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 03/07/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "SoundEffect.h"
00010 
00011 
00012 @implementation SoundEffect
00013 
00014 - (id) init {
00015     self = [super init];
00016     if (self != nil) {
00017         sound = nil;
00018         name = nil;
00019     }
00020     return self;
00021 }
00022 
00023 -(id) initSound:(QTMovie *)newSound withName:(NSString *)newName {
00024     self = [self init];
00025     if (self != nil) {
00026         sound = newSound;
00027         name = newName;
00028     }
00029     return self;
00030 }
00031 
00032 - (bool) loadSoundWithName:(NSString*)soundName {
00033 
00034     // assume in resources/SoundEffects, and an .au file
00035     NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00036     NSString *pathToSound = [NSString stringWithFormat:@"%@/%@", pathToResources, soundName];
00037     [self setName:soundName];    
00038     
00039     [self setSound:[QTMovie movieWithFile:pathToSound error:nil]];
00040     if (sound != nil) {
00041         return YES;
00042     } else {
00043         NSLog(@"SoundEffect.loadSoundWithName %@ failed", soundName);
00044         return NO;
00045     }
00046 }
00047 
00048 -(QTMovie *)sound {
00049     return sound;
00050 }
00051 
00052 -(NSString *)name {
00053     return name;
00054 }
00055 
00056 -(void) setSound:(QTMovie *)newSound {
00057     [sound release];
00058     sound = newSound;
00059     [sound retain];
00060 }
00061 
00062 -(void) setName:(NSString *)newName {
00063     [name release];
00064     name = newName;
00065     [name retain];
00066 }
00067 
00068 -(void) play {
00069 
00070     if (([sound currentTime].timeValue > 0 ) && 
00071         ([sound currentTime].timeValue  < [sound duration].timeValue)) {
00072         NSLog(@"SoundEffect.play already playing sound %@, skipping....", name);
00073         return;
00074     } else {
00075         NSLog(@"SoundEffect.play sound %@", name);
00076     }
00077     [sound gotoBeginning];
00078     [sound play];
00079 }
00080 
00081 -(void) playWithVolume:(float)vol {
00082     [sound setVolume:vol];
00083     NSLog(@"SoundEffect.playWithVolume %f sound %@", vol, name);
00084     [self play];
00085 }
00086 
00087 -(void) playWithVolume:(float)vol balance:(float)bal {
00088     // assume bal is between -1 and 1
00089     // convert to -128 to 127
00090     short balance = 127 * bal; // sort of..    
00091 
00092     // QTMovie * is actually a void pntr to the raw QT data media handler
00093     MediaSetSoundBalance((void*)sound, balance); 
00094     [sound setVolume:vol];
00095     NSLog(@"SoundEffect.playWithVolume %f balance %f (%d) sound %@", vol, bal, balance, name);
00096     [self play];
00097 }
00098 
00099 @end

Generated on Sat Aug 26 21:14:16 2006 for MacTrek by  doxygen 1.4.7