Mercurial > hg > forks > pwpunix
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:7fb3727d16b8 | 10:a379d6a2a717 |
---|---|
15 #include <unistd.h> | 15 #include <unistd.h> |
16 #include <SDL.h> | 16 #include <SDL.h> |
17 | 17 |
18 #include "pwplib.h" | 18 #include "pwplib.h" |
19 | 19 |
20 #define TIMERHZ 72 | 20 #define TIMERHZ 30 |
21 | 21 |
22 static int pwp_sdlaudio_start = 1; | 22 static int pwp_sdlaudio_run = 1; |
23 static int pwp_sdlaudio_frag = -1, pwp_sdlaudio_curr; | |
23 | 24 |
24 extern void gb_sound(int, int, int, int); | 25 extern void gb_sound(int, int, int, int); |
25 | 26 |
26 | 27 |
27 static void pwp_sdlaudio_fill(void *udata, Uint8 * buf, int len) | 28 static void pwp_sdlaudio_fill(void *udata, Uint8 * buf, int len) |
28 { | 29 { |
29 pwplib.player(); | 30 while (len > 0) { |
30 gb_genwave(buf, len); | 31 if (pwp_sdlaudio_curr > len) { |
32 pwp_sdlaudio_curr -= len; | |
33 gb_genwave(buf, len); | |
34 len = 0; | |
35 pwplib.player(); | |
36 } else { | |
37 gb_genwave(buf, pwp_sdlaudio_curr); | |
38 buf += pwp_sdlaudio_curr; | |
39 len -= pwp_sdlaudio_curr; | |
40 pwp_sdlaudio_curr = pwp_sdlaudio_frag; | |
41 pwplib.player(); | |
42 } | |
43 } | |
31 } | 44 } |
32 | 45 |
33 static void pwp_sdlaudio_loopflush(void) | 46 static void pwp_sdlaudio_start(void) |
34 { | 47 { |
35 if (pwp_sdlaudio_start) { | 48 if (pwp_sdlaudio_run) { |
36 pwp_sdlaudio_start = 0; | 49 fprintf(stderr, "\nsound starting\n"); |
50 pwp_sdlaudio_curr = pwp_sdlaudio_frag; | |
51 pwp_sdlaudio_run = 0; | |
37 SDL_PauseAudio(0); | 52 SDL_PauseAudio(0); |
38 } | 53 } |
39 } | 54 } |
40 | 55 |
41 static void pwp_sdlaudio_close(void) | 56 static void pwp_sdlaudio_close(void) |
45 | 60 |
46 int pwp_sdlaudio_init() | 61 int pwp_sdlaudio_init() |
47 { | 62 { |
48 SDL_AudioSpec fmt; | 63 SDL_AudioSpec fmt; |
49 | 64 |
65 pwp_sdlaudio_run = 1; | |
66 | |
50 fmt.freq = 44100; | 67 fmt.freq = 44100; |
51 fmt.format = AUDIO_U8; | 68 fmt.format = AUDIO_U8; |
52 fmt.channels = 1; | 69 fmt.channels = 1; |
53 fmt.samples = fmt.freq / TIMERHZ; | 70 fmt.samples = 2048; |
54 fmt.callback = pwp_sdlaudio_fill; | 71 fmt.callback = pwp_sdlaudio_fill; |
72 pwp_sdlaudio_frag = fmt.freq / TIMERHZ; | |
55 | 73 |
56 if (SDL_OpenAudio(&fmt, NULL) < 0) | 74 if (SDL_OpenAudio(&fmt, NULL) < 0) |
57 { | 75 { |
58 pwpwrite("* SDL: Could not get desired audio format.\n"); | 76 pwpwrite("* SDL: Could not get desired audio format.\n"); |
59 return 0; | 77 return 0; |
60 } | 78 } |
61 | 79 |
62 pwpwrite("* SDL sound\n"); | 80 pwpwrite("* SDL sound\n"); |
63 | 81 |
64 pwplib.sound = gb_sound; | 82 pwplib.sound = gb_sound; |
65 pwplib.loopflush = pwp_sdlaudio_loopflush; | 83 pwplib.loopflush = pwp_sdlaudio_start; |
66 gb_init(fmt.freq); | 84 gb_init(fmt.freq); |
67 | 85 |
68 pwp_regdestr(pwp_sdlaudio_close); | 86 // pwp_regdestr(pwp_sdlaudio_close); |
69 | 87 |
70 return 1; | 88 return 1; |
71 } | 89 } |
72 #endif | 90 #endif |