| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
module audio.audio; |
| 6 |
import derelict.sdl2.sdl; |
| 7 |
import audio.resid.filter; |
| 8 |
import audio.player; |
| 9 |
import audio.callback; |
| 10 |
import std.stdio; |
| 11 |
import core.stdc.stdlib; |
| 12 |
import core.stdc.string; |
| 13 |
import std.conv; |
| 14 |
|
| 15 |
enum MIXBUF_MUL = 2; |
| 16 |
__gshared SDL_AudioSpec audiospec; |
| 17 |
__gshared bool audioInited = false; |
| 18 |
__gshared int framerate = 50; |
| 19 |
__gshared int multiplier; |
| 20 |
__gshared int freq = 48000, bufferSize = 2048; |
| 21 |
__gshared private int callbackCounter = 0; |
| 22 |
__gshared private int bufferUsed; // in samples |
| 23 |
__gshared private int callbackInterval; |
| 24 |
__gshared short* mixbuf = null; |
| 25 |
|
| 26 |
|
| 27 |
extern(C) { |
| 28 |
extern __gshared char[0x19] sidreg; |
| 29 |
extern __gshared int residdelay; |
| 30 |
extern __gshared int sid_init(int, Filterparams*, int, int, int, int, int) nothrow ; |
| 31 |
extern __gshared int sid_fillbuffer(short *, int, int) nothrow ; |
| 32 |
extern __gshared int sid_close(); |
| 33 |
__gshared void function() nothrow callback; |
| 34 |
|
| 35 |
|
| 36 |
int audio_init(int fr, void function() nothrow cb) { |
| 37 |
SDL_AudioSpec requested; |
| 38 |
|
| 39 |
if(audioInited) return 0; |
| 40 |
|
| 41 |
audioInited = true; |
| 42 |
|
| 43 |
framerate = fr; |
| 44 |
|
| 45 |
if(bufferSize < freq / framerate) { |
| 46 |
// fprintf(stderr,"Minimum buffer size is %d bytes.\n", freq / framerate); |
| 47 |
return -1; |
| 48 |
} |
| 49 |
|
| 50 |
requested.freq = freq; |
| 51 |
requested.format = AUDIO_S16LSB; |
| 52 |
requested.channels = 1; |
| 53 |
requested.samples = cast(ushort) bufferSize; |
| 54 |
|
| 55 |
requested.callback = &audio_callback_2; |
| 56 |
|
| 57 |
requested.userdata = null; |
| 58 |
callback = cb; |
| 59 |
if(SDL_OpenAudio(&requested, &audiospec) < 0) { |
| 60 |
writeln("Could not open audio: ", to!string(SDL_GetError())); |
| 61 |
return -1; |
| 62 |
} |
| 63 |
if(audiospec.format != AUDIO_S16LSB) { |
| 64 |
writeln("Incorrect audio format obtained."); |
| 65 |
return -1; |
| 66 |
} |
| 67 |
bufferSize = audiospec.samples; |
| 68 |
mixbuf = cast(short *)malloc(bufferSize * short.sizeof * MIXBUF_MUL); |
| 69 |
memset(mixbuf, 0, bufferSize * short.sizeof * MIXBUF_MUL); |
| 70 |
setCallMultiplier(1); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
int getbufsize() { |
| 75 |
return cast(int)(bufferSize * MIXBUF_MUL); |
| 76 |
} |
| 77 |
|
| 78 |
void audio_close() { |
| 79 |
SDL_CloseAudio(); |
| 80 |
if(mixbuf) free(mixbuf); |
| 81 |
sid_close(); |
| 82 |
} |
| 83 |
|
| 84 |
void reset() { |
| 85 |
bufferUsed = callbackCounter = 0; |
| 86 |
} |
| 87 |
|
| 88 |
void setCallMultiplier(int m) { |
| 89 |
if(m < 1 || m > 16) return; |
| 90 |
if(m == multiplier) return; |
| 91 |
SDL_LockAudio(); |
| 92 |
multiplier = m; |
| 93 |
framerate = 50 * m; |
| 94 |
callbackInterval = audiospec.freq / framerate; |
| 95 |
reset(); |
| 96 |
SDL_UnlockAudio(); |
| 97 |
} |
| 98 |
|
| 99 |
void audio_callback(void *data, ubyte* stream, int len) nothrow { |
| 100 |
int samplesRequested = cast(int) (len / short.sizeof); |
| 101 |
int total = 0,todo = 0,t = 0; |
| 102 |
int steps; |
| 103 |
if(!audio.player.isPlaying()) return; |
| 104 |
while(total < samplesRequested) { |
| 105 |
todo = samplesRequested - total; |
| 106 |
assert(todo >= 0); |
| 107 |
if(callbackCounter + todo >= callbackInterval) { |
| 108 |
int c = callbackInterval - callbackCounter; |
| 109 |
assert(c > 0); |
| 110 |
t = sid_fillbuffer(cast(short*)stream + total, |
| 111 |
c, 0); |
| 112 |
(*callback)(); |
| 113 |
callbackCounter -= callbackInterval; |
| 114 |
assert(callbackCounter + todo >= 0); |
| 115 |
} |
| 116 |
else { |
| 117 |
assert(total + todo <= samplesRequested); |
| 118 |
t = sid_fillbuffer(cast(short*)stream + total, |
| 119 |
todo, 0); |
| 120 |
|
| 121 |
} |
| 122 |
total += t; |
| 123 |
callbackCounter += t; |
| 124 |
steps++; |
| 125 |
} |
| 126 |
assert(total == samplesRequested); |
| 127 |
} |
| 128 |
|
| 129 |
__gshared void audio_callback_2(void *data, ubyte* stream, int len) nothrow { |
| 130 |
int samplesRequested = cast(int) (len / short.sizeof); |
| 131 |
int i,t; |
| 132 |
if(!audio.player.isPlaying()) { |
| 133 |
//memcpy(stream, cast(ubyte*)mixbuf, len); |
| 134 |
memset(stream, 0, len); |
| 135 |
return; |
| 136 |
} |
| 137 |
while((bufferUsed + callbackInterval) <= bufferSize * MIXBUF_MUL) { |
| 138 |
t = sid_fillbuffer(mixbuf+bufferUsed, callbackInterval, cyclesPerFrame); |
| 139 |
bufferUsed += t; |
| 140 |
(*callback)(); |
| 141 |
} |
| 142 |
|
| 143 |
memcpy(stream, cast(ubyte*)mixbuf, len); |
| 144 |
bufferUsed -= samplesRequested; |
| 145 |
if(bufferUsed < 0) { |
| 146 |
// writeln("Audio buffer underrun ", bufferUsed); |
| 147 |
bufferUsed = 0; |
| 148 |
} |
| 149 |
|
| 150 |
short* pi, po; |
| 151 |
for(i = 0, pi = mixbuf, po = mixbuf + samplesRequested; |
| 152 |
i < bufferSize * MIXBUF_MUL - samplesRequested; i++) { |
| 153 |
*(pi++) = *(po++); |
| 154 |
} |
| 155 |
} |
| 156 |
} |