RainyDay.cpp 6.57 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
2
3
4
// ITA includes
#include <ITAException.h>
#include <ITASampleClock.h>
#include <ITANumericUtils.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
5
#include <ITADataSourceUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
6
7
#include <ITAAsioInterface.h>
#include <ITASampleFrame.h>
8
#include <ITAStopWatch.h>
Jonas Stienen's avatar
Jonas Stienen committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <ITAConstants.h>

// ITA sampler
#include <ITASoundSampler.h>
#include <ITASoundSamplePool.h>

// ViSTA includes
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <VistaBase/VistaTimeUtils.h>
#include <VistaTools/VistaRandomNumberGenerator.h>

// OpenDAFF
#include <DAFF.h>

// STL includes
#include <time.h>
#include <iostream>
#include <conio.h>
#include "ITAStreamProbe.h"


using namespace std;

class CRainMaker : public VistaThreadLoop
{
public:
	CRainMaker()
	{
		dUpdateRate = 1.0f; // Hertz
		dRainDropDiameter = 2.5e-3; // m
		dRainPrecipitation = 5.0f; // mm/hour
		dListenerGroundDistance = 1.7; // m
		dRainDropDistanceThreshold = 5; // m

		m_iTimecodeCount = 0;
	}

	bool LoopBody()
	{
		// Threshold underrun, no individual drops audible
		if( dRainDropDistanceThreshold - dListenerGroundDistance < 0 )
			return true;

		double dSampleRate = pDirectionalRainData->getSamplerate();
		int iLength = pDirectionalRainData->getFilterLength();
		if( m_sfRainDropletContainer.length() != iLength )
			m_sfRainDropletContainer.init( 2, iLength, false );

		oStopWatch.start();

		// Random rain process

		double dGroundCircleDiameter = sqrt( dRainDropDistanceThreshold * dRainDropDistanceThreshold - dListenerGroundDistance * dListenerGroundDistance );
		double dGroundCircleArea = ITAConstants::PI_D * dGroundCircleDiameter * dGroundCircleDiameter / 4.0f;

Dipl.-Ing. Jonas Stienen's avatar
Style    
Dipl.-Ing. Jonas Stienen committed
64
		//double dRainDropVolume = ITAConstants::PI_D * dRainDropDiameter * dRainDropDiameter * dRainDropDiameter / 6.0f;
Jonas Stienen's avatar
Jonas Stienen committed
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
118
119
120
121
122
123
		double dRainTotalVolumePerSecond = dRainPrecipitation * dGroundCircleArea / 3.6f; // cubic meter
		
		assert( dRainDropDiameter > 0 );
		double dNumIndividualDropsPerSecond = dRainTotalVolumePerSecond / dRainDropDiameter / 1000;
		
		for( int i=0; i< int( dNumIndividualDropsPerSecond ); i++ )
		{
			double dTimeCode = double( m_iTimecodeCount ) + m_oVRNG.GenerateDouble1();
			double dRainDropDistanceOnGround = dGroundCircleDiameter * m_oVRNG.GenerateDouble1() / 2.0f;
			double dAzimuthalAngleOnGround = 360.0f * m_oVRNG.GenerateDouble1();
			double dRainDropDistanceToLocation = sqrt( dRainDropDistanceOnGround * dRainDropDistanceOnGround + dListenerGroundDistance * dListenerGroundDistance );
			double dElevationToGroundLocation =  180.0f / ITAConstants::PI_D * asin( dRainDropDistanceOnGround / dRainDropDistanceToLocation  ) - 90;
			double dGain = m_oVRNG.GenerateDouble1() / dRainDropDistanceToLocation;

			
			int iIndex = -1;
			float f1 = float( dAzimuthalAngleOnGround );
			float f2 = float( dElevationToGroundLocation );
			pDirectionalRainData->getNearestNeighbour( DAFF_OBJECT_VIEW, f1, f2, iIndex );
			float* pfDestL = m_sfRainDropletContainer[0].data();
			pDirectionalRainData->getFilterCoeffs( iIndex, 0, pfDestL, float( dGain ) );
			float* pfDestR = m_sfRainDropletContainer[1].data();
			pDirectionalRainData->getFilterCoeffs( iIndex, 1, pfDestR, float( dGain ) );
			
			int iNewRainDropletSampleID = pSampler->CreateSample( pfDestL, pfDestR, iLength, dSampleRate ); 
			
			pSampler->AddPlaybackByTimecode( iNewRainDropletSampleID, iRainTrackID, dTimeCode, false );

			//pSampler->FreeSample( iNewRainDropletSampleID );
		}

		m_iTimecodeCount++;

		double dStop = oStopWatch.stop();

		assert( dUpdateRate > 0 );
		double dTimeoutRemainderMilliseconds = 1000.0f * ( 1 / dUpdateRate - dStop );
		VistaTimeUtils::Sleep( int( dTimeoutRemainderMilliseconds ) );

		return true;
	};

	ITASoundSampler* pSampler;
	int iRainDropSampleID;
	int iRainTrackID;
	ITAStopWatch oStopWatch;
	double dUpdateRate;
	double dRainDropDiameter;
	double dRainPrecipitation;
	double dListenerGroundDistance;
	double dRainDropDistanceThreshold;
	DAFFContentIR* pDirectionalRainData;

private:
	int m_iTimecodeCount;
	VistaRandomNumberGenerator m_oVRNG;
	ITASampleFrame m_sfRainDropletContainer;
};

Dipl.-Ing. Jonas Stienen's avatar
Style    
Dipl.-Ing. Jonas Stienen committed
124
int main( int , char** )
Jonas Stienen's avatar
Jonas Stienen committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{

	ITASoundSampler* pSampler = NULL;
	ITASoundSamplePool* pPool = NULL;
	CRainMaker* pRainMaker = NULL;

	try
	{
		pPool = ITASoundSamplePool::Create( 2, 44100 );
		pSampler = ITASoundSampler::Create( 2, 44100, 512, pPool );

		int iBinauralTrackID = pSampler->AddStereoTrack();

		ITASampleFrame oRainDropSignal( "SingleWaterDrip2_short.wav" );
139
		int iBinauralSampleID = pPool->CreateSample( oRainDropSignal[0].data(), oRainDropSignal[0].data(), oRainDropSignal.length(), pPool->GetSampleRate(), "RainDrop" );
Jonas Stienen's avatar
Jonas Stienen committed
140
141

		ITASampleFrame oRainNoiseSignal( "whitenoise.wav" );
142
		int iNoiseSampleID = pPool->CreateSample( oRainNoiseSignal[ 0 ].data(), oRainNoiseSignal[ 0 ].data(), oRainNoiseSignal.length(), pPool->GetSampleRate(), "RainDrop" );
Jonas Stienen's avatar
Jonas Stienen committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
		
		pSampler->AddPlaybackByTimecode( iNoiseSampleID, iBinauralTrackID, 0, false, 0.03f );

		ITAStopWatch sw;
		sw.start();
		DAFFReader* pReader = DAFFReader::create();
		if( pReader->openFile( "rainyday.daff" ) != 0 )
			ITA_EXCEPT1( INVALID_PARAMETER, "File rainyday.daff not found" );

		DAFFContentIR* pDirectionalRain = dynamic_cast< DAFFContentIR* >( pReader->getContent() );

		if( pDirectionalRain == nullptr )
			ITA_EXCEPT1( INVALID_PARAMETER, "DAFF file not valid" );

		cout << "Loading directional rain took " << convertTimeToHumanReadableString( sw.stop() ) << endl;

		pRainMaker = new CRainMaker();
		pRainMaker->pSampler = pSampler;
		pRainMaker->iRainTrackID = iBinauralTrackID;
		pRainMaker->iRainDropSampleID = iBinauralSampleID;
		pRainMaker->pDirectionalRainData = pDirectionalRain;

		pRainMaker->Run();

167
		ITAStreamProbe oOutputDumper( pSampler, "out.wav", ITAQuantization::ITA_FLOAT );
Jonas Stienen's avatar
Jonas Stienen committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

		ITAsioInitializeLibrary();
		ITAsioInitializeDriver( "ASIO4ALL v2" );
		ITAsioCreateBuffers( 0, 2, 512 );
		ITAsioSetPlaybackDatasource( &oOutputDumper );
		ITAsioStart();

		cout << "It's a rainy day. Hit '+' or '-' to control rain intensity. 'q' quits." << endl;

		int iGetCharacter;
		while( ( iGetCharacter = _getch() ) != 'q' )
		{
			if( iGetCharacter == '+' )
			{
				cout << "Increasing rain intensity" << endl;
			}
			else if( iGetCharacter == '-' )
			{
				cout << "Decreasing rain intensity" << endl;
			}
			else if( iGetCharacter == 'q' )
			{
				cout << "Stopping rain." << endl;
			}
			else
			{
				cout << "Unrecognized command " << char( iGetCharacter ) << endl;
			}
		}

		ITAsioStop();

		pSampler->RemoveAllPlaybacks();
		pRainMaker->StopGently( true );

		ITAsioFinalizeDriver();
		ITAsioFinalizeLibrary();
		
		delete pRainMaker;
		delete pSampler;
		delete pPool;
	}
	catch (ITAException& e )
	{
		delete pSampler;
		delete pPool;

		cerr << e << endl;
		return 255;
	}

	return 0;
}