[ create a new paste ] login | about

Link: http://codepad.org/bbfduqML    [ raw code | output | fork ]

AaronMiller - C++, pasted on Jan 11:
/*
	Music Theory - Practice Algorithms
	Written by Aaron J. Miller (aaron.miller@axiosoftonline.com), 2010
	
	Octave 1 - 110Hz (1x)
	Octave 2 - 220Hz (2x)
	Octave 3 - 440Hz (4x)
	           880Hz (8x)
			   1760Hz (16x)
			   3520Hz (32x)
								et cetera
	
	A, A#, B, C, C#, D, D#, E, F, F#, G, G#
	(1/12)

http://home.cc.umanitoba.ca/~krussll/138/sec4/acoust1.htm
http://www.mindspring.com/~scottr/zmusic/

	How to combine waves (probably)
		(sin(a) + sin(b)) * 0.5

	How to generate a sine wave (perhaps)
		y(t) = amplitude * sin(angular_frequency*t + phase)
		y = array of points
		t = some time interval
		amplitude = the amplitude of the wave
		angular_frequency = "how many oscillations occur in a unit time interval, in radians per second"
		phase = the phase of the wave
http://en.wikipedia.org/wiki/Sine_wave
*/

#define NOTE_A					0
#define NOTE_A_SHARP				1
#define NOTE_B					2
#define NOTE_C					3
#define NOTE_C_SHARP				4
#define NOTE_D					5
#define NOTE_D_SHARP				6
#define NOTE_E					7
#define NOTE_F					8
#define NOTE_F_SHARP				9
#define NOTE_G					10
#define NOTE_G_SHARP				11

/* Algorithm: 440 * 2^(n/12) */
double note_freq(unsigned long note, unsigned long octave)
{
	double f;
	f = pow(2.0, ((double)(note+(octave*12))-24.0)/12.0);
	f = f * 440.0;
	return f;
}

/* Test the frequency creating algorithm */
void test_note_freq(void)
{
	float e, f_sharp, a;
	e = (float)note_freq(NOTE_E, 0);
	f_sharp = (float)note_freq(NOTE_F_SHARP, 0);
	a = (float)note_freq(NOTE_A, 0);

	printf("Frequency of E = %.2f\n", e);
	printf("Frequency of F# = %.2f\n", f_sharp);
	printf("Frequency of A = %.2f\n", a);
}



typedef struct
{
	unsigned int string_note[6];
	unsigned int string_octave[6];
} guitar_t;

typedef struct
{
	unsigned int fret;
	unsigned int string;
	unsigned int length[2]; // 0 = numerator, 1 = denominator
} tab_note_t;

guitar_t gtr_eadgbe =
{
	{ NOTE_E, NOTE_B, NOTE_G, NOTE_D, NOTE_A, NOTE_E },
	{ 5,      4,      4,      4,      3,      3      }
};
tab_note_t gtr_tabs[] =
{
	{ 14, 0, { 1, 2 } },
	{ 15, 0, { 1, 2 } },
	{ 12, 0, { 1, 1 } }
};


double convert_tab_to_freq(const guitar_t *pGtr, const tab_note_t *pNote)
{
	unsigned int n, o;
	double f;
	n = pGtr->string_note[pNote->string];
	o = pGtr->string_octave[pNote->string];
	n += pNote->fret;
	f = note_freq(n, o);
	return f;
}

void test_tab_to_freq(void)
{
	unsigned int num_notes = sizeof(gtr_tabs) / sizeof(tab_note_t);
	unsigned int i;
	for(i=0; i<num_notes; i++)
	{
		double f;
		f = convert_tab_to_freq(&gtr_eadgbe, &(gtr_tabs[i]));
		printf("string %u, fret %u = %.3f\n", gtr_tabs[i].string, gtr_tabs[i].fret, (float)f);
	}
}

/*
	Generate a sine wave of the given frequency and length
	-resolution is the number of points in the sine wave for the duration given
	-length is in seconds
*/
void generate_sine_wave(double *pOut, unsigned int resolution, double length, double frequency, double amplitude, double phase)
{
	const double pi = 3.14159265358979323846;
	const double one_second = 1.0;
	const double one_radian = pi * 2.0;
	double time_unit = (one_second/one_radian)/(length/frequency);
	unsigned int t;
	for(t=0; t<resolution; t++)
		pOut[t] = amplitude * sin(((double)t)*time_unit + phase);
}

void test_sine_generator(void)
{
	double freq = 440.0;
	const unsigned int resolution = 256;
	double wave[resolution];
	generate_sine_wave(wave, resolution /* resolution */, 1.0 /* length */, freq, 1.0 /* amplitude */, 0.0 /* phase */);
	unsigned int i;
	for(i=0; i<resolution; i++) {
		printf("%.3f\n", wave[i]);
	}
}

/* Main App */
int main()
{
	test_note_freq();
	test_tab_to_freq();
	test_sine_generator();
}


Output:
Frequency of E = 164.81
Frequency of F# = 185.00
Frequency of A = 110.00
string 0, fret 14 = 11839.821
string 0, fret 15 = 12543.854
string 0, fret 12 = 10548.082
0.000
0.791
0.968
0.391
-0.489
-0.989
-0.720
0.109
0.853
0.934
0.289
-0.581
-0.999
-0.641
0.216
0.905
0.890
0.184
-0.666
-0.997
-0.554
0.321
0.946
0.835
0.076
-0.743
-0.984
-0.460
0.421
0.975
0.771
-0.033
-0.811
-0.959
-0.361
0.517
0.994
0.697
-0.141
-0.870
-0.922
-0.258
0.607
1.000
0.615
-0.248
-0.918
-0.875
-0.151
0.690
0.995
0.526
-0.352
-0.956
-0.817
-0.043
0.764
0.978
0.431
-0.451
-0.982
-0.750
0.066
0.830
0.949
0.330
-0.545
-0.997
-0.673
0.174
0.886
0.909
0.226
-0.633
-1.000
-0.589
0.280
0.931
0.858
0.119
-0.713
-0.991
-0.498
0.382
0.965
0.798
0.010
-0.785
-0.970
-0.401
0.480
0.988
0.727
-0.099
-0.848
-0.938
-0.299
0.573
0.999
0.649
-0.206
-0.900
-0.895
-0.193
0.658
0.998
0.562
-0.311
-0.942
-0.841
-0.086
0.736
0.986
0.469
-0.412
-0.973
-0.777
0.023
0.805
0.962
0.370
-0.509
-0.992
-0.704
0.131
0.865
0.926
0.267
-0.599
-1.000
-0.623
0.238
0.914
0.880
0.161
-0.683
-0.996
-0.535
0.342
0.953
0.823
0.053
-0.758
-0.980
-0.440
0.442
0.980
0.756
-0.056
-0.824
-0.952
-0.340
0.537
0.996
0.681
-0.164
-0.881
-0.913
-0.235
0.625
1.000
0.597
-0.270
-0.927
-0.863
-0.129
0.706
0.992
0.506
-0.373
-0.962
-0.804
-0.020
0.779
0.972
0.410
-0.471
-0.986
-0.734
0.089
0.842
0.941
0.308
-0.564
-0.998
-0.656
0.196
0.896
0.899
0.203
-0.651
-0.999
-0.570
0.301
0.939
0.846
0.096
-0.729
-0.987
-0.478
0.403
0.971
0.784
-0.013
-0.799
-0.964
-0.380
0.500
0.991
0.711
-0.121
-0.860
-0.930
-0.277
0.591
1.000
0.631
-0.228
-0.910
-0.884
-0.171
0.675
0.997
0.543
-0.333
-0.950
-0.828
-0.063
0.751
0.982
0.449
-0.433
-0.978
-0.763
0.046
0.819
0.955
0.349
-0.528
-0.995
-0.688
0.154
0.876
0.917
0.245
-0.617
-1.000
-0.605
0.260
0.923
0.868
0.139
-0.699
-0.993
-0.515
0.363


Create a new paste based on this one


Comments: