-
Notifications
You must be signed in to change notification settings - Fork 14
/
AsioHost.cpp
809 lines (708 loc) · 26.3 KB
/
AsioHost.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/*****************************************************************************/
/* AsioHost.cpp: implementation of class CAsioHost. */
/*****************************************************************************/
/******************************************************************************
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 <math.h>
#include "AsioHost.h"
/*===========================================================================*/
/* Helper functions */
/*===========================================================================*/
/*****************************************************************************/
/* Saturate : keeps value in range without branching */
/*****************************************************************************/
inline float Saturate(float input, float fMax)
{
static const float fGrdDiv = 0.5f;
float x1 = fabsf(input + fMax);
float x2 = fabsf(input - fMax);
return fGrdDiv * (x1 - x2);
}
/*****************************************************************************/
/* ASIO64toDouble: convert 64 bit ASIOSample/ASIOTimeStamp to double float */
/*****************************************************************************/
#if NATIVE_INT64
#define ASIO64toDouble(a) (a)
#else
const double twoRaisedTo32 = 4294967296.;
#define ASIO64toDouble(a) ((a).lo + (a).hi * twoRaisedTo32)
#endif
/*===========================================================================*/
/* Callback functions - relayed to the one and only host object */
/*===========================================================================*/
// The ASIO 2.0 SDK doesn't support more than one driver at a time.
// While it would be possible to create a derivate that supports
// multiple drivers, I don't want to deviate too much from the SDK.
// Besides, since the callbacks don't give any clue about their originator,
// I'd have to allocate a set of callback functions for each concurrently
// loaded driver. Much work for a small gain. So...
// this is the one and only ASIO Host object
static CAsioHost *pHost = NULL;
/*****************************************************************************/
/* bufferSwitchTimeInfo : buffer switch callback with time information */
/*****************************************************************************/
ASIOTime *bufferSwitchTimeInfo
(
ASIOTime *timeInfo,
long index,
ASIOBool processNow
)
{
if (!pHost)
return timeInfo;
return pHost->OnSwitch(timeInfo, index, processNow);
}
/*****************************************************************************/
/* bufferSwitch : old buffer switch callback w/o time information */
/*****************************************************************************/
void bufferSwitch(long index, ASIOBool processNow)
{
ASIOTime ti = {0};
if (ASIOGetSamplePosition(&ti.timeInfo.samplePosition,
&ti.timeInfo.systemTime) == ASE_OK)
ti.timeInfo.flags = kSystemTimeValid | kSamplePositionValid;
bufferSwitchTimeInfo(&ti, index, processNow);
}
/*****************************************************************************/
/* sampleRateChanged : called when the sample rate changes */
/*****************************************************************************/
void sampleRateChanged(ASIOSampleRate sRate)
{
if (pHost)
pHost->OnSampleRateChanged(sRate);
}
/*****************************************************************************/
/* asioMessages : ASIO extension callback */
/*****************************************************************************/
long asioMessages(long selector, long value, void* message, double* opt)
{
if (!pHost)
return 0;
return pHost->OnMessage(selector, value, message, opt);
}
/*===========================================================================*/
/* CAsioHost class members */
/*===========================================================================*/
// extern bool loadAsioDriver(char *name);
// this is a bad hack to remedy another bad hack in the ASIO stuff...
// in AsioDrivers.cpp, this pointer is set by the function
// loadAsioDriver(), but it's never destroyed in an orderly fashion.
// I, however, don't like such a mess, so loadAsioDriver() isn't called
// but I assume rulership over this pointer instead :-)
extern AsioDrivers* asioDrivers;
/*****************************************************************************/
/* Static class data (designed for exactly one ASIO Host!) */
/*****************************************************************************/
ASIOCallbacks CAsioHost::asioCallbacks =
{
bufferSwitch,
sampleRateChanged,
asioMessages,
bufferSwitchTimeInfo
};
/*****************************************************************************/
/* CAsioHost : constructor */
/*****************************************************************************/
CAsioHost::CAsioHost()
{
if (!asioDrivers) /* we are the one and only */
asioDrivers = this; /* asioDrivers pointer! */
bLoaded = false; /* no driver loaded yet */
bAllocated = false; /* no buffers allocated yet */
memset(&driverInfo, 0, sizeof(driverInfo));
}
/*****************************************************************************/
/* ~CAsioHost : destructor */
/*****************************************************************************/
CAsioHost::~CAsioHost()
{
Exit(); /* tidy up before we go */
if (asioDrivers == this) /* with much regret, we have to */
asioDrivers = NULL; /* step back from this task :-) */
}
/*****************************************************************************/
/* LoadDriver : loads an ASIO driver */
/*****************************************************************************/
bool CAsioHost::LoadDriver(char * sDriver, int &nBufSz)
{
if (pHost && (pHost != this)) /* if another CAsioHost is running */
return false; /* return with error - only 1 allowed*/
if (bLoaded) /* make sure to kill off an old one */
UnloadDriver();
try
{
bLoaded = loadDriver(sDriver); /* try to load the driver */
if (!bLoaded) /* upon error */
{
TRACE1("CAsioHost::LoadDriver(\"%s\"): loadDriver() failed\n", sDriver);
throw 1; /* return at once */
}
// the driver can be referenced as theAsioDriver->... from now on
pHost = this;
if (!Init())
{
TRACE1("CAsioHost::LoadDriver(\"%s\"): Init() failed\n", sDriver);
throw 2;
}
if (!GetChannels(&inputChannels, &outputChannels))
{
TRACE1("CAsioHost::LoadDriver(\"%s\"): GetChannels() failed\n", sDriver);
throw 3;
}
if (!GetBufferSize(&minSize, &maxSize, &preferredSize, &granularity))
{
TRACE1("CAsioHost::LoadDriver(\"%s\"): GetBufferSize() failed\n", sDriver);
throw 4;
}
#if (defined(_DEBUG) || defined(_DEBUGFILE))
TRACE("CAsioHost::LoadDriver(\"%s\"): trying %d, minSize=%d maxSize=%d preferredSize=%d granularity=%d\n", sDriver, nBufSz, minSize, maxSize, preferredSize, granularity);
#endif
#if 1
// variant 1 - confine host-defined buffer size to driver range
/* assure it's in granularity range */
if (granularity == -1) /* power of 2? */
{
int nBit = -1, nMask = 0;
int nTestBit;
for (int i = 18; i >= 0; i--) /* find highest bit in buffer size */
{
nTestBit = 1 << i;
if ((nBit == -1) && (nBufSz & nTestBit))
nBit = nTestBit;
else if (nBit >= 0)
nMask |= nTestBit;
}
if (nBit < 0) /* buffer size 0??? */
nBufSz = 4; /* c'mon. Be serious, mate. */
else
{
nBufSz += (nBit - 1); /* make sure to use next HIGHER value*/
nBufSz &= ~nMask; /* then kill */
}
}
else if (granularity == 0) /* if FIXED to preferred size, */
nBufSz = preferredSize; /* use that */
else if (granularity > 1) /* if a granularity is to be forced, */
{
nBufSz += granularity - 1; /* make sure to use this granularity */
nBufSz -= (nBufSz % granularity);
}
if (nBufSz < minSize)
nBufSz = minSize;
else if (nBufSz > maxSize)
nBufSz = maxSize;
#else
// variant 2 - ignore host buffer size, use driver's preferences.
// not really brilliant, but works with less brilliantly written ASIO drivers.
nBufSz = preferredSize;
#endif
usedSize = nBufSz;
TRACE2("CAsioHost::LoadDriver(\"%s\"): using buffer size %d\n", sDriver, usedSize);
if (!GetSampleRate(&sampleRate))
{
TRACE1("CAsioHost::LoadDriver(\"%s\"): GetSampleRate() failed\n", sDriver);
throw 5;
}
if (sampleRate <= 0.0 || sampleRate > 96000.0)
{
// Driver does not store it's internal sample rate,
// so set it to a know one.
// Usually you should check beforehand, that the selected
// sample rate is valid with ASIOCanSampleRate().
if (!SetSampleRate(44100.0))
{
TRACE1("CAsioHost::LoadDriver(\"%s\"): SetSampleRate(default 44100) failed\n", sDriver);
throw 6;
}
if (!GetSampleRate(&sampleRate))
{
TRACE1("CAsioHost::LoadDriver(\"%s\"): GetSampleRate(after default 44100) failed\n", sDriver);
throw 7;
}
}
postOutput = OutputReady(); /* check whether driver wants Ready */
if (!CreateBuffers(nBufSz)) /* let driver create buffers */
{
TRACE2("CAsioHost::LoadDriver(\"%s\"): CreateBuffers(%d) failed\n", sDriver, nBufSz);
throw 8;
}
}
catch (...) /* upon any error */
{
removeCurrentDriver(); /* remove eventually loaded driver */
bLoaded = false; /* and remember we did so */
}
return bLoaded; /* return whether correctly loaded */
}
/*****************************************************************************/
/* UnloadDriver : unloads an eventually loaded ASIO driver */
/*****************************************************************************/
bool CAsioHost::UnloadDriver()
{
if (bLoaded) /* if driver is loaded */
{
Stop(); /* make sure we're stopped */
Exit(); /* remove the driver */
bLoaded = false; /* and remember we did so */
return true;
}
return false;
}
/*****************************************************************************/
/* CreateBuffers : allocates all necessary buffers */
/*****************************************************************************/
bool CAsioHost::CreateBuffers(int nBufSz)
{
if (bAllocated) /* eventually delete old ones */
{
DeleteBuffers();
bAllocated = false;
}
ASIOBufferInfo *info = bufferInfos;
long i;
ASIOError result;
// prepare inputs (Though this is not necessarily required,
// no opened inputs will work, too
inputBuffers = (inputChannels > kMaxInputChannels) ?
kMaxInputChannels : inputChannels;
for(i = 0; i < inputBuffers; i++, info++)
{
info->isInput = ASIOTrue;
info->channelNum = i;
info->buffers[0] = info->buffers[1] = 0;
}
// prepare outputs
outputBuffers = (outputChannels > kMaxOutputChannels) ?
kMaxOutputChannels : outputChannels;
for(i = 0; i < outputBuffers; i++, info++)
{
info->isInput = ASIOFalse;
info->channelNum = i;
info->buffers[0] = info->buffers[1] = 0;
}
// create and activate buffers
result = ASIOCreateBuffers(bufferInfos,
inputBuffers + outputBuffers,
nBufSz,
&asioCallbacks);
if (result == ASE_OK)
// now get all the buffer details, sample word length,
// name, word clock group and activation
for (i = 0; i < inputBuffers + outputBuffers; i++)
{
channelInfos[i].channel = bufferInfos[i].channelNum;
channelInfos[i].isInput = bufferInfos[i].isInput;
result = ASIOGetChannelInfo(&channelInfos[i]);
if (result != ASE_OK)
break;
}
if (result == ASE_OK)
// get the input and output latencies
// Latencies often are only valid after ASIOCreateBuffers()
// (input latency is the age of the first sample in the
// currently returned audio block)
// (output latency is the time the first sample in the
// currently returned audio block requires to get to the output)
return GetLatencies(&inputLatency, &outputLatency);
return (result == ASE_OK);
}
/*****************************************************************************/
/* DeleteBuffers : removes allocated buffers */
/*****************************************************************************/
bool CAsioHost::DeleteBuffers()
{
bool bRC = (ASIODisposeBuffers() == ASE_OK);
bAllocated = false;
return bRC;
}
/*****************************************************************************/
/* Init : initializes the driver I/O */
/*****************************************************************************/
bool CAsioHost::Init()
{
memset(&driverInfo, 0, sizeof(driverInfo));
driverInfo.asioVersion = 2;
driverInfo.sysRef = GetSysRef();
return (ASIOInit(&driverInfo) == ASE_OK);
}
/*****************************************************************************/
/* Start : starts the engine */
/*****************************************************************************/
bool CAsioHost::Start()
{
if (pHost && (pHost != this))
return false;
pHost = this;
return (ASIOStart() == ASE_OK);
}
/*****************************************************************************/
/* Stop : stops the engine */
/*****************************************************************************/
bool CAsioHost::Stop()
{
bool bRC = (ASIOStop() == ASE_OK);
pHost = 0;
return bRC;
}
/*****************************************************************************/
/* CanSampleRate : asks loaded driver for sample rate availability */
/*****************************************************************************/
bool CAsioHost::CanSampleRate(ASIOSampleRate sRate)
{
return (ASIOCanSampleRate(sampleRate) == ASE_OK);
}
/*****************************************************************************/
/* OnSampleRateChanged : called when the sampling rate changes */
/*****************************************************************************/
void CAsioHost::OnSampleRateChanged(ASIOSampleRate sRate)
{
sampleRate = sRate; /* remember new rate */
}
/*****************************************************************************/
/* OnMessage : called whenever an ASIO message comes in */
/*****************************************************************************/
long CAsioHost::OnMessage(long selector, long value, void* message, double* opt)
{
switch (selector)
{
case kAsioSelectorSupported :
return OnSelectorSupported(value);
case kAsioEngineVersion :
return OnEngineVersion();
case kAsioResetRequest :
return OnResetRequest();
case kAsioBufferSizeChange :
return OnBufferSizeChange(value);
case kAsioResyncRequest :
return OnResyncRequest();
case kAsioLatenciesChanged :
return OnLatenciesChanged();
case kAsioSupportsTimeInfo :
return OnSupportsTimeInfo();
case kAsioSupportsTimeCode :
return OnSupportsTimeCode();
}
return 0; // unknown message
}
/*****************************************************************************/
/* OnSelectorSupported : called to find out whether a selector is supported */
/*****************************************************************************/
long CAsioHost::OnSelectorSupported(long lSelector)
{
switch (lSelector)
{
case kAsioSelectorSupported :
case kAsioEngineVersion :
case kAsioResetRequest :
case kAsioBufferSizeChange :
case kAsioResyncRequest :
case kAsioLatenciesChanged :
case kAsioSupportsTimeInfo :
case kAsioSupportsTimeCode :
return 1L;
default :
return 0L;
}
}
/*****************************************************************************/
/* OnLatenciesChanged : called when driver latencies have changed */
/*****************************************************************************/
long CAsioHost::OnLatenciesChanged()
{
ASIOGetLatencies(&inputLatency, &outputLatency);
return 1L;
}
/*****************************************************************************/
/* OnSwitch : called when a buffer switch has happened */
/*****************************************************************************/
ASIOTime *CAsioHost::OnSwitch
(
ASIOTime *timeInfo, /* time info */
long index, /* buffer to fill now */
ASIOBool processNow /* process immediately or not */
)
{
// store the timeInfo for later use
tInfo = *timeInfo;
// get the time stamp of the buffer, not necessary if no
// synchronization to other media is required
if (timeInfo->timeInfo.flags & kSystemTimeValid)
nanoSeconds = ASIO64toDouble(timeInfo->timeInfo.systemTime);
else
nanoSeconds = 0;
if (timeInfo->timeInfo.flags & kSamplePositionValid)
samples = ASIO64toDouble(timeInfo->timeInfo.samplePosition);
else
samples = 0;
if (timeInfo->timeCode.flags & kTcValid)
tcSamples = ASIO64toDouble(timeInfo->timeCode.timeCodeSamples);
else
tcSamples = 0;
// get the system reference time
#if WINDOWS
sysRefTime = timeGetTime();
#elif MAC
static const double twoRaisedTo32 = 4294967296.;
UnsignedWide ys;
Microseconds(&ys);
double r = ((double)ys.hi * twoRaisedTo32 + (double)ys.lo);
sysRefTime = (unsigned long)(r / 1000.);
#endif
return timeInfo;
}
/*****************************************************************************/
/* ReverseEndian2 : reverts 2-byte entities in place */
/*****************************************************************************/
void CAsioHost::ReverseEndian2(void* buffer, long frames)
{
char *a = (char *)buffer;
char c;
while (--frames >= 0)
{
c = a[0];
a[0] = a[1];
a[1] = c;
a += 2;
}
}
/*****************************************************************************/
/* ReverseEndian3 : reverts 3-byte entities in place */
/*****************************************************************************/
void CAsioHost::ReverseEndian3(void* buffer, long frames)
{
char *a = (char *)buffer;
char c;
while (--frames >= 0)
{
c = a[0];
a[0] = a[2];
a[2] = c;
a += 3;
}
}
/*****************************************************************************/
/* ReverseEndian4 : reverts 4-byte entities in place */
/*****************************************************************************/
void CAsioHost::ReverseEndian4(void* buffer, long frames)
{
char *a = (char *)buffer;
char c;
while (--frames >= 0)
{
c = a[0];
a[0] = a[3];
a[3] = c;
c = a[1];
a[1] = a[2];
a[2] = c;
a += 4;
}
}
/*****************************************************************************/
/* ReverseEndian8 : reverts 8-byte entities in place */
/*****************************************************************************/
void CAsioHost::ReverseEndian8(void* buffer, long frames)
{
char *a = (char *)buffer;
char c;
while (--frames >= 0)
{
c = a[0];
a[0] = a[7];
a[7] = c;
c = a[1];
a[1] = a[6];
a[6] = c;
c = a[2];
a[2] = a[5];
a[5] = c;
c = a[3];
a[3] = a[4];
a[4] = c;
a += 8;
}
}
/*****************************************************************************/
/* ToFloat16 : converts 16-bit signed ints to float, simple method */
/*****************************************************************************/
void CAsioHost::ToFloat16(void *source, float *target, long frames)
{
// not extremely fast, but exact and portable
short *src = (short *)source;
while (--frames >= 0)
*target++ = ((*src++) + .5f) * (1.0f / 32767.503f);
}
/*****************************************************************************/
/* FromFloat16 : converts float to 16-bit signed ints, simple method */
/*****************************************************************************/
void CAsioHost::FromFloat16(float *source, void *target, long frames)
{
// not extremely fast, but exact and portable
short *dst = (short *)target;
float finter;
while (--frames >= 0)
{
finter = Saturate(*source++, 1.f);
*dst++ = (short)((finter * 32767.505f) - .5f);
}
}
/*****************************************************************************/
/* ToFloat24 : converts 24-bit signed ints to float, simple method */
/*****************************************************************************/
void CAsioHost::ToFloat24(void *source, float *target, long frames)
{
// not extremely fast, but exact and portable
union
{
long lValue;
char cValue[4];
} u;
char *src = (char *)source;
char *dst;
u.lValue = 0;
while (--frames >= 0)
{
#if ASIO_LITTLE_ENDIAN
dst = &u.cValue[1];
#else
dst = &u.cValue[0];
#endif
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
*target++ = ((u.lValue >> 8) + .5f) * (1.0f / 8388607.75f);
}
}
/*****************************************************************************/
/* FromFloat24 : converts float to 24-bit signed ints, simple method */
/*****************************************************************************/
void CAsioHost::FromFloat24(float *source, void *target, long frames)
{
// not extremely fast, but exact and portable
union
{
long lValue;
char cValue[4];
} u;
char *src;
char *dst = (char *)target;
float finter;
while (--frames >= 0)
{
finter = Saturate(*source++, 1.0f);
u.lValue = ((long)((finter * 8388607.75f) - .5f)) << 8;
#if ASIO_LITTLE_ENDIAN
src = &u.cValue[1];
#else
src = &u.cValue[0];
#endif
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
}
}
/*****************************************************************************/
/* ToFloat32 : converts 32-bit signed ints to float, simple method */
/*****************************************************************************/
void CAsioHost::ToFloat32
(
void *source,
float *target,
long relevantBits,
long frames
)
{
// not absolutely exact, not extremely fast... but portable
// the following logic is based on the layout of a 32bit IEEE floating
// point number which has 23 bits mantissa length and thus can't possibly
// store more than 24 relevant bits for numbers in the range -1..+1.
signed char nShift = (relevantBits - 24);
long *src = (long *)source;
long inter;
if (nShift < 0)
{
nShift = -nShift;
while (--frames >= 0)
{
inter = (*src++) << nShift;
*target++ = (inter + .5f) * (1.0f / 8388607.75f);
}
}
else
{
while (--frames >= 0)
{
inter = (*src++) >> nShift;
*target++ = (inter + .5f) * (1.0f / 8388607.75f);
}
}
}
/*****************************************************************************/
/* FromFloat32 : converts float to 32-bit signed ints, simple method */
/*****************************************************************************/
void CAsioHost::FromFloat32
(
float *source,
void *target,
long relevantBits,
long frames
)
{
// not absolutely exact, not extremely fast... but portable
// the following logic is based on the layout of a 32bit IEEE floating
// point number which has 23 bits mantissa length and thus can't possibly
// store more than 24 relevant bits for numbers in the range -1..+1.
signed char nShift = (relevantBits - 24);
long *dst = (long *)target;
float finter;
if (nShift < 0)
{
nShift = -nShift;
while (--frames >= 0)
{
finter = Saturate(*source++, 1.f);
*dst++ = ((long)((finter * 8388607.75f) - .5f)) >> nShift;
}
}
else
{
while (--frames >= 0)
{
finter = Saturate(*source++, 1.f);
*dst++ = ((long)((finter * 8388607.75f) - .5f)) << nShift;
}
}
}
/*****************************************************************************/
/* ToFloat64 : converts 64bit floats to 32bit float, simple method */
/*****************************************************************************/
void CAsioHost::ToFloat64(void *source, float *target, long frames)
{
// not absolutely exact, not extremely fast... but portable
double *src = (double *)source;
while (--frames >= 0)
*target++ = (float)(*src++);
}
/*****************************************************************************/
/* FromFloat64 : converts float to 64bit float, simple method */
/*****************************************************************************/
void CAsioHost::FromFloat64(float *source, void *target, long frames)
{
// not absolutely exact, not extremely fast... but portable
double *dst = (double *)target;
while (--frames >= 0)
*dst++ = (double)Saturate(*source++, 1.f);
}