RtAAudioDevice.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "RtAAudioDevice.hxx"
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 namespace CLAM {
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 RtAAudioDevice::RtAAudioDevice(const std::string& name,int _device):
00256 AudioDevice(name)
00257 {
00258 mStarted = false;
00259 mRtAudio = 0;
00260 mTickOnRead = false;
00261 mTickOnWrite = false;
00262 mDevice = _device;
00263 }
00264
00265 void RtAAudioDevice::Start(void) throw(Err)
00266 {
00267 if (!mRtAudio)
00268 {
00269 int fs = SampleRate();
00270 mRtAudioBufferSize = Latency();
00271
00272 #ifdef __WINDOWS_DS__
00273 if (mRtAudioBufferSize<4096)
00274 {
00275 mRtAudioBufferSize = 4096;
00276 }
00277 #endif
00278
00279 #ifdef FACTOR2SRC_KLUDGE
00280 mDoFactor2SRC = false;
00281 if (fs == 22050)
00282 {
00283 mDoFactor2SRC = true;
00284 }
00285
00286 if (mDoFactor2SRC)
00287 {
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 mRtAudioBufferSize *= 2;
00299 fs *= 2;
00300 }
00301 #endif
00302
00303 #ifdef MACOSX_WORKAROUND
00304 mInternalRtAudioBufferSize = mRtAudioBufferSize;
00305 if (mInternalRtAudioBufferSize>2048)
00306 {
00307 mInternalRtAudioBufferSize = 2048;
00308 }
00309 #endif
00310
00311 try {
00312 mRtAudio = new RtAudio(&mRtAudioStream,
00313 mDevice, mOutputs.size(),
00314 mDevice, mInputs.size(),
00315 FORMAT, fs,
00316 #ifdef MACOSX_WORKAROUND
00317 &mInternalRtAudioBufferSize,
00318 #else
00319 &mRtAudioBufferSize,
00320 #endif
00321 2);
00322 }
00323 catch (RtError &) {
00324 exit(EXIT_FAILURE);
00325 }
00326
00327 #ifdef MACOSX_WORKAROUND
00328 mRtAudioBufferSize = mInternalRtAudioBufferSize*((mRtAudioBufferSize+mInternalRtAudioBufferSize-1)/mInternalRtAudioBufferSize);
00329 #endif
00330
00331
00332 #ifdef FACTOR2SRC_KLUDGE
00333 if (mDoFactor2SRC)
00334 {
00335
00336 mRtAudioBufferSize /= 2;
00337 #ifdef MACOSX_WORKAROUND
00338 mInternalRtAudioBufferSize /= 2;
00339 #endif
00340 }
00341 #endif
00342
00343
00344 SetLatency(mRtAudioBufferSize);
00345
00346
00347 mWriteBuffer.Alloc(mOutputs.size(),mRtAudioBufferSize*2);
00348 mReadBuffer.Alloc(mInputs.size(),mRtAudioBufferSize*2);
00349
00350 try {
00351 mRtAudioBuffer = (MY_TYPE *) mRtAudio->getStreamBuffer(mRtAudioStream);
00352 }
00353 catch (RtError &) {
00354 exit(EXIT_FAILURE);
00355 }
00356 }
00357
00358 mStarted = false;
00359 }
00360
00361 void RtAAudioDevice::Stop(void) throw(Err)
00362 {
00363 if (mRtAudio)
00364 {
00365 mRtAudio->stopStream(mRtAudioStream);
00366 mStarted = false;
00367 delete mRtAudio;
00368 mRtAudio = 0;
00369 }
00370 }
00371
00372 RtAAudioDevice::~RtAAudioDevice()
00373 {
00374 Stop();
00375 }
00376
00377 void RtAAudioDevice::Read(Audio& a,const int channelID)
00378 {
00379 if (!mStarted)
00380 {
00381 mRtAudio->startStream(mRtAudioStream);
00382 mStarted = true;
00383 mTickOnRead = true;
00384
00385
00386 int k = mRtAudioBufferSize/a.GetSize();
00387 if (a.GetSize()*k==mRtAudioBufferSize)
00388 {
00389 mWriteBuffer.mWriteIndex = mRtAudioBufferSize;
00390 }
00391 }
00392
00393 if (mTickOnRead && mReadBuffer.Filled()<a.GetSize())
00394 {
00395 Tick();
00396 }
00397
00398 mReadBuffer.ChannelCopyTo(a.GetBuffer().GetPtr(),a.GetSize(),channelID);
00399 }
00400
00401 void RtAAudioDevice::Write(const Audio& a,const int channelID)
00402 {
00403 if (!mStarted)
00404 {
00405 mRtAudio->startStream(mRtAudioStream);
00406 mStarted = true;
00407 mTickOnWrite = true;
00408 }
00409
00410 mWriteBuffer.ChannelCopyFrom(a.GetBuffer().GetPtr(),a.GetSize(),channelID);
00411
00412 if (mTickOnWrite && mWriteBuffer.Filled()>=mRtAudioBufferSize)
00413 {
00414 Tick();
00415 }
00416 }
00417
00418 void RtAAudioDevice::Tick(void)
00419 {
00420 #ifdef MACOSX_WORKAROUND
00421 int i = mRtAudioBufferSize/mInternalRtAudioBufferSize;
00422
00423 #ifdef FACTOR2SRC_KLUDGE
00424 if (mDoFactor2SRC)
00425 {
00426 while (i--)
00427 {
00428 mWriteBuffer.CopyToFactor2SRC(mRtAudioBuffer,mInternalRtAudioBufferSize);
00429 mRtAudio->tickStream(mRtAudioStream);
00430 mReadBuffer.CopyFromDoFactor2SRC(mRtAudioBuffer,mInternalRtAudioBufferSize);
00431 }
00432 }else
00433 #endif
00434 {
00435 while (i--)
00436 {
00437 mWriteBuffer.CopyTo(mRtAudioBuffer,mInternalRtAudioBufferSize);
00438 mRtAudio->tickStream(mRtAudioStream);
00439 mReadBuffer.CopyFrom(mRtAudioBuffer,mInternalRtAudioBufferSize);
00440 }
00441 }
00442 #else
00443 #ifdef FACTOR2SRC_KLUDGE
00444 if (mDoFactor2SRC)
00445 {
00446 mWriteBuffer.CopyToFactor2SRC(mRtAudioBuffer,mRtAudioBufferSize);
00447 mRtAudio->tickStream(mRtAudioStream);
00448 mReadBuffer.CopyFromDoFactor2SRC(mRtAudioBuffer,mRtAudioBufferSize);
00449 }
00450 else
00451 #endif
00452 {
00453 mWriteBuffer.CopyTo(mRtAudioBuffer,mRtAudioBufferSize);
00454 mRtAudio->tickStream(mRtAudioStream);
00455 mReadBuffer.CopyFrom(mRtAudioBuffer,mRtAudioBufferSize);
00456 }
00457 #endif
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 RtAAudioDeviceList::RtAAudioDeviceList()
00492 : AudioDeviceList( std::string("rtaudio") )
00493 {
00494
00495 EnumerateAvailableDevices();
00496
00497 AddMe();
00498 }
00499
00500 void RtAAudioDeviceList::init()
00501 {
00502
00503 }
00504
00505 RtAAudioDeviceList::~RtAAudioDeviceList()
00506 {
00507 }
00508
00509 void RtAAudioDeviceList::EnumerateAvailableDevices() throw ( Err )
00510 {
00511 RtAudio* instance = NULL;
00512
00513 try {
00514 instance = new RtAudio();
00515
00516 int numDevs = instance->getDeviceCount();
00517 int k;
00518 RtAudio::RTAUDIO_DEVICE devnfo;
00519
00520 mAvailableDevices.push_back("default");
00521 mDevIDs.push_back( 0 );
00522
00523 for ( k = 1; k <= numDevs; k++ )
00524 {
00525 instance->getDeviceInfo( k, &devnfo );
00526 mAvailableDevices.push_back( devnfo.name );
00527 mDevIDs.push_back( k );
00528 }
00529 delete instance;
00530 } catch( ::RtError& err ) {
00531 if ( instance )
00532 delete instance;
00533 Err new_err("RtAAudioDeviceList::EnumerateAvailableDevices failed");
00534 new_err.Embed( err.getMessage() );
00535 throw ( new_err );
00536 }
00537
00538 }
00539
00540 AudioDevice* RtAAudioDeviceList::Create( const std::string& name, const std::string& device )
00541 {
00542 unsigned int i = 0;
00543
00544 for ( i=0; i < mAvailableDevices.size(); i++ )
00545 {
00546 if ( device == mAvailableDevices[i] )
00547 return new RtAAudioDevice( name, mDevIDs[i] );
00548 }
00549 for ( i=0; i < mAvailableDevices.size(); i++ )
00550 {
00551 char tmp[16];
00552 sprintf(tmp,"%d",mDevIDs[i]);
00553 if ( device == tmp )
00554 return new RtAAudioDevice( name, mDevIDs[i] );
00555 }
00556
00557 return 0;
00558 }
00559
00560 RtAAudioDeviceList RtAAudioDeviceList::sDevices;
00561 }
00562
00563