Implemented threaded asio

parent 0d6b050b
......@@ -4,6 +4,7 @@
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <host/asiodrivers.h>
#include <common/asio.h>
......@@ -11,134 +12,133 @@
#include <VistaBase/VistaTimeUtils.h>
#include <VistaInterProcComm/Concurrency/VistaThread.h>
// Errors are validated ussing assertions. Run this in debug, only.
using namespace std;
ASIOTime* AsioBufferSwitchTimeInfo( ASIOTime *timeInfo, long index, ASIOBool processNow )
AsioDriverList g_oAsioDriverList;
class CThreadedASIO;
typedef map< long, CThreadedASIO* > CThreadedASIORegistry;
CThreadedASIORegistry g_oRegistry;
class CThreadedASIO : public VistaThread
{
/*
for( int i = 0; i < oAsioDriverInfo.inputBuffers + asioDriverInfo.outputBuffers; i++ )
public:
inline CThreadedASIO( const int iDriverIndex )
: m_iDriverIndex( iDriverIndex )
{
if( asioDriverInfo.bufferInfos[ i ].isInput == TRUE )
assert( iDriverIndex < g_oAsioDriverList.asioGetNumDev() && iDriverIndex > 0 );
m_sDriverName.resize( 256 );
m_oDriver.asioGetDriverName( iDriverIndex, &m_sDriverName[ 0 ], int( m_sDriverName.size() ) );
m_sDriverName = std::string( m_sDriverName.c_str() ); // Strip
cout << "New threaded ASIO instance with driver name: " << m_sDriverName << endl;
bool bLoadSuccess = m_oDriver.loadDriver( &m_sDriverName[ 0 ] );
ASIODriverInfo oDriverInfo;
oDriverInfo.asioVersion = 2;
assert( ASIOInit( &oDriverInfo ) == ASE_OK );
ASIOCallbacks oAsioCallback;
//long AsioMessages( long selector, long value, void*, double* )
oAsioCallback.asioMessage = long( CThreadedASIO::* )( long, long, void*, double* );
oAsioCallback.bufferSwitch = &( CThreadedASIO::AsioBufferSwitch );
oAsioCallback.bufferSwitchTimeInfo = &( CThreadedASIO::AsioBufferSwitchTimeInfo );
oAsioCallback.sampleRateDidChange = &( CThreadedASIO::AsioSampleRateChanged );
vector< ASIOBufferInfo > voBufferInfos( 4 );
voBufferInfos[ 0 ].buffers;
voBufferInfos[ 0 ].channelNum = 0;
voBufferInfos[ 0 ].isInput = ASIOTrue;
voBufferInfos[ 1 ].buffers;
voBufferInfos[ 1 ].channelNum = 1;
voBufferInfos[ 1 ].isInput = ASIOTrue;
voBufferInfos[ 2 ].buffers;
voBufferInfos[ 2 ].channelNum = 0;
voBufferInfos[ 2 ].isInput = ASIOFalse;
voBufferInfos[ 3 ].buffers;
voBufferInfos[ 3 ].channelNum = 1;
voBufferInfos[ 3 ].isInput = ASIOFalse;
ASIOSampleRate fs;
assert( ASIOGetSampleRate( &fs ) == ASE_OK );
long minSize, maxSize, preferredSize, granularity;
ASIOError aeResult = ASIOGetBufferSize( &minSize, &maxSize, &preferredSize, &granularity );
preferredSize = ( preferredSize == 0 ? min( 1024, maxSize ) : preferredSize );
assert( ASE_OK == ASIOCreateBuffers( &voBufferInfos.front(), 4, preferredSize, &oAsioCallback ) );
g_oRegistry[ VistaThread::GetThreadIdentity() ] = this;
};
inline void StartStreaming()
{
assert( asioDriverInfo.channelInfos[ i ].type == ASIOSTInt24LSB );
}
}
*/
assert( ASE_OK == ASIOStart() );
};
return timeInfo;
};
inline void ThreadBody()
{
cout << "[" << m_sDriverName << "] Tic Toc" << endl;
VistaTimeUtils::Sleep( 1000 );
};
void AsioBufferSwitch( long index, ASIOBool processNow )
{
ASIOTime timeInfo;
memset( &timeInfo, 0, sizeof( timeInfo ) );
inline void StopStreaming()
{
assert( ASE_OK == ASIOStop() );
};
if( ASIOGetSamplePosition( &timeInfo.timeInfo.samplePosition, &timeInfo.timeInfo.systemTime ) == ASE_OK )
timeInfo.timeInfo.flags = kSystemTimeValid | kSamplePositionValid;
inline static ASIOTime* AsioBufferSwitchTimeInfo( ASIOTime *timeInfo, long index, ASIOBool processNow )
{
return timeInfo;
};
inline static void AsioBufferSwitch( long index, ASIOBool processNow )
{
ASIOTime timeInfo;
memset( &timeInfo, 0, sizeof( timeInfo ) );
AsioBufferSwitchTimeInfo( &timeInfo, index, processNow );
};
if( ASIOGetSamplePosition( &timeInfo.timeInfo.samplePosition, &timeInfo.timeInfo.systemTime ) == ASE_OK )
timeInfo.timeInfo.flags = kSystemTimeValid | kSamplePositionValid;
void AsioSampleRateChanged( ASIOSampleRate fs )
{
cout << "Sample rate changed to " << fs << endl;
return;
};
AsioBufferSwitchTimeInfo( &timeInfo, index, processNow );
long AsioMessages( long selector, long value, void*, double* )
{
return 0;
long iThreadID = VistaThread::GetThreadIdentity();
g_oRegistry[ iThreadID ];
};
inline static void AsioSampleRateChanged( ASIOSampleRate fs )
{
cout << "Sample rate changed to " << fs << endl;
return;
};
inline long AsioMessages( long selector, long value, void*, double* )
{
return 0;
};
private:
int m_iDriverIndex;
std::string m_sDriverName;
AsioDrivers m_oDriver;
};
int main( int argc, char* argv[] )
int main( int, char[] )
{
ASIOError ae;
AsioDriverList* pDrivers = new AsioDriverList();
long nNumDrivers = pDrivers->asioGetNumDev();
cout << "Number of ASIO drivers: " << nNumDrivers << endl;
delete pDrivers;
CThreadedASIO oAsio1( 2 );
CThreadedASIO oAsio2( 2 );
for( int i = 0; i < nNumDrivers; i++ )
{
AsioDrivers* pDriver = new AsioDrivers();
string sDriverName;
sDriverName.resize( 256 );
pDriver->asioGetDriverName( i, &sDriverName[ 0 ], sDriverName.size() );
sDriverName = std::string( sDriverName.c_str() ); // Strip
cout << "Driver name: " << sDriverName << "." << endl;
bool bLoadSuccess = pDriver->loadDriver( &sDriverName[ 0 ] );
if( bLoadSuccess )
{
string sDriverPath;
sDriverPath.resize( 256 );
pDriver->asioGetDriverPath( i, &sDriverPath[ 0 ], sDriverPath.size() );
sDriverPath = std::string( sDriverPath.c_str() ); // Strip
cout << "Driver path: " << sDriverPath << endl;
cout << "Current driver index: " << pDriver->getCurrentDriverIndex() << endl;
string sCurrentDriverName;
sCurrentDriverName.resize( 256 );
pDriver->getCurrentDriverName( &sCurrentDriverName[ 0 ] );
sCurrentDriverName = std::string( sCurrentDriverName.c_str() ); // Strip
cout << "Current driver name: " << sCurrentDriverName << endl;
ASIODriverInfo oDriverInfo;
oDriverInfo.asioVersion = 2;
if( ASIOInit( &oDriverInfo ) != ASE_OK )
{
cout << "Could not initialize " << sCurrentDriverName << ", skipping." << endl;
continue;
}
ASIOCallbacks oAsioCallback;
oAsioCallback.asioMessage = &AsioMessages;
oAsioCallback.bufferSwitch = &AsioBufferSwitch;
oAsioCallback.bufferSwitchTimeInfo = &AsioBufferSwitchTimeInfo;
oAsioCallback.sampleRateDidChange = &AsioSampleRateChanged;
vector< ASIOBufferInfo > voBufferInfos( 4 );
voBufferInfos[ 0 ].buffers;
voBufferInfos[ 0 ].channelNum = 0;
voBufferInfos[ 0 ].isInput = ASIOTrue;
voBufferInfos[ 1 ].buffers;
voBufferInfos[ 1 ].channelNum = 1;
voBufferInfos[ 1 ].isInput = ASIOTrue;
voBufferInfos[ 2 ].buffers;
voBufferInfos[ 2 ].channelNum = 0;
voBufferInfos[ 2 ].isInput = ASIOFalse;
voBufferInfos[ 3 ].buffers;
voBufferInfos[ 3 ].channelNum = 1;
voBufferInfos[ 3 ].isInput = ASIOFalse;
ASIOSampleRate fs;
if( ASIOGetSampleRate( &fs ) == ASE_NotPresent )
continue;
long minSize, maxSize, preferredSize, granularity;
ASIOError aeResult = ASIOGetBufferSize( &minSize, &maxSize, &preferredSize, &granularity );
preferredSize = ( preferredSize == 0 ? min( 1024, maxSize ) : preferredSize );
ae = ASIOCreateBuffers( &voBufferInfos.front(), 4, preferredSize, &oAsioCallback );
assert( ae == ASE_OK );
ae = ASIOStart();
assert( ae == ASE_OK );
VistaTimeUtils::Sleep( 2e3 );
ae = ASIOStop();
assert( ae == ASE_OK );
}
pDriver->removeCurrentDriver();
delete pDriver;
}
oAsio1.StartStreaming();
oAsio2.StartStreaming();
VistaTimeUtils::Sleep( 3000 );
oAsio1.StopStreaming();
oAsio2.StopStreaming();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment