summaryrefslogtreecommitdiffstats
path: root/uisimulator/sdl/sound.c
blob: a95b19d417644c871ce4ef251568f061472bb137 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************
 *             __________               __   ___.                  
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___  
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /  
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <   
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \  
 *                     \/            \/     \/    \/            \/ 
 * $Id$
 *
 * Copyright (C) 2005 by Nick Lanham
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "autoconf.h"

#ifdef ROCKBOX_HAS_SIMSOUND /* play sound in sim enabled */

#include <stdlib.h>
#include <stdbool.h>
#include <memory.h>
#include "debug.h"
#include "kernel.h"
#include "sound.h"
#include "SDL.h"

static bool pcm_playing;
static bool pcm_paused;

static Uint8* pcm_data;
static Uint32 pcm_data_size;

extern bool debug_audio;

static void sdl_dma_start(const void *addr, size_t size)
{
    pcm_playing = true;

    SDL_LockAudio();
    
    pcm_data = (Uint8 *) addr;
    pcm_data_size = size;

    SDL_UnlockAudio();
    
    SDL_PauseAudio(0);
}

static void sdl_dma_stop() 
{
    pcm_playing = false;

    SDL_PauseAudio(1);

    pcm_paused = false;
}

static void (*callback_for_more)(unsigned char**, size_t*) = NULL;
void pcm_play_data(void (*get_more)(unsigned char** start, size_t* size),
        unsigned char* start, size_t size)
{
    callback_for_more = get_more;

    if (!(start && size)) {
        if (get_more)
            get_more(&start, &size);
        else
            return;
    }

    if (start && size) {
        sdl_dma_start(start, size);
    }
}

size_t pcm_get_bytes_waiting(void)
{
    return pcm_data_size;
}

void pcm_mute(bool mute)
{
    (void) mute;
}

void pcm_play_stop(void)
{
    if (pcm_playing) {
        sdl_dma_stop();
    }
}

void pcm_play_pause(bool play)
{
    Uint32 next_size;
    Uint8 *next_start;
    
    if (!pcm_playing) {
        return;
    }

    if(pcm_paused && play) {
        if (pcm_get_bytes_waiting()) {
            printf("unpause\n");

            SDL_PauseAudio(0);
        } else {
            printf("unpause, no data waiting\n");

            void (*get_more)(unsigned char**, size_t*) = callback_for_more;

            if (get_more) {
                get_more(&next_start, &next_size);
            }

            if (next_start && next_size) {
                sdl_dma_start(next_start, next_size);
            } else {
                sdl_dma_stop();
                printf("unpause attempted, no data\n");
            }
        }
    } else if(!pcm_paused && !play) {
        printf("pause\n");

        SDL_PauseAudio(1);
    }

    pcm_paused = !play;
}

bool pcm_is_paused(void)
{
    return pcm_paused;
}

bool pcm_is_playing(void)
{
    return pcm_playing;
}

/*
 * This function goes directly into the DMA buffer to calculate the left and
 * right peak values. To avoid missing peaks it tries to look forward two full
 * peek periods (2/HZ sec, 100% overlap), although it's always possible that
 * the entire period will not be visible. To reduce CPU load it only looks at
 * every third sample, and this can be reduced even further if needed (even
 * every tenth sample would still be pretty accurate).
 */

#define PEAK_SAMPLES  (44100*2/HZ)  /* 44100 samples * 2 / 100 Hz tick */
#define PEAK_STRIDE   3       /* every 3rd sample is plenty... */

void pcm_calculate_peaks(int *left, int *right)
{
    long samples = (long) pcm_data_size / 4;
    short *addr = (short *) pcm_data;

    if (samples > PEAK_SAMPLES)
        samples = PEAK_SAMPLES;

    samples /= PEAK_STRIDE;

    if (left && right) {
        int left_peak = 0, right_peak = 0, value;

        while (samples--) {
            if ((value = addr [0]) > left_peak)
                left_peak = value;
            else if (-value > left_peak)
                left_peak = -value;

            if ((value = addr [PEAK_STRIDE | 1]) > right_peak)
                right_peak = value;
            else if (-value > right_peak)
                right_peak = -value;

            addr += PEAK_STRIDE * 2;
        }

        *left = left_peak;
        *right = right_peak;
    }
    else if (left || right) {
        int peak_value = 0, value;

        if (right)
            addr += (PEAK_STRIDE | 1);

        while (samples--) {
            if ((value = addr [0]) > peak_value)
                peak_value = value;
            else if (-value > peak_value)
                peak_value = -value;

            addr += PEAK_STRIDE * 2;
        }

        if (left)
            *left = peak_value;
        else
            *right = peak_value;
    }
}

void sdl_audio_callback(void *udata, Uint8 *stream, int len)
{
    Uint32 have_now;
    FILE *debug = (FILE *)udata;

    /* At all times we need to write a full 'len' bytes to stream. */

    if (pcm_data_size > 0) {
        have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len;

        memcpy(stream, pcm_data, have_now);

        if (debug != NULL) {
            fwrite(pcm_data, sizeof(Uint8), have_now, debug);
        }
        stream += have_now;
        len -= have_now;
        pcm_data += have_now;
        pcm_data_size -= have_now;
    }

    while (len > 0)
    {
        if (callback_for_more) {
            callback_for_more(&pcm_data, &pcm_data_size);
        } else {
            pcm_data = NULL;
            pcm_data_size = 0;
        }
        if (pcm_data_size > 0) {
            have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len;

            memcpy(stream, pcm_data, have_now);

            if (debug != NULL) {
                fwrite(pcm_data, sizeof(Uint8), have_now, debug);
            }
            stream += have_now;
            len -= have_now;
            pcm_data += have_now;
            pcm_data_size -= have_now;
        } else {
            DEBUGF("sdl_audio_callback: No Data.\n");
            sdl_dma_stop();
            break;
        }
    }
}

int pcm_init(void)
{
    SDL_AudioSpec fmt;
    FILE *debug = NULL;

    if (debug_audio) {
        debug = fopen("audiodebug.raw", "wb");
    }

    /* Set 16-bit stereo audio at 44Khz */
    fmt.freq = 44100;
    fmt.format = AUDIO_S16SYS;
    fmt.channels = 2;
    fmt.samples = 2048;
    fmt.callback = sdl_audio_callback;
    fmt.userdata = debug;

    /* Open the audio device and start playing sound! */
    if(SDL_OpenAudio(&fmt, NULL) < 0) {
        fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
        return -1;
    }

    sdl_dma_stop();

    return 0;
}

#endif /* ROCKBOX_HAS_SIMSOUND */