summaryrefslogtreecommitdiffstats
path: root/apps/plugins/sdl/src/audio/rockbox/SDL_rockboxaudio.c
blob: 2df820bee53f7a40a617cdf5e43dc4a92d356619 (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
/*
  SDL - Simple DirectMedia Layer
  Copyright (C) 1997-2012 Sam Lantinga

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  Sam Lantinga
  slouken@libsdl.org

  This file written by Franklin Wei (franklin@rockbox.org).
*/
#include "SDL_config.h"

/* SDL Rockbox audio driver */

#if HAVE_STDIO_H
#include <stdio.h>
#endif

#include "SDL_rwops.h"
#include "SDL_timer.h"
#include "SDL_audio.h"
#include "../SDL_audiomem.h"
#include "../SDL_audio_c.h"
#include "../SDL_audiodev_c.h"
#include "SDL_rockboxaudio.h"

/* The tag name used by DISK audio */
#define ROCKBOXAUD_DRIVER_NAME         "rockbox"

/* debug */
int rbaud_underruns;

/* Audio driver functions */
static int ROCKBOXAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
static void ROCKBOXAUD_WaitAudio(_THIS);
static void ROCKBOXAUD_PlayAudio(_THIS);
static Uint8 *ROCKBOXAUD_GetAudioBuf(_THIS);
static void ROCKBOXAUD_CloseAudio(_THIS);

/* Audio driver bootstrap functions */
static int ROCKBOXAUD_Available(void)
{
    /* always! */
    return 1;
}

static void ROCKBOXAUD_DeleteDevice(SDL_AudioDevice *device)
{
    SDL_free(device->hidden);
    SDL_free(device);
}

SDL_AudioDevice *current_dev = NULL;

/* filled in OpenAudio() */
static int16_t silence[1024];

/* rockbox callback (in IRQ) */
static void get_more(const void **start, size_t *size)
{
    SDL_AudioDevice *this = current_dev;

    /* Circularly loop through the buffers starting after the one most
     * recently marked as playing, and find the next one that is
     * marked as filled. */
    for(int i = 1; i < this->hidden->n_buffers; ++i)
    {
        int idx = (this->hidden->current_playing + i) % this->hidden->n_buffers;
        if(this->hidden->status[idx] == 1)
        {
            /* Play this one. */
            LOGF("Playing buffer %d", idx);
            *start = this->hidden->rb_buf[idx];
            *size = this->hidden->mixlen;

            /* Mark as playing */
            this->hidden->status[idx] = 2;
            this->hidden->current_playing = idx;
            return;
        }
    }

    /* If the loop failed, we're lagging behind, so we play silence. */
    LOGF("RB AUDIO: falling back to silence %d (%d %d %d)", rbaud_underruns, this->hidden->status[0], this->hidden->status[1], this->hidden->status[2]);
    *start = silence;
    *size = sizeof(silence);

    ++rbaud_underruns;
}

/* This function waits until it is possible to call PlayAudio
 * again. This is called from the SDL_audio thread. */
static void ROCKBOXAUD_WaitAudio(_THIS)
{
    while(1)
    {
        /* We fill in the following cases:
         *  There's an empty buffer (status=0).
         *  There's more than buffers two marked as playing; this
         *  means at least one is stale and needs filling (status=2).
         */
        int n_playing = 0;
        for(int i = 0; i < this->hidden->n_buffers; ++i)
        {
            switch(this->hidden->status[i])
            {
            case 0:
                return;
            case 2:
                if(++n_playing >= 2)
                    return;
            default:
                break;
            }
        }

        rb->yield();
    }
}

/* when this is called, SDL wants us to play the samples in mixbuf */
static void ROCKBOXAUD_PlayAudio(_THIS)
{
    /* There are two cases in which we should be called:
     *  - There is an empty buffer (marked with status = 0)
     *  - There are more than two buffers marked as playing, meaning at least one is stale.
     */
    int idx = -1;

    /* Find the next empty or stale buffer and fill. */
    for(int i = 1; i < this->hidden->n_buffers; ++i)
    {
        idx = (this->hidden->current_playing + i) % this->hidden->n_buffers;

        /* Empty or stale. */
        if(this->hidden->status[idx] == 0 ||
           this->hidden->status[idx] == 2)
            break;
    }
    if(idx < 0)
        return;

    /* probably premature optimization here */
    char *dst = (char*)this->hidden->rb_buf[idx], *src = this->hidden->mixbuf;
    int size = this->hidden->mixlen / 2;
    memcpy(dst, src, size);

    this->hidden->status[idx] = 1;
    rb->yield();

    memcpy(dst + size, src + size, this->hidden->mixlen - size);

    LOGF("filled buffer %d (status %d %d %d)", idx, this->hidden->status[0], this->hidden->status[1], this->hidden->status[2]);
}

static SDL_AudioDevice *ROCKBOXAUD_CreateDevice(int devindex)
{
    SDL_AudioDevice *this;

    /* Initialize all variables that we clean on shutdown */
    this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
    if ( this ) {
        SDL_memset(this, 0, (sizeof *this));
        this->hidden = (struct SDL_PrivateAudioData *)
            SDL_malloc((sizeof *this->hidden));
    }
    if ( (this == NULL) || (this->hidden == NULL) ) {
        SDL_OutOfMemory();
        if ( this ) {
            SDL_free(this);
        }
        return(0);
    }
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));

    /* Set the function pointers */
    this->OpenAudio = ROCKBOXAUD_OpenAudio;
    this->WaitAudio = ROCKBOXAUD_WaitAudio;
    this->PlayAudio = ROCKBOXAUD_PlayAudio;
    this->GetAudioBuf = ROCKBOXAUD_GetAudioBuf;
    this->CloseAudio = ROCKBOXAUD_CloseAudio;

    this->free = ROCKBOXAUD_DeleteDevice;

    return this;
}

AudioBootStrap ROCKBOXAUD_bootstrap = {
    ROCKBOXAUD_DRIVER_NAME, "rockbox audio",
    ROCKBOXAUD_Available, ROCKBOXAUD_CreateDevice
};

static Uint8 *ROCKBOXAUD_GetAudioBuf(_THIS)
{
    /* Tell SDL to put samples in mixbuf */
    return(this->hidden->mixbuf);
}

static void ROCKBOXAUD_CloseAudio(_THIS)
{
    if ( this->hidden->mixbuf != NULL ) {
        SDL_FreeAudioMem(this->hidden->mixbuf);
        this->hidden->mixbuf = NULL;
    }

    for(int i = 0; i < this->hidden->n_buffers; ++i)
    {
        if(this->hidden->rb_buf[i])
            SDL_FreeAudioMem(this->hidden->rb_buf[i]);
    }
    rb->pcm_play_stop();
}

static int ROCKBOXAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
{
    /* change to our format */
    spec->format = AUDIO_S16SYS;
    spec->channels = 2;
    spec->freq = RB_SAMPR;

    /* we've changed it */
    SDL_CalculateAudioSpec(spec);

    LOGF("samplerate %d", spec->freq);
    rb->mixer_set_frequency(spec->freq);

    /* Allocate mixing buffer */
    this->hidden->mixlen = spec->size;
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
    if ( this->hidden->mixbuf == NULL ) {
        return(-1);
    }
    SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);

    /* Increase to reduce skipping at the cost of latency. */
    this->hidden->n_buffers = 4;

    /* Buffer 1 will be filled first. */
    this->hidden->current_playing = 0;

    /* allocate buffers */
    for(int i = 0; i < this->hidden->n_buffers; ++i)
    {
        this->hidden->rb_buf[i] = SDL_AllocAudioMem(this->hidden->mixlen);
        this->hidden->status[i] = 0; /* empty */
    }

    memset(silence, 0, sizeof(silence));

    current_dev = this;

    rbaud_underruns = 0;

    rb->pcm_play_data(get_more, NULL, NULL, 0);

    /* We're ready to rock and roll. :-) */
    return(0);
}