PageRenderTime 113ms CodeModel.GetById 97ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/ObjectAL/OpenAL/ALSoundSourcePool.m

http://github.com/kstenerud/ObjectAL-for-iPhone
Objective C | 171 lines | 104 code | 30 blank | 37 comment | 10 complexity | 36a119386f6d17f4ec7e3130cfaa8567 MD5 | raw file
  1//
  2//  SoundSourcePool.m
  3//  ObjectAL
  4//
  5//  Created by Karl Stenerud on 17/12/09.
  6//
  7// Copyright 2009 Karl Stenerud
  8//
  9// Licensed under the Apache License, Version 2.0 (the "License");
 10// you may not use this file except in compliance with the License.
 11// You may obtain a copy of the License at
 12//
 13// http://www.apache.org/licenses/LICENSE-2.0
 14//
 15// Unless required by applicable law or agreed to in writing, software
 16// distributed under the License is distributed on an "AS IS" BASIS,
 17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18// See the License for the specific language governing permissions and
 19// limitations under the License.
 20//
 21// Note: You are NOT required to make the license available from within your
 22// iOS application. Including it in your project is sufficient.
 23//
 24// Attribution is not required, but appreciated :)
 25//
 26
 27#import "ALSoundSourcePool.h"
 28#import "ObjectALMacros.h"
 29
 30
 31#pragma mark Private Methods
 32
 33/**
 34 * Private interface to SoundSourcePool.
 35 */
 36@interface ALSoundSourcePool (Private)
 37
 38/** (INTERNAL USE) Close any resources belonging to the OS.
 39 */
 40- (void) closeOSResources;
 41
 42/** Move a source to the head of the list.
 43 *
 44 * @param index the index of the source to move.
 45 */
 46- (void) moveToHead:(int) index;
 47
 48@end
 49
 50
 51#pragma mark -
 52#pragma mark SoundSourcePool
 53
 54@implementation ALSoundSourcePool
 55
 56#pragma mark Object Management
 57
 58+ (id) pool
 59{
 60	return [[[self alloc] init] autorelease];
 61}
 62
 63- (id) init
 64{
 65	if(nil != (self = [super init]))
 66	{
 67		sources = [[NSMutableArray alloc] initWithCapacity:10];
 68	}
 69	return self;
 70}
 71
 72- (void) dealloc
 73{
 74	[self closeOSResources];
 75
 76	[sources release];
 77	[super dealloc];
 78}
 79
 80- (void) closeOSResources
 81{
 82	// Not directly holding any OS resources.
 83}
 84
 85- (void) close
 86{
 87	OPTIONALLY_SYNCHRONIZED(self)
 88	{
 89		if(nil != sources)
 90		{
 91			[sources makeObjectsPerformSelector:@selector(close)];
 92			[sources release];
 93			sources = nil;
 94			
 95			[self closeOSResources];
 96		}
 97	}
 98}
 99
100
101#pragma mark Properties
102
103@synthesize sources;
104
105
106#pragma mark Source Management
107
108- (void) addSource:(id<ALSoundSource>) source
109{
110	OPTIONALLY_SYNCHRONIZED(self)
111	{
112		[sources addObject:source];
113	}
114}
115
116- (void) removeSource:(id<ALSoundSource>) source
117{
118	OPTIONALLY_SYNCHRONIZED(self)
119	{
120		[sources removeObject:source];
121	}
122}
123
124- (void) moveToHead:(int) index
125{
126	OPTIONALLY_SYNCHRONIZED(self)
127	{
128		id source = [[sources objectAtIndex:index] retain];
129		[sources removeObjectAtIndex:index];
130		[sources addObject:source];
131		[source release];
132	}
133}
134
135- (id<ALSoundSource>) getFreeSource:(bool) attemptToInterrupt
136{
137	int index = 0;
138	
139	OPTIONALLY_SYNCHRONIZED(self)
140	{
141		// Try to find any free source.
142		for(id<ALSoundSource> source in sources)
143		{
144			if(!source.playing)
145			{
146				[self moveToHead:index];
147				return source;
148			}
149			index++;
150		}
151		
152		if(attemptToInterrupt)
153		{
154			// Try to forcibly free a source.
155			index = 0;
156			for(id<ALSoundSource> source in sources)
157			{
158				if(!source.playing || source.interruptible)
159				{
160					[source stop];
161					[self moveToHead:index];
162					return source;
163				}
164				index++;
165			}
166		}
167	}		
168	return nil;
169}
170
171@end