Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gc_sound/src/gc-sound-channel.c
blob: 28fe33c9e47940fdd7315fa2fd2fdff652d551b2 (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
/* gcompris - gc-sound-channel.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-channel.h>
#include <gc-sound-item.h>
#include <gc-sound-marshallers.h>

/* signals */
enum {
  RUN, /* internal: launch recursive playing of playlist */
  CHUNK_END,
  PLAY_START, /* Start real playing of item */
  PLAY_END,   /* end playing */
  PAUSED,     /* channel paused */
  RESUMED,    /* channel resumed */
  HALTED,     /* channel halted */
  DESTROY,

  N_SIGNALS
};

guint gc_sound_channel_signals[N_SIGNALS] = {0};
static GcSoundObjectClass *parent_class;

gboolean                gc_sound_channel_play_item     (GcSoundChannel * self, GcSoundItem *item)
{
  self->running_sample = item;
  self->stopped = FALSE;

  return gc_sound_mixer_play_item (GC_SOUND_MIXER(GC_SOUND_OBJECT(self)->parent), self, item);
}

GcSoundItem *           gc_sound_channel_get_root     (GcSoundChannel * self)
{
  return self->root;
}

gboolean                gc_sound_channel_pause        (GcSoundChannel * self)
{
  return gc_sound_mixer_pause_channel( GC_SOUND_MIXER(GC_SOUND_OBJECT(self)->parent), self);
}

gboolean                gc_sound_channel_resume       (GcSoundChannel * self)
{
  return gc_sound_mixer_resume_channel( GC_SOUND_MIXER(GC_SOUND_OBJECT(self)->parent), self);
}

gboolean                gc_sound_channel_halt         (GcSoundChannel * self)
{
  return gc_sound_mixer_halt_channel(GC_SOUND_MIXER(GC_SOUND_OBJECT(self)->parent), self);
}

GcSoundPolicy           gc_sound_channel_get_policy   (GcSoundChannel * self)
{
  return self->policy;
}
gboolean                gc_sound_channel_set_policy   (GcSoundChannel * self,
						 GcSoundPolicy  policy)
{
  self->policy = policy;
  return TRUE;
}

gboolean                gc_sound_channel_play         (GcSoundChannel *self, 
						 GcSoundItem *item)
{
      GcSoundPolicy policy;

      /* item policy if it's set */
      if (gc_sound_item_get_policy(item) == POLICY_NONE)
         policy = self->policy;
      else
         policy = gc_sound_item_get_policy(item);

      switch (policy) {
        case PLAY_ONLY_IF_IDLE:
             if (self->running_sample || g_list_length (self->playlist)>0)
                return FALSE;
	     self->playlist = g_list_append (self->playlist, item);
	     // TODO send a signal to run !!! 
	     g_signal_emit(self, gc_sound_channel_signals[RUN], 0);
             break;

      case INTERRUPT_AND_PLAY:
	g_list_free (self->playlist);
	self->playlist = NULL;
	self->playlist = g_list_append (self->playlist, item);
	if (self->running_sample){
	  self->stopped = TRUE;
	  gc_sound_channel_halt(self);
	}
	g_signal_emit(self, gc_sound_channel_signals[RUN], 0);
	break;
	
      default:
	self->playlist = g_list_append (self->playlist, item);
	if (!self->running_sample)
	  g_signal_emit(self, gc_sound_channel_signals[RUN], 0);
	break;
      }
      return TRUE;
}

/* GType stuff */
enum {
	PROP_0,
	PROP_MIXER
};


/* GType */
G_DEFINE_TYPE(GcSoundChannel, gc_sound_channel, GC_TYPE_SOUND_OBJECT);
static void gc_sound_channel_destroy (GcSoundObject *self);

static void root_destroyed (GcSoundObject *root, gpointer data)
{
  GcSoundChannel *self = GC_SOUND_CHANNEL(data);
  if (!(GC_SOUND_OBJECT_FLAGS (GC_SOUND_OBJECT(data)) & GC_SOUND_IN_DESTRUCTION)) {
    g_object_unref(self->root);
    self->root = GC_SOUND_ITEM(g_object_new(GC_TYPE_SOUND_ITEM, "channel", self, NULL));
    g_object_ref_sink(G_OBJECT(self->root));
  }
}

static void
gc_sound_channel_destroy (GcSoundObject *self){
  if (GC_SOUND_CHANNEL(self)->root) {
    g_signal_handlers_disconnect_by_func(GC_SOUND_CHANNEL(self)->root, root_destroyed, self);
    gc_sound_object_destroy(GC_SOUND_OBJECT(GC_SOUND_CHANNEL(self)->root));
    g_object_unref(GC_SOUND_CHANNEL(self)->root);
    GC_SOUND_CHANNEL(self)->root = NULL;
  }
  parent_class->destroy (GC_SOUND_OBJECT(self));
}

static void
gc_sound_channel_init(GcSoundChannel* self) 
{
  // initialisation des variables.
  //g_warning("gc_sound_channel_init");

  self->volume = 1.0;
  self->policy = PLAY_AFTER_CURRENT;
  
  self->stopped = FALSE;

  GC_SOUND_OBJECT(self)->parent = NULL;
  self->root = GC_SOUND_ITEM(g_object_new(GC_TYPE_SOUND_ITEM, "channel", self, NULL));

  g_object_ref_sink(G_OBJECT(self->root));

  g_signal_connect(G_OBJECT(self->root), "destroy", (GCallback) root_destroyed, self);

  self->playlist = NULL;

  //this one is the item with the file
  self->running_sample = NULL;
  // this one is the group playing
  self->running_root = NULL;

}

static void
gc_sound_channel_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) 
{
  switch(prop_id) {
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
}

static void
gc_sound_channel_set_property(GObject* object, guint prop_id, GValue const* value, GParamSpec* pspec) 
{
  switch(prop_id) {
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
  
}

static void
gc_sound_channel_signal_chunk_end (GcSoundChannel *self)
{
  GcSoundItem *chunk = self->running_sample;

  self->running_sample = NULL;

  g_return_if_fail(GC_IS_SOUND_ITEM(chunk));

  g_signal_emit_by_name (chunk, "chunk_end", 0, self->stopped);
}

static void
gc_sound_channel_signal_run (GcSoundChannel *self)
{
  gboolean ret;

  while (g_list_length(self->playlist)>0)
    {
      self->running_root = g_list_first (self->playlist);
      self->playlist = g_list_remove_link  (self->playlist, self->running_root);

      ret = gc_sound_item_run_next (GC_SOUND_ITEM(self->running_root->data), FALSE);
      if (ret)
	return ;          
    }
}

static void
gc_sound_channel_class_init(GcSoundChannelClass* self_class) 
{
  // c'est ici qu'il faut passer les properties et les signals.
  // g_warning("gc_sound_channel_class_init");

   GObjectClass* go_class;
   GcSoundObjectClass * gc_sound_object_class = GC_SOUND_OBJECT_CLASS(self_class);

   parent_class = g_type_class_peek_parent (GC_SOUND_OBJECT_CLASS(self_class));

   /* GObjectClass */
   go_class = G_OBJECT_CLASS(self_class);

  go_class->get_property = gc_sound_channel_get_property;
  go_class->set_property = gc_sound_channel_set_property;

/* 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->run = gc_sound_channel_signal_run;
  self_class->chunk_end = gc_sound_channel_signal_chunk_end;
  gc_sound_object_class->destroy = gc_sound_channel_destroy;

  gc_sound_channel_signals[RUN] =
    g_signal_new("run", /* name */
		 GC_TYPE_SOUND_CHANNEL, /* itype */
		 (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), /* flags */
		 G_STRUCT_OFFSET (GcSoundChannelClass, run), /* offset function */ 
		 NULL,  /* accumulator */ 
		 NULL,  /* accu data */
		 gc_sound_marshal_VOID__VOID, /* marshal */
		 G_TYPE_NONE, /* return value */
		 0 /* n_params */
		 );

  gc_sound_channel_signals[CHUNK_END] =
    g_signal_new("chunk_end", /* name */
		 GC_TYPE_SOUND_CHANNEL, /* itype */
		 (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), /* flags */
		 G_STRUCT_OFFSET (GcSoundChannelClass, 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
		 );

}