6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
7 // |
7 // |
8 |
8 |
9 #import "VoicesViewController.h" |
9 #import "VoicesViewController.h" |
10 #import "CommodityFunctions.h" |
10 #import "CommodityFunctions.h" |
11 #import "openalbridge.h" |
|
12 |
11 |
13 |
12 |
14 @implementation VoicesViewController |
13 @implementation VoicesViewController |
15 @synthesize teamDictionary, voiceArray, lastIndexPath; |
14 @synthesize teamDictionary, voiceArray, lastIndexPath; |
16 |
15 |
24 #pragma mark View lifecycle |
23 #pragma mark View lifecycle |
25 -(void) viewDidLoad { |
24 -(void) viewDidLoad { |
26 [super viewDidLoad]; |
25 [super viewDidLoad]; |
27 srandom(time(NULL)); |
26 srandom(time(NULL)); |
28 |
27 |
29 openal_init(); |
28 voiceBeingPlayed = NULL; |
30 voiceBeingPlayed = -1; |
|
31 |
29 |
32 // load all the voices names and store them into voiceArray |
30 // load all the voices names and store them into voiceArray |
33 // it's here and not in viewWillAppear because user cannot add/remove them |
31 // it's here and not in viewWillAppear because user cannot add/remove them |
34 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
32 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
35 self.voiceArray = array; |
33 self.voiceArray = array; |
44 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
42 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
45 } |
43 } |
46 |
44 |
47 -(void) viewWillDisappear:(BOOL)animated { |
45 -(void) viewWillDisappear:(BOOL)animated { |
48 [super viewWillDisappear:animated]; |
46 [super viewWillDisappear:animated]; |
49 if(voiceBeingPlayed >= 0) { |
47 if(voiceBeingPlayed != NULL) { |
50 openal_stopsound(voiceBeingPlayed); |
48 Mix_HaltChannel(lastChannel); |
51 voiceBeingPlayed = -1; |
49 Mix_FreeChunk(voiceBeingPlayed); |
|
50 voiceBeingPlayed = NULL; |
52 } |
51 } |
53 } |
52 } |
54 |
53 |
55 |
54 |
56 #pragma mark - |
55 #pragma mark - |
103 self.lastIndexPath = indexPath; |
102 self.lastIndexPath = indexPath; |
104 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
103 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
105 } |
104 } |
106 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
105 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
107 |
106 |
108 if (voiceBeingPlayed >= 0) { |
107 if (voiceBeingPlayed != NULL) { |
109 openal_stopsound(voiceBeingPlayed); |
108 Mix_HaltChannel(lastChannel); |
110 voiceBeingPlayed = -1; |
109 Mix_FreeChunk(voiceBeingPlayed); |
|
110 voiceBeingPlayed = NULL; |
111 } |
111 } |
112 |
112 |
113 // the keyword static prevents re-initialization of the variable |
|
114 NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
113 NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
115 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
114 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
116 |
115 |
117 int index = random() % [array count]; |
116 int index = random() % [array count]; |
118 |
117 |
119 voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
118 voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
120 [voiceDir release]; |
119 [voiceDir release]; |
121 openal_playsound(voiceBeingPlayed); |
120 lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0); |
122 } |
121 } |
123 |
122 |
124 |
123 |
125 #pragma mark - |
124 #pragma mark - |
126 #pragma mark Memory management |
125 #pragma mark Memory management |
129 [super didReceiveMemoryWarning]; |
128 [super didReceiveMemoryWarning]; |
130 // Relinquish ownership any cached data, images, etc that aren't in use. |
129 // Relinquish ownership any cached data, images, etc that aren't in use. |
131 } |
130 } |
132 |
131 |
133 -(void) viewDidUnload { |
132 -(void) viewDidUnload { |
134 openal_close(); |
133 voiceBeingPlayed = NULL; |
135 voiceBeingPlayed = -1; |
|
136 self.lastIndexPath = nil; |
134 self.lastIndexPath = nil; |
137 self.teamDictionary = nil; |
135 self.teamDictionary = nil; |
138 self.voiceArray = nil; |
136 self.voiceArray = nil; |
139 MSG_DIDUNLOAD(); |
137 MSG_DIDUNLOAD(); |
140 [super viewDidUnload]; |
138 [super viewDidUnload]; |