Help with Additive Synthesis

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wiebo
    New Member
    • Apr 2007
    • 5

    Help with Additive Synthesis

    Hi, i've been trying to combine a number of sine waves together for the past few days without any luck. No processes i've found on the internet seem to work for me.

    I have code which creates different sine waves at different frequencies, but it always only plays the final frequency, so any pointers on what I should do with this code would be appreciated.
    Code:
    for(int i = 0; i < file.getNumSamples(); i++)
    		{
    			wave = sin(PIX2 * i * (f/SR));
    			buffer[a] = wave * gain;
    			output = (buffer[a] + output)/2;
    
    			file.setSample(output, i);
    		}
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    From what I know about signals and modulation from my amateur radio books when your sine waves have an additive effect on each other all you will have at the end is one complete wave form that modulates at (what seems like) random intervals but still retains the same amplitude. Were you expecting something different?

    Comment

    • Wiebo
      New Member
      • Apr 2007
      • 5

      #3
      Originally posted by RedSon
      From what I know about signals and modulation from my amateur radio books when your sine waves have an additive effect on each other all you will have at the end is one complete wave form that modulates at (what seems like) random intervals but still retains the same amplitude. Were you expecting something different?
      I did expect that to happen but i'm not getting that, all I would get at the end is a sine wave at say 320Hz.

      The problem is that the sine waves don't seem to combine

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Wiebo
        Code:
        for(int i = 0; i < file.getNumSamples(); i++)
        		{
        			wave = sin(PIX2 * i * (f/SR));
        			buffer[a] = wave * gain;
        			output = (buffer[a] + output)/2;
        
        			file.setSample(output, i);
        		}
        I don't understand this piece of code: in the body of your loop you only calculate
        several samples of one sine, i.e. care to elaborate on what 'a' is and what that
        buffer is supposed to do there?

        kind regards,

        Jos

        Comment

        • Wiebo
          New Member
          • Apr 2007
          • 5

          #5
          Originally posted by JosAH
          I don't understand this piece of code: in the body of your loop you only calculate
          several samples of one sine, i.e. care to elaborate on what 'a' is and what that
          buffer is supposed to do there?

          kind regards,

          Jos
          'a' refers to an integer, which is declared previously as part of another while loop to allow the program to run through the above piece of code for each instance of a sine wave. The buffer is merely there to hold the samples before it is then added to 'output'.

          Hope that helps

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Wiebo
            'a' refers to an integer, which is declared previously as part of another while loop to allow the program to run through the above piece of code for each instance of a sine wave. The buffer is merely there to hold the samples before it is then added to 'output'.

            Hope that helps
            'buffer[a]' is completely invariant to that loop, i.e. 'a' doesn't change in that
            loop so you could've used a single variable 'buffer' (or whatever) for the purpose.

            Also the entire loop body only depends on 'i' as I can see it; I think you have
            to supply that outer loop too; from just this snippet I can't tell you any more.

            kind regards,

            Jos

            Comment

            • Wiebo
              New Member
              • Apr 2007
              • 5

              #7
              Originally posted by JosAH
              'buffer[a]' is completely invariant to that loop, i.e. 'a' doesn't change in that
              loop so you could've used a single variable 'buffer' (or whatever) for the purpose.

              Also the entire loop body only depends on 'i' as I can see it; I think you have
              to supply that outer loop too; from just this snippet I can't tell you any more.

              kind regards,

              Jos
              Heres the whole part of the code, including both loops.
              Code:
              int main()
              {
              	//Create ne wav file
              	CWavFile file;
              	file.newWav(SR, 16, 1000, 1);
              	float f = 10;		//frequency
              	float PIX2 = PI*2;
              	float wave;
              	float gain;
              	int g = 0;
              	float buffer[34];	//no. voices
              
              for(int a = 0; a < 34; a++)
              	{
              		gain = 1;
              		
              		for(int i = 0; i < file.getNumSamples(); i++)
              		{
              			wave = sin(PIX2 * i * (f/SR));
              			buffer[a] = wave * gain;
              			output = (buffer[a] + output)/2;
              
              			file.setSample(output, i);
              		}
              		
              		g++;
              		gain = gain/g;
              		f = f + 10;
              }
              I hope that you can help

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Wiebo
                Code:
                			file.setSample(output, i);
                Doesn't that line simply set sample # i with the value of 'output'? If so, the
                behaviour can be exaplained: you set thousand values 34 times. The last
                thousand values (samples) end up in the file if 'setSample' is defined the
                way I just guessed.

                Maybe you should define 34*1000 = 34000 samples and write sample
                numbers 1000*a+i.

                kind regards,

                Jos

                Comment

                • Wiebo
                  New Member
                  • Apr 2007
                  • 5

                  #9
                  Originally posted by JosAH
                  Doesn't that line simply set sample # i with the value of 'output'? If so, the
                  behaviour can be exaplained: you set thousand values 34 times. The last
                  thousand values (samples) end up in the file if 'setSample' is defined the
                  way I just guessed.
                  That is correct, although I thought that having the following line would keep adding the current value of the sine wave 'a' into output and it would build up until all 34 waves are together as one sound.

                  Code:
                  output = (buffer[a] + output)/2;
                  Originally posted by JosAH
                  Maybe you should define 34*1000 = 34000 samples and write sample
                  numbers 1000*a+i.

                  kind regards,

                  Jos
                  This part I don't quite understand, the number of samples is 44100 a second, and at the moment it is a 10 second sine wave, and this can change.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Wiebo
                    That is correct, although I thought that having the following line would keep adding the current value of the sine wave 'a' into output and it would build up until all 34 waves are together as one sound.

                    Code:
                    output = (buffer[a] + output)/2;
                    No it doesn't work that way because the line above the line you just showed
                    you assigned the variable 'buffer[a]' a new value. More important is that you
                    haven't initialized the variable 'output' as far as I can see.

                    kind regards,

                    Jos

                    Comment

                    Working...