Mercurial > hg > forks > pwpunix
annotate pwplib/snd-sdl.c @ 43:093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 24 May 2010 05:07:33 +0300 |
parents | b9d679965320 |
children | 85671798fdb3 |
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" | |
35 | 19 #include "gb.h" |
0 | 20 |
34
32ec3c0d1b6c
Fix a horrible think-o bug in the SDL sound callback fragmentation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
21 #define TIMERHZ 72 |
0 | 22 |
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
|
23 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
|
24 static int pwp_sdlaudio_frag = -1, pwp_sdlaudio_curr; |
0 | 25 |
26 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
27 static void pwp_sdlaudio_fill(void * udata, Uint8 * buf, int len) |
0 | 28 { |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
29 (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
|
30 while (len > 0) { |
34
32ec3c0d1b6c
Fix a horrible think-o bug in the SDL sound callback fragmentation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
31 if (pwp_sdlaudio_curr >= len) { |
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
|
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 } 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
|
36 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
|
37 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 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
|
39 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
|
40 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
|
41 } |
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 } |
0 | 43 } |
44 | |
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
|
45 static void pwp_sdlaudio_start(void) |
0 | 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 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
|
48 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
|
49 pwp_sdlaudio_run = 0; |
34
32ec3c0d1b6c
Fix a horrible think-o bug in the SDL sound callback fragmentation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
50 pwplib.player(); |
6
e2f028bf775a
Possibly start SDL audio more sanely.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
51 SDL_PauseAudio(0); |
e2f028bf775a
Possibly start SDL audio more sanely.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
52 } |
43
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
53 |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
54 /* NOTICE! A small delay is used here, because the prods using pwplib |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
55 * run in a tight loop (they expect that audio rendering blocks, |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
56 * which it doesn't in case of SDL.) This way we avoid consuming |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
57 * excessive amounts of CPU. |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
58 * |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
59 * -- ccr |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
60 */ |
093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
61 SDL_Delay(10); |
0 | 62 } |
63 | |
64 int pwp_sdlaudio_init() | |
65 { | |
66 SDL_AudioSpec fmt; | |
67 | |
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
|
68 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
|
69 |
0 | 70 fmt.freq = 44100; |
71 fmt.format = AUDIO_U8; | |
72 fmt.channels = 1; | |
34
32ec3c0d1b6c
Fix a horrible think-o bug in the SDL sound callback fragmentation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
73 fmt.samples = 512; |
0 | 74 fmt.callback = pwp_sdlaudio_fill; |
75 | |
76 if (SDL_OpenAudio(&fmt, NULL) < 0) | |
77 { | |
78 pwpwrite("* SDL: Could not get desired audio format.\n"); | |
79 return 0; | |
80 } | |
81 | |
31
502d54fd46fa
Remove useless pwp_sdlaudio_close(), move fragment size calculation after SDL_OpenAudio() call as it may possibly modify the frequency. Show acquired audio parameters to user.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
82 pwp_sdlaudio_frag = fmt.freq / TIMERHZ; |
502d54fd46fa
Remove useless pwp_sdlaudio_close(), move fragment size calculation after SDL_OpenAudio() call as it may possibly modify the frequency. Show acquired audio parameters to user.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
83 |
502d54fd46fa
Remove useless pwp_sdlaudio_close(), move fragment size calculation after SDL_OpenAudio() call as it may possibly modify the frequency. Show acquired audio parameters to user.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
84 pwpwrite("* SDL sound (freq=%d, fmt=%d, chn=%d, buf=%d, frag=%d)\n", |
502d54fd46fa
Remove useless pwp_sdlaudio_close(), move fragment size calculation after SDL_OpenAudio() call as it may possibly modify the frequency. Show acquired audio parameters to user.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
85 fmt.freq, fmt.format, fmt.channels, fmt.samples, pwp_sdlaudio_frag); |
0 | 86 |
87 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
|
88 pwplib.loopflush = pwp_sdlaudio_start; |
0 | 89 gb_init(fmt.freq); |
90 | |
91 return 1; | |
92 } | |
93 #endif |