Created
October 19, 2011 23:29
-
-
Save Nub/1299991 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> | |
| #import <AVFoundation/AVFoundation.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 | |
| }; | |
| enum SONG { | |
| SONG_THEME, | |
| SONG_MENU, | |
| SONG_COUNT | |
| }; | |
| @interface SFXEngine : NSObject{ | |
| NSInteger volume; | |
| } | |
| @property (nonatomic, retain) NSMutableArray *preloadedSFX; | |
| @property (nonatomic, retain) AVAudioPlayer *songPlayer; | |
| @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" | |
| const char *SFX_FILENAMES[] = { | |
| "menunext", | |
| "menuselect", | |
| "pain", | |
| "jump", | |
| "pistol", | |
| "shotgun", | |
| "machinegun", | |
| "zombie1", | |
| "zombie2", | |
| "win", | |
| "lose", | |
| "newlife" | |
| }; | |
| const char *SONG_FILENAMES[] = { | |
| "theme", | |
| "menu", | |
| }; | |
| @interface SFXEngine () | |
| { | |
| @private | |
| NSMutableArray *preloadedSFX; | |
| AVAudioPlayer *songPlayer; | |
| } | |
| - (BOOL)isValidSound:(NSInteger)sound; | |
| - (BOOL)isValidSong:(NSInteger)song; | |
| @end | |
| @implementation SFXEngine | |
| @synthesize preloadedSFX; | |
| @synthesize songPlayer; | |
| @synthesize volume; | |
| - (id)init{ | |
| self = [super init]; | |
| if (self) { | |
| preloadedSFX = [[NSMutableArray array] retain]; | |
| for (NSInteger i = 0; i < SFX_COUNT; i++) { | |
| NSString *filename = [NSString stringWithCString:SFX_FILENAMES[i] encoding:NSUTF8StringEncoding]; | |
| NSString *sfxPath = [[NSBundle mainBundle] pathForResource:filename ofType:@"aiff"]; | |
| if (!sfxPath) | |
| continue; | |
| NSURL *sfxURL = [NSURL fileURLWithPath:sfxPath]; | |
| NSError *error = nil; | |
| AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:sfxURL error:&error]; | |
| if (error) { | |
| NSLog(@"Error@[%@]:%@", sfxURL,[error localizedDescription]); | |
| } | |
| 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; | |
| } | |
| - (void)dealloc{ | |
| [preloadedSFX release]; | |
| [songPlayer 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{ | |
| if ([self isValidSong:song]) { | |
| if (songPlayer) | |
| [songPlayer release]; | |
| NSString *filename = [NSString stringWithCString:SONG_FILENAMES[song] encoding:NSUTF8StringEncoding]; | |
| NSString *sfxPath = [[NSBundle mainBundle] pathForResource:filename ofType:@"aiff"]; | |
| if (!sfxPath) | |
| return; | |
| NSURL *sfxURL = [NSURL fileURLWithPath:sfxPath]; | |
| songPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:sfxURL error:nil]; | |
| if (!songPlayer) { | |
| NSLog(@"Failed to init song, %@", filename); | |
| }else{ | |
| if (![songPlayer prepareToPlay]) { | |
| NSLog(@"Failed to load song buffer, %@", filename); | |
| } | |
| } | |
| songPlayer.numberOfLoops = -1;//loop forever | |
| songPlayer.currentTime = 0; | |
| [songPlayer play]; | |
| } | |
| } | |
| #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