-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathsound.cpp
executable file
·1215 lines (1039 loc) · 46.8 KB
/
sound.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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************\
* Copyright (c) 2004-2020
*
* Author(s):
* Volker Fischer
*
* Description:
* Sound card interface for Windows operating systems
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
\******************************************************************************/
#include "sound.h"
/* Implementation *************************************************************/
// external references
extern AsioDrivers* asioDrivers;
bool loadAsioDriver ( char* name );
// pointer to our sound object
CSound* pSound;
/******************************************************************************\
* Common *
\******************************************************************************/
QString CSound::LoadAndInitializeDriver ( QString strDriverName,
bool bOpenDriverSetup )
{
// find and load driver
int iDriverIdx = INVALID_INDEX; // initialize with an invalid index
for ( int i = 0; i < MAX_NUMBER_SOUND_CARDS; i++ )
{
if ( strDriverName.compare ( cDriverNames[i] ) == 0 )
{
iDriverIdx = i;
}
}
// if the selected driver was not found, return an error message
if ( iDriverIdx == INVALID_INDEX )
{
return tr ( "The current selected audio device is no longer present in the system." );
}
loadAsioDriver ( cDriverNames[iDriverIdx] );
if ( ASIOInit ( &driverInfo ) != ASE_OK )
{
// clean up and return error string
asioDrivers->removeCurrentDriver();
return tr ( "The audio driver could not be initialized." );
}
// check device capabilities if it fulfills our requirements
const QString strStat = CheckDeviceCapabilities();
// check if device is capable
if ( strStat.isEmpty() )
{
// only reset the channel mapping if a new device was selected
if ( strCurDevName.compare ( strDriverNames[iDriverIdx] ) != 0 )
{
// the device has changed, per definition we reset the channel
// mapping to the defaults (first two available channels)
ResetChannelMapping();
// store ID of selected driver if initialization was successful
strCurDevName = cDriverNames[iDriverIdx];
}
}
else
{
// if requested, open ASIO driver setup in case of an error
if ( bOpenDriverSetup )
{
OpenDriverSetup();
QMessageBox::question ( nullptr, APP_NAME, "Are you done with your ASIO driver settings of device " + GetDeviceName ( iDriverIdx ) + "?", QMessageBox::Yes );
}
// driver cannot be used, clean up
asioDrivers->removeCurrentDriver();
}
return strStat;
}
void CSound::UnloadCurrentDriver()
{
// clean up ASIO stuff
ASIOStop();
ASIODisposeBuffers();
ASIOExit();
asioDrivers->removeCurrentDriver();
}
QString CSound::CheckDeviceCapabilities()
{
// This function checks if our required input/output channel
// properties are supported by the selected device. If the return
// string is empty, the device can be used, otherwise the error
// message is returned.
// check the sample rate
const ASIOError CanSaRateReturn = ASIOCanSampleRate ( SYSTEM_SAMPLE_RATE_HZ );
if ( ( CanSaRateReturn == ASE_NoClock ) ||
( CanSaRateReturn == ASE_NotPresent ) )
{
// return error string
return tr ( "The audio device does not support the "
"required sample rate. The required sample rate is: " ) +
QString().setNum ( SYSTEM_SAMPLE_RATE_HZ ) + " Hz";
}
// check if sample rate can be set
const ASIOError SetSaRateReturn = ASIOSetSampleRate ( SYSTEM_SAMPLE_RATE_HZ );
if ( ( SetSaRateReturn == ASE_NoClock ) ||
( SetSaRateReturn == ASE_InvalidMode ) ||
( SetSaRateReturn == ASE_NotPresent ) )
{
// return error string
return tr ( "The audio device does not support setting the required sampling "
"rate. This error can happen if you have an audio interface like the "
"Roland UA-25EX where you set the sample rate with a hardware switch "
"on the audio device. If this is the case, please change the sample rate "
"to " ) + QString().setNum ( SYSTEM_SAMPLE_RATE_HZ ) + tr ( " Hz on the "
"device and restart the " ) + APP_NAME + tr ( " software." );
}
// check the number of available channels
ASIOGetChannels ( &lNumInChan, &lNumOutChan );
if ( ( lNumInChan < NUM_IN_OUT_CHANNELS ) ||
( lNumOutChan < NUM_IN_OUT_CHANNELS ) )
{
// return error string
return tr ( "The audio device does not support the "
"required number of channels. The required number of channels "
"for input and output is: " ) +
QString().setNum ( NUM_IN_OUT_CHANNELS );
}
// clip number of input/output channels to our maximum
if ( lNumInChan > MAX_NUM_IN_OUT_CHANNELS )
{
lNumInChan = MAX_NUM_IN_OUT_CHANNELS;
}
if ( lNumOutChan > MAX_NUM_IN_OUT_CHANNELS )
{
lNumOutChan = MAX_NUM_IN_OUT_CHANNELS;
}
// query channel infos for all available input channels
bool bInputChMixingSupported = true;
for ( int i = 0; i < lNumInChan; i++ )
{
// setup for input channels
channelInfosInput[i].isInput = ASIOTrue;
channelInfosInput[i].channel = i;
ASIOGetChannelInfo ( &channelInfosInput[i] );
// Check supported sample formats.
// Actually, it would be enough to have at least two channels which
// support the required sample format. But since we have support for
// all known sample types, the following check should always pass and
// therefore we throw the error message on any channel which does not
// fulfill the sample format requirement (quick hack solution).
if ( !CheckSampleTypeSupported ( channelInfosInput[i].type ) )
{
// return error string
return tr ( "Required audio sample format not available." );
}
// store the name of the channel and check if channel mixing is supported
channelInputName[i] = channelInfosInput[i].name;
if ( !CheckSampleTypeSupportedForCHMixing ( channelInfosInput[i].type ) )
{
bInputChMixingSupported = false;
}
}
// query channel infos for all available output channels
for ( int i = 0; i < lNumOutChan; i++ )
{
// setup for output channels
channelInfosOutput[i].isInput = ASIOFalse;
channelInfosOutput[i].channel = i;
ASIOGetChannelInfo ( &channelInfosOutput[i] );
// Check supported sample formats.
// Actually, it would be enough to have at least two channels which
// support the required sample format. But since we have support for
// all known sample types, the following check should always pass and
// therefore we throw the error message on any channel which does not
// fulfill the sample format requirement (quick hack solution).
if ( !CheckSampleTypeSupported ( channelInfosOutput[i].type ) )
{
// return error string
return tr ( "Required audio sample format not available." );
}
}
// special case with 4 input channels: support adding channels
if ( ( lNumInChan == 4 ) && bInputChMixingSupported )
{
// add four mixed channels (i.e. 4 normal, 4 mixed channels)
lNumInChanPlusAddChan = 8;
for ( int iCh = 0; iCh < lNumInChanPlusAddChan; iCh++ )
{
int iSelCH, iSelAddCH;
GetSelCHAndAddCH ( iCh, lNumInChan, iSelCH, iSelAddCH );
if ( iSelAddCH >= 0 )
{
// for mixed channels, show both audio channel names to be mixed
channelInputName[iCh] =
channelInputName[iSelCH] + " + " + channelInputName[iSelAddCH];
}
}
}
else
{
// regular case: no mixing input channels used
lNumInChanPlusAddChan = lNumInChan;
}
// everything is ok, return empty string for "no error" case
return "";
}
void CSound::SetLeftInputChannel ( const int iNewChan )
{
// apply parameter after input parameter check
if ( ( iNewChan >= 0 ) && ( iNewChan < lNumInChanPlusAddChan ) )
{
vSelectedInputChannels[0] = iNewChan;
}
}
void CSound::SetRightInputChannel ( const int iNewChan )
{
// apply parameter after input parameter check
if ( ( iNewChan >= 0 ) && ( iNewChan < lNumInChanPlusAddChan ) )
{
vSelectedInputChannels[1] = iNewChan;
}
}
void CSound::SetLeftOutputChannel ( const int iNewChan )
{
// apply parameter after input parameter check
if ( ( iNewChan >= 0 ) && ( iNewChan < lNumOutChan ) )
{
vSelectedOutputChannels[0] = iNewChan;
}
}
void CSound::SetRightOutputChannel ( const int iNewChan )
{
// apply parameter after input parameter check
if ( ( iNewChan >= 0 ) && ( iNewChan < lNumOutChan ) )
{
vSelectedOutputChannels[1] = iNewChan;
}
}
int CSound::GetActualBufferSize ( const int iDesiredBufferSizeMono )
{
int iActualBufferSizeMono;
// query the usable buffer sizes
ASIOGetBufferSize ( &HWBufferInfo.lMinSize,
&HWBufferInfo.lMaxSize,
&HWBufferInfo.lPreferredSize,
&HWBufferInfo.lGranularity );
/*
// TEST
#include <QMessageBox>
QMessageBox::information ( 0, "APP_NAME", QString("lMinSize: %1, lMaxSize: %2, lPreferredSize: %3, lGranularity: %4").
arg(HWBufferInfo.lMinSize).arg(HWBufferInfo.lMaxSize).arg(HWBufferInfo.lPreferredSize).arg(HWBufferInfo.lGranularity) );
_exit(1);
*/
// TODO see https://github.com/EddieRingle/portaudio/blob/master/src/hostapi/asio/pa_asio.cpp#L1654 (SelectHostBufferSizeForUnspecifiedUserFramesPerBuffer)
// calculate "nearest" buffer size and set internal parameter accordingly
// first check minimum and maximum values
if ( iDesiredBufferSizeMono <= HWBufferInfo.lMinSize )
{
iActualBufferSizeMono = HWBufferInfo.lMinSize;
}
else
{
if ( iDesiredBufferSizeMono >= HWBufferInfo.lMaxSize )
{
iActualBufferSizeMono = HWBufferInfo.lMaxSize;
}
else
{
// ASIO SDK 2.2: "Notes: When minimum and maximum buffer size are
// equal, the preferred buffer size has to be the same value as
// well; granularity should be 0 in this case."
if ( HWBufferInfo.lMinSize == HWBufferInfo.lMaxSize )
{
iActualBufferSizeMono = HWBufferInfo.lMinSize;
}
else
{
if ( ( HWBufferInfo.lGranularity < -1 ) ||
( HWBufferInfo.lGranularity == 0 ) )
{
// Special case (seen for EMU audio cards): granularity is
// zero or less than zero (make sure to exclude the special
// case of -1).
// There is no definition of this case in the ASIO SDK
// document. We assume here that all buffer sizes in between
// minimum and maximum buffer sizes are allowed.
iActualBufferSizeMono = iDesiredBufferSizeMono;
}
else
{
// General case --------------------------------------------
// initialization
int iTrialBufSize = HWBufferInfo.lMinSize;
int iLastTrialBufSize = HWBufferInfo.lMinSize;
bool bSizeFound = false;
// test loop
while ( ( iTrialBufSize <= HWBufferInfo.lMaxSize ) && ( !bSizeFound ) )
{
if ( iTrialBufSize >= iDesiredBufferSizeMono )
{
// test which buffer size fits better: the old one or the
// current one
if ( ( iTrialBufSize - iDesiredBufferSizeMono ) >
( iDesiredBufferSizeMono - iLastTrialBufSize ) )
{
iTrialBufSize = iLastTrialBufSize;
}
// exit while loop
bSizeFound = true;
}
if ( !bSizeFound )
{
// store old trial buffer size
iLastTrialBufSize = iTrialBufSize;
// increment trial buffer size (check for special
// case first)
if ( HWBufferInfo.lGranularity == -1 )
{
// special case: buffer sizes are a power of 2
iTrialBufSize *= 2;
}
else
{
iTrialBufSize += HWBufferInfo.lGranularity;
}
}
}
// clip trial buffer size (it may happen in the while
// routine that "iTrialBufSize" is larger than "lMaxSize" in
// case "lMaxSize - lMinSize" is not divisible by the
// granularity)
if ( iTrialBufSize > HWBufferInfo.lMaxSize )
{
iTrialBufSize = HWBufferInfo.lMaxSize;
}
// set ASIO buffer size
iActualBufferSizeMono = iTrialBufSize;
}
}
}
}
return iActualBufferSizeMono;
}
int CSound::Init ( const int iNewPrefMonoBufferSize )
{
ASIOMutex.lock(); // get mutex lock
{
// get the actual sound card buffer size which is supported
// by the audio hardware
iASIOBufferSizeMono = GetActualBufferSize ( iNewPrefMonoBufferSize );
// init base class
CSoundBase::Init ( iASIOBufferSizeMono );
// set internal buffer size value and calculate stereo buffer size
iASIOBufferSizeStereo = 2 * iASIOBufferSizeMono;
// set the sample rate
ASIOSetSampleRate ( SYSTEM_SAMPLE_RATE_HZ );
// create memory for intermediate audio buffer
vecsMultChanAudioSndCrd.Init ( iASIOBufferSizeStereo );
// create and activate ASIO buffers (buffer size in samples),
// dispose old buffers (if any)
ASIODisposeBuffers();
// prepare input channels
for ( int i = 0; i < lNumInChan; i++ )
{
bufferInfos[i].isInput = ASIOTrue;
bufferInfos[i].channelNum = i;
bufferInfos[i].buffers[0] = 0;
bufferInfos[i].buffers[1] = 0;
}
// prepare output channels
for ( int i = 0; i < lNumOutChan; i++ )
{
bufferInfos[lNumInChan + i].isInput = ASIOFalse;
bufferInfos[lNumInChan + i].channelNum = i;
bufferInfos[lNumInChan + i].buffers[0] = 0;
bufferInfos[lNumInChan + i].buffers[1] = 0;
}
ASIOCreateBuffers ( bufferInfos, lNumInChan + lNumOutChan,
iASIOBufferSizeMono, &asioCallbacks );
// query the latency of the driver
long lInputLatency = 0;
long lOutputLatency = 0;
if ( ASIOGetLatencies ( &lInputLatency, &lOutputLatency ) != ASE_NotPresent )
{
// add the input and output latencies (returned in number of
// samples) and calculate the time in ms
fInOutLatencyMs =
( static_cast<float> ( lInputLatency ) + lOutputLatency ) *
1000 / SYSTEM_SAMPLE_RATE_HZ;
}
else
{
// no latency available
fInOutLatencyMs = 0.0f;
}
// check whether the driver requires the ASIOOutputReady() optimization
// (can be used by the driver to reduce output latency by one block)
bASIOPostOutput = ( ASIOOutputReady() == ASE_OK );
}
ASIOMutex.unlock();
return iASIOBufferSizeMono;
}
void CSound::Start()
{
// start audio
ASIOStart();
// call base class
CSoundBase::Start();
}
void CSound::Stop()
{
// stop audio
ASIOStop();
// call base class
CSoundBase::Stop();
// make sure the working thread is actually done
// (by checking the locked state)
if ( ASIOMutex.tryLock ( 5000 ) )
{
ASIOMutex.unlock();
}
}
CSound::CSound ( void (*fpNewCallback) ( CVector<int16_t>& psData, void* arg ),
void* arg,
const QString& strMIDISetup,
const bool ,
const QString& ) :
CSoundBase ( "ASIO", fpNewCallback, arg, strMIDISetup ),
lNumInChan ( 0 ),
lNumInChanPlusAddChan ( 0 ),
lNumOutChan ( 0 ),
fInOutLatencyMs ( 0.0f ), // "0.0" means that no latency value is available
vSelectedInputChannels ( NUM_IN_OUT_CHANNELS ),
vSelectedOutputChannels ( NUM_IN_OUT_CHANNELS )
{
int i;
// init pointer to our sound object
pSound = this;
// get available ASIO driver names in system
for ( i = 0; i < MAX_NUMBER_SOUND_CARDS; i++ )
{
// allocate memory for driver names
cDriverNames[i] = new char[32];
}
char cDummyName[] = "dummy";
loadAsioDriver ( cDummyName ); // to initialize external object
lNumDevs = asioDrivers->getDriverNames ( cDriverNames, MAX_NUMBER_SOUND_CARDS );
// in case we do not have a driver available, throw error
if ( lNumDevs == 0 )
{
throw CGenErr ( "<b>" + tr ( "No ASIO audio device (driver) found." ) + "</b><br><br>" +
tr ( "The " ) + APP_NAME + tr ( " software requires the low latency audio "
"interface ASIO to work properly. This is not a standard "
"Windows audio interface and therefore a special audio driver is "
"required. Either your sound card has a native ASIO driver (which "
"is recommended) or you might want to use alternative drivers like "
"the ASIO4All driver." ) );
}
asioDrivers->removeCurrentDriver();
// copy driver names to base class but internally we still have to use
// the char* variable because of the ASIO API :-(
for ( i = 0; i < lNumDevs; i++ )
{
strDriverNames[i] = cDriverNames[i];
}
// init device index as not initialized (invalid)
strCurDevName = "";
// init channel mapping
ResetChannelMapping();
// set up the asioCallback structure
asioCallbacks.bufferSwitch = &bufferSwitch;
asioCallbacks.sampleRateDidChange = &sampleRateChanged;
asioCallbacks.asioMessage = &asioMessages;
asioCallbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfo;
}
void CSound::ResetChannelMapping()
{
// init selected channel numbers with defaults: use first available
// channels for input and output
vSelectedInputChannels[0] = 0;
vSelectedInputChannels[1] = 1;
vSelectedOutputChannels[0] = 0;
vSelectedOutputChannels[1] = 1;
}
// ASIO callbacks -------------------------------------------------------------
ASIOTime* CSound::bufferSwitchTimeInfo ( ASIOTime*,
long index,
ASIOBool processNow )
{
bufferSwitch ( index, processNow );
return 0L;
}
bool CSound::CheckSampleTypeSupported ( const ASIOSampleType SamType )
{
// check for supported sample types
return ( ( SamType == ASIOSTInt16LSB ) ||
( SamType == ASIOSTInt24LSB ) ||
( SamType == ASIOSTInt32LSB ) ||
( SamType == ASIOSTFloat32LSB ) ||
( SamType == ASIOSTFloat64LSB ) ||
( SamType == ASIOSTInt32LSB16 ) ||
( SamType == ASIOSTInt32LSB18 ) ||
( SamType == ASIOSTInt32LSB20 ) ||
( SamType == ASIOSTInt32LSB24 ) ||
( SamType == ASIOSTInt16MSB ) ||
( SamType == ASIOSTInt24MSB ) ||
( SamType == ASIOSTInt32MSB ) ||
( SamType == ASIOSTFloat32MSB ) ||
( SamType == ASIOSTFloat64MSB ) ||
( SamType == ASIOSTInt32MSB16 ) ||
( SamType == ASIOSTInt32MSB18 ) ||
( SamType == ASIOSTInt32MSB20 ) ||
( SamType == ASIOSTInt32MSB24 ) );
}
bool CSound::CheckSampleTypeSupportedForCHMixing ( const ASIOSampleType SamType )
{
// check for supported sample types for audio channel mixing (see bufferSwitch)
return ( ( SamType == ASIOSTInt16LSB ) ||
( SamType == ASIOSTInt24LSB ) ||
( SamType == ASIOSTInt32LSB ) );
}
void CSound::bufferSwitch ( long index, ASIOBool )
{
int iCurSample;
// get references to class members
int& iASIOBufferSizeMono = pSound->iASIOBufferSizeMono;
CVector<int16_t>& vecsMultChanAudioSndCrd = pSound->vecsMultChanAudioSndCrd;
// perform the processing for input and output
pSound->ASIOMutex.lock(); // get mutex lock
{
// CAPTURE -------------------------------------------------------------
for ( int i = 0; i < NUM_IN_OUT_CHANNELS; i++ )
{
int iSelCH, iSelAddCH;
GetSelCHAndAddCH ( pSound->vSelectedInputChannels[i], pSound->lNumInChan,
iSelCH, iSelAddCH );
// copy new captured block in thread transfer buffer (copy
// mono data interleaved in stereo buffer)
switch ( pSound->channelInfosInput[iSelCH].type )
{
case ASIOSTInt16LSB:
{
// no type conversion required, just copy operation
int16_t* pASIOBuf = static_cast<int16_t*> ( pSound->bufferInfos[iSelCH].buffers[index] );
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] = pASIOBuf[iCurSample];
}
if ( iSelAddCH >= 0 )
{
// mix input channels case:
int16_t* pASIOBufAdd = static_cast<int16_t*> ( pSound->bufferInfos[iSelAddCH].buffers[index] );
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
Float2Short ( (float) vecsMultChanAudioSndCrd[2 * iCurSample + i] +
(float) pASIOBufAdd[iCurSample] );
}
}
break;
}
case ASIOSTInt24LSB:
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
int iCurSam = 0;
memcpy ( &iCurSam, ( (char*) pSound->bufferInfos[iSelCH].buffers[index] ) + iCurSample * 3, 3 );
iCurSam >>= 8;
vecsMultChanAudioSndCrd[2 * iCurSample + i] = static_cast<int16_t> ( iCurSam );
}
if ( iSelAddCH >= 0 )
{
// mix input channels case:
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
int iCurSam = 0;
memcpy ( &iCurSam, ( (char*) pSound->bufferInfos[iSelAddCH].buffers[index] ) + iCurSample * 3, 3 );
iCurSam >>= 8;
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
Float2Short ( (float) vecsMultChanAudioSndCrd[2 * iCurSample + i] +
(float) static_cast<int16_t> ( iCurSam ) );
}
}
break;
case ASIOSTInt32LSB:
{
int32_t* pASIOBuf = static_cast<int32_t*> ( pSound->bufferInfos[iSelCH].buffers[index] );
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( pASIOBuf[iCurSample] >> 16 );
}
if ( iSelAddCH >= 0 )
{
// mix input channels case:
int32_t* pASIOBufAdd = static_cast<int32_t*> ( pSound->bufferInfos[iSelAddCH].buffers[index] );
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
Float2Short ( (float) vecsMultChanAudioSndCrd[2 * iCurSample + i] +
(float) static_cast<int16_t> ( pASIOBufAdd[iCurSample] >> 16 ) );
}
}
break;
}
case ASIOSTFloat32LSB: // IEEE 754 32 bit float, as found on Intel x86 architecture
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( static_cast<float*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] * _MAXSHORT );
}
break;
case ASIOSTFloat64LSB: // IEEE 754 64 bit double float, as found on Intel x86 architecture
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( static_cast<double*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] * _MAXSHORT );
}
break;
case ASIOSTInt32LSB16: // 32 bit data with 16 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] & 0xFFFF );
}
break;
case ASIOSTInt32LSB18: // 32 bit data with 18 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] & 0x3FFFF ) >> 2 );
}
break;
case ASIOSTInt32LSB20: // 32 bit data with 20 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] & 0xFFFFF ) >> 4 );
}
break;
case ASIOSTInt32LSB24: // 32 bit data with 24 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] & 0xFFFFFF ) >> 8 );
}
break;
case ASIOSTInt16MSB:
// NOT YET TESTED
// flip bits
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
Flip16Bits ( ( static_cast<int16_t*> (
pSound->bufferInfos[iSelCH].buffers[index] ) )[iCurSample] );
}
break;
case ASIOSTInt24MSB:
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// because the bits are flipped, we do not have to perform the
// shift by 8 bits
int iCurSam = 0;
memcpy ( &iCurSam, ( (char*) pSound->bufferInfos[iSelCH].buffers[index] ) + iCurSample * 3, 3 );
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
Flip16Bits ( static_cast<int16_t> ( iCurSam ) );
}
break;
case ASIOSTInt32MSB:
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// flip bits and convert to 16 bit
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( Flip32Bits ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] ) >> 16 );
}
break;
case ASIOSTFloat32MSB: // IEEE 754 32 bit float, as found on Intel x86 architecture
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( static_cast<float> (
Flip32Bits ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] ) ) * _MAXSHORT );
}
break;
case ASIOSTFloat64MSB: // IEEE 754 64 bit double float, as found on Intel x86 architecture
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( static_cast<double> (
Flip64Bits ( static_cast<int64_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] ) ) * _MAXSHORT );
}
break;
case ASIOSTInt32MSB16: // 32 bit data with 16 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( Flip32Bits ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] ) & 0xFFFF );
}
break;
case ASIOSTInt32MSB18: // 32 bit data with 18 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( ( Flip32Bits ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] ) & 0x3FFFF ) >> 2 );
}
break;
case ASIOSTInt32MSB20: // 32 bit data with 20 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( ( Flip32Bits ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] ) & 0xFFFFF ) >> 4 );
}
break;
case ASIOSTInt32MSB24: // 32 bit data with 24 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
vecsMultChanAudioSndCrd[2 * iCurSample + i] =
static_cast<int16_t> ( ( Flip32Bits ( static_cast<int32_t*> (
pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] ) & 0xFFFFFF ) >> 8 );
}
break;
}
}
// call processing callback function
pSound->ProcessCallback ( vecsMultChanAudioSndCrd );
// PLAYBACK ------------------------------------------------------------
for ( int i = 0; i < NUM_IN_OUT_CHANNELS; i++ )
{
const int iSelCH = pSound->lNumInChan + pSound->vSelectedOutputChannels[i];
// copy data from sound card in output buffer (copy
// interleaved stereo data in mono sound card buffer)
switch ( pSound->channelInfosOutput[pSound->vSelectedOutputChannels[i]].type )
{
case ASIOSTInt16LSB:
{
// no type conversion required, just copy operation
int16_t* pASIOBuf = static_cast<int16_t*> ( pSound->bufferInfos[iSelCH].buffers[index] );
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
pASIOBuf[iCurSample] = vecsMultChanAudioSndCrd[2 * iCurSample + i];
}
break;
}
case ASIOSTInt24LSB:
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// convert current sample in 24 bit format
int32_t iCurSam = static_cast<int32_t> (
vecsMultChanAudioSndCrd[2 * iCurSample + i] );
iCurSam <<= 8;
memcpy ( ( (char*) pSound->bufferInfos[iSelCH].buffers[index] ) + iCurSample * 3, &iCurSam, 3 );
}
break;
case ASIOSTInt32LSB:
{
int32_t* pASIOBuf = static_cast<int32_t*> ( pSound->bufferInfos[iSelCH].buffers[index] );
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// convert to 32 bit
const int32_t iCurSam = static_cast<int32_t> (
vecsMultChanAudioSndCrd[2 * iCurSample + i] );
pASIOBuf[iCurSample] = ( iCurSam << 16 );
}
break;
}
case ASIOSTFloat32LSB: // IEEE 754 32 bit float, as found on Intel x86 architecture
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
const float fCurSam = static_cast<float> (
vecsMultChanAudioSndCrd[2 * iCurSample + i] );
static_cast<float*> ( pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] =
fCurSam / _MAXSHORT;
}
break;
case ASIOSTFloat64LSB: // IEEE 754 64 bit double float, as found on Intel x86 architecture
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
const double fCurSam = static_cast<double> (
vecsMultChanAudioSndCrd[2 * iCurSample + i] );
static_cast<double*> ( pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] =
fCurSam / _MAXSHORT;
}
break;
case ASIOSTInt32LSB16: // 32 bit data with 16 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// convert to 32 bit
const int32_t iCurSam = static_cast<int32_t> (
vecsMultChanAudioSndCrd[2 * iCurSample + i] );
static_cast<int32_t*> ( pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] =
iCurSam;
}
break;
case ASIOSTInt32LSB18: // 32 bit data with 18 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// convert to 32 bit
const int32_t iCurSam = static_cast<int32_t> (
vecsMultChanAudioSndCrd[2 * iCurSample + i] );
static_cast<int32_t*> ( pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] =
( iCurSam << 2 );
}
break;
case ASIOSTInt32LSB20: // 32 bit data with 20 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// convert to 32 bit
const int32_t iCurSam = static_cast<int32_t> (
vecsMultChanAudioSndCrd[2 * iCurSample + i] );
static_cast<int32_t*> ( pSound->bufferInfos[iSelCH].buffers[index] )[iCurSample] =
( iCurSam << 4 );
}
break;
case ASIOSTInt32LSB24: // 32 bit data with 24 bit alignment
// NOT YET TESTED
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
{
// convert to 32 bit
const int32_t iCurSam = static_cast<int32_t> (