Commit b3805a29 authored by Anne's avatar Anne

Merged develop into ba_2016_heimes

parents 212b3edd 75f6836b
......@@ -8,6 +8,7 @@ include( VistaCommon )
# dependencies
vista_use_package( ITABase REQUIRED FIND_DEPENDENCIES )
vista_use_package( VistaCoreLibs REQUIRED COMPONENTS VistaInterProcComm FIND_DEPENDENCIES )
vista_use_package( ASIO QUIET )
vista_use_package( Portaudio QUIET )
vista_use_package( JACK QUIET )
......@@ -46,6 +47,8 @@ set( ITADataSourcesHeader
"include/ITADataSourcesDefinitions.h"
"include/ITAFileDataSink.h"
"include/ITAFileDataSource.h"
"include/ITANetAudioStream.h"
"include/ITANetAudioSampleServer.h"
"include/ITAPeakDetector.h"
"include/ITARMSDetector.h"
"include/ITAStreamAmplifier.h"
......@@ -66,6 +69,7 @@ set( ITADataSourcesSources
"src/ITADataSourceRealization.cpp"
"src/ITAFileDataSink.cpp"
"src/ITAFileDataSource.cpp"
"src/ITANetAudioStream.cpp"
"src/ITAPeakDetector.cpp"
"src/ITARMSDetector.cpp"
"src/ITAStreamAmplifier.cpp"
......@@ -131,8 +135,6 @@ endif( NOT WIN32)
add_library( ITADataSources ${ITADataSourcesHeader} ${ITADataSourcesSources} )
target_link_libraries( ITADataSources ${VISTA_USE_PACKAGE_LIBRARIES} )
set( BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_TEMP} )
......
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2016
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_NET_AUDIO_SAMPLE_SERVER
#define INCLUDE_WATCHER_ITA_NET_AUDIO_SAMPLE_SERVER
#include <ITADataSourcesDefinitions.h>
#include <string>
#include <vector>
#include <ITASampleFrame.h>
class ITADatasource;
class CITANetAudioStreamServer;
//! Network audio sample server (for connecting a net audio stream)
/**
* Audio sample transmitter for a networked signal source that can connect via TCP/IP.
*
* \sa CITANetAudioStream
* \note not thread-safe
*/
class ITA_DATA_SOURCES_API CITANetAudioSampleServer
{
public:
CITANetAudioSampleServer();
virtual ~CITANetAudioSampleServer();
bool Start( const std::string& sAddress, int iPort );
bool IsClientConnected() const;
std::string GetNetworkAddress() const;
int GetNetworkPort() const;
int Stop();
void SetInputStream( ITADatasource* pInStream );
int GetNetStreamBlocklength() const;
int GetNetStreamNumberOfChannels() const;
double GetNetStreamSampleRate() const;
protected:
int Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples );
private:
CITANetAudioStreamServer* m_pNetAudioServer;
ITASampleFrame m_sfTempTransmitBuffer;
ITADatasource* m_pInputStream;
friend class CITANetAudioStreamServer;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_SAMPLE_SERVER
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2016
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_NET_AUDIO_STREAM
#define INCLUDE_WATCHER_ITA_NET_AUDIO_STREAM
#include <ITADataSourcesDefinitions.h>
#include <ITADataSource.h>
#include <ITASampleFrame.h>
#include <string>
#include <vector>
class ITA_DATA_SOURCES_API CITANetAudioStreamConnection;
//! Network audio stream
/**
* Audio streaming for a signal source that is connected via TCP/IP.
*
* \note not thread-safe
*/
class CITANetAudioStream : public ITADatasource
{
public:
CITANetAudioStream( int iChannels, double dSamplingRate, int iBufferSize, int iRingBufferCapacity );
virtual ~CITANetAudioStream();
bool Connect( const std::string& sAddress, int iPort );
bool IsConnected() const;
std::string GetNetworkAddress() const;
int GetNetworkPort() const;
int GetRingBufferSize() const;
unsigned int GetBlocklength() const;
unsigned int GetNumberOfChannels() const;
double GetSampleRate() const;
const float* GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* );
void IncrementBlockPointer();
protected:
int Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples );
private:
CITANetAudioStreamConnection* m_pNetAudioProducer;
double m_dSampleRate;
ITASampleFrame m_sfOutputStreamBuffer;
int m_iReadCursor; //!< Cursor where samples will be consumed from ring buffer on next block
int m_iWriteCursor; //!< Cursor where samples will feeded into ring buffer from net audio producer
ITASampleFrame m_sfRingBuffer;
friend class CITANetAudioStreamConnection;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_STREAM
......@@ -23,10 +23,12 @@
#include <ITADataSourcesDefinitions.h>
#include <ITADataSourceRealization.h>
class ITA_DATA_SOURCES_API ITAStreamFunctionGenerator : public ITADatasourceRealization {
class ITA_DATA_SOURCES_API ITAStreamFunctionGenerator : public ITADatasourceRealization
{
public:
//! Signal functions
enum {
enum SignalFunctions
{
SINE = 0, //!< Sine signal1
TRIANGLE, //!< Triangle signal
SAWTOOTH, //!< Sawtooth signal
......@@ -43,9 +45,7 @@ public:
* \param dSamplerate Sampling rate [Hz]
* \param uiBlocklength Blocklength [samples]
*/
ITAStreamFunctionGenerator(unsigned int uiChannels,
double dSamplerate,
unsigned int uiBlocklength);
ITAStreamFunctionGenerator( unsigned int uiChannels, double dSamplerate, unsigned int uiBlocklength );
//! Constructor
/**
......@@ -59,14 +59,8 @@ public:
* \param dAmplitude Signal amplitude
* \param bPeriodic Generate a periodic signal?
*/
ITAStreamFunctionGenerator(unsigned int uiChannels,
double dSamplerate,
unsigned int uiBlocklength,
int iSignalFunction,
double dFrequency,
float fAmplitude,
bool bPeriodic);
ITAStreamFunctionGenerator( unsigned int uiChannels, double dSamplerate, unsigned int uiBlocklength, int iSignalFunction, double dFrequency, float fAmplitude, bool bPeriodic );
//! Destructor
virtual ~ITAStreamFunctionGenerator();
......
#include <ITANetAudioStream.h>
// ITA includes
#include <ITAException.h>
// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaInterProcComm/IPNet/VistaTCPServer.h>
#include <VistaInterProcComm/IPNet/VistaTCPSocket.h>
//#include <VistaBase/VistaTimeUtils.h>
#include <VistaInterProcComm/IPNet/VistaIPAddress.h>
// STL
#include <cmath>
class CITANetAudioStreamConnection : public VistaThreadLoop
{
public:
enum MessageType
{
NET_MESSAGE_NONE = 0,
NET_MESSAGE_OPEN,
NET_MESSAGE_CLOSE,
NET_MESSAGE_SAMPLES,
};
inline CITANetAudioStreamConnection( CITANetAudioStream* pParent )
: m_pParent( pParent )
, m_pConnection( NULL )
, m_bStopIndicated( false )
{
};
inline bool Connect( const std::string& sAddress, int iPort )
{
if( m_pConnection )
ITA_EXCEPT1( MODAL_EXCEPTION, "This net stream is already connected" );
// Attempt to connect and check parameters
m_pConnection = new VistaConnectionIP( VistaConnectionIP::CT_TCP, sAddress, iPort );
if( !m_pConnection->GetIsConnected() )
{
delete m_pConnection;
m_pConnection = NULL;
return false;
}
int iMessageType = NET_MESSAGE_OPEN;
m_pConnection->Send( &iMessageType, sizeof( int ) );
int iNumChannels = ( int ) m_pParent->GetNumberOfChannels();
m_pConnection->Send( &iNumChannels, sizeof( int ) );
double dSampleRate = m_pParent->GetSampleRate();
m_pConnection->Send( &dSampleRate, sizeof( double ) );
int iBlockLength = ( int ) m_pParent->GetBlocklength();
m_pConnection->Send( &iBlockLength, sizeof( int ) );
int iRingBufferSize = ( int ) m_pParent->GetRingBufferSize();
m_pConnection->Send( &iRingBufferSize, sizeof( int ) );
m_pConnection->WaitForSendFinish();
int iServerMessageType;
m_pConnection->Receive( &iServerMessageType, sizeof( int ) );
Run();
};
inline void Disconnect()
{
m_bStopIndicated = true;
StopGently( true );
delete m_pConnection;
m_pConnection = NULL;
m_bStopIndicated = false;
};
inline ~CITANetAudioStreamConnection()
{
int iMessageType = NET_MESSAGE_CLOSE;
m_pConnection->Send( &iMessageType, sizeof( int ) );
};
inline bool LoopBody()
{
if( m_bStopIndicated )
return true;
// Receive messages
while( true )
{
m_pConnection->Receive( NULL, 0 ); // @todo: receive messages and react
int iNumSamples = 12;
if( true )
m_pParent->Transmit( m_sfReceivingBuffer, iNumSamples );
}
};
private:
VistaConnectionIP* m_pConnection;
CITANetAudioStream* m_pParent;
ITASampleFrame m_sfReceivingBuffer;
bool m_bStopIndicated;
};
CITANetAudioStream::CITANetAudioStream( int iChannels, double dSamplingRate, int iBufferSize, int iRingBufferCapacity )
: m_sfOutputStreamBuffer( iChannels, iBufferSize, true )
, m_dSampleRate( dSamplingRate )
, m_sfRingBuffer( iChannels, iRingBufferCapacity, true )
{
m_pNetAudioProducer = new CITANetAudioStreamConnection( this );
}
bool CITANetAudioStream::Connect( const std::string& sAddress, int iPort )
{
return m_pNetAudioProducer->Connect( sAddress, iPort );
}
CITANetAudioStream::~CITANetAudioStream()
{
delete m_pNetAudioProducer;
}
const float* CITANetAudioStream::GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* )
{
// @todo: implement cyclic read from ring buffer
return m_sfOutputStreamBuffer[ uiChannel ].GetData();
}
void CITANetAudioStream::IncrementBlockPointer()
{
// Increment read cursor by one audio block and wrap around if exceeding ring buffer
m_iReadCursor = ( m_iReadCursor + m_sfOutputStreamBuffer.GetLength() ) % m_sfRingBuffer.GetLength();
}
int CITANetAudioStream::Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples )
{
ITA_EXCEPT0( NOT_IMPLEMENTED );
}
int CITANetAudioStream::GetRingBufferSize() const
{
return m_sfRingBuffer.GetLength();
}
unsigned int CITANetAudioStream::GetBlocklength() const
{
return ( unsigned int ) m_sfOutputStreamBuffer.GetLength();
}
unsigned int CITANetAudioStream::GetNumberOfChannels() const
{
return ( unsigned int ) m_sfOutputStreamBuffer.channels();
}
double CITANetAudioStream::GetSampleRate() const
{
return m_dSampleRate;
}
......@@ -30,3 +30,23 @@ vista_install( ITANAPlayerTest )
vista_create_default_info_file( ITANAPlayerTest )
set_property( TARGET ITANAPlayerTest PROPERTY FOLDER "ITACoreLibs/Tests/ITADataSources/NetAudio" )
add_executable( ITANetAudioSampleServerTest ITANetAudioSampleServerTest.cpp )
target_link_libraries( ITANetAudioSampleServerTest ${VISTA_USE_PACKAGE_LIBRARIES} )
vista_configure_app( ITANetAudioSampleServerTest )
vista_install( ITANetAudioSampleServerTest )
vista_create_default_info_file( ITANetAudioSampleServerTest )
set_property( TARGET ITANetAudioSampleServerTest PROPERTY FOLDER "ITACoreLibs/Tests/ITADataSources/NetAudio" )
add_executable( ITANetAudioClientTest ITANetAudioClientTest.cpp )
target_link_libraries( ITANetAudioClientTest ${VISTA_USE_PACKAGE_LIBRARIES} )
vista_configure_app( ITANetAudioClientTest )
vista_install( ITANetAudioClientTest )
vista_create_default_info_file( ITANetAudioClientTest )
set_property( TARGET ITANetAudioClientTest PROPERTY FOLDER "ITACoreLibs/Tests/ITADataSources/NetAudio" )
#include <iostream>
#include <string>
#include <ITANetAudioStream.h>
#include <ITAPortaudioInterface.h>
#include <ITAStreamMultiplier1N.h>
#include <ITAException.h>
using namespace std;
static string g_sServerName = "localhost";
static int g_iServerPort = 12480;
static double g_dSampleRate = 44.1e3;
static int g_iBufferSize = 256;
int main( int , char** )
{
CITANetAudioStream oNetAudioStream( 1, g_dSampleRate, g_iBufferSize, 4 * g_iBufferSize );
ITAStreamMultiplier1N oMultiplier( &oNetAudioStream, 2 );
ITAPortaudioInterface ITAPA( g_dSampleRate, g_iBufferSize );
ITAPA.Initialize();
ITAPA.SetPlaybackDatasource( &oMultiplier );
ITAPA.Open();
ITAPA.Start();
cout << "Waiting 3 seconds (net audio stream not connected and returning zeros)" << endl;
ITAPA.Sleep( 3.0f );
cout << "Will now connect to '" << g_sServerName << "' on port " << g_iServerPort << endl;
try
{
oNetAudioStream.Connect( g_sServerName, g_iServerPort );
}
catch( ITAException e )
{
cout << "Connection failed." << endl;
cerr << e << endl;
return 255;
}
cout << "Connected." << endl;
// Playback
float fSeconds = 10.0f;
cout << "Playback started, waiting " << fSeconds << " seconds" << endl;
ITAPA.Sleep( fSeconds ); // blocking
cout << "Done." << endl;
cout << "Will now disconnect from '" << g_sServerName << "' and port " << g_iServerPort << endl;
cout << "Closing in 3 seconds (net audio stream not connected and returning zeros)" << endl;
ITAPA.Sleep( 3.0f );
ITAPA.Stop();
ITAPA.Close();
ITAPA.Finalize();
return 0;
}
#include <iostream>
#include <string>
#include <ITANetAudioSampleServer.h>
#include <ITAStreamFunctionGenerator.h>
using namespace std;
static string g_sServerName = "localhost";
static int g_iServerPort = 12480;
static double g_dSampleRate = 44.1e3;
static int g_iBlockLength = 265;
int main( int , char** )
{
ITAStreamFunctionGenerator oGenerator( 1, g_dSampleRate, g_iBlockLength );
oGenerator.SetFunction( ITAStreamFunctionGenerator::SINE );
oGenerator.SetFrequency( 567.89f );
CITANetAudioSampleServer oSampleServer;
oSampleServer.SetInputStream( &oGenerator );
cout << "Starting server and waiting for connections on '" << oSampleServer.GetNetworkAddress() << "' on port " << oSampleServer.GetNetworkPort() << endl;
oSampleServer.Start( g_sServerName, g_iServerPort );
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