ITANetAudioSampleServer.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * ----------------------------------------------------------------
 *
 *		ITA core libs
 *		(c) Copyright Institute of Technical Acoustics (ITA)
 *		RWTH Aachen University, Germany, 2015-2017
 *
 * ----------------------------------------------------------------
 *				    ____  __________  _______
 *				   //  / //__   ___/ //  _   |
 *				  //  /    //  /    //  /_|  |
 *				 //  /    //  /    //  ___   |
 *				//__/    //__/    //__/   |__|
 *
 * ----------------------------------------------------------------
 *
 */

#ifndef INCLUDE_WATCHER_ITA_NET_AUDIO_SAMPLE_SERVER
#define INCLUDE_WATCHER_ITA_NET_AUDIO_SAMPLE_SERVER

#include <ITADataSourcesDefinitions.h>
#include <ITANetAudioStreamingServer.h>
#include <ITADataSourceRealization.h>


27 28 29 30 31 32 33 34
//! Sample-generation class with abstract method for providing samples
/*
 * This ready-to-use class helps to provide samples for a NetAudio streaming server with
 * a single method for processing that has to be implemented ...
 * ... just derive and implement Process() method. Have a look at Zero() method
 * for exemplary usage of sample buffer.
 */ 
class ITA_DATA_SOURCES_API CITASampleProcessor : public ITADatasourceRealization
35 36
{
public:
37 38 39 40 41 42 43
	//! Create a sample processor with streaming parameters
	/*
	 * @param[in] iNumChannels Channels provided
	 * @param[in] dSampleRate Audio processing sampling rate
	 * @param[in] iBlockLength Audio processing block length / buffer size
	 */ 
	inline CITASampleProcessor( const int iNumChannels, const double dSampleRate, const int iBlockLength )
44 45
		: ITADatasourceRealization( ( unsigned int ) ( iNumChannels ), dSampleRate, ( unsigned int ) ( iBlockLength ) )
	{
46
		m_vvfSampleBuffer.resize( iNumChannels );
47
		for( size_t c = 0; c < iNumChannels; c++ )
48 49 50
			m_vvfSampleBuffer[ c ].resize( iBlockLength );

		Zero();
51 52
	};

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	//! Sets all channels and samples to zero
	inline void Zero()
	{
		/* 
		 * Use this as an example how to work with the buffer structure.
		*/
		
		// Iterate over channels
		for( size_t c = 0; c < m_vvfSampleBuffer.size(); c++ ) 
		{
			std::vector< float >& vfSingleChannelSampleBuffer( m_vvfSampleBuffer[ c ] ); // One channel
			
			// Iterate over samples of channel
			for( size_t n = 0; n < vfSingleChannelSampleBuffer.size(); n++ )
			{
				float& fSample( vfSingleChannelSampleBuffer[ n ] ); // One sample
				fSample = 0.0f; // -> Manipulation
			}
		}
	};
	
74 75 76 77 78 79
	//! Process samples (overwrite this virtual method)
	/**
	  * Method that is called in audio streaming context and requests
	  * to produce or copy audio samples into the internal buffer m_vvfSampleBuffer
	  *
	  * @param[in] pStreamInfo Information over streaming status, i.e. sample count and time stamp
80
	  *
81 82 83 84
	  */
	virtual void Process( const ITAStreamInfo* pStreamInfo ) =0;

protected:
85
	std::vector< std::vector< float > > m_vvfSampleBuffer; //!< Multi-channel sample buffer to be filled
86

87
private:
88
	//! Delegate internal buffer to audio stream (ITADatasource)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	inline void ProcessStream( const ITAStreamInfo* pInfo )
	{
		Process( pInfo );

		for( size_t c = 0; c < m_vvfSampleBuffer.size(); c++ )
		{
			float* pfData = GetWritePointer( ( unsigned int ) ( c ) );
			for( size_t n = 0; n < m_vvfSampleBuffer[ c ].size(); n++ )
				pfData[ n ] = m_vvfSampleBuffer[ c ][ n ];
		}

		IncrementWritePointer();
	};
};

104
//! Network audio sample server (for providing samples via derived generator class)
105 106 107
/**
  * Audio sample transmitter for a networked sample callback function that can connect via TCP/IP.
  *
108
  * @sa CITANetAudioStream CITANetAudioStreamingServer CITASampleProcessor
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  * @note not thread-safe
  */
class ITA_DATA_SOURCES_API CITANetAudioSampleServer : public CITANetAudioStreamingServer
{
public:
	inline CITANetAudioSampleServer( CITASampleProcessor* pProcessor )
		: m_pSampleProcessor( pProcessor )
	{
		SetInputStream( m_pSampleProcessor );
	};

	inline ~CITANetAudioSampleServer()
	{};

private:
	//! Prohibit public access to streaming context and delegate
	inline void SetInputStream( ITADatasource* pDataSource )
	{
		CITANetAudioStreamingServer::SetInputStream( pDataSource );
	};

	//! Prohibit public access to streaming context and delegate
	inline ITADatasource* GetInputStream() const
	{
		return CITANetAudioStreamingServer::GetInputStream();
	};

	CITASampleProcessor* m_pSampleProcessor; //!< Callback / sample processor
};

#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_SAMPLE_SERVER