Hello, I am trying to create a tone, then another tone using this code. It currently only plays one of them. Is there something missing or am i going about this wrong?

  for(int n = 0; n < num_samples; n++) //where n is the step |start of for loop
    {
    int fr = 523;          //Frequency A  
    data[n] = amp*sin(2*pi*fr*n/fs);  
    cout << n << "\t" << data <<"\n"; 
    }     // | end of for loop

    for(int n = 0; n < num_samples; n++) //where n is the step |start of for loop
    {
    int fre = 587;       //Frequency B  
    data[n] = amp*sin(2*pi*fre*n/fs);  
    cout << n << "\t" << data <<"\n"; 
    }     // | end of for loop


    write_wav_file(data, num_samples, fs, fp);

Please do not create a new topic with the same question; this can be classed as spam and your question will be deleted.

What you are doing here is using the same array, which, in tern most likely segment and thus time out.

Try, something like the following:

  for(int n = 0; n < num_samples; n++) //where n is the step |start of for loop
  {
     int fr = 523;          //Frequency A  
     data[n] = amp*sin(2*pi*fr*n/fs);  
     cout << n << "\t" << data <<"\n"; 

     int fre = 587;       //Frequency B  
     data[n] = amp*sin(2*pi*fre*n/fs);  
     cout << n << "\t" << data <<"\n"; 
   }

But, even this would not work. Why won't it work? Simply because your structure has to be like this:

|one tone| -> -> -> |two tone|

If you take a look at the code sample that I give you, using vectors, I created 2 vectors which stored the two tones, then, having a separate vector which concatenated the data series. This is what you need to use.

My best advice would be to sit down and really think about this problem; what you're trying to do in C++ is not for the average user, you should have atleast some experience in arrays, and, data structure before you begin tackling this problem.

P.S. num_samples will need to be double the size of the array that you have.. If size is currently 512, it would be 512*512

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.