ITANetAudioStreamingClientTest.cpp 3.33 KB
Newer Older
Anne Heimes's avatar
Anne Heimes committed
1 2
#include <iostream>
#include <string>
Jonas Stienen's avatar
Jonas Stienen committed
3
#include <sstream>
Anne Heimes's avatar
Anne Heimes committed
4 5 6 7 8

#include <ITANetAudioStream.h>
#include <ITAPortaudioInterface.h>
#include <ITAStreamMultiplier1N.h>
#include <ITAException.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
9
#include <ITAFileDataSource.h>
Anne Heimes's avatar
Anne Heimes committed
10
#include <ITAStreamProbe.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
11
#include <ITAStreamPatchBay.h>
Anne Heimes's avatar
Anne Heimes committed
12
#include <ITAAsioInterface.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
13
#include <VistaBase/VistaTimeUtils.h>
Anne Heimes's avatar
Anne Heimes committed
14 15 16

using namespace std;

Anne Heimes's avatar
Anne Heimes committed
17
string g_sServerName = "137.226.61.85";
18 19
int g_iServerPort = 12480;
double g_dSampleRate = 44100.0;
Anne Heimes's avatar
Anne Heimes committed
20
int g_iBlockLength = 32;
21
int g_iChannels = 2;
Anne Heimes's avatar
Anne Heimes committed
22 23 24
int g_iTargetLatencySamples = 2 * g_iBlockLength; // 1.4512ms
int g_iRingBufferSize = 2 * g_iTargetLatencySamples;
double g_dPlaybackDuration = 10 ; // seconds
25 26

int main( int argc, char* argv[] )
Anne Heimes's avatar
Anne Heimes committed
27
{
Anne Heimes's avatar
Anne Heimes committed
28
	if( argc >= 8 )
Anne Heimes's avatar
Anne Heimes committed
29
	{
30
		g_sServerName = argv[ 1 ];
Anne Heimes's avatar
Anne Heimes committed
31

32
		if( argc >= 3 )
Anne Heimes's avatar
Anne Heimes committed
33
		{
34 35 36
			g_iServerPort = atoi( argv[ 2 ] );
			g_dSampleRate = strtod( argv[ 3 ], NULL );
			g_iBlockLength = atoi( argv[ 4 ] );
Anne Heimes's avatar
Anne Heimes committed
37 38 39
			g_iChannels = atoi(argv[5]);
			g_iTargetLatencySamples = atoi(argv[6]);
			g_iRingBufferSize = atoi(argv[7]);
Anne Heimes's avatar
Anne Heimes committed
40
		}
Anne Heimes's avatar
Anne Heimes committed
41

Anne Heimes's avatar
Anne Heimes committed
42 43
		if( argc >= 9 )
			g_dPlaybackDuration = strtod( argv[ 8 ], NULL );;
44 45 46
	}
	else
	{
Anne Heimes's avatar
Anne Heimes committed
47
		cout << "Syntax: ServerName ServerPort SampleRate BufferSize Channel TargetLatencySamples RingBufferSize" << endl;
48 49
		cout << "Using default values ..." << endl;
	}
Anne Heimes's avatar
Anne Heimes committed
50

51
	cout << "Number of NetAudio channels: " << g_iChannels << endl;
Anne Heimes's avatar
Anne Heimes committed
52

53
	CITANetAudioStream oNetAudioStream( g_iChannels, g_dSampleRate, g_iBlockLength, g_iRingBufferSize );
Anne Heimes's avatar
Anne Heimes committed
54

Jonas Stienen's avatar
Jonas Stienen committed
55
	stringstream ss;
Anne Heimes's avatar
Anne Heimes committed
56
	ss << "ITANetAudioStreamingClientTest";
Jonas Stienen's avatar
Jonas Stienen committed
57 58
	ss << "_C" << g_iChannels;
	ss << "_B" << g_iBlockLength;
Anne Heimes's avatar
Anne Heimes committed
59
	ss << "_TL" << g_iTargetLatencySamples;
Jonas Stienen's avatar
Jonas Stienen committed
60 61 62
	ss << "_RB" << g_iRingBufferSize;
	oNetAudioStream.SetNetAudioStreamingLoggerBaseName( ss.str() );

63 64 65
	ITAStreamPatchbay oPatchbay( g_dSampleRate, g_iBlockLength );
	oPatchbay.AddInput( &oNetAudioStream );
	int iOutputID = oPatchbay.AddOutput( 2 );
Anne Heimes's avatar
Anne Heimes committed
66

67 68 69
	int N = int( oNetAudioStream.GetNumberOfChannels() );
	for( int i = 0; i < N; i++ )
		oPatchbay.ConnectChannels( 0, i, 0, i % 2, 1 / double( N ) );
Anne Heimes's avatar
Anne Heimes committed
70

71
	ITAStreamProbe oProbe( oPatchbay.GetOutputDatasource( iOutputID ), "ITANetAudioTest.netstream.wav" );
Anne Heimes's avatar
Anne Heimes committed
72 73


74
	cout << "Will attempt to connect to '" << g_sServerName << "' on port " << g_iServerPort << endl;
Anne Heimes's avatar
Anne Heimes committed
75

76
	ITAsioInitializeLibrary();
Anne Heimes's avatar
Anne Heimes committed
77
	ITAsioInitializeDriver( "ASIO MADIface USB" );
78 79 80 81 82 83 84 85 86 87 88

	long lBuffersize, lDummy;
	ITAsioGetBufferSize( &lDummy, &lDummy, &lBuffersize, &lDummy );
	ITAsioSetSampleRate( ( ASIOSampleRate ) g_dSampleRate );
	long lNumInputChannels, lNumOutputChannels;
	ITAsioGetChannels( &lNumInputChannels, &lNumOutputChannels );
	ITAsioCreateBuffers( 0, 2, lBuffersize );
	ITAsioSetPlaybackDatasource( &oProbe );
	
	ITAsioStart();
	cout << "ASIO streaming started." << endl;
Anne Heimes's avatar
Anne Heimes committed
89

90 91 92 93 94 95
	try
	{
		cout << "Connecting to NetAudio server ..." << endl;
		if( !oNetAudioStream.Connect( g_sServerName, g_iServerPort ) )
			ITA_EXCEPT1( INVALID_PARAMETER, "Could not connect to server" );
		cout << "Connection established." << endl;
Anne Heimes's avatar
Anne Heimes committed
96

97 98
		cout << "Will now stream for " << g_dPlaybackDuration << " seconds ..." << endl;
		VistaTimeUtils::Sleep( int( g_dPlaybackDuration ) * 1000 );
Anne Heimes's avatar
Anne Heimes committed
99
	}
100 101
	catch( ITAException& e )
	{
Anne Heimes's avatar
Anne Heimes committed
102 103
		cerr << e << endl;
	}
104 105 106 107 108 109

	cout << "Stopping ASIO stream and finalizing." << endl;
	ITAsioStop();

	ITAsioDisposeBuffers();
	ITAsioFinalizeDriver();
Anne Heimes's avatar
Anne Heimes committed
110
	ITAsioFinalizeLibrary();
Anne Heimes's avatar
Anne Heimes committed
111 112
	return 0;
}