Tests.cpp 4.58 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2 3 4
#include <ITAException.h>
#include <ITASoundSampler.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 8 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
#include <time.h>
#include <iostream>

// Zufalls-Ganzzahl im Interval [0, n-1] zurckgeben
int irandx(int n) {
	return rand()%n;
}

// Zufalls-Match im Interval [0, n-1] zurckgeben
bool randmatch(double prob) {
	return (rand()%((int) ceil(1/prob))) == 0;
}

// Zufalls-Lautstrke (Intervall [-15 dB, 0 dB], Auflsung 1 dB) zurckgeben (Faktor)
double randgain() {
	return db20_to_ratio(-irandx(15+1));
}

double randdisp() {
	return rand()%2 - 1;
}

int main(int argc, char* argv[]) {
	
	srand( (unsigned) time(NULL) ) ;

	ITASoundSampler* sampler=NULL;

	try {
		sampler = ITASoundSampler::Create( 2, 44100, 512 );

		// Viervierteltakt!
		double bpm = 220;
39
		ITASampleClock sclock(sampler->GetSampleRate(), 4*60.0/bpm);
Jonas Stienen's avatar
Jonas Stienen committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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
		sampler->SetSampleClock(&sclock);

		const int TRACK_CRASH = sampler->AddStereoTrack();
		const int TRACK_HIHAT = sampler->AddStereoTrack();
		const int TRACK_SNARE = sampler->AddStereoTrack();
		const int TRACK_KICK = sampler->AddStereoTrack();
		const int TRACK_TOMS = sampler->AddStereoTrack();

		sampler->SetTrackName(TRACK_CRASH, "Crash");
		sampler->SetTrackGain(TRACK_CRASH, db20_to_ratio(-12));

		sampler->SetTrackName(TRACK_HIHAT, "HiHat");
		sampler->SetTrackGain(TRACK_HIHAT, db20_to_ratio(-2));

		sampler->SetTrackName(TRACK_SNARE, "Snare");
		sampler->SetTrackGain(TRACK_SNARE, db20_to_ratio(-2));

		sampler->SetTrackName(TRACK_KICK, "Bassdrum");
		sampler->SetTrackGain(TRACK_KICK, db20_to_ratio(-2));

		sampler->SetTrackName(TRACK_TOMS, "Tom Toms");
		sampler->SetTrackGain(TRACK_TOMS, db20_to_ratio(-6));

		sampler->SetMasterGain(db20_to_ratio(-6));

		const int SAMPLE_CRASH = sampler->LoadSample("crash.wav");
		const int SAMPLE_HIHAT = sampler->LoadSample("hihat.wav");
		const int SAMPLE_SNARE = sampler->LoadSample("snare.wav");
		const int SAMPLE_KICK  = sampler->LoadSample("kick.wav");
		const int SAMPLE_TOM1  = sampler->LoadSample("tom1.wav");
		const int SAMPLE_TOM2  = sampler->LoadSample("tom2.wav");
		const int SAMPLE_TOM3  = sampler->LoadSample("tom3.wav");

		sampler->Print();

		const double N1 = 1;
		const double N2 = 1/2.0;
		const double N4 = 1/4.0;
		const double N8 = 1/8.0;
		const double N16 = 1/16.0;
		const double N32 = 1/32.0;

		double t = 0;

		for (int i=0; i<100; i++) {
			
86
			if( randmatch( 1 / 8.0 ) ) sampler->AddPlaybackBySamplecount( SAMPLE_CRASH, TRACK_CRASH, int( t + 0 * N4 ) );
Jonas Stienen's avatar
Jonas Stienen committed
87 88 89 90 91 92
			
			double HH_ACCENT_GAIN = 0;
			double HH_NORMAL_GAIN = -12;

			// HiHat hinzufgen (On-beats akzentuiert)
			for (int j=0; j<8; j++) {
93
				sampler->AddPlaybackBySamplecount(SAMPLE_HIHAT, TRACK_HIHAT, int(t + j*N8), false,
Jonas Stienen's avatar
Jonas Stienen committed
94 95 96 97
					                 db20_to_ratio(j%3 == 0 ? HH_ACCENT_GAIN : HH_NORMAL_GAIN));
				
			}

98
			sampler->AddPlaybackBySamplecount( SAMPLE_KICK, TRACK_KICK, int( t + 0 * N4 ) );
Jonas Stienen's avatar
Jonas Stienen committed
99 100
			if (!randmatch(1/10.0))
				// Snare auf 3
101
				sampler->AddPlaybackBySamplecount( SAMPLE_SNARE, TRACK_SNARE, int( t + 4 * N8 ) );
Jonas Stienen's avatar
Jonas Stienen committed
102 103 104
			else
				if (randmatch(1/2.0)) {
					// Snare auf 3, 3+ (zweiter etwas leiser)
105 106
				sampler->AddPlaybackBySamplecount( SAMPLE_SNARE, TRACK_SNARE, int( t + 4 * N8 ) );
				sampler->AddPlaybackBySamplecount( SAMPLE_SNARE, TRACK_SNARE, int( t + 5 * N8 ) , false, db20_to_ratio( -3 ) );
Jonas Stienen's avatar
Jonas Stienen committed
107 108
				} else {
					// Versetzte Snare
109
				sampler->AddPlaybackBySamplecount( SAMPLE_SNARE, TRACK_SNARE, int( t + ( 4 + randdisp() )*N8 ) );
Jonas Stienen's avatar
Jonas Stienen committed
110 111 112 113
				}


			// Zufall: 4+ auf der Basstrommel (etwas leiser)
114
			if( randmatch( 1 / 4.0 ) ) sampler->AddPlaybackBySamplecount( SAMPLE_KICK, TRACK_KICK, int( t + 7 * N8 ), false, db20_to_ratio( -3 ) );
Jonas Stienen's avatar
Jonas Stienen committed
115
			// Zufall: 1+ auf der Basstrommel (etwas leiser)
116
			if( randmatch( 1 / 6.0 ) ) sampler->AddPlaybackBySamplecount( SAMPLE_KICK, TRACK_KICK, int( t + 1 * N8 ), false, db20_to_ratio( -1 ) );
Jonas Stienen's avatar
Jonas Stienen committed
117 118 119
			

			if (randmatch(1/2.0)) 
120
				sampler->AddPlaybackBySamplecount( SAMPLE_TOM1 + irandx( 3 ), TRACK_TOMS, int( t + ( 2 + randdisp() )*N8 ) );
Jonas Stienen's avatar
Jonas Stienen committed
121 122 123 124 125 126
			else {
				if (randmatch(1/4.0)) {
					double u = irandx(2)==0 ? N8 : N16;
					double p = irandx(10);
					int j;
					for (j=0; j<irandx(80); j++)
127
						sampler->AddPlaybackBySamplecount( SAMPLE_TOM1 + irandx( 3 ), TRACK_TOMS, int( t + ( j + p )*u), false, randgain() );
Jonas Stienen's avatar
Jonas Stienen committed
128 129

					if (randmatch(0.75))
130
						sampler->AddPlaybackBySamplecount( SAMPLE_CRASH, TRACK_CRASH, int( t + ( j + p + 1 )*u ), false, randgain() );
Jonas Stienen's avatar
Jonas Stienen committed
131 132 133 134 135 136
				}
			}

			t += 1;
		}
		
137
		WriteFromDatasourceToFile( sampler, "out.wav", int( 60 * sampler->GetSampleRate() ), 1.0f, false, false );
Jonas Stienen's avatar
Jonas Stienen committed
138 139 140 141 142 143 144 145 146

		delete sampler;
	} catch (ITAException& e) {
		std::cerr << e << std::endl;
		delete sampler;
	}

	return 0;
}