/*
 *  AudioFileArray.m
 *  AudioUIdemo
 *
 *	Code and Sound Design by Peter "pdx" Drescher.
 *  Copyright 2010 twittering machine. All rights reserved.
 *
 */

#import "AudioFileArray.h"

@implementation AudioFileArray

/*
*	load and play the "initializing!" sound, then release it, and load in the "clearing screen!" sound
*	create the array, and load it with the default "beep" sound set
*/
- (id)init
{
	if ([super init])
	{
		tSounds		= [[NSMutableArray alloc] init];
		NSBundle* initBundle = [NSBundle mainBundle];
		initClearSound	= [[NSSound alloc] initWithContentsOfFile:[initBundle pathForResource:@"initSound" ofType:@"aif"] byReference:NO];
		[initClearSound play];
		[initClearSound release];
		initClearSound	= [[NSSound alloc] initWithContentsOfFile:[initBundle pathForResource:@"clearSound" ofType:@"aif"] byReference:NO];		
		[self loadAudioArray:@"beep"];
	}
	return self;
}

- (void)dealloc
{
	[tSounds release];
	[initClearSound release];
	[alertSound1 release];
	[alertSound2 release];
	[super dealloc];
}

/*
*	sound sets consist of 41 .aif files, naming convention: "prefix" + "4 numbers between '0000' and '1000'" + ".aif"
*	array created by scanning through the files, and loading the sounds into the array in numerical sequence.
*	each sound set also contains 2 related alert sounds (white alert = informational, red alert = attention grabbing)
*/
- (id)loadAudioArray:(NSString *)audioFilePrefix
{
	[tSounds removeAllObjects]; //clear array
	NSBundle* bundle = [NSBundle mainBundle];
	NSMutableString	*audioFileName;
	NSSound *loadSound;
	int audioFileNum, arrayCt;

	NSFileManager *localFileManager=[[NSFileManager alloc] init];
	for (audioFileNum = 0, arrayCt = 0; audioFileNum < 1001; audioFileNum++)
	{
		audioFileName = [NSMutableString stringWithFormat:@"%@%04d", audioFilePrefix, audioFileNum];
		if ([localFileManager fileExistsAtPath:[bundle pathForResource:audioFileName ofType:@"aif"]] == YES) {
			loadSound = [[NSSound alloc] initWithContentsOfFile:[bundle pathForResource:audioFileName ofType:@"aif"] byReference:NO];
			[loadSound setVolume : 0.4];
			[tSounds insertObject:loadSound atIndex:arrayCt];
			arrayCt++;
			[loadSound release];
		}
	}
	[localFileManager release];
	
	
	audioFileName = [NSString stringWithFormat:@"%@%@", audioFilePrefix, @"Alert1"];
	[alertSound1 release];
	alertSound1 = [[NSSound alloc] initWithContentsOfFile:[bundle pathForResource:audioFileName ofType:@"aif"] byReference:NO];
	
	audioFileName = [NSString stringWithFormat:@"%@%@", audioFilePrefix, @"Alert2"];
	[alertSound2 release];
	alertSound2 = [[NSSound alloc] initWithContentsOfFile:[bundle pathForResource:audioFileName ofType:@"aif"] byReference:NO];
	
	return self;	
}

//  for each charater printed, play the sound at the selected position in the array
- (void)playSoundAt:(int)arrayPos
{
	[[tSounds objectAtIndex: arrayPos] play];
}

//	play the white or red alert, during a "flashing error message" sequence
- (void)playAlertSound:(int)alertType
{
	switch (alertType) {
		case 1:
			[alertSound1 play];
			break;
		case 2:
			[alertSound2 play];
			break;
		default:
			break;
	}
}

//	play when user blanks screen by typing "clear" or pressing shift-delete
- (void)playClearSound;
{
	[initClearSound play];
}

@end
