fix jsut committed crasher by rewriting startDrag action
[swfdec.git] / libswfdec / swfdec_audio_event.c
blobe0f05dcb6e275860c0d8824bcd4a7c03565c9c98
1 /* Swfdec
2 * Copyright (C) 2003-2006 David Schleef <ds@schleef.org>
3 * 2005-2006 Eric Anholt <eric@anholt.net>
4 * 2006 Benjamin Otte <otte@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <string.h>
27 #include "swfdec_audio_event.h"
28 #include "swfdec_debug.h"
29 #include "swfdec_player_internal.h"
32 G_DEFINE_TYPE (SwfdecAudioEvent, swfdec_audio_event, SWFDEC_TYPE_AUDIO)
34 static guint
35 swfdec_audio_event_iterate (SwfdecAudio *audio, guint remove)
37 SwfdecAudioEvent *event = SWFDEC_AUDIO_EVENT (audio);
38 SwfdecSoundChunk *chunk = event->chunk;
40 event->offset += remove;
41 if (event->offset >= chunk->stop_sample)
42 event->offset = chunk->stop_sample;
43 return chunk->stop_sample - event->offset;
46 static void
47 swfdec_audio_event_render (SwfdecAudio *audio, gint16* dest,
48 guint start, guint n_samples)
50 SwfdecAudioEvent *event = SWFDEC_AUDIO_EVENT (audio);
51 guint offset = event->offset + start;
53 if (offset >= event->chunk->stop_sample)
54 return;
55 //n_samples = MIN (n_samples, event->chunk->stop_sample - offset);
56 swfdec_sound_render (event->sound, dest, offset, n_samples);
59 static void
60 swfdec_audio_event_dispose (GObject *object)
62 //SwfdecAudioEvent *audio_event = SWFDEC_AUDIO_EVENT (object);
64 G_OBJECT_CLASS (swfdec_audio_event_parent_class)->dispose (object);
67 static void
68 swfdec_audio_event_class_init (SwfdecAudioEventClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
71 SwfdecAudioClass *audio_class = SWFDEC_AUDIO_CLASS (klass);
73 object_class->dispose = swfdec_audio_event_dispose;
75 audio_class->iterate = swfdec_audio_event_iterate;
76 audio_class->render = swfdec_audio_event_render;
79 static void
80 swfdec_audio_event_init (SwfdecAudioEvent *audio_event)
84 static SwfdecAudio *
85 swfdec_audio_event_get (SwfdecPlayer *player, SwfdecSound *sound)
87 GList *walk;
89 if (player == NULL)
90 return NULL;
92 for (walk = player->audio; walk; walk = walk->next) {
93 SwfdecAudio *audio = walk->data;
94 if (!SWFDEC_IS_AUDIO_EVENT (audio))
95 continue;
96 if (SWFDEC_AUDIO_EVENT (audio)->sound == sound) {
97 return audio;
100 return NULL;
104 * swfdec_audio_event_new:
105 * @player: a #SwfdecPlayer or NULL
106 * @chunk: a sound chunk to start playing back
108 * Starts playback of the given sound chunk (or, when @player is NULL, creates
109 * an element for playing back the given sound).
111 * Returns: the sound effect or NULL if no new sound was created. You don't
112 * own a reference to it.
114 SwfdecAudio *
115 swfdec_audio_event_new (SwfdecPlayer *player, SwfdecSoundChunk *chunk)
117 SwfdecAudioEvent *event;
119 g_return_val_if_fail (player == NULL || SWFDEC_IS_PLAYER (player), NULL);
120 g_return_val_if_fail (chunk != NULL, NULL);
122 if (chunk->stop) {
123 SwfdecAudio *audio = swfdec_audio_event_get (player, chunk->sound);
124 if (audio) {
125 SWFDEC_LOG ("stopping sound %d", SWFDEC_CHARACTER (chunk->sound)->id);
126 swfdec_audio_remove (audio);
128 return NULL;
130 SWFDEC_LOG ("adding sound %d to playing sounds", SWFDEC_CHARACTER (chunk->sound)->id);
131 if (chunk->no_restart &&
132 (event = (SwfdecAudioEvent *) swfdec_audio_event_get (player, chunk->sound))) {
133 SWFDEC_DEBUG ("sound %d is already playing, reusing it",
134 SWFDEC_CHARACTER (chunk->sound)->id);
135 g_object_ref (event);
136 return SWFDEC_AUDIO (event);
138 event = g_object_new (SWFDEC_TYPE_AUDIO_EVENT, NULL);
139 event->sound = chunk->sound;
140 event->chunk = chunk;
141 event->offset = chunk->start_sample;
142 SWFDEC_DEBUG ("playing sound %d from offset %d now", SWFDEC_CHARACTER (chunk->sound)->id,
143 chunk->start_sample);
144 swfdec_audio_add (SWFDEC_AUDIO (event), player);
146 return SWFDEC_AUDIO (event);