~ubuntu-branches/ubuntu/jaunty/swfdec0.8/jaunty

« back to all changes in this revision

Viewing changes to swfdec/swfdec_sound_object.c

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2008-10-10 19:15:24 UTC
  • Revision ID: package-import@ubuntu.com-20081010191524-5z85qiky1d4bvgfa
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Swfdec
 
2
 * Copyright (C) 2007-2008 Benjamin Otte <otte@gnome.org>
 
3
 *
 
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.
 
13
 * 
 
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
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
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"
 
36
 
 
37
/*** SwfdecSoundObject ***/
 
38
 
 
39
G_DEFINE_TYPE (SwfdecSoundObject, swfdec_sound_object, SWFDEC_TYPE_AS_OBJECT)
 
40
 
 
41
static void
 
42
swfdec_sound_object_mark (SwfdecGcObject *object)
 
43
{
 
44
  SwfdecSoundObject *sound = SWFDEC_SOUND_OBJECT (object);
 
45
 
 
46
  if (sound->target != NULL)
 
47
    swfdec_as_string_mark (sound->target);
 
48
 
 
49
  SWFDEC_GC_OBJECT_CLASS (swfdec_sound_object_parent_class)->mark (object);
 
50
}
 
51
 
 
52
static void
 
53
swfdec_sound_object_dispose (GObject *object)
 
54
{
 
55
  SwfdecSoundObject *sound = SWFDEC_SOUND_OBJECT (object);
 
56
 
 
57
  if (sound->provider) {
 
58
    g_object_unref (sound->provider);
 
59
    sound->provider = NULL;
 
60
  }
 
61
 
 
62
  G_OBJECT_CLASS (swfdec_sound_object_parent_class)->dispose (object);
 
63
}
 
64
 
 
65
static void
 
66
swfdec_sound_object_class_init (SwfdecSoundObjectClass *klass)
 
67
{
 
68
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
69
  SwfdecGcObjectClass *gc_class = SWFDEC_GC_OBJECT_CLASS (klass);
 
70
 
 
71
  object_class->dispose = swfdec_sound_object_dispose;
 
72
 
 
73
  gc_class->mark = swfdec_sound_object_mark;
 
74
}
 
75
 
 
76
static void
 
77
swfdec_sound_object_init (SwfdecSoundObject *sound)
 
78
{
 
79
}
 
80
 
 
81
static SwfdecActor *
 
82
swfdec_sound_object_get_actor (SwfdecSoundObject *sound)
 
83
{
 
84
  SwfdecPlayer *player = SWFDEC_PLAYER (swfdec_gc_object_get_context (sound));
 
85
  SwfdecMovie *movie;
 
86
 
 
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);
 
92
}
 
93
 
 
94
static SwfdecSound *
 
95
swfdec_sound_object_get_sound (SwfdecSoundObject *sound, const char *name)
 
96
{
 
97
  SwfdecActor *actor = swfdec_sound_object_get_actor (sound);
 
98
  
 
99
  if (actor == NULL)
 
100
    return NULL;
 
101
 
 
102
  return swfdec_resource_get_export (SWFDEC_MOVIE (actor)->resource, name);
 
103
}
 
104
 
 
105
/*** AS CODE ***/
 
106
 
 
107
static SwfdecSoundMatrix *
 
108
swfdec_sound_object_get_matrix (SwfdecSoundObject *sound)
 
109
{
 
110
  if (sound->provider) {
 
111
    SwfdecSoundMatrix *ret = swfdec_sound_provider_get_matrix (sound->provider);
 
112
    if (ret)
 
113
      return ret;
 
114
  }
 
115
 
 
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;
 
122
  }
 
123
  return NULL;
 
124
}
 
125
 
 
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)
 
130
{
 
131
  SwfdecSoundObject *sound;
 
132
  const SwfdecSoundMatrix *matrix;
 
133
 
 
134
  SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "");
 
135
 
 
136
  matrix = swfdec_sound_object_get_matrix (sound);
 
137
  if (matrix == NULL)
 
138
    return;
 
139
 
 
140
  SWFDEC_AS_VALUE_SET_INT (ret, swfdec_sound_matrix_get_pan (matrix));
 
141
}
 
142
 
 
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)
 
147
{
 
148
  SwfdecSoundObject *sound;
 
149
  const SwfdecSoundMatrix *matrix;
 
150
  SwfdecAsObject *obj;
 
151
  SwfdecAsValue val;
 
152
 
 
153
  SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "");
 
154
 
 
155
  matrix = swfdec_sound_object_get_matrix (sound);
 
156
  if (matrix == NULL)
 
157
    return;
 
158
 
 
159
  obj = swfdec_as_object_new (cx);
 
160
 
 
161
  SWFDEC_AS_VALUE_SET_INT (&val, matrix->ll);
 
162
  swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_ll, &val);
 
163
  SWFDEC_AS_VALUE_SET_INT (&val, matrix->lr);
 
164
  swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_lr, &val);
 
165
  SWFDEC_AS_VALUE_SET_INT (&val, matrix->rl);
 
166
  swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_rl, &val);
 
167
  SWFDEC_AS_VALUE_SET_INT (&val, matrix->rr);
 
168
  swfdec_as_object_set_variable (obj, SWFDEC_AS_STR_rr, &val);
 
169
 
 
170
  SWFDEC_AS_VALUE_SET_OBJECT (ret, obj);
 
171
}
 
172
 
 
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)
 
177
{
 
178
  SwfdecSoundObject *sound;
 
179
  const SwfdecSoundMatrix *matrix;
 
180
 
 
181
  SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "");
 
182
 
 
183
  matrix = swfdec_sound_object_get_matrix (sound);
 
184
  if (matrix == NULL)
 
185
    return;
 
186
 
 
187
  SWFDEC_AS_VALUE_SET_INT (ret, matrix->volume);
 
188
}
 
189
 
 
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)
 
194
{
 
195
  SwfdecSoundObject *sound;
 
196
  SwfdecSoundMatrix *matrix;
 
197
  int pan;
 
198
 
 
199
  SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "i", &pan);
 
200
 
 
201
  matrix = swfdec_sound_object_get_matrix (sound);
 
202
  if (matrix == NULL)
 
203
    return;
 
204
 
 
205
  swfdec_sound_matrix_set_pan (matrix, pan);
 
206
}
 
207
 
 
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)
 
212
{
 
213
  SwfdecSoundObject *sound;
 
214
  SwfdecSoundMatrix *matrix;
 
215
  SwfdecAsObject *trans;
 
216
  SwfdecAsValue *val;
 
217
 
 
218
  SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "o", &trans);
 
219
 
 
220
  matrix = swfdec_sound_object_get_matrix (sound);
 
221
  if (matrix == NULL)
 
222
    return;
 
223
 
 
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;
 
230
  }
 
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;
 
237
  }
 
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;
 
244
  }
 
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;
 
251
  }
 
252
}
 
253
 
 
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)
 
258
{
 
259
  SwfdecSoundObject *sound;
 
260
  SwfdecSoundMatrix *matrix;
 
261
  int volume;
 
262
 
 
263
  SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "i", &volume);
 
264
 
 
265
  matrix = swfdec_sound_object_get_matrix (sound);
 
266
  if (matrix == NULL)
 
267
    return;
 
268
 
 
269
  matrix->volume = volume;
 
270
}
 
271
 
 
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)
 
276
{
 
277
  SWFDEC_STUB ("Sound.getDuration");
 
278
}
 
279
 
 
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)
 
284
{
 
285
  SWFDEC_STUB ("Sound.setDuration");
 
286
}
 
287
 
 
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)
 
292
{
 
293
  SWFDEC_STUB ("Sound.getPosition");
 
294
}
 
295
 
 
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)
 
300
{
 
301
  SWFDEC_STUB ("Sound.setPosition");
 
302
}
 
303
 
 
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)
 
308
{
 
309
  SwfdecSoundObject *sound;
 
310
  SwfdecActor *actor;
 
311
  const char *url;
 
312
  gboolean stream;
 
313
 
 
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;
 
318
 
 
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);
 
324
}
 
325
 
 
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)
 
330
{
 
331
  SWFDEC_STUB ("Sound.getBytesLoaded");
 
332
}
 
333
 
 
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)
 
338
{
 
339
  SWFDEC_STUB ("Sound.getBytesTotal");
 
340
}
 
341
 
 
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)
 
347
{
 
348
  SWFDEC_STUB ("Sound.checkPolicyFile (get)");
 
349
}
 
350
 
 
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)
 
356
{
 
357
  SWFDEC_STUB ("Sound.checkPolicyFile (set)");
 
358
}
 
359
 
 
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)
 
365
{
 
366
  SWFDEC_STUB ("Sound.areSoundsInaccessible");
 
367
}
 
368
 
 
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)
 
373
{
 
374
  SwfdecSoundObject *sound;
 
375
  const char *name;
 
376
  SwfdecSound *new;
 
377
 
 
378
  SWFDEC_AS_CHECK (SWFDEC_TYPE_SOUND_OBJECT, &sound, "s", &name);
 
379
 
 
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);
 
385
  }
 
386
}
 
387
 
 
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)
 
392
{
 
393
  SwfdecSoundObject *sound;
 
394
  SwfdecActor *actor;
 
395
  double offset = 0;
 
396
  int loops = 1;
 
397
 
 
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;
 
402
 
 
403
  if (sound->provider == NULL) {
 
404
    SWFDEC_INFO ("no sound attached when calling Sound.start()");
 
405
    return;
 
406
  }
 
407
  if (loops <= 0)
 
408
    loops = 1;
 
409
  if (offset < 0 || !isfinite (offset))
 
410
    offset = 0;
 
411
 
 
412
  swfdec_sound_provider_start (sound->provider, actor, offset * 44100, loops);
 
413
}
 
414
 
 
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)
 
419
{
 
420
  SwfdecSoundObject *sound;
 
421
  const char *name = NULL;
 
422
  SwfdecSound *stopme;
 
423
  SwfdecActor *actor;
 
424
 
 
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;
 
429
 
 
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);
 
438
  }
 
439
}
 
440
 
 
441
SWFDEC_AS_CONSTRUCTOR (500, 16, swfdec_sound_object_construct, swfdec_sound_object_get_type)
 
442
void
 
443
swfdec_sound_object_construct (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc, 
 
444
    SwfdecAsValue *argv, SwfdecAsValue *ret)
 
445
{
 
446
  SwfdecSoundObject *sound;
 
447
  SwfdecPlayer *player;
 
448
    
 
449
  if (!swfdec_as_context_is_constructing (cx))
 
450
    return;
 
451
 
 
452
  sound = SWFDEC_SOUND_OBJECT (object);
 
453
  player = SWFDEC_PLAYER (cx);
 
454
 
 
455
  if (argc == 0 || SWFDEC_AS_VALUE_IS_UNDEFINED (&argv[0])) {
 
456
    sound->target = NULL;
 
457
  } else {
 
458
    sound->target = swfdec_as_value_to_string (cx, &argv[0]);
 
459
  }
 
460
}
 
461