Hello everyone,
i am going to create a small tune though i have no idea how to go about it. I currently have a sine wave that plays for one second using a loop similar to this

// loop to generate audio samples
    for(int n = 0; n < num_samples; n++) 
    int fr = 500;           //Frequency 
    data[n] = amp*sin(2*pi*fr*n/fs);  
    cout << n << "\t" << data <<"\n"; 

This generates a 500Hz tone. The next step is for me to play another tone, at a higher frequency straight after it. I am new to C++ and was wondering if anyone could guide me in figuring out how to do the 'next step'. I thought about just copying the loop down and changing the frequency but this did nothing for reasons i do not know

2 Years
Discussion Span
Last Post by phorce

What do you mean by "Tune", what it looks like you're doing is creating a sine wave at a particular frequency Htz. Where do you declae the sampling rate?

If you're talking about creating a "tune" in terms of a synthesizer then this is a lot more complex and you should probably begin somewhere small if this is your first time in this.

P.s You also haven't given your variables amp, fs etc..

If you want to change the length, increase num_samples if you want to change the frequency, change the frequency and so on and so on..


all of that has been declared in the full code. When i say tune, i mean small scale, say the c scale for example. i have the first c note with that loop, the next step is D playing straight after. Any ideas?


Thank you for that it is interesting; though I understand the frequencies, i just put 500 as an easy number. My problem is when i open up the wav file, it plays a 1 second tone Beeeeeeeeeep, i want it to go beeeeeeeeeep, booooooooooooop. would a second loop be the answer? if so how would i format the code?


So, as I said in my first post. If you increase the total number of n values, then, you would therefore increase the total size of the audio file.. n < 256 > 1024 That being said. You could (as a test) have two different loops that write to two different arrays and then concatenate these two arrays before writing them to the .wav file.

Let me know how you get on

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.