Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gc_sound/src/gc-sound-item.c
blob: e8b5a352ecec969ce6ad33086dc8006ca362a7b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* gcompris - gc-sound-item.c
 *
 * Copyright (C) 2006 Yves Combe
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <gc-sound-item.h>
#include <gc-sound-marshallers.h>


/* signals */
enum {
  REAL_PLAY, /* internal: real launch play*/

  PLAY_START, /* Start real playing of item */
  PLAY_END,   /* end playing */
  CHUNK_START, /* Start real playing of item */
  CHUNK_END,   /* Start real playing of item */
  PAUSED,     /* channel paused */
  RESUMED,    /* channel resumed */
  HALTED,     /* channel halted */
  DESTROY,

  N_SIGNALS
};

guint gc_sound_item_signals[N_SIGNALS] = {0};

// internal use
gboolean        gc_sound_item_run_next (GcSoundItem *self, 
                                        gboolean stopped)
{
  gboolean ret;

  /* if we are stopped, we don't continue */
  if (stopped) {
    g_signal_emit(self, gc_sound_item_signals[PLAY_END], 0, stopped);
    return TRUE;
  }  

  /* If we are muted, we have nothing to do */
  if (self->mute)
    return FALSE;
  
  /* mark if we are in play tree */
  self->has_played = TRUE;

  if (g_list_length(self->children) == 0)
    return FALSE;
  
  if (self->playing == NULL)
    self->playing = g_list_first(self->children);
  else
    self->playing = g_list_next (self->playing );
  
  if ((self->playing == NULL) && (self->loop)) {
    self->playing = g_list_first (self->children );         
  }
  
  while (self->playing) {
    if ( GC_SOUND_ITEM(self->playing->data) == self){
      if (!self->started)
	g_signal_emit(self, gc_sound_item_signals[PLAY_START], 0);
      g_signal_emit(self, gc_sound_item_signals[REAL_PLAY], 0);
      return TRUE;
    }
    else {
      ret= gc_sound_item_run_next ( GC_SOUND_ITEM(self->playing->data), FALSE);
      if (ret)
	return ret;
    } 
    self->playing = g_list_next (self->playing );
  }    

  /* if we have play, it's finished */
  if (self->started) {
    g_signal_emit(self, gc_sound_item_signals[PLAY_END], 0, FALSE);
  }           

  return FALSE;
}

void            gc_sound_item_set_loop(GcSoundItem *self, gboolean loop)
{
  self->loop = loop;
}

GcSoundPolicy           gc_sound_item_get_policy   (GcSoundItem * self)
{
  return self->policy;
}
gboolean                gc_sound_item_set_policy   (GcSoundItem * self,
						    GcSoundPolicy  policy)
{
  self->policy = policy;
  return TRUE;
}

void gc_sound_item_child_start( GcSoundItem *child, gpointer data)
{
  GcSoundItem *self = GC_SOUND_ITEM(data);

  g_return_if_fail(GC_IS_SOUND_ITEM(self));

  if (self->has_played){
    self->started = TRUE;
    g_signal_emit( self, gc_sound_item_signals[PLAY_START], 0);
  }

}

void gc_sound_item_child_end( GcSoundItem *child, gboolean stopped, gpointer data)
{
  GcSoundItem *self = GC_SOUND_ITEM(data);

  if (self->has_played)
    gc_sound_item_run_next(self, stopped);
}

void gc_sound_item_child_destroyed( GcSoundItem *child, gpointer data)
{
  GcSoundItem *self = GC_SOUND_ITEM(data);

  self->children = g_list_remove ( self->children, child);
  g_object_unref (G_OBJECT(child));
}

GcSoundItem *   gc_sound_item_append_child (GcSoundItem *self, const gchar *first_arg_name, ...)
{
  GcSoundItem *child;
  va_list args;

  child = GC_SOUND_ITEM(g_object_new(GC_TYPE_SOUND_ITEM, 
				     "parent", self, 
				     "channel", self->channel,
				     NULL));
  va_start (args, first_arg_name);
  g_object_set_valist( G_OBJECT(child), 
		       first_arg_name, args);
  va_end (args);

  /* Child is added to our lists */
  self->children = g_list_append (self->children, child);

  /* we get a ref */
  g_object_ref_sink (G_OBJECT(child));

  g_signal_connect( child, "play-end", (GCallback) gc_sound_item_child_end, self);
  g_signal_connect( child, "play-start", (GCallback)  gc_sound_item_child_start, self);
  g_signal_connect( child, "destroy", (GCallback)  gc_sound_item_child_destroyed, self);
  return child;
}

gboolean        gc_sound_item_play (GcSoundItem *self)
{
  return gc_sound_channel_play (self->channel, self);
}

void            gc_sound_item_set_filename (GcSoundItem *self, gchar *filename)
{
  if (self->filename)
    g_free (self->filename);

  if (filename) {
    self->filename = g_strdup(filename);
  
    if ( (!self->children) || (self != self->children->data))
      self->children = g_list_prepend( self->children, self);
  } else {
    self->filename = NULL;
    if ( (self->children) && (self == self->children->data))
      self->children = g_list_remove ( self->children, self);
  }
}

gchar *         gc_sound_item_get_filename (GcSoundItem *self)
{
  return g_strdup(self->filename);
}
 
static void
gc_sound_item_signal_real_play (GcSoundItem *self)
{
  g_signal_emit(self, gc_sound_item_signals[CHUNK_START], 0);
  gc_sound_channel_play_item (self->channel, self);
}
 
static void
gc_sound_item_signal_play_end (GcSoundItem *self, gboolean stopped)
{
  if (self->destroy_after_play){
    gc_sound_object_destroy(GC_SOUND_OBJECT(self));
  }

  if (self->has_played || stopped) {
    self->started = FALSE;
    self->has_played = FALSE;
  }

}
 
static void
gc_sound_item_signal_play_start (GcSoundItem *self)
{
  self->started = TRUE;
}
 
static void
gc_sound_item_signal_chunk_end (GcSoundItem *self, gboolean stopped)
{
  /*
  We receive this signal from channel when play of chunk if finished.
  We have to continue play in our group.
  */
   
  gc_sound_item_run_next (self, stopped);
}
 
static void
gc_sound_item_signal_chunk_start (GcSoundItem *self)
{
  self->started = TRUE;
}

/* GType stuff */
enum {
	PROP_0,
	PROP_CHANNEL,
	PROP_FILENAME,
	PROP_DESTROY_AFTER_PLAY
};

/* GType */
G_DEFINE_TYPE(GcSoundItem, gc_sound_item, GC_TYPE_SOUND_OBJECT);

static void
gc_sound_item_init(GcSoundItem* self) 
{
  // initialisation des variables.
  self->volume = -1.0;
  self->mute = FALSE;

  self->policy = PLAY_AFTER_CURRENT;
  self->loop = FALSE;

  self->filename = NULL;

  self->channel = NULL;
  self->parent = NULL;
  self->children = NULL;

  //debug. will be removed

  //internal use
  // TRUE if we are in running play tree.
  self->has_played = FALSE;

  // our group already started
  self->started = FALSE;

  // child we are playing
  self->playing = NULL;

  self->destroy_after_play = FALSE;
}

static void
gc_sound_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) 
{
  GcSoundItem *self = GC_SOUND_ITEM(object);
  switch(prop_id) {
  case PROP_CHANNEL:
    g_value_set_object(value, self->channel);
    break;
  case PROP_FILENAME:
    g_value_set_string(value, g_strdup(self->filename));
    break;
  case PROP_DESTROY_AFTER_PLAY:
    g_value_set_boolean(value, self->destroy_after_play);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
}

static void
gc_sound_item_set_property(GObject* object, guint prop_id, GValue const* value, GParamSpec* pspec) 
{
  GcSoundItem *self = GC_SOUND_ITEM(object);

  switch(prop_id) {
  case PROP_CHANNEL:
    self->channel = g_value_get_object(value);
    break;
  case PROP_FILENAME:
    gc_sound_item_set_filename( self, (gchar *)g_value_get_string(value));
    break;
  case PROP_DESTROY_AFTER_PLAY:
    self->destroy_after_play = g_value_get_boolean(value);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
  
}

static void
gc_sound_item_class_init(GcSoundItemClass* self_class) 
{
  // c'est ici qu'il faut passer les properties et les signals.
   GObjectClass* go_class;
      
   /* GObjectClass */
   go_class = G_OBJECT_CLASS(self_class);

  go_class->get_property = gc_sound_item_get_property;
  go_class->set_property = gc_sound_item_set_property;

  g_object_class_install_property(go_class,
				  PROP_CHANNEL,
				  g_param_spec_object ("channel",
						     "GCompris channel",
						      "The channel where this channel stand",
						       GC_TYPE_SOUND_CHANNEL,
						       G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
  g_object_class_install_property(go_class,
				  PROP_FILENAME,
				  g_param_spec_string ("filename",
						     "File to play",
						       "The sound file to play",
						       NULL,
						       G_PARAM_READWRITE));
  g_object_class_install_property(go_class,
				  PROP_DESTROY_AFTER_PLAY,
				  g_param_spec_boolean ("destroy_after_play",
						     "destroy the item when played",
						       "Destroy the item when played",
						       FALSE,
						       G_PARAM_READWRITE));

/* signals */
/* enum { */
/*   RUN, /\* internal: launch recursive playing of playlist *\/ */

/*   PLAY_START, /\* Start real playing of item *\/ */
/*   PLAY_END,   /\* end playing *\/ */
/*   PAUSED,     /\* channel paused *\/ */
/*   RESUMED,    /\* channel resumed *\/ */
/*   HALTED,     /\* channel halted *\/ */
/*   DESTROY, */

/*   N_SIGNALS */
/* }; */

  self_class->real_play = gc_sound_item_signal_real_play;
  self_class->play_start = gc_sound_item_signal_play_start;
  self_class->play_end = gc_sound_item_signal_play_end;
  self_class->chunk_start = gc_sound_item_signal_chunk_start;
  self_class->chunk_end = gc_sound_item_signal_chunk_end;

  gc_sound_item_signals[REAL_PLAY] =
    g_signal_new("real_play", /* name */
		 GC_TYPE_SOUND_ITEM, /* itype */
		 (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), /* flags */
		 G_STRUCT_OFFSET (GcSoundItemClass, real_play), /* offset function */
		 NULL,  /* accumulator */
		 NULL,  /* accu data */
		 gc_sound_marshal_VOID__VOID, /* marshal */
		 G_TYPE_NONE, /* return value */
		 0 /* n_params */
		 );
  gc_sound_item_signals[PLAY_START] =
    g_signal_new("play_start", /* name */
		 GC_TYPE_SOUND_ITEM, /* itype */
		 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION), /* flags */
		 G_STRUCT_OFFSET (GcSoundItemClass, play_start), /* offset function */
		 NULL,  /* accumulator */
		 NULL,  /* accu data */
		 gc_sound_marshal_VOID__VOID, /* marshal */
		 G_TYPE_NONE, /* return value */
		 0 /* n_params */
		 );
  gc_sound_item_signals[PLAY_END] =
    g_signal_new("play_end", /* name */
		 GC_TYPE_SOUND_ITEM, /* itype */
		 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION), /* flags */
		 G_STRUCT_OFFSET (GcSoundItemClass, play_end), /* offset function */
		 NULL,  /* accumulator */
		 NULL,  /* accu data */
		 gc_sound_marshal_VOID__BOOLEAN, /* marshal */
		 G_TYPE_NONE, /* return value */
		 1 /* n_params */,
		 G_TYPE_BOOLEAN
		 );
  gc_sound_item_signals[CHUNK_START] =
    g_signal_new("chunk_start", /* name */
		 GC_TYPE_SOUND_ITEM, /* itype */
		 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION), /* flags */
		 G_STRUCT_OFFSET (GcSoundItemClass, chunk_start), /* offset function */
		 NULL,  /* accumulator */
		 NULL,  /* accu data */
		 gc_sound_marshal_VOID__VOID, /* marshal */
		 G_TYPE_NONE, /* return value */
		 0 /* n_params */
		 );
  gc_sound_item_signals[CHUNK_END] =
    g_signal_new("chunk_end", /* name */
		 GC_TYPE_SOUND_ITEM, /* itype */
		 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION), /* flags */
		 G_STRUCT_OFFSET (GcSoundItemClass, chunk_end), /* offset function */
		 NULL,  /* accumulator */
		 NULL,  /* accu data */
		 gc_sound_marshal_VOID__BOOLEAN, /* marshal */
		 G_TYPE_NONE, /* return value */
		 1, /* n_params */
		 G_TYPE_BOOLEAN
		 );

}