Mercurial > hg > forks > pwpunix
annotate pwplib/snd-sdl.c @ 10:a379d6a2a717
Take into account that SDL does not guarantee audio buffer size to be what
we request it to be, so implement fragment / tick handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 18 May 2010 17:58:37 +0300 |
parents | e2f028bf775a |
children | e3b0773ba1be |
rev | line source |
---|---|
0 | 1 /* |
2 * pwplib SDL (Simple Directmedia Layer) audio "driver" | |
3 * (C) Copyright 2010 ccr/TNSP^PWP <ccr@tnsp.org> | |
4 * | |
5 * This file and other changes are distributed under same | |
6 * license as pwplib itself. | |
7 */ | |
8 | |
9 #include "config.h" | |
10 | |
11 #ifdef DRIVE_SDL | |
12 | |
13 #include <stdio.h> | |
14 #include <stdlib.h> | |
15 #include <unistd.h> | |
16 #include <SDL.h> | |
17 | |
18 #include "pwplib.h" | |
19 | |
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
|
20 #define TIMERHZ 30 |
0 | 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 | 24 |
25 extern void gb_sound(int, int, int, int); | |
26 | |
27 | |
28 static void pwp_sdlaudio_fill(void *udata, Uint8 * buf, int len) | |
29 { | |
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
|
30 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
|
31 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
|
32 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 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
|
34 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
|
35 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
|
36 } 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
|
37 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
|
38 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 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
|
40 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
|
41 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
|
42 } |
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 } |
0 | 44 } |
45 | |
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
|
46 static void pwp_sdlaudio_start(void) |
0 | 47 { |
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
|
48 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
|
49 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
|
50 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
|
51 pwp_sdlaudio_run = 0; |
6
e2f028bf775a
Possibly start SDL audio more sanely.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
52 SDL_PauseAudio(0); |
e2f028bf775a
Possibly start SDL audio more sanely.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
53 } |
0 | 54 } |
55 | |
56 static void pwp_sdlaudio_close(void) | |
57 { | |
58 SDL_CloseAudio(); | |
59 } | |
60 | |
61 int pwp_sdlaudio_init() | |
62 { | |
63 SDL_AudioSpec fmt; | |
64 | |
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
|
65 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
|
66 |
0 | 67 fmt.freq = 44100; |
68 fmt.format = AUDIO_U8; | |
69 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
|
70 fmt.samples = 2048; |
0 | 71 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
|
72 pwp_sdlaudio_frag = fmt.freq / TIMERHZ; |
0 | 73 |
74 if (SDL_OpenAudio(&fmt, NULL) < 0) | |
75 { | |
76 pwpwrite("* SDL: Could not get desired audio format.\n"); | |
77 return 0; | |
78 } | |
79 | |
80 pwpwrite("* SDL sound\n"); | |
81 | |
82 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
|
83 pwplib.loopflush = pwp_sdlaudio_start; |
0 | 84 gb_init(fmt.freq); |
85 | |
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
|
86 // pwp_regdestr(pwp_sdlaudio_close); |
0 | 87 |
88 return 1; | |
89 } | |
90 #endif |