~ubuntu-branches/ubuntu/wily/gargoyle-free/wily-proposed

« back to all changes in this revision

Viewing changes to garglk/sndsdl.c

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Beucler
  • Date: 2009-09-11 20:09:43 UTC
  • Revision ID: james.westby@ubuntu.com-20090911200943-idgzoyupq6650zpn
Tags: upstream-2009-08-25
ImportĀ upstreamĀ versionĀ 2009-08-25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *                                                                            *
 
3
 * Copyright (C) 2006-2009 by Tor Andersson.                                  *
 
4
 *                                                                            *
 
5
 * This file is part of Gargoyle.                                             *
 
6
 *                                                                            *
 
7
 * Gargoyle is free software; you can redistribute it and/or modify           *
 
8
 * it under the terms of the GNU General Public License as published by       *
 
9
 * the Free Software Foundation; either version 2 of the License, or          *
 
10
 * (at your option) any later version.                                        *
 
11
 *                                                                            *
 
12
 * Gargoyle is distributed in the hope that it will be useful,                *
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 
15
 * GNU General Public License for more details.                               *
 
16
 *                                                                            *
 
17
 * You should have received a copy of the GNU General Public License          *
 
18
 * along with Gargoyle; if not, write to the Free Software                    *
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 
20
 *                                                                            *
 
21
 *****************************************************************************/
 
22
 
 
23
/* SDL support donated by Lorenzo Marcantonio */
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include <assert.h>
 
29
 
 
30
#include <SDL.h>
 
31
#include <SDL_mixer.h>
 
32
#include <SDL_sound.h>
 
33
 
 
34
#include "glk.h"
 
35
#include "garglk.h"
 
36
 
 
37
#include "gi_blorb.h"
 
38
 
 
39
#define giblorb_ID_MOD  (giblorb_make_id('M', 'O', 'D', ' '))
 
40
#define giblorb_ID_OGG  (giblorb_make_id('O', 'G', 'G', 'V'))
 
41
#define giblorb_ID_FORM (giblorb_make_id('F', 'O', 'R', 'M'))
 
42
 
 
43
/* non-standard types */
 
44
#define giblorb_ID_MP3  (giblorb_make_id('M', 'P', '3', ' '))
 
45
#define giblorb_ID_WAVE (giblorb_make_id('W', 'A', 'V', 'E'))
 
46
 
 
47
#define SDL_CHANNELS 64
 
48
 
 
49
static channel_t *gli_channellist = NULL;
 
50
static channel_t *sound_channels[SDL_CHANNELS];
 
51
static channel_t *music_channel;
 
52
 
 
53
static Sound_AudioInfo *output = NULL;
 
54
static const int FREE = 1;
 
55
static const int BUSY = 2;
 
56
 
 
57
void gli_initialize_sound(void)
 
58
{
 
59
    if (gli_conf_sound == 1) {
 
60
        if (SDL_Init(SDL_INIT_AUDIO) == -1) {
 
61
            gli_strict_warning("SDL init failed\n");
 
62
            gli_strict_warning(SDL_GetError());
 
63
            gli_conf_sound = 0;
 
64
            return;
 
65
        }
 
66
        if (Sound_Init() == -1) {
 
67
            gli_strict_warning("SDL Sound init failed\n");
 
68
            gli_strict_warning(Sound_GetError());
 
69
            gli_conf_sound = 0;
 
70
            return;
 
71
        }
 
72
        Sound_AudioInfo *audio;
 
73
        audio = malloc(sizeof(Sound_AudioInfo));
 
74
        audio->format = MIX_DEFAULT_FORMAT;
 
75
        audio->channels = 2;
 
76
        audio->rate = 44100;
 
77
        output = audio;
 
78
        if (Mix_OpenAudio(output->rate, output->format, output->channels, 4096) == -1) {
 
79
            gli_strict_warning("SDL Mixer init failed\n");
 
80
            gli_strict_warning(Mix_GetError());
 
81
            gli_conf_sound = 0;
 
82
            return;
 
83
        }
 
84
    int channels = Mix_AllocateChannels(SDL_CHANNELS);
 
85
    Mix_GroupChannels(0, channels - 1 , FREE);
 
86
    }
 
87
}
 
88
 
 
89
schanid_t glk_schannel_create(glui32 rock)
 
90
{
 
91
    channel_t *chan;
 
92
 
 
93
    if (!gli_conf_sound)
 
94
        return NULL;
 
95
    chan = malloc(sizeof(channel_t));
 
96
 
 
97
    if (!chan)
 
98
        return NULL;
 
99
 
 
100
    chan->rock = rock;
 
101
    chan->status = CHANNEL_IDLE;
 
102
    chan->volume = 0x10000;
 
103
    chan->resid = 0;
 
104
    chan->loop = 0;
 
105
    chan->notify = 0;
 
106
    chan->sdl_memory = 0;
 
107
    chan->sdl_rwops = 0;
 
108
    chan->sample = 0;
 
109
    chan->decode = 0;
 
110
    chan->buffered = 0;
 
111
    chan->sdl_channel = -1;
 
112
    chan->music = 0;
 
113
 
 
114
    chan->chain_prev = NULL;
 
115
    chan->chain_next = gli_channellist;
 
116
    gli_channellist = chan;
 
117
    if (chan->chain_next) {
 
118
        chan->chain_next->chain_prev = chan;
 
119
    }
 
120
 
 
121
    if (gli_register_obj) {
 
122
        chan->disprock = (*gli_register_obj)(chan, gidisp_Class_Schannel);
 
123
     } else {
 
124
        chan->disprock.ptr = NULL;
 
125
     }
 
126
 
 
127
    return chan;        
 
128
}
 
129
 
 
130
static void cleanup_channel(schanid_t chan)
 
131
{
 
132
    if (chan->sdl_rwops) {
 
133
    if (!chan->decode)
 
134
        SDL_FreeRW(chan->sdl_rwops);
 
135
    else
 
136
        Sound_FreeSample(chan->decode);
 
137
    chan->sdl_rwops = 0;
 
138
    chan->decode = 0;
 
139
    chan->buffered = 0;
 
140
    }
 
141
    if (chan->sdl_memory) {
 
142
        free(chan->sdl_memory);
 
143
        chan->sdl_memory = 0;
 
144
    }
 
145
    switch (chan->status) {
 
146
    case CHANNEL_SOUND:
 
147
        if (chan->sample) {
 
148
                Mix_FreeChunk(chan->sample);
 
149
        }
 
150
        if (chan->sdl_channel >= 0) {
 
151
                Mix_GroupChannel(chan->sdl_channel, FREE);
 
152
                sound_channels[chan->sdl_channel] = 0;
 
153
        }
 
154
        break;
 
155
    case CHANNEL_MUSIC:
 
156
        if (chan->music) {
 
157
                Mix_FreeMusic(chan->music);
 
158
                music_channel = 0;
 
159
        }
 
160
        break;
 
161
    }
 
162
    chan->status = CHANNEL_IDLE;
 
163
    chan->sdl_channel = -1;
 
164
    chan->music = 0;
 
165
}
 
166
 
 
167
void glk_schannel_destroy(schanid_t chan)
 
168
{
 
169
    channel_t *prev, *next;
 
170
 
 
171
    if (!chan) {
 
172
        gli_strict_warning("schannel_destroy: invalid id.");
 
173
        return;
 
174
    }
 
175
 
 
176
    glk_schannel_stop(chan);
 
177
    cleanup_channel(chan);
 
178
    if (gli_unregister_obj) {
 
179
        (*gli_unregister_obj)(chan, gidisp_Class_Schannel, chan->disprock);
 
180
    }
 
181
 
 
182
    prev = chan->chain_prev;
 
183
    next = chan->chain_next;
 
184
    chan->chain_prev = NULL;
 
185
    chan->chain_next = NULL;
 
186
 
 
187
    if (prev) {
 
188
        prev->chain_next = next;
 
189
    } else {
 
190
        gli_channellist = next;
 
191
    }
 
192
    if (next) {
 
193
        next->chain_prev = prev;
 
194
    }
 
195
 
 
196
    free(chan);
 
197
}
 
198
 
 
199
schanid_t glk_schannel_iterate(schanid_t chan, glui32 *rock)
 
200
{
 
201
    if (!chan) {
 
202
        chan = gli_channellist;
 
203
    } else {
 
204
        chan = chan->chain_next;
 
205
    }
 
206
 
 
207
    if (chan) {
 
208
        if (rock)
 
209
            *rock = chan->rock;
 
210
        return chan;
 
211
    }
 
212
 
 
213
    if (rock)
 
214
        *rock = 0;
 
215
    return NULL;
 
216
}
 
217
 
 
218
glui32 glk_schannel_get_rock(schanid_t chan)
 
219
{
 
220
    if (!chan) {
 
221
        gli_strict_warning("schannel_get_rock: invalid id.");
 
222
        return 0;
 
223
    }
 
224
    return chan->rock;
 
225
}
 
226
 
 
227
glui32 glk_schannel_play(schanid_t chan, glui32 snd)
 
228
{
 
229
    return glk_schannel_play_ext(chan, snd, 1, 0);
 
230
}
 
231
 
 
232
void glk_sound_load_hint(glui32 snd, glui32 flag)
 
233
{
 
234
        /* nop */
 
235
}
 
236
 
 
237
void glk_schannel_set_volume(schanid_t chan, glui32 vol)
 
238
{
 
239
    if (!chan) {
 
240
        gli_strict_warning("schannel_set_volume: invalid id.");
 
241
        return;
 
242
    }
 
243
    chan->volume = vol;
 
244
    switch (chan->status) {
 
245
    case CHANNEL_IDLE:
 
246
        break;
 
247
    case CHANNEL_SOUND:
 
248
        Mix_Volume(chan->sdl_channel, vol / 512);
 
249
        break;
 
250
    case CHANNEL_MUSIC:
 
251
        Mix_VolumeMusic(vol / 512);
 
252
        break;
 
253
    }
 
254
}
 
255
 
 
256
/* Notify the music channel completion */
 
257
static void music_completion_callback()
 
258
{
 
259
    if (!music_channel) {
 
260
        gli_strict_warning("music callback failed");
 
261
        return;
 
262
    }
 
263
    if (music_channel->notify) {
 
264
        gli_event_store(evtype_SoundNotify, 0, 
 
265
                music_channel->resid, music_channel->notify);
 
266
    }
 
267
    cleanup_channel(music_channel);
 
268
}
 
269
 
 
270
/* Notify the sound channel completion */
 
271
static void sound_completion_callback(int chan)
 
272
{
 
273
    channel_t *sound_channel = sound_channels[chan];
 
274
    if (!sound_channel || Mix_Playing(chan)) {
 
275
        gli_strict_warning("sound callback failed");
 
276
        return;
 
277
    }
 
278
    if (!sound_channel->buffered || !sound_channel->decode) {
 
279
        if (sound_channel->notify) {
 
280
        gli_event_store(evtype_SoundNotify, 0, 
 
281
            sound_channel->resid, sound_channel->notify);
 
282
        }
 
283
        cleanup_channel(sound_channel);
 
284
        sound_channels[chan] = 0;
 
285
        return;
 
286
    }
 
287
    Uint32 soundbytes = Sound_Decode(sound_channel->decode);
 
288
    if (!soundbytes) {
 
289
        sound_channel->loop--;
 
290
        if (!sound_channel->loop) {
 
291
            if (sound_channel->notify) {
 
292
            gli_event_store(evtype_SoundNotify, 0, 
 
293
                sound_channel->resid, sound_channel->notify);
 
294
            }
 
295
            cleanup_channel(sound_channel);
 
296
            sound_channels[chan] = 0;
 
297
            return;
 
298
        } else {
 
299
            Sound_Rewind(sound_channel->decode);
 
300
            soundbytes = Sound_Decode(sound_channel->decode);
 
301
        }
 
302
    }
 
303
    Sound_Sample *sample = sound_channel->decode;
 
304
    sound_channel->sample = Mix_QuickLoad_RAW(sample->buffer, soundbytes);
 
305
    Mix_ChannelFinished(&sound_completion_callback);
 
306
    if (Mix_PlayChannel(sound_channel->sdl_channel, 
 
307
            sound_channel->sample, 
 
308
            FALSE) >= 0) {
 
309
        return;
 
310
    }
 
311
    gli_strict_warning("buffer sound failed");
 
312
    gli_strict_warning(Mix_GetError());
 
313
    cleanup_channel(sound_channel);
 
314
    return;
 
315
}
 
316
 
 
317
static glui32 load_sound_resource(glui32 snd, long *len, char **buf)
 
318
{
 
319
    if (!giblorb_is_resource_map()) {
 
320
        FILE *file;
 
321
        char name[1024];
 
322
 
 
323
        sprintf(name, "%s/SND%ld", gli_workdir, (long) snd); 
 
324
 
 
325
        file = fopen(name, "rb");
 
326
        if (!file)
 
327
            return 0;
 
328
 
 
329
        fseek(file, 0, SEEK_END);
 
330
        *len = ftell(file);
 
331
 
 
332
        *buf = malloc(*len);
 
333
        if (!*buf) {
 
334
            fclose(file);
 
335
            return 0;
 
336
        }
 
337
 
 
338
        fseek(file, 0, 0);
 
339
        if (fread(*buf, 1, *len, file) != *len && !feof(file)) return 0;
 
340
        fclose(file);
 
341
 
 
342
        /* AIFF */
 
343
        if (*len > 4 && !memcmp(*buf, "FORM", 4)) {
 
344
            return giblorb_ID_FORM;
 
345
        }
 
346
 
 
347
        /* WAVE */
 
348
        if (*len > 4 && !memcmp(*buf, "WAVE", 4)) {
 
349
            return giblorb_ID_WAVE;
 
350
        }
 
351
 
 
352
        /* s3m */
 
353
        if (*len > 0x30 && !memcmp(*buf + 0x2c, "SCRM", 4)) {
 
354
            return giblorb_ID_MOD;
 
355
        }
 
356
 
 
357
        /* XM */
 
358
        if (*len > 20 && !memcmp(*buf, "Extended Module: ", 17)) {
 
359
            return giblorb_ID_MOD;
 
360
        }
 
361
 
 
362
        /* MOD */
 
363
        if (*len > 1084) {
 
364
            char resname[4];
 
365
            memcpy(resname, (*buf) + 1080, 4);
 
366
            if (!strcmp(resname+1, "CHN") ||        /* 4CHN, 6CHN, 8CHN */
 
367
                    !strcmp(resname+2, "CN") ||         /* 16CN, 32CN */
 
368
                    !strcmp(resname, "M.K.") || !strcmp(resname, "M!K!") ||
 
369
                    !strcmp(resname, "FLT4") || !strcmp(resname, "CD81") ||
 
370
                    !strcmp(resname, "OKTA") || !strcmp(resname, "    "))
 
371
                return giblorb_ID_MOD;
 
372
        }
 
373
 
 
374
        if (!memcmp(*buf, "\377\372", 2)) {     /* mp3 */
 
375
            return giblorb_ID_MP3;
 
376
        }
 
377
 
 
378
        return giblorb_ID_MP3;
 
379
    } else {
 
380
        FILE *file;
 
381
        glui32 type;
 
382
        long pos;
 
383
 
 
384
        giblorb_get_resource(giblorb_ID_Snd, snd, &file, &pos, len, &type);
 
385
        if (!file)
 
386
            return 0;
 
387
 
 
388
        *buf = malloc(*len);
 
389
        if (!*buf)
 
390
            return 0;
 
391
 
 
392
        fseek(file, pos, 0);
 
393
        if (fread(*buf, 1, *len, file) != *len && !feof(file)) return 0;
 
394
        return type;
 
395
    }
 
396
}
 
397
 
 
398
/** Start a sound channel */
 
399
static glui32 play_sound(schanid_t chan)
 
400
{
 
401
    SDL_LockAudio;
 
402
    chan->status = CHANNEL_SOUND;
 
403
    chan->buffered = 0;
 
404
    chan->sdl_channel = Mix_GroupAvailable(FREE);
 
405
    Mix_GroupChannel(chan->sdl_channel, BUSY);
 
406
    SDL_UnlockAudio;
 
407
    chan->sample = Mix_LoadWAV_RW(chan->sdl_rwops, FALSE);
 
408
    if (chan->sdl_channel < 0) {
 
409
        gli_strict_warning("No available sound channels");
 
410
    }
 
411
    if (chan->sdl_channel >= 0 && chan->sample) {
 
412
        SDL_LockAudio;
 
413
        sound_channels[chan->sdl_channel] = chan;
 
414
        SDL_UnlockAudio;
 
415
        Mix_Volume(chan->sdl_channel, chan->volume / 512);
 
416
        Mix_ChannelFinished(&sound_completion_callback);
 
417
        if (Mix_PlayChannel(chan->sdl_channel, chan->sample, 
 
418
                        chan->loop-1) >= 0) {
 
419
                return 1;
 
420
        }
 
421
    }
 
422
    gli_strict_warning("play sound failed");
 
423
    gli_strict_warning(Mix_GetError());
 
424
    SDL_LockAudio;
 
425
    cleanup_channel(chan);
 
426
    SDL_UnlockAudio;
 
427
    return 0;
 
428
}
 
429
 
 
430
/** Start a compressed sound channel */
 
431
static glui32 play_compressed(schanid_t chan, char *ext)
 
432
{
 
433
    SDL_LockAudio;
 
434
    chan->status = CHANNEL_SOUND;
 
435
    chan->buffered = 1;
 
436
    chan->sdl_channel = Mix_GroupAvailable(FREE);
 
437
    Mix_GroupChannel(chan->sdl_channel, BUSY);
 
438
    SDL_UnlockAudio;
 
439
    chan->decode = Sound_NewSample(chan->sdl_rwops, ext, output, 65536);
 
440
    Uint32 soundbytes = Sound_Decode(chan->decode);
 
441
    Sound_Sample *sample = chan->decode;
 
442
    chan->sample = Mix_QuickLoad_RAW(sample->buffer, soundbytes);
 
443
    if (chan->sdl_channel < 0) {
 
444
        gli_strict_warning("No available sound channels");
 
445
    }
 
446
    if (chan->sdl_channel >= 0 && chan->sample) {
 
447
        SDL_LockAudio;
 
448
        sound_channels[chan->sdl_channel] = chan;
 
449
        SDL_UnlockAudio;
 
450
        Mix_Volume(chan->sdl_channel, chan->volume / 512);
 
451
        Mix_ChannelFinished(&sound_completion_callback);
 
452
        if (Mix_PlayChannel(chan->sdl_channel, chan->sample, 0) >= 0) {
 
453
            return 1;
 
454
        }
 
455
    }
 
456
    gli_strict_warning("play sound failed");
 
457
    gli_strict_warning(Mix_GetError());
 
458
    SDL_LockAudio;
 
459
    cleanup_channel(chan);
 
460
    SDL_UnlockAudio;
 
461
    return 0;
 
462
}
 
463
 
 
464
/** Start a mod music channel */
 
465
static glui32 play_mod(schanid_t chan, long len)
 
466
{
 
467
    FILE *file;
 
468
    char *tn;
 
469
    char *tempdir;
 
470
    int music_busy;
 
471
 
 
472
    chan->status = CHANNEL_MUSIC;
 
473
    /* The fscking mikmod lib want to read the mod only from disk! */
 
474
    tempdir = getenv("TEMP");
 
475
    if (tempdir == NULL) tempdir = ".";
 
476
    tn = tempnam(tempdir, "gargtmp");
 
477
    file = fopen(tn, "wb");
 
478
    fwrite(chan->sdl_memory, 1, len, file);
 
479
    fclose(file);
 
480
    chan->music = Mix_LoadMUS(tn);
 
481
    remove(tn);
 
482
    free(tn);
 
483
    music_busy = Mix_PlayingMusic();
 
484
    if (music_busy) {
 
485
        gli_strict_warning("MOD player already in use");
 
486
    }
 
487
    if (!music_busy && chan->music) {
 
488
        SDL_LockAudio;
 
489
        music_channel = chan;
 
490
        SDL_UnlockAudio;
 
491
        Mix_VolumeMusic(chan->volume / 512);
 
492
        Mix_HookMusicFinished(&music_completion_callback);
 
493
        if (Mix_PlayMusic(chan->music, chan->loop-1) >= 0) {
 
494
                return 1;
 
495
        }
 
496
    }
 
497
    gli_strict_warning("play mod failed");
 
498
    gli_strict_warning(Mix_GetError());
 
499
    SDL_LockAudio;
 
500
    cleanup_channel(chan);
 
501
    SDL_UnlockAudio;
 
502
    return 0;
 
503
}
 
504
 
 
505
glui32 glk_schannel_play_ext(schanid_t chan, glui32 snd, glui32 repeats, glui32 notify)
 
506
{
 
507
    long len;
 
508
    glui32 type;
 
509
    char *buf = 0;
 
510
 
 
511
    if (!chan) {
 
512
        gli_strict_warning("schannel_play_ext: invalid id.");
 
513
        return 0;
 
514
    }
 
515
 
 
516
    /* stop previous noise */
 
517
    glk_schannel_stop(chan);
 
518
 
 
519
    if (repeats == 0)
 
520
        return 1;
 
521
 
 
522
    /* load sound resource into memory */
 
523
    type = load_sound_resource(snd, &len, &buf);
 
524
 
 
525
    chan->sdl_memory = (unsigned char*)buf;
 
526
    chan->sdl_rwops = SDL_RWFromConstMem(buf, len);
 
527
    chan->notify = notify;
 
528
    chan->resid = snd;
 
529
    chan->loop = repeats;
 
530
 
 
531
    switch (type) {
 
532
    case giblorb_ID_FORM:
 
533
    case giblorb_ID_WAVE:
 
534
        return play_sound(chan);
 
535
    break;
 
536
 
 
537
    case giblorb_ID_OGG:
 
538
        return play_compressed(chan, "OGG");
 
539
    break;
 
540
 
 
541
    case giblorb_ID_MP3:
 
542
        return play_compressed(chan, "MP3");
 
543
    break;
 
544
 
 
545
    case giblorb_ID_MOD:
 
546
        return play_mod(chan, len);
 
547
    break;
 
548
 
 
549
    default:
 
550
        gli_strict_warning("schannel_play_ext: unknown resource type.");
 
551
    }
 
552
 
 
553
    return 0;
 
554
}
 
555
 
 
556
void glk_schannel_stop(schanid_t chan)
 
557
{
 
558
    if (!chan) {
 
559
        gli_strict_warning("schannel_stop: invalid id.");
 
560
        return;
 
561
    }
 
562
    SDL_LockAudio;
 
563
    chan->buffered = 0;
 
564
    SDL_UnlockAudio;
 
565
    switch (chan->status) {
 
566
    case CHANNEL_SOUND:
 
567
        Mix_HaltChannel(chan->sdl_channel);
 
568
        break;
 
569
    case CHANNEL_MUSIC:
 
570
        Mix_HaltMusic();
 
571
        break;
 
572
    }
 
573
    SDL_LockAudio;
 
574
    cleanup_channel(chan);
 
575
    SDL_UnlockAudio;
 
576
}
 
577