Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/soundutil.c
blob: 3afa6440530ce62a8d232cc4d0f01a217f9690aa (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
/* 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
 */

#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;
static void*	 thread_play_ogg (void*);
static char*	 get_next_sound_to_play( );
static void*	 scheduler ( );
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;

  printf("...calling ao_initialize\n");
  ao_initialize();
  printf("...calling ao_initialize done\n");

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

/* =====================================================================
 *
 * =====================================================================*/
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 :
 *	- 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;

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

	  printf ("   in scheduler: after cond_wait \n");

	  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];
  pthread_t pid_ogg = 0;

  fprintf (stderr, "+++thread_play_ogg:%s<-\n", s);

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

  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
	  {
	    g_free(file);
	    g_warning("Can't find sound %s", s);
	    return NULL;
	  }
      }

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

  fprintf (stderr, "---thread_play_ogg\n");
  //  pthread_exit( NULL );
  return NULL;
}

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

  printf("+++get_next_sound_to_play\n");
  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 );
  printf("---get_next_sound_to_play\n");

  return tmpSound;
}
/* =====================================================================
 * Play a list of OGG sound files. The list must be NULL terminated
 * 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(char *sound, ...)
{
  int err;
  va_list ap;
  char* tmp = NULL;
  char* tmpSound = NULL;

  printf("+++gcompris_play_ogg\n");

  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 );

  if (g_list_length(pending_queue) < MAX_QUEUE_LENGTH)
    pending_queue = g_list_append(pending_queue, g_strdup( sound ) );

  va_start( ap, sound);
  while( (tmp = va_arg (ap, char *)))
    {
      if (g_list_length(pending_queue) < MAX_QUEUE_LENGTH)
	{
	  pending_queue = g_list_append(pending_queue, g_strdup( tmp ));
	}
    }

  va_end(ap);

  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));

  printf("---gcompris_play_ogg\n");
}
/* =====================================================================
 *     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);

  if (!g_file_test ((filename), G_FILE_TEST_EXISTS))
    g_error (_("Couldn't find file %s !"), filename);

  if (gcompris_get_properties()->fx)
    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: */