Playing 2 different frequencies

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....

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include <iostream>
#include <cstdlib>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include "dp_lib.h"

using namespace std;


int main() 
{
	int A = 1;
	float* data;
	long int num_samples;
	int f;
	float pi = 3.14159265358;
	float fs = 44100;
	int duration;
	
	cout << "Please Enter the Tone Frequency: ";
	cin >> f;
	cout << "\nPlease enter the duration in seconds:" ;
	cin >> duration;
	
	num_samples = duration * fs;
	
	data = (float*) allocate_memory(num_samples);
	
	FILE* fp = fopen("audio.wav", "wb");
	
	for(int n = 0; n < num_samples; n++)
	{
		data[n] = A*sin(2*pi*n*f/fs);
	}
			
	write_wav_file(data, num_samples, fs, fp);
	
	free(data);
	
	cout << "\nMemory Now Released\n\n";
	
	cout << "\n\n";
	return 0;
}
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;
    }
Last edited on
How would i define f1 and f2 though.... The more i try and understand the more i get confused.
Your code has a single variable:
1
2
3
    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.
Last edited on
Take a look in this thread:

http://www.cplusplus.com/forum/general/109119/

It doesn't deal with your problem specifically, but you may be able to salvage some information.
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.
Perhaps something like this. Note this is untested as I don't have "dp_lib.h" or know where it comes from.

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
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
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include "dp_lib.h"

using namespace std;

int main() 
{
    int A = 1;
    float* data;
    long int num_samples1, num_samples2, total_samples;
    int f1, f2;
    float pi = 3.14159265358;
    float fs = 44100;
    int duration1, duration2;
    
    cout << "Please Enter the First Tone Frequency: ";
    cin >> f1;
    cout << "\nPlease enter the first duration in seconds:" ;
    cin >> duration1;
    
    cout << "Please Enter the Second Tone Frequency: ";
    cin >> f2;
    cout << "\nPlease enter the second duration in seconds:" ;
    cin >> duration2;
    
    num_samples1 = duration1 * fs;
    num_samples2 = duration2 * fs;
    total_samples = num_samples1 + num_samples2;
    
    data = (float*) allocate_memory(total_samples);
    
    FILE* fp = fopen("audio.wav", "wb");
    
    for (int n = 0; n < num_samples1; n++)
    {
        data[n] = A*sin(2*pi*n*f1/fs);
    }
            
    for (int n = num_samples1; n < total_samples; n++)
    {
        data[n] = A*sin(2*pi*n*f2/fs);
    }
            
    write_wav_file(data, total_samples, fs, fp);
    
    free(data);
    
    cout << "\nMemory Now Released\n\n";
    
    cout << "\n\n";
    return 0;
}
Topic archived. No new replies allowed.