/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. #import "ALSoundSourcePool.h"
  27. #import "ObjectALMacros.h"
  28. #pragma mark Private Methods
  29. /**
  30. * Private interface to SoundSourcePool.
  31. */
  32. @interface ALSoundSourcePool (Private)
  33. /** (INTERNAL USE) Close any resources belonging to the OS.
  34. */
  35. - (void) closeOSResources;
  36. /** Move a source to the head of the list.
  37. *
  38. * @param index the index of the source to move.
  39. */
  40. - (void) moveToHead:(int) index;
  41. @end
  42. #pragma mark -
  43. #pragma mark SoundSourcePool
  44. @implementation ALSoundSourcePool
  45. #pragma mark Object Management
  46. + (id) pool
  47. {
  48. return [[[self alloc] init] autorelease];
  49. }
  50. - (id) init
  51. {
  52. if(nil != (self = [super init]))
  53. {
  54. sources = [[NSMutableArray alloc] initWithCapacity:10];
  55. }
  56. return self;
  57. }
  58. - (void) dealloc
  59. {
  60. [self closeOSResources];
  61. [sources release];
  62. [super dealloc];
  63. }
  64. - (void) closeOSResources
  65. {
  66. // Not directly holding any OS resources.
  67. }
  68. - (void) close
  69. {
  70. OPTIONALLY_SYNCHRONIZED(self)
  71. {
  72. if(nil != sources)
  73. {
  74. [sources makeObjectsPerformSelector:@selector(close)];
  75. [sources release];
  76. sources = nil;
  77. [self closeOSResources];
  78. }
  79. }
  80. }
  81. #pragma mark Properties
  82. @synthesize sources;
  83. #pragma mark Source Management
  84. - (void) addSource:(id<ALSoundSource>) source
  85. {
  86. OPTIONALLY_SYNCHRONIZED(self)
  87. {
  88. [sources addObject:source];
  89. }
  90. }
  91. - (void) removeSource:(id<ALSoundSource>) source
  92. {
  93. OPTIONALLY_SYNCHRONIZED(self)
  94. {
  95. [sources removeObject:source];
  96. }
  97. }
  98. - (void) moveToHead:(int) index
  99. {
  100. OPTIONALLY_SYNCHRONIZED(self)
  101. {
  102. id source = [[sources objectAtIndex:index] retain];
  103. [sources removeObjectAtIndex:index];
  104. [sources addObject:source];
  105. [source release];
  106. }
  107. }
  108. - (id<ALSoundSource>) getFreeSource:(bool) attemptToInterrupt
  109. {
  110. int index = 0;
  111. OPTIONALLY_SYNCHRONIZED(self)
  112. {
  113. // Try to find any free source.
  114. for(id<ALSoundSource> source in sources)
  115. {
  116. if(!source.playing)
  117. {
  118. [self moveToHead:index];
  119. return source;
  120. }
  121. index++;
  122. }
  123. if(attemptToInterrupt)
  124. {
  125. // Try to forcibly free a source.
  126. index = 0;
  127. for(id<ALSoundSource> source in sources)
  128. {
  129. if(!source.playing || source.interruptible)
  130. {
  131. [source stop];
  132. [self moveToHead:index];
  133. return source;
  134. }
  135. index++;
  136. }
  137. }
  138. }
  139. return nil;
  140. }
  141. @end