annotate pwplib/snd-sdl.c @ 17:c60e531d19cd

Some misc. cleanups and minor warning removals.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 22 May 2010 20:32:49 +0300
parents e3b0773ba1be
children 56848f781b4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * pwplib SDL (Simple Directmedia Layer) audio "driver"
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * (C) Copyright 2010 ccr/TNSP^PWP <ccr@tnsp.org>
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 *
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * This file and other changes are distributed under same
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * license as pwplib itself.
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "config.h"
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #ifdef DRIVE_SDL
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include <stdio.h>
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include <stdlib.h>
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include <unistd.h>
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 #include <SDL.h>
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 #include "pwplib.h"
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
16
e3b0773ba1be Try this timer frequency.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
20 #define TIMERHZ (72/2)
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
22 static int pwp_sdlaudio_run = 1;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
23 static int pwp_sdlaudio_frag = -1, pwp_sdlaudio_curr;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 extern void gb_sound(int, int, int, int);
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
17
c60e531d19cd Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
28 static void pwp_sdlaudio_fill(void * udata, Uint8 * buf, int len)
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 {
17
c60e531d19cd Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
30 (void) udata;
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
31 while (len > 0) {
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
32 if (pwp_sdlaudio_curr > len) {
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
33 pwp_sdlaudio_curr -= len;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
34 gb_genwave(buf, len);
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
35 len = 0;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
36 pwplib.player();
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
37 } else {
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
38 gb_genwave(buf, pwp_sdlaudio_curr);
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
39 buf += pwp_sdlaudio_curr;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
40 len -= pwp_sdlaudio_curr;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
41 pwp_sdlaudio_curr = pwp_sdlaudio_frag;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
42 pwplib.player();
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
43 }
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
44 }
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
47 static void pwp_sdlaudio_start(void)
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 {
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
49 if (pwp_sdlaudio_run) {
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
50 fprintf(stderr, "\nsound starting\n");
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
51 pwp_sdlaudio_curr = pwp_sdlaudio_frag;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
52 pwp_sdlaudio_run = 0;
6
e2f028bf775a Possibly start SDL audio more sanely.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
53 SDL_PauseAudio(0);
e2f028bf775a Possibly start SDL audio more sanely.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
54 }
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 static void pwp_sdlaudio_close(void)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 SDL_CloseAudio();
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 int pwp_sdlaudio_init()
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 SDL_AudioSpec fmt;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
66 pwp_sdlaudio_run = 1;
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
67
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 fmt.freq = 44100;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 fmt.format = AUDIO_U8;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 fmt.channels = 1;
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
71 fmt.samples = 2048;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 fmt.callback = pwp_sdlaudio_fill;
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
73 pwp_sdlaudio_frag = fmt.freq / TIMERHZ;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 if (SDL_OpenAudio(&fmt, NULL) < 0)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 pwpwrite("* SDL: Could not get desired audio format.\n");
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 return 0;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 pwpwrite("* SDL sound\n");
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 pwplib.sound = gb_sound;
10
a379d6a2a717 Take into account that SDL does not guarantee audio buffer size to be what
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
84 pwplib.loopflush = pwp_sdlaudio_start;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 gb_init(fmt.freq);
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86
17
c60e531d19cd Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
87 // pwplib_regdestr(pwp_sdlaudio_close);
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 return 1;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 #endif