| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
module audio.callback; |
| 6 |
import derelict.sdl2.sdl; |
| 7 |
import audio.player; |
| 8 |
import com.session; |
| 9 |
import com.cpu; |
| 10 |
import ct.base; |
| 11 |
static import audio.timer, audio.audio; |
| 12 |
|
| 13 |
/+ holds the state of cpu and memory for frame debug dumps. not implemented for now. +/ |
| 14 |
class SongState { |
| 15 |
int totalFramecallCounter, subframeCounter; |
| 16 |
private CPU cpu; |
| 17 |
private ubyte[65536] data; |
| 18 |
CPUException exception; |
| 19 |
/+ |
| 20 |
this(Song song, int tfc, int sfc) { |
| 21 |
data = song.data; |
| 22 |
cpu = new CPU(data); |
| 23 |
totalFramecallCounter = tfc; |
| 24 |
subframeCounter = sfc; |
| 25 |
exception = null; |
| 26 |
} |
| 27 |
|
| 28 |
void dump() { |
| 29 |
writefln("Frame %d(%d)", totalFrameCallCounter, subframeCounter); |
| 30 |
cpu.execute(pc, true); |
| 31 |
}+/ |
| 32 |
} |
| 33 |
|
| 34 |
__gshared private int frameCallCounter, totalFrameCallCounter; // for multispeed |
| 35 |
__gshared private Exception playbackStatus = null; |
| 36 |
__gshared private bool dumpFrameRequested = false; |
| 37 |
__gshared private bool dumpRequested = false; |
| 38 |
__gshared int cyclesPerFrame; |
| 39 |
__gshared int linesPerFrame, maxCycles, maxLines, maxCycleFrame; |
| 40 |
__gshared char[0x19][5*50] dump; |
| 41 |
__gshared int dumpctr; |
| 42 |
__gshared auto avgsPerFrame = new int[](20); |
| 43 |
|
| 44 |
Exception getException() { |
| 45 |
Exception ex = playbackStatus; |
| 46 |
playbackStatus = null; |
| 47 |
return ex; |
| 48 |
} |
| 49 |
|
| 50 |
void reset() { |
| 51 |
maxCycles = 0; |
| 52 |
cyclesPerFrame = 0; |
| 53 |
frameCallCounter = 0; |
| 54 |
totalFrameCallCounter = 0; |
| 55 |
} |
| 56 |
|
| 57 |
void requestDump() { |
| 58 |
dumpRequested = true; |
| 59 |
} |
| 60 |
|
| 61 |
// called each frame from soundbuffer callback |
| 62 |
extern(C) __gshared void audio_frame() nothrow { |
| 63 |
static char[0x19][5*50] dumpbak; |
| 64 |
|
| 65 |
switch(audio.player.getPlaystatus()) { |
| 66 |
case Status.Play: |
| 67 |
audio.timer.tick(); |
| 68 |
cpuCall(frameCallCounter > 0 ? 0x1006 : 0x1003, false); |
| 69 |
break; |
| 70 |
case Status.Keyjam: |
| 71 |
address call = 0x100c; |
| 72 |
song.cpu.regs.x = 0; |
| 73 |
if(song.ver > 7) { |
| 74 |
call = song.offsets[Offsets.Submplayplay]; |
| 75 |
} |
| 76 |
cpuCall(call,false); |
| 77 |
break; |
| 78 |
default: |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
frameCallCounter++; |
| 83 |
totalFrameCallCounter++; |
| 84 |
if(frameCallCounter >= audio.audio.multiplier) { |
| 85 |
frameCallCounter = 0; |
| 86 |
frameDone(); |
| 87 |
} |
| 88 |
|
| 89 |
for(int i=0; i<0x19; i++) { |
| 90 |
sidreg[i] = song.sidbuf[i]; |
| 91 |
// if(com.session.debugMode) |
| 92 |
// dump[dumpctr][i] = song.sidbuf[i]; |
| 93 |
} |
| 94 |
// if(com.session.debugMode) { |
| 95 |
// dumpctr++; |
| 96 |
// if(dumpctr >= dump.length) { |
| 97 |
// dumpbak = dump[]; |
| 98 |
// dumpctr--; |
| 99 |
// dump[0 .. $-1] = dumpbak[1 .. $]; |
| 100 |
|
| 101 |
// } |
| 102 |
// } |
| 103 |
} |
| 104 |
|
| 105 |
private void frameDone() nothrow { |
| 106 |
static int[5] avgs; |
| 107 |
int totalCycles; |
| 108 |
auto playerFrameCounter = song.data[song.offsets[2]]; |
| 109 |
static int[][] _avgsPerFrame = new int[][](20,5); |
| 110 |
|
| 111 |
|
| 112 |
if(cyclesPerFrame > maxCycles) { |
| 113 |
maxCycles = cyclesPerFrame; |
| 114 |
maxCycleFrame = song.data[song.offsets[2]]; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
foreach(idx, ref avg; avgs) { |
| 119 |
if(idx == avgs.length - 1) { |
| 120 |
avg = cyclesPerFrame; |
| 121 |
} |
| 122 |
else avg = avgs[idx+1]; |
| 123 |
totalCycles += avg; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
int[] _avgPerFrame = _avgsPerFrame[playerFrameCounter]; |
| 128 |
import std.stdio; |
| 129 |
_avgPerFrame[0 .. $-1] = _avgPerFrame[1 .. $].dup; |
| 130 |
_avgPerFrame[$-1] = cyclesPerFrame; |
| 131 |
|
| 132 |
int avg; |
| 133 |
foreach(i; 0 .. 5) { |
| 134 |
avg += _avgPerFrame[i]; |
| 135 |
} |
| 136 |
avg /= 5; |
| 137 |
avgsPerFrame[playerFrameCounter] = avg / 10; |
| 138 |
|
| 139 |
|
| 140 |
import std.math : ceil; |
| 141 |
maxLines = cast(int)ceil(maxCycles / 63.0); |
| 142 |
|
| 143 |
cyclesPerFrame = 0; |
| 144 |
linesPerFrame = cast(int)(totalCycles / avgs.length / 63); |
| 145 |
|
| 146 |
if(dumpRequested) { |
| 147 |
dumpFrameRequested = true; |
| 148 |
dumpRequested = false; |
| 149 |
} |
| 150 |
else dumpFrameRequested = false; |
| 151 |
|
| 152 |
audio.timer.tickFullFrame(); |
| 153 |
} |
| 154 |
|
| 155 |
void cpuCall(ushort pc, bool lockAudio) nothrow{ cpuCall(pc, lockAudio, false); } |
| 156 |
void cpuCall(ushort pc, bool lockAudio, bool forcedump) nothrow { |
| 157 |
if(lockAudio) SDL_LockAudio(); |
| 158 |
try { |
| 159 |
if(dumpFrameRequested) { |
| 160 |
// state = new SongState(song, totalFrameCallCounter, frameCallCounter); |
| 161 |
} |
| 162 |
int i = song.cpu.execute(pc, false); |
| 163 |
if(muted[0]) song.sidbuf[0..7] = 0; |
| 164 |
if(muted[1]) song.sidbuf[7..14] = 0; |
| 165 |
if(muted[2]) song.sidbuf[14..21] = 0; |
| 166 |
cyclesPerFrame += i; |
| 167 |
} |
| 168 |
catch(Exception e) { |
| 169 |
playbackStatus = e; |
| 170 |
stop(); // < TODO: player.d should check if playback is in error state and stop by itself |
| 171 |
//UI.statusline.display(e.toString()); |
| 172 |
//stop(); |
| 173 |
} |
| 174 |
finally { |
| 175 |
if(lockAudio) SDL_UnlockAudio(); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
extern(C) { |
| 180 |
__gshared extern char[0x19] sidreg; |
| 181 |
} |