Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/soundutil.c
blob: dc7aa925cbe5d8b9dbf3a6ce796e6366911af7d1 (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
/* gcompris - soundutil.c
 *
 * Copyright (C) 2002 Pascal Georges
 *
 *   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
 */

#ifdef __APPLE__
#   include <sys/types.h>
#endif
#include <dirent.h>

#include "gcompris.h"
#include <signal.h>
#include <pthread.h>
#include <ao/ao.h>

static GList *pending_queue = NULL;
static int sound_policy;
static gboolean is_playing;

/* Forward function declarations */
pthread_t thread_scheduler, thread_play;
pthread_t thread_scheduler_bgnd, thread_play_bgnd;
static void*	 thread_play_ogg (void*);
static char*	 get_next_sound_to_play( );
static void*	 scheduler ( );
static void*	 scheduler_bgnd ( );
extern int	 ogg123(char * sound);

/* mutex */
pthread_mutex_t lock;
pthread_cond_t cond;

/* =====================================================================
 *
 * =====================================================================*/
void initSound()
{
  pthread_mutexattr_t mutattr;
  pthread_mutexattr_init (&mutattr);
  pthread_mutex_init( &lock, &mutattr );
  pthread_cond_init( &cond, NULL );
  sound_policy = PLAY_AFTER_CURRENT;
  is_playing = FALSE;

  ao_initialize();

  if ( pthread_create ( &thread_scheduler, NULL, scheduler, NULL ) != 0)
    perror("create failed for scheduler");

  if ( pthread_create ( &thread_scheduler_bgnd, NULL, scheduler_bgnd, NULL ) != 0)
    perror("create failed for scheduler background");

}

/* =====================================================================
 *
 * =====================================================================*/
void setSoundPolicy(int policy)
{
  switch (policy)
    {
    case PLAY_ONLY_IF_IDLE : sound_policy = PLAY_ONLY_IF_IDLE; break;
    case PLAY_AFTER_CURRENT : sound_policy = PLAY_AFTER_CURRENT; break;
    case PLAY_OVERRIDE_ALL : sound_policy = PLAY_OVERRIDE_ALL; break;
    default : sound_policy = PLAY_AFTER_CURRENT;
    }
}
/* =====================================================================
 *
 * =====================================================================*/
int getSoundPolicy()
{
  return sound_policy;
}

/* =====================================================================
 * Thread scheduler background :
 *	- launches a single thread for playing and play any file found
 *        in the gcompris music directory
 ======================================================================*/
static void* scheduler_bgnd ()
{
  gint i;
  gchar *str;
  gchar *filename;
  struct dirent **namelist = NULL;
  int namelistlength = 0;
  GList *musiclist = NULL;

  if ( !gcompris_get_properties()->music )
    return;

  /* Sleep to let gcompris intialisation and intro music to complete */
  sleep(20);

  /* Load the Music directory file names */
  filename = g_strdup_printf("%s", PACKAGE_DATA_DIR "/music/background");
  namelistlength = scandir(filename,
			   &namelist, 0, NULL);
  
  if (namelistlength < 0)
    g_warning (_("Couldn't open music dir: %s"), filename);
  
  g_free(filename);
  
  /* Fill up the music list */
  for(i=2; i<namelistlength; i++)
    {
      str = g_strdup_printf("%s/%s", PACKAGE_DATA_DIR "/music/background", namelist[i]->d_name);

      g_free(namelist[i]);

      musiclist = g_list_append (musiclist, str);
    }

  g_free(namelist);

  /* No music no play */
  if(g_list_length(musiclist)==0)
    return NULL;

  /* Now loop over all our music files */
  while (TRUE)
    {
      for(i=0; i<g_list_length(musiclist); i++)
	{
	  /* WARNING Displaying stuff in a thread seems to make gcompris unstable */
	  /*	  display_ogg_file_credits((char *)g_list_nth_data(musiclist, i)); */
	  decode_ogg_file((char *)g_list_nth_data(musiclist, i));
	}
    }

  return NULL;
}
/* =====================================================================
 * Thread scheduler :
 *	- launches a single thread for playing a file
 *	- joins the previous thread at its end
 *	-	then launches another thread if some sounds are pending
 *	-	the thread never ends
 ======================================================================*/
static void* scheduler ()
{
  int retcode;
  char *sound = NULL;

  while (TRUE)
    {
      if ( ( sound = get_next_sound_to_play( ) ) != NULL )
	{
	  thread_play_ogg(sound);
	  is_playing = FALSE;
	  g_free(sound);
	}
      else
	{
	  int err;

	  err = pthread_cond_wait (&cond, &lock);
	  if (err)
	    printf ("cond_wait  : %s\n", strerror (err));

	  err = pthread_mutex_unlock ( &lock);
	  if (err)
	    printf ("mutex_unlock: %s\n", strerror (err));
	}
    }
  return NULL;
}
/* =====================================================================
 * Thread function for playing a single file
 ======================================================================*/
static void* thread_play_ogg (void *s)
{
  char* file = NULL;
  char locale[3];

  strncpy( locale, gcompris_get_locale(), 2 );
  locale[2] = 0; // because strncpy does not put a '\0' at the end of the string

  if(((char *)s)[0]=='/')
    {
      /* If the given file starts with a / then we don't need to search it */
      file = g_strdup(s);
    }
  else
    {
      file = g_strdup_printf("%s/%s/%s.ogg", PACKAGE_DATA_DIR "/sounds", locale, s);
      
      if (g_file_test ((file), G_FILE_TEST_EXISTS))
	{
	  printf("trying to play %s\n", file);
	} else
	  {
	    g_free(file);
	    file = g_strdup_printf("%s/%s.ogg", PACKAGE_DATA_DIR "/music", s);
	    if (g_file_test ((file), G_FILE_TEST_EXISTS))
	      {
		printf("trying to play %s\n", file);
	      } else
		/* Try to find a sound file that does not need to be localized 
		   (ie directly in root /sounds directory) */
		{
		  g_free(file);
		  file = g_strdup_printf("%s/%s.ogg", PACKAGE_DATA_DIR "/sounds", s);
		  if (g_file_test ((file), G_FILE_TEST_EXISTS))
		    {
		      printf("trying to play %s\n", file);
		    } else
		      {
			g_free(file);
			g_warning("Can't find sound %s", s);
			return NULL;
		      }
		}
	  }
    }

  if ( file )
    {
      printf("Calling decode_ogg_file(%s)\n", file);
      decode_ogg_file(file);
      g_free( file );
    }

  return NULL;
}

/* =====================================================================
 * Returns the next sound play, or NULL if there is no
 ======================================================================*/
char* get_next_sound_to_play( )
{
  char* tmpSound = NULL;

  pthread_mutex_lock( &lock );

  if ( g_list_length(pending_queue) > 0 )
    {
      tmpSound = g_list_nth_data( pending_queue, 0 );
      pending_queue = g_list_remove( pending_queue, tmpSound );
      printf( "... get_next_sound_to_play : %s\n", tmpSound );
    }

  pthread_mutex_unlock( &lock );

  return tmpSound;
}
/* =====================================================================
 * Play a list of OGG sound files. The list must be NULL terminated
 * This function wraps the var args into a GList and call the 
 * gcompris_play_ogg_list function to process the sounds.
 ======================================================================*/
void gcompris_play_ogg(char *sound, ...)
{
  va_list ap;
  char* tmp = NULL;
  GList* list = NULL;

  list = g_list_append(list, sound);

  va_start( ap, sound);
  while( (tmp = va_arg (ap, char *)))
    {
      list = g_list_append(list, tmp);
    }
  va_end(ap);

  gcompris_play_ogg_list( list );
  
  g_list_free(list);  
}

/* =====================================================================
 * Play a list of OGG sound files. 
 * The given ogg files will be first tested as a locale dependant sound file:
 * sounds/<current gcompris locale>/<sound>
 * If it doesn't exists, then the test is done with a music file:
 * music/<sound>
 =====================================================================*/
void gcompris_play_ogg_list( GList* files )
{
  GList* list;
  int err;
  char* tmp = NULL;
  char* tmpSound = NULL;

  if ( !gcompris_get_properties()->fx )
    return;

  if ( 	sound_policy == PLAY_ONLY_IF_IDLE &&
	( is_playing == TRUE || g_list_length( pending_queue ) > 0 ) )
    return;

  if (sound_policy == PLAY_OVERRIDE_ALL)
    {
      // cancel playing thread
      if ( pthread_cancel(thread_play) != 0)
	perror("thread cancel failed:");

      // cancel all pending sounds
      pthread_mutex_lock( &lock );
      while ( g_list_length(pending_queue) > 0 )
	{
	  tmpSound = g_list_nth_data(pending_queue, 0);
	  pending_queue = g_list_remove(pending_queue, tmpSound);
	  g_free(tmpSound);
	}
      pthread_mutex_unlock( &lock );

    } // PLAY_OVERRIDE_ALL

  pthread_mutex_lock( &lock );

  list = g_list_first( files );
  while( list!=NULL )
    {
      if (g_list_length(pending_queue) < MAX_QUEUE_LENGTH)
	{
	  pending_queue = g_list_append(pending_queue, g_strdup( (gchar*)(list->data) ));
	}
      list = g_list_next(list);
    }
  
  err = pthread_mutex_unlock ( &lock);
  if (err)
    printf ("mutex_unlock: %s\n", strerror (err));

  // Tell the scheduler to check for new sounds to play
  printf("Tell the scheduler to check for new sounds to play\n");
  err = pthread_cond_signal (&cond);
  if (err)
    printf ("cond_signal : %s\n", strerror (err));

}
/* =====================================================================
 *     Play a sound installed in the Gnome sound list
 * ======================================================================*/
void gcompris_play_sound (const char *soundlistfile, const char *which)
{
  gchar *filename;

  if (!gcompris_get_properties()->fx)
    return;

  filename = g_strdup_printf("%s/%s.wav", PACKAGE_SOUNDS_DIR, which);
  // DEBUG
  printf("gcompris_play_sound %s\n", filename);
  if (!g_file_test ((filename), G_FILE_TEST_EXISTS))
    g_error (_("Couldn't find file %s !"), filename);

  gnome_sound_play (filename);

  g_free (filename);
}

/* Local Variables: */
/* mode:c */
/* eval:(load-library "time-stamp") */
/* eval:(make-local-variable 'write-file-hooks) */
/* eval:(add-hook 'write-file-hooks 'time-stamp) */
/* eval:(setq time-stamp-format '(time-stamp-yyyy/mm/dd time-stamp-hh:mm:ss user-login-name)) */
/* End: */