[6d9958]: portaudio / src / common / pa_front.c Maximize Restore History

Download this file

pa_front.c    1766 lines (1374 with data), 54.7 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
/*
* $Id: pa_front.c 1396 2008-11-03 19:31:30Z philburk $
* Portable Audio I/O Library Multi-Host API front end
* Validate function parameters and manage multiple host APIs.
*
* Based on the Open Source API proposed by Ross Bencina
* Copyright (c) 1999-2008 Ross Bencina, Phil Burk
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
/** @file
@ingroup common_src
@brief Implements PortAudio API functions defined in portaudio.h, checks
some errors, delegates platform-specific behavior to host API implementations.
Implements the functions defined in the PortAudio API (portaudio.h),
validates some parameters and checks for state inconsistencies before
forwarding API requests to specific Host API implementations (via the
interface declared in pa_hostapi.h), and Streams (via the interface
declared in pa_stream.h).
This file manages initialization and termination of Host API
implementations via initializer functions stored in the paHostApiInitializers
global array (usually defined in an os-specific pa_[os]_hostapis.c file).
This file maintains a list of all open streams and closes them at Pa_Terminate().
Some utility functions declared in pa_util.h are implemented in this file.
All PortAudio API functions can be conditionally compiled with logging code.
To compile with logging, define the PA_LOG_API_CALLS precompiler symbol.
@todo Consider adding host API specific error text in Pa_GetErrorText() for
paUnanticipatedHostError
@todo Consider adding a new error code for when (inputParameters == NULL)
&& (outputParameters == NULL)
@todo review whether Pa_CloseStream() should call the interface's
CloseStream function if aborting the stream returns an error code.
@todo Create new error codes if a NULL buffer pointer, or a
zero frame count is passed to Pa_ReadStream or Pa_WriteStream.
*/
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <assert.h> /* needed by PA_VALIDATE_ENDIANNESS */
#include "portaudio.h"
#include "pa_util.h"
#include "pa_endianness.h"
#include "pa_types.h"
#include "pa_hostapi.h"
#include "pa_stream.h"
#include "pa_trace.h" /* still usefull?*/
#include "pa_debugprint.h"
#define PA_VERSION_ 1899
#define PA_VERSION_TEXT_ "PortAudio V19-devel (built " __DATE__ " " __TIME__ ")"
int Pa_GetVersion( void )
{
return PA_VERSION_;
}
const char* Pa_GetVersionText( void )
{
return PA_VERSION_TEXT_;
}
#define PA_LAST_HOST_ERROR_TEXT_LENGTH_ 1024
static char lastHostErrorText_[ PA_LAST_HOST_ERROR_TEXT_LENGTH_ + 1 ] = {0};
static PaHostErrorInfo lastHostErrorInfo_ = { (PaHostApiTypeId)-1, 0, lastHostErrorText_ };
void PaUtil_SetLastHostErrorInfo( PaHostApiTypeId hostApiType, long errorCode,
const char *errorText )
{
lastHostErrorInfo_.hostApiType = hostApiType;
lastHostErrorInfo_.errorCode = errorCode;
strncpy( lastHostErrorText_, errorText, PA_LAST_HOST_ERROR_TEXT_LENGTH_ );
}
static PaUtilHostApiRepresentation **hostApis_ = 0;
static int hostApisCount_ = 0;
static int initializationCount_ = 0;
static int deviceCount_ = 0;
PaUtilStreamRepresentation *firstOpenStream_ = NULL;
#define PA_IS_INITIALISED_ (initializationCount_ != 0)
static int CountHostApiInitializers( void )
{
int result = 0;
while( paHostApiInitializers[ result ] != 0 )
++result;
return result;
}
static void TerminateHostApis( void )
{
/* terminate in reverse order from initialization */
PA_DEBUG(("TerminateHostApis in \n"));
while( hostApisCount_ > 0 )
{
--hostApisCount_;
hostApis_[hostApisCount_]->Terminate( hostApis_[hostApisCount_] );
}
hostApisCount_ = 0;
deviceCount_ = 0;
if( hostApis_ != 0 )
PaUtil_FreeMemory( hostApis_ );
hostApis_ = 0;
PA_DEBUG(("TerminateHostApis out\n"));
}
static PaError InitializeHostApis( void )
{
PaError result = paNoError;
int i, initializerCount, baseDeviceIndex;
initializerCount = CountHostApiInitializers();
hostApis_ = (PaUtilHostApiRepresentation**)PaUtil_AllocateMemory(
sizeof(PaUtilHostApiRepresentation*) * initializerCount );
if( !hostApis_ )
{
result = paInsufficientMemory;
goto error;
}
hostApisCount_ = 0;
deviceCount_ = 0;
baseDeviceIndex = 0;
for( i=0; i< initializerCount; ++i )
{
hostApis_[hostApisCount_] = NULL;
PA_DEBUG(( "before paHostApiInitializers[%d].\n",i));
result = paHostApiInitializers[i]( &hostApis_[hostApisCount_], hostApisCount_ );
if( result != paNoError )
goto error;
PA_DEBUG(( "after paHostApiInitializers[%d].\n",i));
if( hostApis_[hostApisCount_] )
{
PaUtilHostApiRepresentation* hostApi = hostApis_[hostApisCount_];
assert( hostApi->info.defaultInputDevice < hostApi->info.deviceCount );
assert( hostApi->info.defaultOutputDevice < hostApi->info.deviceCount );
hostApi->privatePaFrontInfo.baseDeviceIndex = baseDeviceIndex;
if( hostApi->info.defaultInputDevice != paNoDevice )
hostApi->info.defaultInputDevice += baseDeviceIndex;
if( hostApi->info.defaultOutputDevice != paNoDevice )
hostApi->info.defaultOutputDevice += baseDeviceIndex;
baseDeviceIndex += hostApi->info.deviceCount;
deviceCount_ += hostApi->info.deviceCount;
++hostApisCount_;
}
}
return result;
error:
TerminateHostApis();
return result;
}
/*
FindHostApi() finds the index of the host api to which
<device> belongs and returns it. if <hostSpecificDeviceIndex> is
non-null, the host specific device index is returned in it.
returns -1 if <device> is out of range.
*/
static int FindHostApi( PaDeviceIndex device, int *hostSpecificDeviceIndex )
{
int i=0;
if( !PA_IS_INITIALISED_ )
return -1;
if( device < 0 )
return -1;
while( i < hostApisCount_
&& device >= hostApis_[i]->info.deviceCount )
{
device -= hostApis_[i]->info.deviceCount;
++i;
}
if( i >= hostApisCount_ )
return -1;
if( hostSpecificDeviceIndex )
*hostSpecificDeviceIndex = device;
return i;
}
static void AddOpenStream( PaStream* stream )
{
((PaUtilStreamRepresentation*)stream)->nextOpenStream = firstOpenStream_;
firstOpenStream_ = (PaUtilStreamRepresentation*)stream;
}
static void RemoveOpenStream( PaStream* stream )
{
PaUtilStreamRepresentation *previous = NULL;
PaUtilStreamRepresentation *current = firstOpenStream_;
while( current != NULL )
{
if( ((PaStream*)current) == stream )
{
if( previous == NULL )
{
firstOpenStream_ = current->nextOpenStream;
}
else
{
previous->nextOpenStream = current->nextOpenStream;
}
return;
}
else
{
previous = current;
current = current->nextOpenStream;
}
}
}
static void CloseOpenStreams( void )
{
/* we call Pa_CloseStream() here to ensure that the same destruction
logic is used for automatically closed streams */
while( firstOpenStream_ != NULL )
Pa_CloseStream( firstOpenStream_ );
}
PaError Pa_Initialize( void )
{
PaError result;
PA_LOGAPI_ENTER( "Pa_Initialize" );
if( PA_IS_INITIALISED_ )
{
++initializationCount_;
result = paNoError;
}
else
{
PA_VALIDATE_TYPE_SIZES;
PA_VALIDATE_ENDIANNESS;
PaUtil_InitializeClock();
PaUtil_ResetTraceMessages();
result = InitializeHostApis();
if( result == paNoError )
++initializationCount_;
}
PA_LOGAPI_EXIT_PAERROR( "Pa_Initialize", result );
return result;
}
PaError Pa_Terminate( void )
{
PaError result;
PA_LOGAPI_ENTER( "Pa_Terminate" );
if( PA_IS_INITIALISED_ )
{
if( --initializationCount_ == 0 )
{
CloseOpenStreams();
TerminateHostApis();
PaUtil_DumpTraceMessages();
}
result = paNoError;
}
else
{
result= paNotInitialized;
}
PA_LOGAPI_EXIT_PAERROR( "Pa_Terminate", result );
return result;
}
const PaHostErrorInfo* Pa_GetLastHostErrorInfo( void )
{
return &lastHostErrorInfo_;
}
const char *Pa_GetErrorText( PaError errorCode )
{
const char *result;
switch( errorCode )
{
case paNoError: result = "Success"; break;
case paNotInitialized: result = "PortAudio not initialized"; break;
/** @todo could catenate the last host error text to result in the case of paUnanticipatedHostError */
case paUnanticipatedHostError: result = "Unanticipated host error"; break;
case paInvalidChannelCount: result = "Invalid number of channels"; break;
case paInvalidSampleRate: result = "Invalid sample rate"; break;
case paInvalidDevice: result = "Invalid device"; break;
case paInvalidFlag: result = "Invalid flag"; break;
case paSampleFormatNotSupported: result = "Sample format not supported"; break;
case paBadIODeviceCombination: result = "Illegal combination of I/O devices"; break;
case paInsufficientMemory: result = "Insufficient memory"; break;
case paBufferTooBig: result = "Buffer too big"; break;
case paBufferTooSmall: result = "Buffer too small"; break;
case paNullCallback: result = "No callback routine specified"; break;
case paBadStreamPtr: result = "Invalid stream pointer"; break;
case paTimedOut: result = "Wait timed out"; break;
case paInternalError: result = "Internal PortAudio error"; break;
case paDeviceUnavailable: result = "Device unavailable"; break;
case paIncompatibleHostApiSpecificStreamInfo: result = "Incompatible host API specific stream info"; break;
case paStreamIsStopped: result = "Stream is stopped"; break;
case paStreamIsNotStopped: result = "Stream is not stopped"; break;
case paInputOverflowed: result = "Input overflowed"; break;
case paOutputUnderflowed: result = "Output underflowed"; break;
case paHostApiNotFound: result = "Host API not found"; break;
case paInvalidHostApi: result = "Invalid host API"; break;
case paCanNotReadFromACallbackStream: result = "Can't read from a callback stream"; break;
case paCanNotWriteToACallbackStream: result = "Can't write to a callback stream"; break;
case paCanNotReadFromAnOutputOnlyStream: result = "Can't read from an output only stream"; break;
case paCanNotWriteToAnInputOnlyStream: result = "Can't write to an input only stream"; break;
case paIncompatibleStreamHostApi: result = "Incompatible stream host API"; break;
case paBadBufferPtr: result = "Bad buffer pointer"; break;
default:
if( errorCode > 0 )
result = "Invalid error code (value greater than zero)";
else
result = "Invalid error code";
break;
}
return result;
}
PaHostApiIndex Pa_HostApiTypeIdToHostApiIndex( PaHostApiTypeId type )
{
PaHostApiIndex result;
int i;
PA_LOGAPI_ENTER_PARAMS( "Pa_HostApiTypeIdToHostApiIndex" );
PA_LOGAPI(("\tPaHostApiTypeId type: %d\n", type ));
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
}
else
{
result = paHostApiNotFound;
for( i=0; i < hostApisCount_; ++i )
{
if( hostApis_[i]->info.type == type )
{
result = i;
break;
}
}
}
PA_LOGAPI_EXIT_PAERROR_OR_T_RESULT( "Pa_HostApiTypeIdToHostApiIndex", "PaHostApiIndex: %d", result );
return result;
}
PaError PaUtil_GetHostApiRepresentation( struct PaUtilHostApiRepresentation **hostApi,
PaHostApiTypeId type )
{
PaError result;
int i;
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
}
else
{
result = paHostApiNotFound;
for( i=0; i < hostApisCount_; ++i )
{
if( hostApis_[i]->info.type == type )
{
*hostApi = hostApis_[i];
result = paNoError;
break;
}
}
}
return result;
}
PaError PaUtil_DeviceIndexToHostApiDeviceIndex(
PaDeviceIndex *hostApiDevice, PaDeviceIndex device, struct PaUtilHostApiRepresentation *hostApi )
{
PaError result;
PaDeviceIndex x;
x = device - hostApi->privatePaFrontInfo.baseDeviceIndex;
if( x < 0 || x >= hostApi->info.deviceCount )
{
result = paInvalidDevice;
}
else
{
*hostApiDevice = x;
result = paNoError;
}
return result;
}
PaHostApiIndex Pa_GetHostApiCount( void )
{
int result;
PA_LOGAPI_ENTER( "Pa_GetHostApiCount" );
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
}
else
{
result = hostApisCount_;
}
PA_LOGAPI_EXIT_PAERROR_OR_T_RESULT( "Pa_GetHostApiCount", "PaHostApiIndex: %d", result );
return result;
}
PaHostApiIndex Pa_GetDefaultHostApi( void )
{
int result;
PA_LOGAPI_ENTER( "Pa_GetDefaultHostApi" );
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
}
else
{
result = paDefaultHostApiIndex;
/* internal consistency check: make sure that the default host api
index is within range */
if( result < 0 || result >= hostApisCount_ )
{
result = paInternalError;
}
}
PA_LOGAPI_EXIT_PAERROR_OR_T_RESULT( "Pa_GetDefaultHostApi", "PaHostApiIndex: %d", result );
return result;
}
const PaHostApiInfo* Pa_GetHostApiInfo( PaHostApiIndex hostApi )
{
PaHostApiInfo *info;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetHostApiInfo" );
PA_LOGAPI(("\tPaHostApiIndex hostApi: %d\n", hostApi ));
if( !PA_IS_INITIALISED_ )
{
info = NULL;
PA_LOGAPI(("Pa_GetHostApiInfo returned:\n" ));
PA_LOGAPI(("\tPaHostApiInfo*: NULL [ PortAudio not initialized ]\n" ));
}
else if( hostApi < 0 || hostApi >= hostApisCount_ )
{
info = NULL;
PA_LOGAPI(("Pa_GetHostApiInfo returned:\n" ));
PA_LOGAPI(("\tPaHostApiInfo*: NULL [ hostApi out of range ]\n" ));
}
else
{
info = &hostApis_[hostApi]->info;
PA_LOGAPI(("Pa_GetHostApiInfo returned:\n" ));
PA_LOGAPI(("\tPaHostApiInfo*: 0x%p\n", info ));
PA_LOGAPI(("\t{\n" ));
PA_LOGAPI(("\t\tint structVersion: %d\n", info->structVersion ));
PA_LOGAPI(("\t\tPaHostApiTypeId type: %d\n", info->type ));
PA_LOGAPI(("\t\tconst char *name: %s\n", info->name ));
PA_LOGAPI(("\t}\n" ));
}
return info;
}
PaDeviceIndex Pa_HostApiDeviceIndexToDeviceIndex( PaHostApiIndex hostApi, int hostApiDeviceIndex )
{
PaDeviceIndex result;
PA_LOGAPI_ENTER_PARAMS( "Pa_HostApiDeviceIndexToPaDeviceIndex" );
PA_LOGAPI(("\tPaHostApiIndex hostApi: %d\n", hostApi ));
PA_LOGAPI(("\tint hostApiDeviceIndex: %d\n", hostApiDeviceIndex ));
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
}
else
{
if( hostApi < 0 || hostApi >= hostApisCount_ )
{
result = paInvalidHostApi;
}
else
{
if( hostApiDeviceIndex < 0 ||
hostApiDeviceIndex >= hostApis_[hostApi]->info.deviceCount )
{
result = paInvalidDevice;
}
else
{
result = hostApis_[hostApi]->privatePaFrontInfo.baseDeviceIndex + hostApiDeviceIndex;
}
}
}
PA_LOGAPI_EXIT_PAERROR_OR_T_RESULT( "Pa_HostApiDeviceIndexToPaDeviceIndex", "PaDeviceIndex: %d", result );
return result;
}
PaDeviceIndex Pa_GetDeviceCount( void )
{
PaDeviceIndex result;
PA_LOGAPI_ENTER( "Pa_GetDeviceCount" );
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
}
else
{
result = deviceCount_;
}
PA_LOGAPI_EXIT_PAERROR_OR_T_RESULT( "Pa_GetDeviceCount", "PaDeviceIndex: %d", result );
return result;
}
PaDeviceIndex Pa_GetDefaultInputDevice( void )
{
PaHostApiIndex hostApi;
PaDeviceIndex result;
PA_LOGAPI_ENTER( "Pa_GetDefaultInputDevice" );
hostApi = Pa_GetDefaultHostApi();
if( hostApi < 0 )
{
result = paNoDevice;
}
else
{
result = hostApis_[hostApi]->info.defaultInputDevice;
}
PA_LOGAPI_EXIT_T( "Pa_GetDefaultInputDevice", "PaDeviceIndex: %d", result );
return result;
}
PaDeviceIndex Pa_GetDefaultOutputDevice( void )
{
PaHostApiIndex hostApi;
PaDeviceIndex result;
PA_LOGAPI_ENTER( "Pa_GetDefaultOutputDevice" );
hostApi = Pa_GetDefaultHostApi();
if( hostApi < 0 )
{
result = paNoDevice;
}
else
{
result = hostApis_[hostApi]->info.defaultOutputDevice;
}
PA_LOGAPI_EXIT_T( "Pa_GetDefaultOutputDevice", "PaDeviceIndex: %d", result );
return result;
}
const PaDeviceInfo* Pa_GetDeviceInfo( PaDeviceIndex device )
{
int hostSpecificDeviceIndex;
int hostApiIndex = FindHostApi( device, &hostSpecificDeviceIndex );
PaDeviceInfo *result;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetDeviceInfo" );
PA_LOGAPI(("\tPaDeviceIndex device: %d\n", device ));
if( hostApiIndex < 0 )
{
result = NULL;
PA_LOGAPI(("Pa_GetDeviceInfo returned:\n" ));
PA_LOGAPI(("\tPaDeviceInfo* NULL [ invalid device index ]\n" ));
}
else
{
result = hostApis_[hostApiIndex]->deviceInfos[ hostSpecificDeviceIndex ];
PA_LOGAPI(("Pa_GetDeviceInfo returned:\n" ));
PA_LOGAPI(("\tPaDeviceInfo*: 0x%p:\n", result ));
PA_LOGAPI(("\t{\n" ));
PA_LOGAPI(("\t\tint structVersion: %d\n", result->structVersion ));
PA_LOGAPI(("\t\tconst char *name: %s\n", result->name ));
PA_LOGAPI(("\t\tPaHostApiIndex hostApi: %d\n", result->hostApi ));
PA_LOGAPI(("\t\tint maxInputChannels: %d\n", result->maxInputChannels ));
PA_LOGAPI(("\t\tint maxOutputChannels: %d\n", result->maxOutputChannels ));
PA_LOGAPI(("\t}\n" ));
}
return result;
}
/*
SampleFormatIsValid() returns 1 if sampleFormat is a sample format
defined in portaudio.h, or 0 otherwise.
*/
static int SampleFormatIsValid( PaSampleFormat format )
{
switch( format & ~paNonInterleaved )
{
case paFloat32: return 1;
case paInt16: return 1;
case paInt32: return 1;
case paInt24: return 1;
case paInt8: return 1;
case paUInt8: return 1;
case paCustomFormat: return 1;
default: return 0;
}
}
/*
NOTE: make sure this validation list is kept syncronised with the one in
pa_hostapi.h
ValidateOpenStreamParameters() checks that parameters to Pa_OpenStream()
conform to the expected values as described below. This function is
also designed to be used with the proposed Pa_IsFormatSupported() function.
There are basically two types of validation that could be performed:
Generic conformance validation, and device capability mismatch
validation. This function performs only generic conformance validation.
Validation that would require knowledge of device capabilities is
not performed because of potentially complex relationships between
combinations of parameters - for example, even if the sampleRate
seems ok, it might not be for a duplex stream - we have no way of
checking this in an API-neutral way, so we don't try.
On success the function returns PaNoError and fills in hostApi,
hostApiInputDeviceID, and hostApiOutputDeviceID fields. On failure
the function returns an error code indicating the first encountered
parameter error.
If ValidateOpenStreamParameters() returns paNoError, the following
assertions are guaranteed to be true.
- at least one of inputParameters & outputParmeters is valid (not NULL)
- if inputParameters & outputParameters are both valid, that
inputParameters->device & outputParameters->device both use the same host api
PaDeviceIndex inputParameters->device
- is within range (0 to Pa_GetDeviceCount-1) Or:
- is paUseHostApiSpecificDeviceSpecification and
inputParameters->hostApiSpecificStreamInfo is non-NULL and refers
to a valid host api
int inputParameters->channelCount
- if inputParameters->device is not paUseHostApiSpecificDeviceSpecification, channelCount is > 0
- upper bound is NOT validated against device capabilities
PaSampleFormat inputParameters->sampleFormat
- is one of the sample formats defined in portaudio.h
void *inputParameters->hostApiSpecificStreamInfo
- if supplied its hostApi field matches the input device's host Api
PaDeviceIndex outputParmeters->device
- is within range (0 to Pa_GetDeviceCount-1)
int outputParmeters->channelCount
- if inputDevice is valid, channelCount is > 0
- upper bound is NOT validated against device capabilities
PaSampleFormat outputParmeters->sampleFormat
- is one of the sample formats defined in portaudio.h
void *outputParmeters->hostApiSpecificStreamInfo
- if supplied its hostApi field matches the output device's host Api
double sampleRate
- is not an 'absurd' rate (less than 1000. or greater than 200000.)
- sampleRate is NOT validated against device capabilities
PaStreamFlags streamFlags
- unused platform neutral flags are zero
- paNeverDropInput is only used for full-duplex callback streams with
variable buffer size (paFramesPerBufferUnspecified)
*/
static PaError ValidateOpenStreamParameters(
const PaStreamParameters *inputParameters,
const PaStreamParameters *outputParameters,
double sampleRate,
unsigned long framesPerBuffer,
PaStreamFlags streamFlags,
PaStreamCallback *streamCallback,
PaUtilHostApiRepresentation **hostApi,
PaDeviceIndex *hostApiInputDevice,
PaDeviceIndex *hostApiOutputDevice )
{
int inputHostApiIndex = -1, /* Surpress uninitialised var warnings: compiler does */
outputHostApiIndex = -1; /* not see that if inputParameters and outputParame- */
/* ters are both nonzero, these indices are set. */
if( (inputParameters == NULL) && (outputParameters == NULL) )
{
return paInvalidDevice; /** @todo should be a new error code "invalid device parameters" or something */
}
else
{
if( inputParameters == NULL )
{
*hostApiInputDevice = paNoDevice;
}
else if( inputParameters->device == paUseHostApiSpecificDeviceSpecification )
{
if( inputParameters->hostApiSpecificStreamInfo )
{
inputHostApiIndex = Pa_HostApiTypeIdToHostApiIndex(
((PaUtilHostApiSpecificStreamInfoHeader*)inputParameters->hostApiSpecificStreamInfo)->hostApiType );
if( inputHostApiIndex != -1 )
{
*hostApiInputDevice = paUseHostApiSpecificDeviceSpecification;
*hostApi = hostApis_[inputHostApiIndex];
}
else
{
return paInvalidDevice;
}
}
else
{
return paInvalidDevice;
}
}
else
{
if( inputParameters->device < 0 || inputParameters->device >= deviceCount_ )
return paInvalidDevice;
inputHostApiIndex = FindHostApi( inputParameters->device, hostApiInputDevice );
if( inputHostApiIndex < 0 )
return paInternalError;
*hostApi = hostApis_[inputHostApiIndex];
if( inputParameters->channelCount <= 0 )
return paInvalidChannelCount;
if( !SampleFormatIsValid( inputParameters->sampleFormat ) )
return paSampleFormatNotSupported;
if( inputParameters->hostApiSpecificStreamInfo != NULL )
{
if( ((PaUtilHostApiSpecificStreamInfoHeader*)inputParameters->hostApiSpecificStreamInfo)->hostApiType
!= (*hostApi)->info.type )
return paIncompatibleHostApiSpecificStreamInfo;
}
}
if( outputParameters == NULL )
{
*hostApiOutputDevice = paNoDevice;
}
else if( outputParameters->device == paUseHostApiSpecificDeviceSpecification )
{
if( outputParameters->hostApiSpecificStreamInfo )
{
outputHostApiIndex = Pa_HostApiTypeIdToHostApiIndex(
((PaUtilHostApiSpecificStreamInfoHeader*)outputParameters->hostApiSpecificStreamInfo)->hostApiType );
if( outputHostApiIndex != -1 )
{
*hostApiOutputDevice = paUseHostApiSpecificDeviceSpecification;
*hostApi = hostApis_[outputHostApiIndex];
}
else
{
return paInvalidDevice;
}
}
else
{
return paInvalidDevice;
}
}
else
{
if( outputParameters->device < 0 || outputParameters->device >= deviceCount_ )
return paInvalidDevice;
outputHostApiIndex = FindHostApi( outputParameters->device, hostApiOutputDevice );
if( outputHostApiIndex < 0 )
return paInternalError;
*hostApi = hostApis_[outputHostApiIndex];
if( outputParameters->channelCount <= 0 )
return paInvalidChannelCount;
if( !SampleFormatIsValid( outputParameters->sampleFormat ) )
return paSampleFormatNotSupported;
if( outputParameters->hostApiSpecificStreamInfo != NULL )
{
if( ((PaUtilHostApiSpecificStreamInfoHeader*)outputParameters->hostApiSpecificStreamInfo)->hostApiType
!= (*hostApi)->info.type )
return paIncompatibleHostApiSpecificStreamInfo;
}
}
if( (inputParameters != NULL) && (outputParameters != NULL) )
{
/* ensure that both devices use the same API */
if( inputHostApiIndex != outputHostApiIndex )
return paBadIODeviceCombination;
}
}
/* Check for absurd sample rates. */
if( (sampleRate < 1000.0) || (sampleRate > 200000.0) )
return paInvalidSampleRate;
if( ((streamFlags & ~paPlatformSpecificFlags) & ~(paClipOff | paDitherOff | paNeverDropInput | paPrimeOutputBuffersUsingStreamCallback ) ) != 0 )
return paInvalidFlag;
if( streamFlags & paNeverDropInput )
{
/* must be a callback stream */
if( !streamCallback )
return paInvalidFlag;
/* must be a full duplex stream */
if( (inputParameters == NULL) || (outputParameters == NULL) )
return paInvalidFlag;
/* must use paFramesPerBufferUnspecified */
if( framesPerBuffer != paFramesPerBufferUnspecified )
return paInvalidFlag;
}
return paNoError;
}
PaError Pa_IsFormatSupported( const PaStreamParameters *inputParameters,
const PaStreamParameters *outputParameters,
double sampleRate )
{
PaError result;
PaUtilHostApiRepresentation *hostApi = 0;
PaDeviceIndex hostApiInputDevice = paNoDevice, hostApiOutputDevice = paNoDevice;
PaStreamParameters hostApiInputParameters, hostApiOutputParameters;
PaStreamParameters *hostApiInputParametersPtr, *hostApiOutputParametersPtr;
#ifdef PA_LOG_API_CALLS
PA_LOGAPI_ENTER_PARAMS( "Pa_IsFormatSupported" );
if( inputParameters == NULL ){
PA_LOGAPI(("\tPaStreamParameters *inputParameters: NULL\n" ));
}else{
PA_LOGAPI(("\tPaStreamParameters *inputParameters: 0x%p\n", inputParameters ));
PA_LOGAPI(("\tPaDeviceIndex inputParameters->device: %d\n", inputParameters->device ));
PA_LOGAPI(("\tint inputParameters->channelCount: %d\n", inputParameters->channelCount ));
PA_LOGAPI(("\tPaSampleFormat inputParameters->sampleFormat: %d\n", inputParameters->sampleFormat ));
PA_LOGAPI(("\tPaTime inputParameters->suggestedLatency: %f\n", inputParameters->suggestedLatency ));
PA_LOGAPI(("\tvoid *inputParameters->hostApiSpecificStreamInfo: 0x%p\n", inputParameters->hostApiSpecificStreamInfo ));
}
if( outputParameters == NULL ){
PA_LOGAPI(("\tPaStreamParameters *outputParameters: NULL\n" ));
}else{
PA_LOGAPI(("\tPaStreamParameters *outputParameters: 0x%p\n", outputParameters ));
PA_LOGAPI(("\tPaDeviceIndex outputParameters->device: %d\n", outputParameters->device ));
PA_LOGAPI(("\tint outputParameters->channelCount: %d\n", outputParameters->channelCount ));
PA_LOGAPI(("\tPaSampleFormat outputParameters->sampleFormat: %d\n", outputParameters->sampleFormat ));
PA_LOGAPI(("\tPaTime outputParameters->suggestedLatency: %f\n", outputParameters->suggestedLatency ));
PA_LOGAPI(("\tvoid *outputParameters->hostApiSpecificStreamInfo: 0x%p\n", outputParameters->hostApiSpecificStreamInfo ));
}
PA_LOGAPI(("\tdouble sampleRate: %g\n", sampleRate ));
#endif
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
PA_LOGAPI_EXIT_PAERROR( "Pa_IsFormatSupported", result );
return result;
}
result = ValidateOpenStreamParameters( inputParameters,
outputParameters,
sampleRate, 0, paNoFlag, 0,
&hostApi,
&hostApiInputDevice,
&hostApiOutputDevice );
if( result != paNoError )
{
PA_LOGAPI_EXIT_PAERROR( "Pa_IsFormatSupported", result );
return result;
}
if( inputParameters )
{
hostApiInputParameters.device = hostApiInputDevice;
hostApiInputParameters.channelCount = inputParameters->channelCount;
hostApiInputParameters.sampleFormat = inputParameters->sampleFormat;
hostApiInputParameters.suggestedLatency = inputParameters->suggestedLatency;
hostApiInputParameters.hostApiSpecificStreamInfo = inputParameters->hostApiSpecificStreamInfo;
hostApiInputParametersPtr = &hostApiInputParameters;
}
else
{
hostApiInputParametersPtr = NULL;
}
if( outputParameters )
{
hostApiOutputParameters.device = hostApiOutputDevice;
hostApiOutputParameters.channelCount = outputParameters->channelCount;
hostApiOutputParameters.sampleFormat = outputParameters->sampleFormat;
hostApiOutputParameters.suggestedLatency = outputParameters->suggestedLatency;
hostApiOutputParameters.hostApiSpecificStreamInfo = outputParameters->hostApiSpecificStreamInfo;
hostApiOutputParametersPtr = &hostApiOutputParameters;
}
else
{
hostApiOutputParametersPtr = NULL;
}
result = hostApi->IsFormatSupported( hostApi,
hostApiInputParametersPtr, hostApiOutputParametersPtr,
sampleRate );
#ifdef PA_LOG_API_CALLS
PA_LOGAPI(("Pa_OpenStream returned:\n" ));
if( result == paFormatIsSupported )
PA_LOGAPI(("\tPaError: 0 [ paFormatIsSupported ]\n" ));
else
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
#endif
return result;
}
PaError Pa_OpenStream( PaStream** stream,
const PaStreamParameters *inputParameters,
const PaStreamParameters *outputParameters,
double sampleRate,
unsigned long framesPerBuffer,
PaStreamFlags streamFlags,
PaStreamCallback *streamCallback,
void *userData )
{
PaError result;
PaUtilHostApiRepresentation *hostApi = 0;
PaDeviceIndex hostApiInputDevice = paNoDevice, hostApiOutputDevice = paNoDevice;
PaStreamParameters hostApiInputParameters, hostApiOutputParameters;
PaStreamParameters *hostApiInputParametersPtr, *hostApiOutputParametersPtr;
#ifdef PA_LOG_API_CALLS
PA_LOGAPI_ENTER_PARAMS( "Pa_OpenStream" );
PA_LOGAPI(("\tPaStream** stream: 0x%p\n", stream ));
if( inputParameters == NULL ){
PA_LOGAPI(("\tPaStreamParameters *inputParameters: NULL\n" ));
}else{
PA_LOGAPI(("\tPaStreamParameters *inputParameters: 0x%p\n", inputParameters ));
PA_LOGAPI(("\tPaDeviceIndex inputParameters->device: %d\n", inputParameters->device ));
PA_LOGAPI(("\tint inputParameters->channelCount: %d\n", inputParameters->channelCount ));
PA_LOGAPI(("\tPaSampleFormat inputParameters->sampleFormat: %d\n", inputParameters->sampleFormat ));
PA_LOGAPI(("\tPaTime inputParameters->suggestedLatency: %f\n", inputParameters->suggestedLatency ));
PA_LOGAPI(("\tvoid *inputParameters->hostApiSpecificStreamInfo: 0x%p\n", inputParameters->hostApiSpecificStreamInfo ));
}
if( outputParameters == NULL ){
PA_LOGAPI(("\tPaStreamParameters *outputParameters: NULL\n" ));
}else{
PA_LOGAPI(("\tPaStreamParameters *outputParameters: 0x%p\n", outputParameters ));
PA_LOGAPI(("\tPaDeviceIndex outputParameters->device: %d\n", outputParameters->device ));
PA_LOGAPI(("\tint outputParameters->channelCount: %d\n", outputParameters->channelCount ));
PA_LOGAPI(("\tPaSampleFormat outputParameters->sampleFormat: %d\n", outputParameters->sampleFormat ));
PA_LOGAPI(("\tPaTime outputParameters->suggestedLatency: %f\n", outputParameters->suggestedLatency ));
PA_LOGAPI(("\tvoid *outputParameters->hostApiSpecificStreamInfo: 0x%p\n", outputParameters->hostApiSpecificStreamInfo ));
}
PA_LOGAPI(("\tdouble sampleRate: %g\n", sampleRate ));
PA_LOGAPI(("\tunsigned long framesPerBuffer: %d\n", framesPerBuffer ));
PA_LOGAPI(("\tPaStreamFlags streamFlags: 0x%x\n", streamFlags ));
PA_LOGAPI(("\tPaStreamCallback *streamCallback: 0x%p\n", streamCallback ));
PA_LOGAPI(("\tvoid *userData: 0x%p\n", userData ));
#endif
if( !PA_IS_INITIALISED_ )
{
result = paNotInitialized;
PA_LOGAPI(("Pa_OpenStream returned:\n" ));
PA_LOGAPI(("\t*(PaStream** stream): undefined\n" ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
return result;
}
/* Check for parameter errors.
NOTE: make sure this validation list is kept syncronised with the one
in pa_hostapi.h
*/
if( stream == NULL )
{
result = paBadStreamPtr;
PA_LOGAPI(("Pa_OpenStream returned:\n" ));
PA_LOGAPI(("\t*(PaStream** stream): undefined\n" ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
return result;
}
result = ValidateOpenStreamParameters( inputParameters,
outputParameters,
sampleRate, framesPerBuffer,
streamFlags, streamCallback,
&hostApi,
&hostApiInputDevice,
&hostApiOutputDevice );
if( result != paNoError )
{
PA_LOGAPI(("Pa_OpenStream returned:\n" ));
PA_LOGAPI(("\t*(PaStream** stream): undefined\n" ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
return result;
}
if( inputParameters )
{
hostApiInputParameters.device = hostApiInputDevice;
hostApiInputParameters.channelCount = inputParameters->channelCount;
hostApiInputParameters.sampleFormat = inputParameters->sampleFormat;
hostApiInputParameters.suggestedLatency = inputParameters->suggestedLatency;
hostApiInputParameters.hostApiSpecificStreamInfo = inputParameters->hostApiSpecificStreamInfo;
hostApiInputParametersPtr = &hostApiInputParameters;
}
else
{
hostApiInputParametersPtr = NULL;
}
if( outputParameters )
{
hostApiOutputParameters.device = hostApiOutputDevice;
hostApiOutputParameters.channelCount = outputParameters->channelCount;
hostApiOutputParameters.sampleFormat = outputParameters->sampleFormat;
hostApiOutputParameters.suggestedLatency = outputParameters->suggestedLatency;
hostApiOutputParameters.hostApiSpecificStreamInfo = outputParameters->hostApiSpecificStreamInfo;
hostApiOutputParametersPtr = &hostApiOutputParameters;
}
else
{
hostApiOutputParametersPtr = NULL;
}
result = hostApi->OpenStream( hostApi, stream,
hostApiInputParametersPtr, hostApiOutputParametersPtr,
sampleRate, framesPerBuffer, streamFlags, streamCallback, userData );
if( result == paNoError )
AddOpenStream( *stream );
PA_LOGAPI(("Pa_OpenStream returned:\n" ));
PA_LOGAPI(("\t*(PaStream** stream): 0x%p\n", *stream ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
return result;
}
PaError Pa_OpenDefaultStream( PaStream** stream,
int inputChannelCount,
int outputChannelCount,
PaSampleFormat sampleFormat,
double sampleRate,
unsigned long framesPerBuffer,
PaStreamCallback *streamCallback,
void *userData )
{
PaError result;
PaStreamParameters hostApiInputParameters, hostApiOutputParameters;
PaStreamParameters *hostApiInputParametersPtr, *hostApiOutputParametersPtr;
PA_LOGAPI_ENTER_PARAMS( "Pa_OpenDefaultStream" );
PA_LOGAPI(("\tPaStream** stream: 0x%p\n", stream ));
PA_LOGAPI(("\tint inputChannelCount: %d\n", inputChannelCount ));
PA_LOGAPI(("\tint outputChannelCount: %d\n", outputChannelCount ));
PA_LOGAPI(("\tPaSampleFormat sampleFormat: %d\n", sampleFormat ));
PA_LOGAPI(("\tdouble sampleRate: %g\n", sampleRate ));
PA_LOGAPI(("\tunsigned long framesPerBuffer: %d\n", framesPerBuffer ));
PA_LOGAPI(("\tPaStreamCallback *streamCallback: 0x%p\n", streamCallback ));
PA_LOGAPI(("\tvoid *userData: 0x%p\n", userData ));
if( inputChannelCount > 0 )
{
hostApiInputParameters.device = Pa_GetDefaultInputDevice();
if( hostApiInputParameters.device == paNoDevice )
return paDeviceUnavailable;
hostApiInputParameters.channelCount = inputChannelCount;
hostApiInputParameters.sampleFormat = sampleFormat;
/* defaultHighInputLatency is used below instead of
defaultLowInputLatency because it is more important for the default
stream to work reliably than it is for it to work with the lowest
latency.
*/
hostApiInputParameters.suggestedLatency =
Pa_GetDeviceInfo( hostApiInputParameters.device )->defaultHighInputLatency;
hostApiInputParameters.hostApiSpecificStreamInfo = NULL;
hostApiInputParametersPtr = &hostApiInputParameters;
}
else
{
hostApiInputParametersPtr = NULL;
}
if( outputChannelCount > 0 )
{
hostApiOutputParameters.device = Pa_GetDefaultOutputDevice();
if( hostApiOutputParameters.device == paNoDevice )
return paDeviceUnavailable;
hostApiOutputParameters.channelCount = outputChannelCount;
hostApiOutputParameters.sampleFormat = sampleFormat;
/* defaultHighOutputLatency is used below instead of
defaultLowOutputLatency because it is more important for the default
stream to work reliably than it is for it to work with the lowest
latency.
*/
hostApiOutputParameters.suggestedLatency =
Pa_GetDeviceInfo( hostApiOutputParameters.device )->defaultHighOutputLatency;
hostApiOutputParameters.hostApiSpecificStreamInfo = NULL;
hostApiOutputParametersPtr = &hostApiOutputParameters;
}
else
{
hostApiOutputParametersPtr = NULL;
}
result = Pa_OpenStream(
stream, hostApiInputParametersPtr, hostApiOutputParametersPtr,
sampleRate, framesPerBuffer, paNoFlag, streamCallback, userData );
PA_LOGAPI(("Pa_OpenDefaultStream returned:\n" ));
PA_LOGAPI(("\t*(PaStream** stream): 0x%p", *stream ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
return result;
}
PaError PaUtil_ValidateStreamPointer( PaStream* stream )
{
if( !PA_IS_INITIALISED_ ) return paNotInitialized;
if( stream == NULL ) return paBadStreamPtr;
if( ((PaUtilStreamRepresentation*)stream)->magic != PA_STREAM_MAGIC )
return paBadStreamPtr;
return paNoError;
}
PaError Pa_CloseStream( PaStream* stream )
{
PaUtilStreamInterface *interface;
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_CloseStream" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
/* always remove the open stream from our list, even if this function
eventually returns an error. Otherwise CloseOpenStreams() will
get stuck in an infinite loop */
RemoveOpenStream( stream ); /* be sure to call this _before_ closing the stream */
if( result == paNoError )
{
interface = PA_STREAM_INTERFACE(stream);
/* abort the stream if it isn't stopped */
result = interface->IsStopped( stream );
if( result == 1 )
result = paNoError;
else if( result == 0 )
result = interface->Abort( stream );
if( result == paNoError ) /** @todo REVIEW: shouldn't we close anyway? */
result = interface->Close( stream );
}
PA_LOGAPI_EXIT_PAERROR( "Pa_CloseStream", result );
return result;
}
PaError Pa_SetStreamFinishedCallback( PaStream *stream, PaStreamFinishedCallback* streamFinishedCallback )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_SetStreamFinishedCallback" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
PA_LOGAPI(("\tPaStreamFinishedCallback* streamFinishedCallback: 0x%p\n", streamFinishedCallback ));
if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
if( result == 0 )
{
result = paStreamIsNotStopped ;
}
if( result == 1 )
{
PA_STREAM_REP( stream )->streamFinishedCallback = streamFinishedCallback;
result = paNoError;
}
}
PA_LOGAPI_EXIT_PAERROR( "Pa_SetStreamFinishedCallback", result );
return result;
}
PaError Pa_StartStream( PaStream *stream )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_StartStream" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
if( result == 0 )
{
result = paStreamIsNotStopped ;
}
else if( result == 1 )
{
result = PA_STREAM_INTERFACE(stream)->Start( stream );
}
}
PA_LOGAPI_EXIT_PAERROR( "Pa_StartStream", result );
return result;
}
PaError Pa_StopStream( PaStream *stream )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_StopStream" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Stop( stream );
}
else if( result == 1 )
{
result = paStreamIsStopped;
}
}
PA_LOGAPI_EXIT_PAERROR( "Pa_StopStream", result );
return result;
}
PaError Pa_AbortStream( PaStream *stream )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_AbortStream" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( result == paNoError )
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Abort( stream );
}
else if( result == 1 )
{
result = paStreamIsStopped;
}
}
PA_LOGAPI_EXIT_PAERROR( "Pa_AbortStream", result );
return result;
}
PaError Pa_IsStreamStopped( PaStream *stream )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_IsStreamStopped" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( result == paNoError )
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
PA_LOGAPI_EXIT_PAERROR( "Pa_IsStreamStopped", result );
return result;
}
PaError Pa_IsStreamActive( PaStream *stream )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_IsStreamActive" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( result == paNoError )
result = PA_STREAM_INTERFACE(stream)->IsActive( stream );
PA_LOGAPI_EXIT_PAERROR( "Pa_IsStreamActive", result );
return result;
}
const PaStreamInfo* Pa_GetStreamInfo( PaStream *stream )
{
PaError error = PaUtil_ValidateStreamPointer( stream );
const PaStreamInfo *result;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetStreamInfo" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( error != paNoError )
{
result = 0;
PA_LOGAPI(("Pa_GetStreamInfo returned:\n" ));
PA_LOGAPI(("\tconst PaStreamInfo*: 0 [PaError error:%d ( %s )]\n", error, Pa_GetErrorText( error ) ));
}
else
{
result = &PA_STREAM_REP( stream )->streamInfo;
PA_LOGAPI(("Pa_GetStreamInfo returned:\n" ));
PA_LOGAPI(("\tconst PaStreamInfo*: 0x%p:\n", result ));
PA_LOGAPI(("\t{" ));
PA_LOGAPI(("\t\tint structVersion: %d\n", result->structVersion ));
PA_LOGAPI(("\t\tPaTime inputLatency: %f\n", result->inputLatency ));
PA_LOGAPI(("\t\tPaTime outputLatency: %f\n", result->outputLatency ));
PA_LOGAPI(("\t\tdouble sampleRate: %f\n", result->sampleRate ));
PA_LOGAPI(("\t}\n" ));
}
return result;
}
PaTime Pa_GetStreamTime( PaStream *stream )
{
PaError error = PaUtil_ValidateStreamPointer( stream );
PaTime result;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetStreamTime" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( error != paNoError )
{
result = 0;
PA_LOGAPI(("Pa_GetStreamTime returned:\n" ));
PA_LOGAPI(("\tPaTime: 0 [PaError error:%d ( %s )]\n", result, error, Pa_GetErrorText( error ) ));
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetTime( stream );
PA_LOGAPI(("Pa_GetStreamTime returned:\n" ));
PA_LOGAPI(("\tPaTime: %g\n", result ));
}
return result;
}
double Pa_GetStreamCpuLoad( PaStream* stream )
{
PaError error = PaUtil_ValidateStreamPointer( stream );
double result;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetStreamCpuLoad" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( error != paNoError )
{
result = 0.0;
PA_LOGAPI(("Pa_GetStreamCpuLoad returned:\n" ));
PA_LOGAPI(("\tdouble: 0.0 [PaError error: %d ( %s )]\n", error, Pa_GetErrorText( error ) ));
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetCpuLoad( stream );
PA_LOGAPI(("Pa_GetStreamCpuLoad returned:\n" ));
PA_LOGAPI(("\tdouble: %g\n", result ));
}
return result;
}
PaError Pa_ReadStream( PaStream* stream,
void *buffer,
unsigned long frames )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_ReadStream" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( result == paNoError )
{
if( frames == 0 )
{
/* XXX: Should we not allow the implementation to signal any overflow condition? */
result = paNoError;
}
else if( buffer == 0 )
{
result = paBadBufferPtr;
}
else
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Read( stream, buffer, frames );
}
else if( result == 1 )
{
result = paStreamIsStopped;
}
}
}
PA_LOGAPI_EXIT_PAERROR( "Pa_ReadStream", result );
return result;
}
PaError Pa_WriteStream( PaStream* stream,
const void *buffer,
unsigned long frames )
{
PaError result = PaUtil_ValidateStreamPointer( stream );
PA_LOGAPI_ENTER_PARAMS( "Pa_WriteStream" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( result == paNoError )
{
if( frames == 0 )
{
/* XXX: Should we not allow the implementation to signal any underflow condition? */
result = paNoError;
}
else if( buffer == 0 )
{
result = paBadBufferPtr;
}
else
{
result = PA_STREAM_INTERFACE(stream)->IsStopped( stream );
if( result == 0 )
{
result = PA_STREAM_INTERFACE(stream)->Write( stream, buffer, frames );
}
else if( result == 1 )
{
result = paStreamIsStopped;
}
}
}
PA_LOGAPI_EXIT_PAERROR( "Pa_WriteStream", result );
return result;
}
signed long Pa_GetStreamReadAvailable( PaStream* stream )
{
PaError error = PaUtil_ValidateStreamPointer( stream );
signed long result;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetStreamReadAvailable" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( error != paNoError )
{
result = 0;
PA_LOGAPI(("Pa_GetStreamReadAvailable returned:\n" ));
PA_LOGAPI(("\tunsigned long: 0 [ PaError error: %d ( %s ) ]\n", error, Pa_GetErrorText( error ) ));
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetReadAvailable( stream );
PA_LOGAPI(("Pa_GetStreamReadAvailable returned:\n" ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
}
return result;
}
signed long Pa_GetStreamWriteAvailable( PaStream* stream )
{
PaError error = PaUtil_ValidateStreamPointer( stream );
signed long result;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetStreamWriteAvailable" );
PA_LOGAPI(("\tPaStream* stream: 0x%p\n", stream ));
if( error != paNoError )
{
result = 0;
PA_LOGAPI(("Pa_GetStreamWriteAvailable returned:\n" ));
PA_LOGAPI(("\tunsigned long: 0 [ PaError error: %d ( %s ) ]\n", error, Pa_GetErrorText( error ) ));
}
else
{
result = PA_STREAM_INTERFACE(stream)->GetWriteAvailable( stream );
PA_LOGAPI(("Pa_GetStreamWriteAvailable returned:\n" ));
PA_LOGAPI(("\tPaError: %d ( %s )\n", result, Pa_GetErrorText( result ) ));
}
return result;
}
PaError Pa_GetSampleSize( PaSampleFormat format )
{
int result;
PA_LOGAPI_ENTER_PARAMS( "Pa_GetSampleSize" );
PA_LOGAPI(("\tPaSampleFormat format: %d\n", format ));
switch( format & ~paNonInterleaved )
{
case paUInt8:
case paInt8:
result = 1;
break;
case paInt16:
result = 2;
break;
case paInt24:
result = 3;
break;
case paFloat32:
case paInt32:
result = 4;
break;
default:
result = paSampleFormatNotSupported;
break;
}
PA_LOGAPI_EXIT_PAERROR_OR_T_RESULT( "Pa_GetSampleSize", "int: %d", result );
return (PaError) result;
}