ITANetAudioTest.cpp 3.73 KB
Newer Older
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
1
#include <cmath>
Anne Heimes's avatar
Anne Heimes committed
2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <string>

#include <ITANetAudioStreamingServer.h>
#include <ITANetAudioServer.h>
#include <ITANetAudioStream.h>
#include <ITAPortaudioInterface.h>
#include <ITAStreamFunctionGenerator.h>
#include <ITAStreamMultiplier1N.h>
#include <ITAException.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
12
#include <ITAFileDataSource.h>
Anne Heimes's avatar
Anne Heimes committed
13
#include <ITAStreamProbe.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
14
#include <ITAStreamPatchBay.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
15

Anne Heimes's avatar
Anne Heimes committed
16 17 18 19
#include <VistaBase/VistaStreamUtils.h>

using namespace std;

Anne Heimes's avatar
Anne Heimes committed
20
const static string g_sServerName = "137.226.61.67";
Anne Heimes's avatar
Anne Heimes committed
21 22 23 24 25 26
const static string g_sInputFilePath = "gershwin-mono.wav";
const static int g_iServerPort = 12480;
const static double g_dSampleRate = 44100;
const static int g_iBlockLength = 1024;
const static int g_iChannels = 300;

Anne's avatar
Anne committed
27 28 29
class CServer : public VistaThread
{
public:
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
30
	inline CServer( const string& sInputFilePath )
Anne Heimes's avatar
Anne Heimes committed
31 32 33 34 35 36 37
	{
		pInputFile = new ITAFileDatasource( sInputFilePath, g_iBlockLength );
		assert( pInputFile->GetNumberOfChannels() == 1 );
		pMuliplier = new ITAStreamMultiplier1N( pInputFile, g_iChannels );
		pStreamingServer = new CITANetAudioStreamingServer;

		pStreamingServer->SetInputStream( pMuliplier );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
38 39

		Run();
Anne's avatar
Anne committed
40
	};
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
41 42

	inline ~CServer( )
Anne's avatar
Anne committed
43
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
44
		delete pInputFile;
Anne's avatar
Anne committed
45 46 47
		delete pMuliplier;
		delete pStreamingServer;
	};
Anne's avatar
Anne committed
48

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
49
	void ThreadBody( )
Anne Heimes's avatar
Anne Heimes committed
50
	{
51
		vstr::out() << "[ NetAudioTestServer ] Starting net audio server and waiting for client connections on '" << g_sServerName << "' on port " << g_iServerPort << endl;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
52
		pStreamingServer->Start( g_sServerName, g_iServerPort );		
Anne's avatar
Anne committed
53
	};
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
54

Anne's avatar
Anne committed
55
private:
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
56
	ITAFileDatasource* pInputFile;
Anne's avatar
Anne committed
57 58
	ITAStreamMultiplier1N* pMuliplier;
	CITANetAudioStreamingServer* pStreamingServer;
Anne Heimes's avatar
Anne Heimes committed
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
};

int main( int, char** )
{
	// Sample server (forked away into a thread)
	CServer oServer( g_sInputFilePath );

	// Client dumping received stream and mixing down to two channels
	CITANetAudioStream oNetAudioStream( g_iChannels, g_dSampleRate, g_iBlockLength, 100 * g_iBlockLength );

	ITAStreamPatchbay oPatchbay( g_dSampleRate, g_iBlockLength );
	oPatchbay.AddInput( &oNetAudioStream );
	int iOutputID = oPatchbay.AddOutput( 2 );

	int N = int( oNetAudioStream.GetNumberOfChannels() );
	for ( int i = 0; i < N ; i++ )
		oPatchbay.ConnectChannels( 0, i, 0, i % 2, 1 / double( N ) );
	
	ITAStreamProbe oProbe( oPatchbay.GetOutputDatasource( iOutputID ), "ITANetAudioTest.stream.wav" );

	ITAPortaudioInterface ITAPA( g_dSampleRate, g_iBlockLength );
	ITAPA.Initialize();
	ITAPA.SetPlaybackDatasource( &oProbe );
	ITAPA.Open();
	ITAPA.Start();

	vstr::out() << "[ NetAudioTestClient ] Waiting 1 second (net audio stream not connected and playing back zeros)" << endl;
	ITAPA.Sleep( 1.0f );

	vstr::out() << "[ NetAudioTestClient ] Will now connect to net audio server '" << g_sServerName << "' on port " << g_iServerPort << endl;
	try
	{
		if ( !oNetAudioStream.Connect( g_sServerName, g_iServerPort ) )
			ITA_EXCEPT1( INVALID_PARAMETER, "Could not connect to net audio server" );
	}
	catch ( ITAException e )
	{
		vstr::warn() << "[ NetAudioTestClient ] Connection failed." << endl;
		vstr::err() << e << endl;
		return 255;
	}
	vstr::out() << "[ NetAudioTestClient ] Connected." << endl;

	// Playback
	float fSeconds = 5.0f;
	vstr::out() << "[ NetAudioTestClient ] Playback started, waiting " << fSeconds << " seconds" << endl;
	ITAPA.Sleep( fSeconds ); // blocking
	vstr::out() << "[ NetAudioTestClient ] Done." << endl;

	vstr::out() << "[ NetAudioTestClient ] Will now disconnect from net audio server '" << g_sServerName << "' and port " << g_iServerPort << endl;
	vstr::out() << "[ NetAudioTestClient ] Closing in 1 second (net audio stream not connected and playing back zeros)" << endl;
	ITAPA.Sleep( 1.0f );

	ITAPA.Stop( );
	ITAPA.Close( );
	ITAPA.Finalize( );

	return 0;
}