summaryrefslogtreecommitdiff
path: root/src/libaudcore/drct.cc
blob: d8a05894189aa8de07463642816cf7c676d8acc8 (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
/*
 * drct.c
 * Copyright 2009-2013 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "drct.h"

#include "hook.h"
#include "i18n.h"
#include "internal.h"
#include "playlist-internal.h"
#include "plugins-internal.h"
#include "runtime.h"
#include "tuple.h"

/* --- PLAYBACK CONTROL --- */

EXPORT void aud_drct_play ()
{
    if (aud_drct_get_playing ())
    {
        if (aud_drct_get_paused ())
            aud_drct_pause ();
        else
        {
            int a, b;
            aud_drct_get_ab_repeat (a, b);
            aud_drct_seek (aud::max (a, 0));
        }
    }
    else
    {
        int playlist = aud_playlist_get_active ();
        aud_playlist_set_position (playlist, aud_playlist_get_position (playlist));
        aud_playlist_play (playlist);
    }
}

EXPORT void aud_drct_play_pause ()
{
    if (aud_drct_get_playing ())
        aud_drct_pause ();
    else
        aud_drct_play ();
}

EXPORT void aud_drct_stop ()
{
    aud_playlist_play (-1);
}

EXPORT int aud_drct_get_position ()
{
    int playlist = aud_playlist_get_playing ();
    return aud_playlist_get_position (playlist);
}

EXPORT String aud_drct_get_filename ()
{
    int playlist = aud_playlist_get_playing ();
    int position = aud_playlist_get_position (playlist);
    return aud_playlist_entry_get_filename (playlist, position);
}

/* --- RECORDING CONTROL --- */

/* The recording plugin is currently hard-coded to FileWriter.  Someday
 * aud_drct_set_record_plugin() may be added. */

static PluginHandle * record_plugin;

static bool record_plugin_watcher (PluginHandle *, void *)
{
    hook_call ("enable record", nullptr);
    return true;
}

void record_init ()
{
    auto plugin = aud_plugin_lookup_basename ("filewriter");
    if (plugin && aud_plugin_get_type (plugin) == PluginType::Output)
    {
        record_plugin = plugin;
        aud_plugin_add_watch (plugin, record_plugin_watcher, nullptr);
    }
}

void record_cleanup ()
{
    if (record_plugin)
    {
        aud_plugin_remove_watch (record_plugin, record_plugin_watcher, nullptr);
        record_plugin = nullptr;
    }
}

EXPORT PluginHandle * aud_drct_get_record_plugin ()
{
    /* recording is disabled when FileWriter is the primary output plugin */
    if (! record_plugin || plugin_get_enabled (record_plugin) == PluginEnabled::Primary)
        return nullptr;

    return record_plugin;
}

EXPORT bool aud_drct_get_record_enabled ()
{
    return (record_plugin && plugin_get_enabled (record_plugin) == PluginEnabled::Secondary);
}

EXPORT bool aud_drct_enable_record (bool enable)
{
    if (! record_plugin || plugin_get_enabled (record_plugin) == PluginEnabled::Primary)
        return false;

    return plugin_enable_secondary (record_plugin, enable);
}

/* --- VOLUME CONTROL --- */

EXPORT int aud_drct_get_volume_main ()
{
    StereoVolume volume = aud_drct_get_volume ();
    return aud::max (volume.left, volume.right);
}

EXPORT void aud_drct_set_volume_main (int volume)
{
    StereoVolume old = aud_drct_get_volume ();
    int main = aud::max (old.left, old.right);

    if (main > 0)
        aud_drct_set_volume ({
            aud::rescale (old.left, main, volume),
            aud::rescale (old.right, main, volume)
        });
    else
        aud_drct_set_volume ({volume, volume});
}

EXPORT int aud_drct_get_volume_balance ()
{
    StereoVolume volume = aud_drct_get_volume ();

    if (volume.left == volume.right)
        return 0;
    else if (volume.left > volume.right)
        return -100 + aud::rescale (volume.right, volume.left, 100);
    else
        return 100 - aud::rescale (volume.left, volume.right, 100);
}

EXPORT void aud_drct_set_volume_balance (int balance)
{
    int main = aud_drct_get_volume_main ();

    if (balance < 0)
        aud_drct_set_volume ({main, aud::rescale (main, 100, 100 + balance)});
    else
        aud_drct_set_volume ({aud::rescale (main, 100, 100 - balance), main});
}

/* --- PLAYLIST CONTROL --- */

EXPORT void aud_drct_pl_next ()
{
    int playlist = aud_playlist_get_playing ();
    if (playlist < 0)
        playlist = aud_playlist_get_active ();

    playlist_next_song (playlist, aud_get_bool (nullptr, "repeat"));
}

EXPORT void aud_drct_pl_prev ()
{
    int playlist = aud_playlist_get_playing ();
    if (playlist < 0)
        playlist = aud_playlist_get_active ();

    playlist_prev_song (playlist);
}

static void add_list (Index<PlaylistAddItem> && items, int at, bool to_temp, bool play)
{
    if (to_temp)
        aud_playlist_set_active (aud_playlist_get_temporary ());

    aud_playlist_entry_insert_batch (aud_playlist_get_active (), at, std::move (items), play);
}

EXPORT void aud_drct_pl_add (const char * filename, int at)
{
    Index<PlaylistAddItem> items;
    items.append (String (filename));
    add_list (std::move (items), at, false, false);
}

EXPORT void aud_drct_pl_add_list (Index<PlaylistAddItem> && items, int at)
{
    add_list (std::move (items), at, false, false);
}

EXPORT void aud_drct_pl_open (const char * filename)
{
    Index<PlaylistAddItem> items;
    items.append (String (filename));
    add_list (std::move (items), -1, aud_get_bool (nullptr, "open_to_temporary"), true);
}

EXPORT void aud_drct_pl_open_list (Index<PlaylistAddItem> && items)
{
    add_list (std::move (items), -1, aud_get_bool (nullptr, "open_to_temporary"), true);
}

EXPORT void aud_drct_pl_open_temp (const char * filename)
{
    Index<PlaylistAddItem> items;
    items.append (String (filename));
    add_list (std::move (items), -1, true, true);
}

EXPORT void aud_drct_pl_open_temp_list (Index<PlaylistAddItem> && items)
{
    add_list (std::move (items), -1, true, true);
}