Implemented sound repeats
authorP. F. Chimento <philip.chimento@gmail.com>
Sun, 30 Jan 2011 13:40:36 +0000 (14:40 +0100)
committerP. F. Chimento <philip.chimento@gmail.com>
Sun, 30 Jan 2011 13:40:36 +0000 (14:40 +0100)
Now you can request a sound to be played a certain number of times, or
to loop forever.

libchimara/schannel.c
libchimara/schannel.h

index 354bf3c881749e3b93e5a0488060886c2f74c319..d2df4892a49d77a3b724d23e89cc116fd2c5914d 100644 (file)
@@ -70,9 +70,18 @@ on_pipeline_message(GstBus *bus, GstMessage *message, schanid_t s)
                g_free(debug_message);
        }
                break;
-       case GST_MESSAGE_EOS:
-               /* end-of-stream */
-               clean_up_after_playing_sound(s);
+       case GST_MESSAGE_EOS: /* End of stream */
+               /* Decrease repeats if not set to forever */
+               if(s->repeats != (glui32)-1)
+                       s->repeats--;
+               if(s->repeats > 0) {
+                       if(!gst_element_seek_simple(s->pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 0)) {
+                               WARNING(_("Could not execute GStreamer seek"));
+                               clean_up_after_playing_sound(s);
+                       }
+               } else {
+                       clean_up_after_playing_sound(s);
+               }
                break;
        default:
                /* unhandled message */
@@ -371,13 +380,20 @@ glui32
 glk_schannel_play_ext(schanid_t chan, glui32 snd, glui32 repeats, glui32 notify)
 {
        VALID_SCHANNEL(chan, return 0);
-#ifdef GSTREAMER_SOUND 
+       g_printerr("Play sound %d with repeats %d and notify %d\n", snd, repeats, notify);
+#ifdef GSTREAMER_SOUND
        ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
        GInputStream *stream;
 
        /* Stop the previous sound */
        clean_up_after_playing_sound(chan);
 
+       /* Don't play if repeats = 0 */
+       if(repeats == 0) {
+               chan->repeats = 0;
+               return 1;
+       }
+
        /* Load the sound into a GInputStream, by whatever method */
        if(!glk_data->resource_map) {
                if(!glk_data->resource_load_callback) {
@@ -396,6 +412,7 @@ glk_schannel_play_ext(schanid_t chan, glui32 snd, glui32 repeats, glui32 notify)
                stream = g_memory_input_stream_new_from_data(resource.data.ptr, resource.length, NULL);
        }
 
+       chan->repeats = repeats;
        g_object_set(chan->source, "stream", stream, NULL);
        
        if(!gst_element_set_state(chan->pipeline, GST_STATE_PLAYING)) {
index 846f13ef0e7381458683ed8b70601d8680a90911..4a4b1fdcfea5964d184f4fb5746a936c127bbc90 100644 (file)
@@ -18,6 +18,9 @@ struct glk_schannel_struct
         this sound channel */
        GList *schannel_list;
 
+       /* How many times to repeat the last sound played (-1 = forever) */
+       glui32 repeats;
+       
 #ifdef GSTREAMER_SOUND
        /* Each sound channel is represented as a GStreamer pipeline.  */
        GstElement *pipeline, *source, *typefind, *demux, *decode, *convert, *filter, *sink;