Created
October 17, 2011 04:44
-
-
Save Nub/1291941 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // SFXEngine.h | |
| // Sound Effects Test | |
| // | |
| // Created by Zachry Thayer on 10/16/11. | |
| // Copyright (c) 2011 Zachry Thayer. All rights reserved. | |
| // | |
| /* | |
| // sound.c | |
| enum Sfx { | |
| SFX_MENUNEXT,SFX_MENUSELECT,SFX_PAIN,SFX_JUMP,SFX_PISTOL,SFX_SHOTGUN,SFX_MACHINEGUN, | |
| SFX_ZOMBIEDEAD1,SFX_ZOMBIEDEAD2,SFX_WIN,SFX_LOSE,SFX_NEWLIFE,MAXSFX | |
| }; | |
| enum Song { | |
| SONG_THEME,SONG_MENU | |
| }; | |
| void initSound(); | |
| void loadSounds(); | |
| void playSound(int id); | |
| void playSong(int id); | |
| void songVolume(int vol); | |
| */ | |
| #import <Foundation/Foundation.h> | |
| enum SFX { | |
| SFX_MENUNEXT, | |
| SFX_MENUSELECT, | |
| SFX_PAIN, | |
| SFX_JUMP, | |
| SFX_PISTOL, | |
| SFX_SHOTGUN, | |
| SFX_MACHINEGUN, | |
| SFX_ZOMBIEDEAD1, | |
| SFX_ZOMBIEDEAD2, | |
| SFX_WIN, | |
| SFX_LOSE, | |
| SFX_NEWLIFE, | |
| SFX_COUNT | |
| }; | |
| const char *SFX_FILENAMES[] = { | |
| "menunext", | |
| "menuselect", | |
| "pain", | |
| "jump", | |
| "pistol", | |
| "shotgun", | |
| "machinegun", | |
| "zombie1", | |
| "zombie2", | |
| "win", | |
| "lose", | |
| "newlife" | |
| }; | |
| enum SONG { | |
| SONG_THEME, | |
| SONG_MENU, | |
| SONG_COUNT | |
| }; | |
| const char *SONG_FILENAMES[] = { | |
| "theme", | |
| "menu", | |
| }; | |
| @interface SFXEngine : NSObject{ | |
| NSInteger volume; | |
| } | |
| @property (nonatomic) NSInteger volume; | |
| - (void)playSound:(NSInteger)sound; | |
| - (void)playSong:(NSInteger)song; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // SFXEngine.m | |
| // Sound Effects Test | |
| // | |
| // Created by Zachry Thayer on 10/16/11. | |
| // Copyright (c) 2011 Zachry Thayer. All rights reserved. | |
| // | |
| #import "SFXEngine.h" | |
| #import <AVFoundation/AVFoundation.h> | |
| @interface SFXEngine () | |
| { | |
| @private | |
| NSMutableArray *preloadedSFX; | |
| } | |
| - (BOOL)isValidSound:(NSInteger)sound; | |
| - (BOOL)isValidSong:(NSInteger)song; | |
| @end | |
| @implementation SFXEngine | |
| @synthesize volume; | |
| - (id)init{ | |
| self = [super init]; | |
| if (self) { | |
| preloadedSFX = [NSMutableArray array]; | |
| for (NSInteger i = 0; i < SFX_COUNT; i++) { | |
| NSString *filename = [NSString stringWithCString:SFX_FILENAMES[i] encoding:NSUTF8StringEncoding]; | |
| NSString *sfxPath = [[NSBundle mainBundle] pathForResource:filename ofType:@"wav"]; | |
| NSURL *sfxURL = [NSURL URLWithString:sfxPath]; | |
| AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:sfxURL error:nil]; | |
| if (!player) { | |
| NSLog(@"Failed to init sound, %@", filename); | |
| [preloadedSFX addObject:[NSNull null]];//blank index | |
| }else{ | |
| if (![player prepareToPlay]) { | |
| NSLog(@"Failed to load sound buffer, %@", filename); | |
| } | |
| [preloadedSFX addObject:player]; | |
| } | |
| } | |
| } | |
| return self; | |
| } | |
| #warning commented out because I'm compiling for ARC (memory management) | |
| /* | |
| - (void)dealloc{ | |
| [preloadedSFX release]; | |
| [super dealloc]; | |
| } | |
| */ | |
| #pragma mark - Public Methods | |
| - (void)playSound:(NSInteger)sound{ | |
| if([self isValidSound:sound]){ | |
| AVAudioPlayer *player = [preloadedSFX objectAtIndex:sound]; | |
| if ([player isMemberOfClass:[NSNull class]]) {//Check for valid class | |
| NSLog(@"Tried to play null sound, %s", SFX_FILENAMES[sound]); | |
| }else{ | |
| player.currentTime = 0;//rewind | |
| [player play]; | |
| } | |
| } | |
| } | |
| - (void)playSong:(NSInteger)song{ | |
| } | |
| #pragma marl - Private Helpers | |
| - (BOOL)isValidSound:(NSInteger)sound{ | |
| if (sound > -1 && sound < SFX_COUNT) { | |
| return YES; | |
| } | |
| return FALSE; | |
| } | |
| - (BOOL)isValidSong:(NSInteger)song{ | |
| if (song > -1 && song < SONG_COUNT) { | |
| return YES; | |
| } | |
| return FALSE; | |
| } | |
| @end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment