forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler.cpp
232 lines (206 loc) · 5.33 KB
/
sampler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Parallel port audio digitizer
*
* Toni Wilen 2010
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "events.h"
#include "custom.h"
#include "sampler.h"
#include "dxwrap.h"
#include <dsound.h>
#include <math.h>
#include "win32.h"
#define SAMPLESIZE 4
#define RECORDBUFFER 10000
#define SAMPLEBUFFER 2000
static LPDIRECTSOUNDCAPTURE lpDS2r = NULL;
static LPDIRECTSOUNDCAPTUREBUFFER lpDSBprimary2r = NULL;
static LPDIRECTSOUNDCAPTUREBUFFER lpDSB2r = NULL;
static int inited;
static uae_u8 *samplebuffer;
static int samplerate = 44100;
static float clockspersample;
static int vsynccnt;
static int safepos;
float sampler_evtime;
static int capture_init (void)
{
HRESULT hr;
DSCBUFFERDESC sound_buffer_rec;
WAVEFORMATEX wavfmt;
wavfmt.wFormatTag = WAVE_FORMAT_PCM;
wavfmt.nChannels = 2;
wavfmt.nSamplesPerSec = samplerate;
wavfmt.wBitsPerSample = 16;
wavfmt.nBlockAlign = wavfmt.wBitsPerSample / 8 * wavfmt.nChannels;
wavfmt.nAvgBytesPerSec = wavfmt.nBlockAlign * wavfmt.nSamplesPerSec;
wavfmt.cbSize = 0;
hr = DirectSoundCaptureCreate (&record_devices[currprefs.win32_samplersoundcard].guid, &lpDS2r, NULL);
if (FAILED (hr)) {
write_log (L"SAMPLER: DirectSoundCaptureCreate() failure: %s\n", DXError (hr));
return 0;
}
memset (&sound_buffer_rec, 0, sizeof (DSCBUFFERDESC));
sound_buffer_rec.dwSize = sizeof (DSCBUFFERDESC);
sound_buffer_rec.dwBufferBytes = RECORDBUFFER * SAMPLESIZE;
sound_buffer_rec.lpwfxFormat = &wavfmt;
sound_buffer_rec.dwFlags = 0 ;
hr = lpDS2r->CreateCaptureBuffer (&sound_buffer_rec, &lpDSB2r, NULL);
if (FAILED (hr)) {
write_log (L"SAMPLER: CreateCaptureSoundBuffer() failure: %s\n", DXError(hr));
return 0;
}
hr = lpDSB2r->Start (DSCBSTART_LOOPING);
if (FAILED (hr)) {
write_log (L"SAMPLER: DirectSoundCaptureBuffer_Start failed: %s\n", DXError (hr));
return 0;
}
samplebuffer = xcalloc (uae_u8, SAMPLEBUFFER * SAMPLESIZE);
write_log (L"SAMPLER: Parallel port sampler initialized\n");
return 1;
}
static void capture_free (void)
{
if (lpDSB2r) {
lpDSB2r->Stop ();
lpDSB2r->Release ();
write_log (L"SAMPLER: Parallel port sampler freed\n");
}
lpDSB2r = NULL;
if (lpDS2r)
lpDS2r->Release ();
lpDS2r = NULL;
xfree (samplebuffer);
samplebuffer = NULL;
}
static evt oldcycles;
static int oldoffset;
uae_u8 sampler_getsample (int channel)
{
HRESULT hr;
static DWORD cap_pos;
static float diffsample;
DWORD t, cur_pos;
void *p1, *p2;
DWORD len1, len2;
evt cycles;
int offset;
int sample, samplecnt, diff;
// if (channel)
// return 0;
channel = 0;
if (!inited) {
if (!capture_init ()) {
capture_free ();
return 0;
}
inited = 1;
oldcycles = get_cycles ();
oldoffset = -1;
safepos = -RECORDBUFFER / 10 * SAMPLESIZE;
hr = lpDSB2r->GetCurrentPosition (&t, &cap_pos);
cap_pos += safepos;
if (cap_pos > 10 * RECORDBUFFER * SAMPLESIZE)
cap_pos += RECORDBUFFER * SAMPLESIZE;
if (cap_pos >= RECORDBUFFER * SAMPLESIZE)
cap_pos -= RECORDBUFFER * SAMPLESIZE;
if (FAILED (hr)) {
sampler_free ();
return 0;
}
clockspersample = sampler_evtime / samplerate + 41000;
}
if (clockspersample < 1)
return 0;
uae_s16 *sbuf = (uae_s16*)samplebuffer;
vsynccnt = 0;
sample = 0;
samplecnt = 0;
cycles = get_cycles () - oldcycles;
float cps = clockspersample + diffsample;
offset = (cycles + cps - 1) / cps;
if (oldoffset < 0 || offset >= SAMPLEBUFFER || offset < 0) {
if (oldoffset >= 0 && offset >= SAMPLEBUFFER) {
while (oldoffset < SAMPLEBUFFER) {
sample += sbuf[oldoffset * SAMPLESIZE / 2 + channel];
oldoffset++;
samplecnt++;
}
}
hr = lpDSB2r->Lock (cap_pos, SAMPLEBUFFER * SAMPLESIZE, &p1, &len1, &p2, &len2, 0);
if (FAILED (hr))
return 0;
memcpy (samplebuffer, p1, len1);
if (p2)
memcpy (samplebuffer + len1, p2, len2);
lpDSB2r->Unlock (p1, len1, p2, len2);
cap_pos += SAMPLEBUFFER * SAMPLESIZE;
hr = lpDSB2r->GetCurrentPosition (&t, &cur_pos);
if (FAILED (hr))
return 0;
cur_pos += safepos;
if (cur_pos >= 10 * RECORDBUFFER * SAMPLESIZE)
cur_pos += RECORDBUFFER * SAMPLESIZE;
if (cur_pos >= RECORDBUFFER * SAMPLESIZE)
cur_pos -= RECORDBUFFER * SAMPLESIZE;
if (cur_pos >= cap_pos)
diff = cur_pos - cap_pos;
else
diff = RECORDBUFFER * SAMPLESIZE - cap_pos + cur_pos;
if (diff > RECORDBUFFER * SAMPLESIZE / 2)
diff -= RECORDBUFFER * SAMPLESIZE;
diff /= SAMPLESIZE;
int diff2 = 100 * diff / (RECORDBUFFER / 2);
#if 0
diffsample = -pow (diff2 < 0 ? -diff2 : diff2, 3.1);
if (diff2 < 0)
diffsample = -diffsample;
#endif
// write_log (L"%d:%.1f\n", diff, diffsample);
cap_pos += SAMPLEBUFFER * SAMPLESIZE;
if (cap_pos < 0)
cap_pos += RECORDBUFFER * SAMPLESIZE;
if (cap_pos >= RECORDBUFFER * SAMPLESIZE)
cap_pos -= RECORDBUFFER * SAMPLESIZE;
if (offset < 0)
offset = 0;
if (offset >= SAMPLEBUFFER)
offset -= SAMPLEBUFFER;
oldoffset = 0;
oldcycles = get_cycles ();
}
while (oldoffset <= offset) {
sample += sbuf[oldoffset * SAMPLESIZE / 2 + channel];
samplecnt++;
oldoffset++;
}
oldoffset = offset;
if (samplecnt > 0)
sample /= samplecnt;
return (sample / 256) - 128;
}
int sampler_init (void)
{
if (currprefs.win32_samplersoundcard < 0)
return 0;
return 1;
}
void sampler_free (void)
{
inited = 0;
vsynccnt = 0;
capture_free ();
}
void sampler_vsync (void)
{
if (!inited)
return;
vsynccnt++;
if (vsynccnt > 50) {
sampler_free ();
return;
}
}