Source/GGBUtils.m
author Jens Alfke <jens@mooseyard.com>
Tue Jul 08 13:12:01 2008 -0700 (2008-07-08)
changeset 13 db7bb080c3d5
parent 10 6c78cc6bd7a6
child 16 28392c9a969f
permissions -rw-r--r--
Fixed some memory leaks, and took the address-related properties out of Player.
     1 /*  Copyright © 2008 Jens Alfke. All Rights Reserved.
     2 
     3     Redistribution and use in source and binary forms, with or without modification, are permitted
     4     provided that the following conditions are met:
     5 
     6     * Redistributions of source code must retain the above copyright notice, this list of conditions
     7       and the following disclaimer.
     8     * Redistributions in binary form must reproduce the above copyright notice, this list of
     9       conditions and the following disclaimer in the documentation and/or other materials provided
    10       with the distribution.
    11 
    12     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    13     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    14     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    15     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    16     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    17     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    18     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    19     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    20 */
    21 #import "GGBUtils.h"
    22 
    23 #if TARGET_OS_IPHONE
    24 #import <AudioToolbox/AudioToolbox.h>
    25 #endif
    26 
    27 
    28 #ifndef _MYUTILITIES_COLLECTIONUTILS_
    29 void setObj( id *variable, id newValue )
    30 {
    31     if( *variable != newValue ) {
    32         [*variable release];
    33         *variable = [newValue retain];
    34     }
    35 }
    36 
    37 void setObjCopy( id<NSCopying> *variable, id<NSCopying> newValue )
    38 {
    39     if( *variable != newValue ) {
    40         [*variable release];
    41         *variable = [(id)newValue copy];
    42     }
    43 }
    44 #endif
    45 
    46 
    47 void DelayFor( NSTimeInterval interval )
    48 {
    49     NSDate *end = [NSDate dateWithTimeIntervalSinceNow: interval];
    50     while( [end timeIntervalSinceNow] > 0 ) {
    51         if( ! [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: end] )
    52             break;
    53     }
    54 }    
    55 
    56 
    57 #if TARGET_OS_IPHONE
    58 static SystemSoundID GetSound( NSString *name )
    59 {
    60     static NSMutableDictionary *sSoundIDs;
    61     NSNumber *soundIDObj = [sSoundIDs objectForKey: name];
    62     if( ! soundIDObj ) {
    63         NSLog(@"Loading sound '%@'",name);
    64         NSString *type = name.pathExtension;
    65         if( ! type.length )
    66             type = @"aiff";
    67         NSString *path = [[NSBundle mainBundle] pathForResource: name.stringByDeletingPathExtension
    68                                                          ofType: type];
    69         NSURL *url;
    70         if( path )
    71             url = [NSURL fileURLWithPath: path];
    72         else {
    73             NSLog(@"Couldn't find sound %@",name);
    74             return 0;
    75         }
    76         //url = [NSURL fileURLWithPath: [@"/Library/Sounds/" stringByAppendingPathComponent: name]];
    77         SystemSoundID soundID;
    78         if( AudioServicesCreateSystemSoundID((CFURLRef)url,&soundID) != noErr ) {
    79             NSLog(@"Couldn't load sound %@",url);
    80             return 0;
    81         }
    82         
    83         soundIDObj = [NSNumber numberWithUnsignedInt: soundID];
    84         if( ! sSoundIDs )
    85             sSoundIDs = [[NSMutableDictionary alloc] init];
    86         [sSoundIDs setObject: soundIDObj forKey: name];
    87     }
    88     return [soundIDObj unsignedIntValue];
    89 }
    90 #endif
    91 
    92 
    93 void PreloadSound( NSString* name )
    94 {
    95 #if TARGET_OS_IPHONE
    96     GetSound(name);
    97 #else
    98     NSSound *sound = [[NSSound soundNamed: @"Pop"] copy];
    99     sound.volume = 0;
   100     [sound play];
   101     [sound release];
   102 #endif
   103 }    
   104 
   105 
   106 void PlaySound( NSString* name )
   107 {
   108 #if TARGET_OS_IPHONE
   109     AudioServicesPlaySystemSound( GetSound(name) );
   110 #else
   111     [[NSSound soundNamed: name] play];
   112 #endif
   113 }
   114 
   115 void Beep()
   116 {
   117 #if TARGET_OS_IPHONE
   118     AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
   119 #else
   120     NSBeep();
   121 #endif
   122 }