|
| 1 | +// Ken Silverman's official web site: "http://www.advsys.net/ken" |
| 2 | +// See the included license file "BUILDLIC.TXT" for license info. |
| 3 | +// |
| 4 | +// This file has been modified from Ken Silverman's original release |
| 5 | +// by Jonathon Fowler (jf@jonof.id.au) |
| 6 | + |
| 7 | +#if defined __APPLE__ |
| 8 | +# include <SDL3/SDL.h> |
| 9 | +#else |
| 10 | +# include "SDL.h" |
| 11 | +#endif |
| 12 | + |
| 13 | +#define KDMSOUND_INTERNAL |
| 14 | +#include "kdmsound.h" |
| 15 | + |
| 16 | +#if (SDL_MAJOR_VERSION != 3) |
| 17 | +# error This must be built with SDL3 |
| 18 | +#endif |
| 19 | + |
| 20 | +static SDL_AudioStream *dev; |
| 21 | +static int bytespertic; |
| 22 | + |
| 23 | +static void SDLCALL preparesndbuf(void *udata, SDL_AudioStream *stream, int need, int total); |
| 24 | + |
| 25 | +#define min(a,b) ((a)<(b)?(a):(b)) |
| 26 | +#define max(a,b) ((a)>(b)?(a):(b)) |
| 27 | + |
| 28 | +void initsb(char dadigistat, char damusistat, int dasamplerate, char danumspeakers, char dabytespersample, char daintspersec, char daquality) |
| 29 | +{ |
| 30 | + SDL_AudioSpec want; |
| 31 | + |
| 32 | + (void)daintspersec; (void)daquality; |
| 33 | + |
| 34 | + if (dev) return; |
| 35 | + |
| 36 | + if ((dadigistat == 0) && (damusistat != 1)) |
| 37 | + return; |
| 38 | + |
| 39 | + SDL_memset(&want, 0, sizeof(want)); |
| 40 | + want.freq = dasamplerate; |
| 41 | + want.format = dabytespersample == 1 ? SDL_AUDIO_U8 : SDL_AUDIO_S16; |
| 42 | + want.channels = max(1, min(2, danumspeakers)); |
| 43 | + |
| 44 | + if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) { |
| 45 | + SDL_Log("Failed to initialise SDL audio subsystem: %s", SDL_GetError()); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + dev = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &want, preparesndbuf, NULL); |
| 50 | + if (!dev) { |
| 51 | + SDL_Log("Failed to open audio: %s", SDL_GetError()); |
| 52 | + return; |
| 53 | + } |
| 54 | + if (initkdm(dadigistat, damusistat, want.freq, want.channels, SDL_AUDIO_BITSIZE(want.format)>>3)) { |
| 55 | + SDL_DestroyAudioStream(dev); |
| 56 | + dev = NULL; |
| 57 | + SDL_Log("Failed to initialise KDM"); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + loadwaves("waves.kwv"); |
| 62 | + bytespertic = (SDL_AUDIO_BITSIZE(want.format)>>3)*want.channels*(((want.freq/120)+1)&~1); |
| 63 | + |
| 64 | + SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(dev)); |
| 65 | +} |
| 66 | + |
| 67 | +void uninitsb(void) |
| 68 | +{ |
| 69 | + if (dev) SDL_DestroyAudioStream(dev); |
| 70 | + dev = NULL; |
| 71 | + |
| 72 | + uninitkdm(); |
| 73 | +} |
| 74 | + |
| 75 | +int lockkdm(void) |
| 76 | +{ |
| 77 | + if (!dev) return -1; |
| 78 | + SDL_LockAudioStream(dev); |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +void unlockkdm(void) |
| 83 | +{ |
| 84 | + if (!dev) return; |
| 85 | + SDL_UnlockAudioStream(dev); |
| 86 | +} |
| 87 | + |
| 88 | +void refreshaudio(void) |
| 89 | +{ |
| 90 | +} |
| 91 | + |
| 92 | +void SDLCALL preparesndbuf(void *udata, SDL_AudioStream *stream, int need, int total) |
| 93 | +{ |
| 94 | + Uint8 *sndoffsplc; |
| 95 | + int sndbufsiz = bytespertic; |
| 96 | + |
| 97 | + (void)udata; (void)total; |
| 98 | + |
| 99 | + if (need > 0) { |
| 100 | + while (sndbufsiz < need) sndbufsiz+=bytespertic; |
| 101 | + if ((sndoffsplc = SDL_stack_alloc(Uint8, sndbufsiz))) { |
| 102 | + preparekdmsndbuf(sndoffsplc, sndbufsiz); |
| 103 | + SDL_PutAudioStreamData(stream, sndoffsplc, sndbufsiz); |
| 104 | + SDL_stack_free(sndoffsplc); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments