473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with Additive Synthesis

5
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.
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < file.getNumSamples(); i++)
  2.         {
  3.             wave = sin(PIX2 * i * (f/SR));
  4.             buffer[a] = wave * gain;
  5.             output = (buffer[a] + output)/2;
  6.  
  7.             file.setSample(output, i);
  8.         }
Apr 20 '07 #1
9 2429
RedSon
5,000 Expert 4TB
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?
Apr 20 '07 #2
Wiebo
5
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
Apr 20 '07 #3
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < file.getNumSamples(); i++)
  2.         {
  3.             wave = sin(PIX2 * i * (f/SR));
  4.             buffer[a] = wave * gain;
  5.             output = (buffer[a] + output)/2;
  6.  
  7.             file.setSample(output, i);
  8.         }
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
Apr 20 '07 #4
Wiebo
5
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
Apr 20 '07 #5
JosAH
11,448 Expert 8TB
'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
Apr 20 '07 #6
Wiebo
5
'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.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     //Create ne wav file
  4.     CWavFile file;
  5.     file.newWav(SR, 16, 1000, 1);
  6.     float f = 10;        //frequency
  7.     float PIX2 = PI*2;
  8.     float wave;
  9.     float gain;
  10.     int g = 0;
  11.     float buffer[34];    //no. voices
  12.  
  13. for(int a = 0; a < 34; a++)
  14.     {
  15.         gain = 1;
  16.  
  17.         for(int i = 0; i < file.getNumSamples(); i++)
  18.         {
  19.             wave = sin(PIX2 * i * (f/SR));
  20.             buffer[a] = wave * gain;
  21.             output = (buffer[a] + output)/2;
  22.  
  23.             file.setSample(output, i);
  24.         }
  25.  
  26.         g++;
  27.         gain = gain/g;
  28.         f = f + 10;
  29. }
I hope that you can help
Apr 20 '07 #7
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1.             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
Apr 21 '07 #8
Wiebo
5
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.

Expand|Select|Wrap|Line Numbers
  1. output = (buffer[a] + output)/2;
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.
Apr 22 '07 #9
JosAH
11,448 Expert 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. 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
Apr 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Steve | last post by:
Is there a way to make style classes that are additive without the use of additional elements? ..Header { font-weight:bold; } ..SubEntry { padding-left:10px; )
1
by: clemenr | last post by:
Hi. Are there any python bindings for the Synthesis Toolkit? http://ccrma.stanford.edu/software/stk/ I've done a quick search on the web but found nothing. Cheers, Ross-c
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
2
by: Xiangliang Meng | last post by:
Hi, all. As far as I know, the speical arithmetic operator on void pointer is an externsion in GNU CC. However, I could not find the relative topics in C99. Would someone like to help me find...
10
by: Nemok | last post by:
Hi, I am trying to write an additive encryption algorithm in C++ that will encrypt a text by adding a random numer to each character in a string. The code looks similar to this: for(int...
0
by: paultawk | last post by:
I have to write a c++ program for sound synthesis and rather complicated,i don't really know from where to start: My program is to be used in robotics. It should read an input from an mp3 file. It...
0
by: Mangabasi | last post by:
Howdy, I would like to use the Synthesis Toolkit for a demo. I downloaded the STK from http://ccrma.stanford.edu/software/stk/index.html. It seems very powerful and user friendly. There are...
4
by: rsaharia | last post by:
Hello All, I need help with this particular .pl file I picked up from http://www.veritools-usa.com/xnf2vhdl.htm What it's supposed to do is really convert an xnf file to a vhdl file. I need it for...
2
by: Joris van Lier | last post by:
I'm looking to add Additive Styles to a CssStyleCollection, however the Add(HtmlTextWriteStyle, String) method seems to replace styles (see example below). void Style(CssStyleCollection styles) {...
2
by: Mesvak | last post by:
Hi Does anyone know the algorithm or have a source code for applying Additive white gaussian noise? thanx very much -M-
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.