Oolite
Loading...
Searching...
No Matches
OOOpenALController.m
Go to the documentation of this file.
1/*
2
3OOOpenALController.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
27#import "OOALSoundMixer.h"
28#import "OOLogging.h"
29
30static id sSingleton = nil;
31
32@implementation OOOpenALController
33
35{
36 if (sSingleton == nil) {
37 sSingleton = [[self alloc] init];
38 }
39
40 return sSingleton;
41}
42
43- (id)init
44{
45 self = [super init];
46 if (self != nil) {
47 NSArray* arguments = nil;
48 NSEnumerator* argEnum = nil;
49 NSString* arg = nil;
50
51 arguments = [[NSProcessInfo processInfo] arguments];
52 for (argEnum = [arguments objectEnumerator]; (arg = [argEnum nextObject]);) {
53 if ([arg isEqual:@"-nosound"] || [arg isEqual:@"--nosound"]) {
54 [self release];
55 return nil;
56 }
57 }
58
59 ALuint error;
60 device = alcOpenDevice(NULL); // default device
61 if (!device) {
62 OOLog(kOOLogSoundInitError, @"%@", @"Failed to open default sound device");
63 [self release];
64 return nil;
65 }
66 context = alcCreateContext(device, NULL); // default context
67 if (!alcMakeContextCurrent(context)) {
68 OOLog(kOOLogSoundInitError, @"%@", @"Failed to create default sound context");
69 [self release];
70 return nil;
71 }
72 if ((error = alGetError()) != AL_NO_ERROR) {
73 OOLog(kOOLogSoundInitError, @"Error %d creating sound context", error);
74 }
75 OOAL(alDistanceModel(AL_NONE));
76 }
77 return self;
78}
79
80- (void)setMasterVolume:(ALfloat)fraction
81{
82 OOAL(alListenerf(AL_GAIN, fraction));
83}
84
85- (ALfloat)masterVolume
86{
87 ALfloat fraction = 0.0;
88 OOAL(alGetListenerf(AL_GAIN, &fraction));
89 return fraction;
90}
91
92// only to be called at app shutdown
93// is there a better way to handle this?
94- (void)shutdown
95{
96 [[OOSoundMixer sharedMixer] shutdown];
97 OOAL(alcMakeContextCurrent(NULL));
98 OOAL(alcDestroyContext(context));
99 OOAL(alcCloseDevice(device));
100}
101
102@end
#define OOLog(class, format,...)
Definition OOLogging.h:84
static OONullTexture * sSingleton
static NSString *const kOOLogSoundInitError
#define OOAL(cmd)
Definition OOOpenAL.h:40
return nil
OOOpenALController * sharedController()