actually set the variable here
[swfdec.git] / swfdec / swfdec_sound_object.c
blobac553dae1b2f4d9e153c837d4e3a75b6b5adddae
1 /* Swfdec
2 * Copyright (C) 2007-2008 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <math.h>
25 #include "swfdec_sound_object.h"
26 #include "swfdec_as_context.h"
27 #include "swfdec_as_internal.h"
28 #include "swfdec_as_native_function.h"
29 #include "swfdec_as_object.h"
30 #include "swfdec_as_strings.h"
31 #include "swfdec_debug.h"
32 #include "swfdec_internal.h"
33 #include "swfdec_player_internal.h"
34 #include "swfdec_resource.h"
35 #include "swfdec_sound.h"
37 /*** SwfdecSoundObject ***/
39 G_DEFINE_TYPE (SwfdecSoundObject, swfdec_sound_object, SWFDEC_TYPE_AS_RELAY)
41 static void
42 swfdec_sound_object_mark (SwfdecGcObject *object)
44 SwfdecSoundObject *sound = SWFDEC_SOUND_OBJECT (object);
46 if (sound->target != NULL)
47 swfdec_as_string_mark (sound->target);
49 SWFDEC_GC_OBJECT_CLASS (swfdec_sound_object_parent_class)->mark (object);
52 static void
53 swfdec_sound_object_dispose (GObject *object)
55 SwfdecSoundObject *sound = SWFDEC_SOUND_OBJECT (object);
57 if (sound->provider) {
58 g_object_unref (sound->provider);
59 sound->provider = NULL;
62 G_OBJECT_CLASS (swfdec_sound_object_parent_class)->dispose (object);
65 static void
66 swfdec_sound_object_class_init (SwfdecSoundObjectClass *klass)
68 GObjectClass *object_class = G_OBJECT_CLASS (klass);
69 SwfdecGcObjectClass *gc_class = SWFDEC_GC_OBJECT_CLASS (klass);
71 object_class->dispose = swfdec_sound_object_dispose;
73 gc_class->mark = swfdec_sound_object_mark;
76 static void
77 swfdec_sound_object_init (SwfdecSoundObject *sound)
81 static SwfdecActor *
82 swfdec_sound_object_get_actor (SwfdecSoundObject *sound)
84 SwfdecPlayer *player = SWFDEC_PLAYER (swfdec_gc_object_get_context (sound));
85 SwfdecMovie *movie;
87 movie = swfdec_player_get_movie_from_string (player,
88 sound->target ? sound->target : "");
89 if (!SWFDEC_IS_ACTOR (movie))
90 return NULL;
91 return SWFDEC_ACTOR (movie);
94 static SwfdecSound *
95 swfdec_sound_object_get_sound (SwfdecSoundObject *sound, const char *name)
97 SwfdecActor *actor = swfdec_sound_object_get_actor (sound);
99 if (actor == NULL)
100 return NULL;
102 return swfdec_resource_get_export (SWFDEC_MOVIE (actor)->resource, name);
105 /*** AS CODE ***/
107 static SwfdecSoundMatrix *
108 swfdec_sound_object_get_matrix (SwfdecSoundObject *sound)
110 if (sound->provider) {
111 SwfdecSoundMatrix *ret = swfdec_sound_provider_get_matrix (sound->provider);
112 if (ret)
113 return ret;
116 if (sound->target == NULL) {
117 return &SWFDEC_PLAYER (swfdec_gc_object_get_context (sound))->priv->sound_matrix;
118 } else {
119 SwfdecActor *actor = swfdec_sound_object_get_actor (sound);
120 if (actor)
121 return &actor->sound_matrix;
123 return NULL;
126 SWFDEC_AS_NATIVE (500, 0, swfdec_sound_object_getPan)
127 void
128 swfdec_sound_object_getPan (SwfdecAsContext *cx, SwfdecAsObject *object,
129 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
131 SwfdecSoundObject *sound;
132 const SwfdecSoundMatrix *matrix;
134 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "");
136 matrix = swfdec_sound_object_get_matrix (sound);
137 if (matrix == NULL)
138 return;
140 swfdec_as_value_set_integer (cx, ret, swfdec_sound_matrix_get_pan (matrix));
143 SWFDEC_AS_NATIVE (500, 1, swfdec_sound_object_getTransform)
144 void
145 swfdec_sound_object_getTransform (SwfdecAsContext *cx, SwfdecAsObject *object,
146 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
148 SwfdecSoundObject *sound;
149 const SwfdecSoundMatrix *matrix;
150 SwfdecAsObject *obj;
151 SwfdecAsValue val;
153 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "");
155 matrix = swfdec_sound_object_get_matrix (sound);
156 if (matrix == NULL)
157 return;
159 obj = swfdec_as_object_new (cx, SWFDEC_AS_STR_Object, NULL);
161 swfdec_as_value_set_integer (cx, &val, matrix->ll);
162 swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_ll, &val);
163 swfdec_as_value_set_integer (cx, &val, matrix->lr);
164 swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_lr, &val);
165 swfdec_as_value_set_integer (cx, &val, matrix->rl);
166 swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_rl, &val);
167 swfdec_as_value_set_integer (cx, &val, matrix->rr);
168 swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_rr, &val);
170 SWFDEC_AS_VALUE_SET_OBJECT (ret, obj);
173 SWFDEC_AS_NATIVE (500, 2, swfdec_sound_object_getVolume)
174 void
175 swfdec_sound_object_getVolume (SwfdecAsContext *cx, SwfdecAsObject *object,
176 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
178 SwfdecSoundObject *sound;
179 const SwfdecSoundMatrix *matrix;
181 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "");
183 matrix = swfdec_sound_object_get_matrix (sound);
184 if (matrix == NULL)
185 return;
187 swfdec_as_value_set_integer (cx, ret, matrix->volume);
190 SWFDEC_AS_NATIVE (500, 3, swfdec_sound_object_setPan)
191 void
192 swfdec_sound_object_setPan (SwfdecAsContext *cx, SwfdecAsObject *object,
193 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
195 SwfdecSoundObject *sound;
196 SwfdecSoundMatrix *matrix;
197 int pan;
199 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "i", &pan);
201 matrix = swfdec_sound_object_get_matrix (sound);
202 if (matrix == NULL)
203 return;
205 swfdec_sound_matrix_set_pan (matrix, pan);
208 SWFDEC_AS_NATIVE (500, 4, swfdec_sound_object_setTransform)
209 void
210 swfdec_sound_object_setTransform (SwfdecAsContext *cx, SwfdecAsObject *object,
211 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
213 SwfdecSoundObject *sound;
214 SwfdecSoundMatrix *matrix;
215 SwfdecAsObject *trans;
216 SwfdecAsValue *val;
218 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "o", &trans);
220 matrix = swfdec_sound_object_get_matrix (sound);
221 if (matrix == NULL)
222 return;
224 /* ll */
225 val = swfdec_as_object_peek_variable (trans, SWFDEC_AS_STR_ll);
226 if (val) {
227 matrix->ll = swfdec_as_value_to_integer (cx, val);
228 } else if (swfdec_as_object_has_variable (trans, SWFDEC_AS_STR_ll) == trans) {
229 matrix->ll = 0;
231 /* lr */
232 val = swfdec_as_object_peek_variable (trans, SWFDEC_AS_STR_lr);
233 if (val) {
234 matrix->lr = swfdec_as_value_to_integer (cx, val);
235 } else if (swfdec_as_object_has_variable (trans, SWFDEC_AS_STR_lr) == trans) {
236 matrix->lr = 0;
238 /* rr */
239 val = swfdec_as_object_peek_variable (trans, SWFDEC_AS_STR_rr);
240 if (val) {
241 matrix->rr = swfdec_as_value_to_integer (cx, val);
242 } else if (swfdec_as_object_has_variable (trans, SWFDEC_AS_STR_rr) == trans) {
243 matrix->rr = 0;
245 /* rl */
246 val = swfdec_as_object_peek_variable (trans, SWFDEC_AS_STR_rl);
247 if (val) {
248 matrix->rl = swfdec_as_value_to_integer (cx, val);
249 } else if (swfdec_as_object_has_variable (trans, SWFDEC_AS_STR_rl) == trans) {
250 matrix->rl = 0;
254 SWFDEC_AS_NATIVE (500, 5, swfdec_sound_object_setVolume)
255 void
256 swfdec_sound_object_setVolume (SwfdecAsContext *cx, SwfdecAsObject *object,
257 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
259 SwfdecSoundObject *sound;
260 SwfdecSoundMatrix *matrix;
261 int volume;
263 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "i", &volume);
265 matrix = swfdec_sound_object_get_matrix (sound);
266 if (matrix == NULL)
267 return;
269 matrix->volume = volume;
272 SWFDEC_AS_NATIVE (500, 9, swfdec_sound_object_getDuration)
273 void
274 swfdec_sound_object_getDuration (SwfdecAsContext *cx, SwfdecAsObject *object,
275 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
277 SWFDEC_STUB ("Sound.getDuration");
280 SWFDEC_AS_NATIVE (500, 10, swfdec_sound_object_setDuration)
281 void
282 swfdec_sound_object_setDuration (SwfdecAsContext *cx, SwfdecAsObject *object,
283 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
285 SWFDEC_STUB ("Sound.setDuration");
288 SWFDEC_AS_NATIVE (500, 11, swfdec_sound_object_getPosition)
289 void
290 swfdec_sound_object_getPosition (SwfdecAsContext *cx, SwfdecAsObject *object,
291 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
293 SWFDEC_STUB ("Sound.getPosition");
296 SWFDEC_AS_NATIVE (500, 12, swfdec_sound_object_setPosition)
297 void
298 swfdec_sound_object_setPosition (SwfdecAsContext *cx, SwfdecAsObject *object,
299 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
301 SWFDEC_STUB ("Sound.setPosition");
304 SWFDEC_AS_NATIVE (500, 13, swfdec_sound_object_loadSound)
305 void
306 swfdec_sound_object_loadSound (SwfdecAsContext *cx, SwfdecAsObject *object,
307 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
309 SwfdecSoundObject *sound;
310 SwfdecActor *actor;
311 const char *url;
312 gboolean stream;
314 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "sb", &url, &stream);
315 actor = swfdec_sound_object_get_actor (sound);
316 if (actor == NULL)
317 return;
319 if (sound->provider)
320 g_object_unref (sound->provider);
321 sound->provider = SWFDEC_SOUND_PROVIDER (swfdec_load_sound_new (object, url));
322 if (stream)
323 swfdec_sound_provider_start (sound->provider, actor, 0, 1);
326 SWFDEC_AS_NATIVE (500, 14, swfdec_sound_object_getBytesLoaded)
327 void
328 swfdec_sound_object_getBytesLoaded (SwfdecAsContext *cx, SwfdecAsObject *object,
329 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
331 SWFDEC_STUB ("Sound.getBytesLoaded");
334 SWFDEC_AS_NATIVE (500, 15, swfdec_sound_object_getBytesTotal)
335 void
336 swfdec_sound_object_getBytesTotal (SwfdecAsContext *cx, SwfdecAsObject *object,
337 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
339 SWFDEC_STUB ("Sound.getBytesTotal");
342 SWFDEC_AS_NATIVE (500, 18, swfdec_sound_object_get_checkPolicyFile)
343 void
344 swfdec_sound_object_get_checkPolicyFile (SwfdecAsContext *cx,
345 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
346 SwfdecAsValue *ret)
348 SWFDEC_STUB ("Sound.checkPolicyFile (get)");
351 SWFDEC_AS_NATIVE (500, 19, swfdec_sound_object_set_checkPolicyFile)
352 void
353 swfdec_sound_object_set_checkPolicyFile (SwfdecAsContext *cx,
354 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
355 SwfdecAsValue *ret)
357 SWFDEC_STUB ("Sound.checkPolicyFile (set)");
360 SWFDEC_AS_NATIVE (500, 16, swfdec_sound_object_areSoundsInaccessible)
361 void
362 swfdec_sound_object_areSoundsInaccessible (SwfdecAsContext *cx,
363 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
364 SwfdecAsValue *ret)
366 SWFDEC_STUB ("Sound.areSoundsInaccessible");
369 SWFDEC_AS_NATIVE (500, 7, swfdec_sound_object_attachSound)
370 void
371 swfdec_sound_object_attachSound (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
372 SwfdecAsValue *argv, SwfdecAsValue *ret)
374 SwfdecSoundObject *sound;
375 const char *name;
376 SwfdecSound *new;
378 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "s", &name);
380 new = swfdec_sound_object_get_sound (sound, name);
381 if (new) {
382 if (sound->provider)
383 g_object_unref (sound->provider);
384 sound->provider = g_object_ref (new);
388 SWFDEC_AS_NATIVE (500, 8, swfdec_sound_object_start)
389 void
390 swfdec_sound_object_start (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
391 SwfdecAsValue *argv, SwfdecAsValue *ret)
393 SwfdecSoundObject *sound;
394 SwfdecActor *actor;
395 double offset = 0;
396 int loops = 1;
398 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "|ni", &offset, &loops);
399 actor = swfdec_sound_object_get_actor (sound);
400 if (actor == NULL)
401 return;
403 if (sound->provider == NULL) {
404 SWFDEC_INFO ("no sound attached when calling Sound.start()");
405 return;
407 if (loops <= 0)
408 loops = 1;
409 if (offset < 0 || !isfinite (offset))
410 offset = 0;
412 swfdec_sound_provider_start (sound->provider, actor, offset * 44100, loops);
415 SWFDEC_AS_NATIVE (500, 6, swfdec_sound_object_stop)
416 void
417 swfdec_sound_object_stop (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
418 SwfdecAsValue *argv, SwfdecAsValue *ret)
420 SwfdecSoundObject *sound;
421 const char *name = NULL;
422 SwfdecSound *stopme;
423 SwfdecActor *actor;
425 SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "|s", &name);
426 actor = swfdec_sound_object_get_actor (sound);
427 if (actor == NULL)
428 return;
430 if (name) {
431 stopme = swfdec_sound_object_get_sound (sound, name);
432 if (stopme == NULL)
433 return;
434 if (sound->provider == NULL || SWFDEC_IS_SOUND (sound->provider))
435 swfdec_sound_provider_stop (SWFDEC_SOUND_PROVIDER (stopme), actor);
436 } else if (sound->provider) {
437 swfdec_sound_provider_stop (sound->provider, actor);
441 SWFDEC_AS_NATIVE (500, 17, swfdec_sound_object_construct)
442 void
443 swfdec_sound_object_construct (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
444 SwfdecAsValue *argv, SwfdecAsValue *ret)
446 SwfdecSoundObject *sound;
448 if (!swfdec_as_context_is_constructing (cx))
449 return;
451 sound = g_object_new (SWFDEC_TYPE_SOUND_OBJECT, "context", cx, NULL);
452 swfdec_as_object_set_relay (object, SWFDEC_AS_RELAY (sound));
454 if (argc == 0 || SWFDEC_AS_VALUE_IS_UNDEFINED (&argv[0])) {
455 sound->target = NULL;
456 } else {
457 sound->target = swfdec_as_value_to_string (cx, &argv[0]);
460 SWFDEC_AS_VALUE_SET_OBJECT (ret, object);