Does anybody know how to generate a .WAV file comprising of 2 seperate frequencies of a defined sample rate in C++?
How can i get this code to ask for 2 frequencies and still work....
Perhaps something like this - but I haven't tested it.
32 33 34 35 36 37 38
// f1 and f2 are the two frequencies
for(int n = 0; n < num_samples; n++)
{
float sample1 = A*sin(2*pi*n*f1/fs);
float sample2 = A*sin(2*pi*n*f2/fs);
data[n] = (sample1 + sample2) / 2.0f;
}
int f;
cout << "Please Enter the Tone Frequency: ";
cin >> f;
I was suggesting to duplicate that part:
1 2 3 4 5
int f1, f2;
cout << "Please Enter the First Tone Frequency: ";
cin >> f1;
cout << "Please Enter the second Tone Frequency: ";
cin >> f2;
Actually that's the easy bit. What you do with those two values is up to you. I gave one suggestion above, but you might try other ways of combining them, to modulate one tone with the other or whatever you wish.
It really depends on what you are planning to do. Personally I'd use a floating-point variable instead of an integer for the frequency. For example, the note A on a violin is 440Hz, but the D below it is 293.6648Hz (on an even-tempered scale) though it might also be 293.3333Hz depending on the type of harmony. At any rate, there's not really anything to lose by adding the extra flexibility.
Chervil, I tried that option already, the program seems to just register the second frequency along with the 2nd duration time.
If i can get the program to write to a .wav where it plays 440hz for 6 seconds, then after that play 700hz for 5 seconds. My previous description probably wasn't clear enough.
Thanks for your help anyway.
Ok I don't think i understood the original question. It seems you want two sounds, one after the other. i thought you wanted them to play simultaneously.