forked from Arakula/vsthost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecAsioHost.cpp
428 lines (380 loc) · 13.8 KB
/
SpecAsioHost.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*****************************************************************************/
/* SpecAsioHost.cpp: implementation of the CSpecAsioHost class. */
/*****************************************************************************/
/******************************************************************************
Copyright (C) 2006 Hermann Seib
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "stdafx.h"
#include "WorkThread.h"
#include "SpecAsioHost.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#pragma intrinsic(memcpy,memset)
/*===========================================================================*/
/* CSpecAsioHost class members */
/*===========================================================================*/
/*****************************************************************************/
/* CSpecAsioHost : constructor */
/*****************************************************************************/
CSpecAsioHost::CSpecAsioHost()
{
pWorkThread = 0;
pBuffers[0] = pBuffers[1] = 0;
allocSize = 0;
/* start up with "use any 2 channels"*/
for (int i = 0; i < kMaxInputChannels + kMaxOutputChannels; i++)
bActiveChannel[i] = true;
}
/*****************************************************************************/
/* ~CSpecAsioHost : destructor */
/*****************************************************************************/
CSpecAsioHost::~CSpecAsioHost()
{
for (int i = 0; i < 2; i++)
if (pBuffers[i])
delete[] pBuffers[i];
}
/*****************************************************************************/
/* AllocBuffers : (re-)allocates the buffers */
/*****************************************************************************/
bool CSpecAsioHost::AllocBuffers(long nBufSz)
{
bool bRC = true;
int nAllocd = 0;
if (nBufSz <= allocSize)
return true;
for (int i = 0; i < 2; i++)
{
if (pBuffers[i])
delete[] pBuffers[i];
if ((pBuffers[i] = new float[nBufSz]) != 0)
nAllocd++;
else if (i)
{
delete[] pBuffers[0];
pBuffers[0] = 0;
}
}
if (nAllocd < 2)
{
allocSize = 0;
return false;
}
else
{
allocSize = nBufSz;
return true;
}
}
/*****************************************************************************/
/* LoadDriver : loads a driver & allocates the necessary buffers */
/*****************************************************************************/
bool CSpecAsioHost::LoadDriver(char *sDriver, int &nBufSz)
{
if (!CAsioHost::LoadDriver(sDriver, nBufSz))
{
TRACE2("CAsioHost::LoadDriver(%s) failed: \"%s\"\n",
sDriver, driverInfo.errorMessage);
return false;
}
#if defined(_DEBUG) || defined(_DEBUGFILE)
TRACE("CAsioHost::LoadDriver(%s) loaded: \"%s\" V%d Asio%d\n",
sDriver,
driverInfo.name, driverInfo.driverVersion,
driverInfo.asioVersion);
#endif
return AllocBuffers(nBufSz);
}
/*****************************************************************************/
/* OnBufferSizeChange : called when the driver wants to switch the buffer sz */
/*****************************************************************************/
long CSpecAsioHost::OnBufferSizeChange(long lNewSize)
{
return AllocBuffers(lNewSize);
}
/*****************************************************************************/
/* OnSwitch : called when a buffer switch occurs */
/*****************************************************************************/
ASIOTime *CSpecAsioHost::OnSwitch
(
ASIOTime *timeInfo,
long index,
ASIOBool processNow
)
{
/* call base class first to fill vars*/
timeInfo = CAsioHost::OnSwitch(timeInfo, index, processNow);
int i, j;
int nBufs;
#if defined(_DEBUGFILE)
#if defined(_DEBUG) || defined(_DEBUGFILE)
static double last_samples = 0;
TRACE("OnSwitch(%d): diff %d ms, ref %d ms, %d samples\n",
index,
sysRefTime - (long)(nanoSeconds / 1000000.0),
sysRefTime,
(long)(samples - last_samples));
last_samples = samples;
#endif
#endif
curIndex = index;
// convert input buffers to 32bit floating point
// all ASIO...LSB/MSB conversions are without #ifdefs, since
// this class is for a specialized Win32 ASIOHost.
//for (i = nBufs = 0; i < inputBuffers + outputBuffers; i++)
for (i = nBufs = 0; i < inputBuffers; i++)
{
if (bActiveChannel[i])
{
ConvertFromASIO(i, bufferInfos[i].buffers[index], pBuffers[nBufs], usedSize);
nBufs++;
}
if (nBufs >= 2) /* VSTHost processes only 2 buffers */
break;
}
for (; nBufs < 2; nBufs++) /* fill up unavailable input buffers */
{ /* as we process EXACTLY 2 buffers */
if (pBuffers[nBufs])
for (j = 0; j < usedSize; j++)
pBuffers[nBufs][j] = .0f;
}
pOBuffers[0] = pBuffers[0]; /* copy pointers to output ptrs */
pOBuffers[1] = pBuffers[1];
if (pWorkThread) /* if work thread there, process it */
{
DWORD dwStamp = (DWORD)(nanoSeconds / 1000.0L);
if (processNow == ASIOTrue) /* either immediately */
pWorkThread->ProcessImmediately(pBuffers, usedSize, 2, dwStamp);
else /* or in separate thread */
pWorkThread->Process(pBuffers, usedSize, 2, dwStamp);
}
else /* otherwise */
OnProcessed(); /* just pass on the unprocessed stuff*/
return timeInfo;
}
/*****************************************************************************/
/* OnProcessed : called when the work thread has finished processing */
/*****************************************************************************/
void CSpecAsioHost::OnProcessed(float **pBuffers)
{
// called from SmpVstHost with the result buffer addresses
pOBuffers[0] = pBuffers[0];
pOBuffers[1] = pBuffers[1];
OnProcessed();
}
void CSpecAsioHost::OnProcessed()
{
int i;
int nBufs;
#if defined(_DEBUG) || defined(_DEBUGFILE)
int nType = -1;
#endif
// convert from float to target format
// convert input buffers to 32bit floating point
// all ASIO...LSB/MSB conversions are without #ifdefs, since
// this class is for a specialized Win32 ASIOHost.
//for (i = nBufs = 0; i < inputBuffers + outputBuffers; i++)
for (i = inputBuffers, nBufs = 0; i < inputBuffers + outputBuffers; i++)
{
// if ((!bufferInfos[i].isInput) && (bActiveChannel[i]))
if (bActiveChannel[i])
{
#if defined(_DEBUG) || defined(_DEBUGFILE)
nType = channelInfos[i].type;
#endif
ConvertToASIO(i, pOBuffers[nBufs], bufferInfos[i].buffers[curIndex], usedSize);
nBufs++;
}
if (nBufs >= 2) /* VSTHost processes only 2 buffers */
break;
}
#if defined(_DEBUGFILE)
#if defined(_DEBUG) || defined(_DEBUGFILE)
// get the system reference time
long diff = timeGetTime() - sysRefTime;
TRACE("Output type %d ready after %dms for buffer %d\n", nType, diff, curIndex);
#endif
#endif
if (postOutput) /* if driver uses the notification, */
OutputReady(); /* tell it that output is processed */
}
/*****************************************************************************/
/* ClearBuffers : clears all the allocated buffers */
/*****************************************************************************/
void CSpecAsioHost::ClearBuffers()
{
int i;
float *pBuf = new float[usedSize];
if (!pBuf)
return;
/* clear the buffer */
memset(pBuf, 0, usedSize * sizeof(float));
/* then put them into ASIO buffers */
for (i = inputBuffers; i < inputBuffers + outputBuffers; i++)
{
ConvertToASIO(i, pBuf, bufferInfos[i].buffers[0], usedSize);
ConvertToASIO(i, pBuf, bufferInfos[i].buffers[1], usedSize);
}
delete[] pBuf;
}
/*****************************************************************************/
/* ConvertFromASIO : converts one of the buffers from ASIO to float */
/*****************************************************************************/
void CSpecAsioHost::ConvertFromASIO
(
int nIndex,
void *source,
float *target,
long frames
)
{
switch (channelInfos[nIndex].type)
{
case ASIOSTInt16MSB:
ReverseEndian2(source, frames);
case ASIOSTInt16LSB:
ToFloat16(source, target, frames);
break;
case ASIOSTInt24MSB: /* used for 20 bits as well */
ReverseEndian3(source, frames);
case ASIOSTInt24LSB:
ToFloat24(source, target, frames);
break;
case ASIOSTFloat32MSB: /* IEEE 754 32 bit float */
ReverseEndian4(source, frames);
case ASIOSTFloat32LSB:
memcpy(target,
source,
frames * sizeof(float));
break;
case ASIOSTFloat64MSB: /* IEEE 754 64 bit double float */
ReverseEndian8(source, frames);
case ASIOSTFloat64LSB:
ToFloat64(source, target, frames);
break;
case ASIOSTInt32MSB: /* 32 bit data */
ReverseEndian4(source, frames);
case ASIOSTInt32LSB:
ToFloat32(source, target, 32, frames);
break;
// these are used for 32 bit data buffer, with different alignment of the data inside
// 32 bit PCI bus systems can more easily used with these
case ASIOSTInt32MSB16: /* 32 bit data with 16 bit alignment */
ReverseEndian4(source, frames);
case ASIOSTInt32LSB16:
ToFloat32(source, target, 16, frames);
break;
case ASIOSTInt32MSB18: /* 32 bit data with 18 bit alignment */
ReverseEndian4(source, frames);
case ASIOSTInt32LSB18:
ToFloat32(source, target, 18, frames);
break;
case ASIOSTInt32MSB20: /* 32 bit data with 20 bit alignment */
ReverseEndian4(source, frames);
case ASIOSTInt32LSB20:
ToFloat32(source, target, 20, frames);
break;
case ASIOSTInt32MSB24: /* 32 bit data with 24 bit alignment */
ReverseEndian4(source, frames);
case ASIOSTInt32LSB24:
ToFloat32(source, target, 24, frames);
break;
}
}
/*****************************************************************************/
/* ConvertToASIO : converts one of the buffers from float to ASIO format */
/*****************************************************************************/
void CSpecAsioHost::ConvertToASIO
(
int nIndex,
float *source,
void *target,
long frames
)
{
switch (channelInfos[nIndex].type)
{
case ASIOSTInt16LSB:
FromFloat16(source, target, frames);
break;
case ASIOSTInt24LSB:
FromFloat24(source, target, frames);
break;
case ASIOSTInt32LSB:
FromFloat32(source, target, 32, frames);
break;
// these are used for 32 bit data buffer, with different alignment of the
// data inside 32 bit PCI bus systems can more easily used with these
case ASIOSTInt32LSB16: // 32 bit data with 16 bit alignment
FromFloat32(source, target, 16, frames);
break;
case ASIOSTInt32LSB18: // 32 bit data with 18 bit alignment
FromFloat32(source, target, 18, frames);
break;
case ASIOSTInt32LSB20: // 32 bit data with 20 bit alignment
FromFloat32(source, target, 20, frames);
break;
case ASIOSTInt32LSB24: // 32 bit data with 24 bit alignment
FromFloat32(source, target, 24, frames);
break;
case ASIOSTFloat32LSB: // IEEE 754 32 bit float
memcpy(target,
source,
frames * sizeof(float));
break;
case ASIOSTFloat64LSB: // IEEE 754 64 bit double float
FromFloat64(source, target, frames);
break;
case ASIOSTInt16MSB:
FromFloat16(source, target, frames);
ReverseEndian2(target, frames);
break;
case ASIOSTInt24MSB: // used for 20 bits as well
FromFloat24(source, target, frames);
ReverseEndian3(target, frames);
break;
case ASIOSTInt32MSB:
// these are used for 32 bit data buffer, with different alignment of the data inside
// 32 bit PCI bus systems can more easily used with these
case ASIOSTInt32MSB16: // 32 bit data with 16 bit alignment
FromFloat32(source, target, 16, frames);
ReverseEndian4(target, frames);
break;
case ASIOSTInt32MSB18: // 32 bit data with 18 bit alignment
FromFloat32(source, target, 18, frames);
ReverseEndian4(target, frames);
break;
case ASIOSTInt32MSB20: // 32 bit data with 20 bit alignment
FromFloat32(source, target, 20, frames);
ReverseEndian4(target, frames);
break;
case ASIOSTInt32MSB24: // 32 bit data with 24 bit alignment
FromFloat32(source, target, 24, frames);
ReverseEndian4(target, frames);
break;
case ASIOSTFloat32MSB: // IEEE 754 32 bit float
memcpy(source,
target,
frames * sizeof(float));
ReverseEndian4(target, frames);
break;
case ASIOSTFloat64MSB: // IEEE 754 64 bit double float
FromFloat64(source, target, frames);
ReverseEndian8(target, frames);
break;
}
}