summaryrefslogtreecommitdiff
path: root/src/audacious/vis_runner.c
blob: 8e3505a8da92617901f1a6d9455e42a08a4d5e67 (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
/*
 * vis_runner.c
 * Copyright 2009-2010 John Lindgren
 *
 * This file is part of Audacious.
 *
 * Audacious 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, version 3 of the License.
 *
 * Audacious 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
 * Audacious. If not, see <http://www.gnu.org/licenses/>.
 *
 * The Audacious team does not consider modular code linking to Audacious or
 * using our public API to be a derived work.
 */

#include <glib.h>
#include <libaudcore/hook.h>

#include "compatibility.h"
#include "misc.h"
#include "output.h"
#include "vis_runner.h"

#define INTERVAL 30 /* milliseconds */

typedef struct {
    VisHookFunc func;
    void * user;
} VisHookItem;

G_LOCK_DEFINE_STATIC (mutex);
static gboolean playing = FALSE, paused = FALSE, active = FALSE;
static GList * hooks = NULL;
static VisNode * current_node = NULL;
static GQueue vis_list = G_QUEUE_INIT;
static gint send_source = 0, clear_source = 0;

static gboolean send_audio (void * unused)
{
    G_LOCK (mutex);

    if (! send_source)
    {
        G_UNLOCK (mutex);
        return FALSE;
    }

    /* We need raw time, not changed for effects and gapless playback. */
    gint outputted = current_output_plugin->output_time ();

    VisNode * vis_node = NULL;
    VisNode * next;

    while ((next = g_queue_peek_head (& vis_list)))
    {
        /* If we are considering a node, stop searching and use it if it is the
         * most recent (that is, the next one is in the future).  Otherwise,
         * consider the next node if it is not in the future by more than the
         * length of an interval. */
        if (next->time > outputted + (vis_node ? 0 : INTERVAL))
            break;

        g_free (vis_node);
        vis_node = g_queue_pop_head (& vis_list);
    }

    G_UNLOCK (mutex);

    if (! vis_node)
        return TRUE;

    for (GList * node = hooks; node; node = node->next)
    {
        VisHookItem * item = node->data;
        item->func (vis_node, item->user);
    }

    g_free (vis_node);
    return TRUE;
}

static gboolean send_clear (void * unused)
{
    G_LOCK (mutex);
    clear_source = 0;
    G_UNLOCK (mutex);

    hook_call ("visualization clear", NULL);
    return FALSE;
}

static void flush_locked (void)
{
    g_free (current_node);
    current_node = NULL;
    g_queue_foreach (& vis_list, (GFunc) g_free, NULL);
    g_queue_clear (& vis_list);

    clear_source = g_timeout_add (0, send_clear, NULL);
}

void vis_runner_start_stop (gboolean new_playing, gboolean new_paused)
{
    G_LOCK (mutex);

    playing = new_playing;
    paused = new_paused;
    active = playing && hooks;

    if (send_source)
    {
        g_source_remove (send_source);
        send_source = 0;
    }

    if (clear_source)
    {
        g_source_remove (clear_source);
        clear_source = 0;
    }

    if (! active)
        flush_locked ();
    else if (! paused)
        send_source = g_timeout_add (INTERVAL, send_audio, NULL);

    G_UNLOCK (mutex);
}

void vis_runner_pass_audio (gint time, gfloat * data, gint samples, gint
 channels, gint rate)
{
    G_LOCK (mutex);

    if (! active)
        goto UNLOCK;

    if (current_node && current_node->nch != MIN (channels, 2))
    {
        g_free (current_node);
        current_node = NULL;
    }

    gint at = 0;

    while (1)
    {
        if (! current_node)
        {
            gint node_time = time;
            VisNode * last;

            if ((last = g_queue_peek_tail (& vis_list)))
                node_time = last->time + INTERVAL;

            at = channels * (gint) ((gint64) (node_time - time) * rate / 1000);

            if (at < 0)
                at = 0;
            if (at >= samples)
                break;

            current_node = g_malloc (sizeof (VisNode));
            current_node->time = node_time;
            current_node->nch = MIN (channels, 2);
            current_node->length = 0;
        }

        gint copy = MIN (samples - at, channels * (512 - current_node->length));

        for (gint channel = 0; channel < current_node->nch; channel ++)
        {
            gfloat * from = data + at + channel;
            gfloat * end = from + copy;
            gint16 * to = current_node->data[channel] + current_node->length;

            while (from < end)
            {
                register gfloat temp = * from;
                * to ++ = CLAMP (temp, -1, 1) * 32767;
                from += channels;
            }
        }

        current_node->length += copy / channels;

        if (current_node->length < 512)
            break;

        g_queue_push_tail (& vis_list, current_node);
        current_node = NULL;
    }

UNLOCK:
    G_UNLOCK (mutex);
}

static void time_offset_cb (VisNode * vis_node, void * offset)
{
    vis_node->time += GPOINTER_TO_INT (offset);
}

void vis_runner_time_offset (gint offset)
{
    G_LOCK (mutex);

    if (current_node)
        current_node->time += offset;

    g_queue_foreach (& vis_list, (GFunc) time_offset_cb, GINT_TO_POINTER (offset));

    G_UNLOCK (mutex);
}

void vis_runner_flush (void)
{
    G_LOCK (mutex);
    flush_locked ();
    G_UNLOCK (mutex);
}

void vis_runner_add_hook (VisHookFunc func, void * user)
{
    G_LOCK (mutex);

    VisHookItem * item = g_malloc (sizeof (VisHookItem));
    item->func = func;
    item->user = user;
    hooks = g_list_prepend (hooks, item);

    G_UNLOCK (mutex);
    vis_runner_start_stop (playing, paused);
}

void vis_runner_remove_hook (VisHookFunc func)
{
    G_LOCK (mutex);

    for (GList * node = hooks; node; node = node->next)
    {
        if (((VisHookItem *) node->data)->func == func)
        {
            g_free (node->data);
            hooks = g_list_delete_link (hooks, node);
            break;
        }
    }

    G_UNLOCK (mutex);
    vis_runner_start_stop (playing, paused);
}