Line data Source code
1 0 : /* 2 : 3 : OOALMusic.m 4 : 5 : 6 : OOALSound - OpenAL sound implementation for Oolite. 7 : Copyright (C) 2005-2013 Jens Ayton 8 : 9 : Permission is hereby granted, free of charge, to any person obtaining a copy 10 : of this software and associated documentation files (the "Software"), to deal 11 : in the Software without restriction, including without limitation the rights 12 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 : copies of the Software, and to permit persons to whom the Software is 14 : furnished to do so, subject to the following conditions: 15 : 16 : The above copyright notice and this permission notice shall be included in all 17 : copies or substantial portions of the Software. 18 : 19 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 : SOFTWARE. 26 : 27 : */ 28 : 29 : #import "OOALMusic.h" 30 : 31 0 : static OOMusic *sPlayingMusic = nil; 32 0 : static OOSoundSource *sMusicSource = nil; 33 : 34 : 35 : @implementation OOMusic 36 : 37 0 : + (id)allocWithZone:(NSZone *)inZone 38 : { 39 : return NSAllocateObject([OOMusic class], 0, inZone); 40 : } 41 : 42 : 43 0 : - (void)dealloc 44 : { 45 : if (sPlayingMusic == self) [self stop]; 46 : [sound release]; 47 : 48 : [super dealloc]; 49 : } 50 : 51 0 : - (id)initWithContentsOfFile:(NSString *)inPath 52 : { 53 : self = [super init]; 54 : if (nil != self) 55 : { 56 : sound = [[OOSound alloc] initWithContentsOfFile:inPath]; 57 : if (nil == sound) 58 : { 59 : [self release]; 60 : self = nil; 61 : } 62 : } 63 : 64 : return self; 65 : } 66 : 67 : 68 0 : - (NSString *)name 69 : { 70 : return [sound name]; 71 : } 72 : 73 : 74 : - (void)setMusicGain:(float)newValue 75 : { 76 : if (nil != sMusicSource) 77 : { 78 : [sMusicSource setGain:newValue]; 79 : } 80 : } 81 : 82 : 83 : - (float) musicGain 84 : { 85 : if (nil == sMusicSource) return 0.0f; 86 : return [sMusicSource gain]; 87 : } 88 : 89 : 90 : - (void)playLooped:(BOOL)inLoop 91 : { 92 : if (sPlayingMusic != self) 93 : { 94 : if (nil == sMusicSource) 95 : { 96 : sMusicSource = [[OOSoundSource alloc] init]; 97 : } 98 : [sMusicSource stop]; 99 : [sMusicSource setLoop:inLoop]; 100 : [sMusicSource setSound:sound]; 101 : [sMusicSource play]; 102 : 103 : sPlayingMusic = self; 104 : } 105 : } 106 : 107 : 108 : - (OOSoundSource *)musicSoundSource 109 : { 110 : return sMusicSource; 111 : } 112 : 113 : 114 : - (BOOL)isPlaying 115 : { 116 : return sPlayingMusic == self && [sMusicSource isPlaying]; 117 : } 118 : 119 : 120 : - (void)stop 121 : { 122 : if (sPlayingMusic == self) 123 : { 124 : sPlayingMusic = nil; 125 : [sMusicSource stop]; 126 : [sMusicSource setSound:nil]; 127 : } 128 : } 129 : 130 : @end