Oolite
Loading...
Searching...
No Matches
OOJSSoundSource.m
Go to the documentation of this file.
1/*
2
3OOJSSoundSource.m (QuickJS-ng)
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOJSSoundSource.h"
26#import "OOJSSound.h"
27#import "OOJSVector.h"
29#import "OOSound.h"
30#import "ResourceManager.h"
31
32static JSClassID sSoundSourceClassID;
33
37
38// Methods
39static JSValue SoundSourcePlay(OOJS_NATIVE_ARGS);
40static JSValue SoundSourceStop(OOJS_NATIVE_ARGS);
42
43static JSClassDef sSoundSourceClass = {
44 .class_name = "SoundSource",
45 .finalizer = OOJSObjectWrapperFinalize,
46};
47
48enum {
49 // Property IDs
57};
58
69
70static const JSCFunctionListEntry sSoundSourceMethods[] = {
71 // JS name, min args, function
72 JS_CFUNC_DEF("toString", 0, OOJSObjectWrapperToString),
73 JS_CFUNC_DEF("play", 0, SoundSourcePlay),
74 JS_CFUNC_DEF("playOrRepeat", 0, SoundSourcePlayOrRepeat),
75 // playSound is defined in oolite-global-prefix.js.
76 JS_CFUNC_DEF("stop", 0, SoundSourceStop),
77};
78
80
81// *** Public ***
82
90
92{
93 OOJS_NATIVE_ENTER(context)
94
96 OOJSReportError(context, @"SoundSource() cannot be called as a function, it must be used as a constructor (as in new SoundSource()).");
97 return JS_EXCEPTION;
98 }
99
100 OOJS_RETURN_OBJECT([[[OOSoundSource alloc] init] autorelease]);
101
103}
104
105// *** Implementation stuff ***
106
108{
109 OOJS_NATIVE_ENTER(context)
110
111 OOSoundSource* soundSource = nil;
112
113 if (!JSSoundSourceGetSoundSource(context, this, &soundSource))
114 return OOJSNativeFailure(context);
115
116 switch (magic) {
118 return OOJSValueFromNativeObject(context, [soundSource sound]);
119
121 return OOJSValueFromBOOL([soundSource isPlaying]);
122
124 return OOJSValueFromBOOL([soundSource loop]);
125
127 return JS_NewInt32(context, [soundSource repeatCount]);
128
130 OOJS_RETURN_VECTOR([soundSource position]);
131
133 return OOJSValueFromBOOL([soundSource positional]);
134
136 return JS_NewNumber(context, [soundSource gain]);
137
138 default:
140 return OOJSNativeFailure(context);
141 }
142
144}
145
147{
148 OOJS_NATIVE_ENTER(context)
149
150 OOSoundSource* soundSource = nil;
151 int32_t iValue;
152 int bValue;
153 Vector vValue;
154 double fValue;
155
156 if (!JSSoundSourceGetSoundSource(context, this, &soundSource))
157 return OOJSNativeFailure(context);
158
159 switch (magic) {
161 [soundSource setSound:SoundFromJSValue(context, value)];
162 return JS_UNDEFINED;
163 break;
164
166 if ((bValue = JS_ToBool(context, value)) >= 0) {
167 [soundSource setLoop:bValue];
168 return JS_UNDEFINED;
169 }
170 break;
171
173 if (OOJSValueToInt32(context, value, &iValue)) {
174 if (iValue > 100)
175 iValue = 100;
176 if (100 < 1)
177 iValue = 1;
178 [soundSource setRepeatCount:iValue];
179 return JS_UNDEFINED;
180 }
181 break;
182
184 if (JSValueToVector(context, value, &vValue)) {
185 [soundSource setPosition:vValue];
186 return JS_UNDEFINED;
187 }
188 break;
189
191 if ((bValue = JS_ToBool(context, value)) >= 0) {
192 [soundSource setPositional:bValue];
193 return JS_UNDEFINED;
194 }
195 break;
196
198 if (JS_ToFloat64(context, &fValue, value) == 0) {
199 fValue = OOClamp_0_max_d(fValue, 1);
200 [soundSource setGain:fValue];
201 return JS_UNDEFINED;
202 }
203 break;
204
205 default:
207 return OOJSNativeFailure(context);
208 }
209
211 return OOJSNativeFailure(context);
212
214}
215
216// *** Methods ***
217
218// play([count : Number])
220{
221 OOJS_NATIVE_ENTER(context)
222
223 OOSoundSource* thisv = nil;
224 int32_t count = 0;
225
226 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv)))
227 return OOJSNativeFailure(context);
228 if (argc > 0 && !JS_IsUndefined(OOJS_ARGV[0]) && !OOJSValueToInt32(context, OOJS_ARGV[0], &count)) {
229 OOJSReportBadArguments(context, @"SoundSource", @"play", 1, OOJS_ARGV, nil, @"integer count or no argument");
230 return OOJSNativeFailure(context);
231 }
232
233 if (count > 0) {
234 if (count > 100)
235 count = 100;
236 [thisv setRepeatCount:count];
237 }
238
240 [thisv play];
242
244
246}
247
248// stop()
250{
251 OOJS_NATIVE_ENTER(context)
252
253 OOSoundSource* thisv = nil;
254
255 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv)))
256 return OOJSNativeFailure(context);
257
259 [thisv stop];
261
263
265}
266
267// playOrRepeat()
269{
270 OOJS_NATIVE_ENTER(context)
271
272 OOSoundSource* thisv = nil;
273
274 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv)))
275 return OOJSNativeFailure(context);
276
278 [thisv playOrRepeat];
280
282
284}
285
286@implementation OOSoundSource (OOJavaScriptExtentions)
287
288- (JSValue)oo_jsValueInContext:(JSContext*)context
289{
290 JSValue result = JS_NewObjectClass(context, (int)sSoundSourceClassID);
291 if (JS_IsException(result)) {
292 JS_FreeValue(context, JS_GetException(context));
293 return JS_NULL;
294 }
295 JS_SetOpaque(result, [self retain]);
296
297 return result;
298}
299
300- (NSString*)oo_jsClassName
301{
302 return @"SoundSource";
303}
304
305@end
OOINLINE double OOClamp_0_max_d(double value, double max) INLINE_CONST_FUNC
#define EXPECT_NOT(x)
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
void InitOOJSSoundSource(JSContext *context, JSObject *global)
static JSBool SoundSourceConstruct(JSContext *context, uintN argc, jsval *vp)
static JSBool SoundSourcePlay(JSContext *context, uintN argc, jsval *vp)
static JSClass sSoundSourceClass
static JSBool SoundSourcePlayOrRepeat(JSContext *context, uintN argc, jsval *vp)
static JSBool SoundSourceGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool SoundSourceSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSBool SoundSourceStop(JSContext *context, uintN argc, jsval *vp)
static JSFunctionSpec sSoundSourceMethods[]
@ kSoundSource_loop
@ kSoundSource_positional
@ kSoundSource_volume
@ kSoundSource_position
@ kSoundSource_sound
@ kSoundSource_repeatCount
@ kSoundSource_isPlaying
static JSPropertySpec sSoundSourceProperties[]
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector) NONNULL_FUNC
Definition OOJSVector.m:341
#define OOJS_THIS
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
OOINLINE BOOL OOJSValueToInt32(JSContext *context, jsval value, int32_t *outValue)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_ARGV
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_RETURN_VOID
#define OOJS_RETURN_VECTOR(value)
unsigned count
return nil
static JSClassID sSoundSourceClassID
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
#define OOJS_IS_CONSTRUCTING
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
#define OOJS_SETTER_ARGS
#define OOJS_PROPERTY_RO(name, getter, magic)
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_GETTER_ARGS
#define OOJS_NATIVE_ARGS
#define OOJS_COUNT(array)
void setPositional:(BOOL inPositional)
void setLoop:(BOOL inLoop)
void setRepeatCount:(uint8_t inCount)
void setGain:(float gain)
void setPosition:(Vector inPosition)
void setSound:(OOSound *inSound)