### Scales

What a scale is and how it's calculated i'll leave to others to explain, here is a couple of links.#### Don't disregard cycle fractions!

If you generate signals at 44100hz (cd quality), then you'll have a scale table that look like the table below (only first and last octave is shown). In the table you can see that C-0 has a frequency of 16.35, which means that in 1 second the signal has to traverse from positive to negative and back 16.35 times, which leads to a cycle size of 44100/16.35=2696.98. As you can also see in the table is that in the lower spectrum the decimal fraction of the cycle size is not very dominant but in the high spectrum the case is very different. So don't disregard the fraction!!!Also think about this fact when you apply vibrato. Vibrato is typically a fraction of a note in strength. So if you round cycle size when applying vibrato it will typically be unhearable in octave 5 and up.

Note Idx | Name | Freq | Cycles | Note Idx | Name | Freq | Cycles | |
---|---|---|---|---|---|---|---|---|

0 | C-0 | 16.35 | 2696.98 | 84 | C-7 | 2093.00 | 21.07 | |

1 | C#0 | 17.32 | 2545.61 | 85 | C#7 | 2217.46 | 19.89 | |

2 | D-0 | 18.35 | 2402.74 | 86 | D-7 | 2349.32 | 18.77 | |

3 | D#0 | 19.45 | 2267.88 | 87 | D#7 | 2489.02 | 17.72 | |

4 | E-0 | 20.60 | 2140.60 | 88 | E-7 | 2637.02 | 16.72 | |

5 | F-0 | 21.83 | 2020.46 | 89 | F-7 | 2793.83 | 15.78 | |

6 | F#0 | 23.12 | 1907.06 | 90 | F#7 | 2959.96 | 14.90 | |

7 | G-0 | 24.50 | 1800.02 | 91 | G-7 | 3135.96 | 14.06 | |

8 | G#0 | 25.96 | 1698.99 | 92 | G#7 | 3322.44 | 13.27 | |

9 | A-0 | 27.50 | 1603.64 | 93 | A-7 | 3520.00 | 12.53 | |

10 | A#0 | 29.14 | 1513.63 | 94 | A#7 | 3729.31 | 11.83 | |

11 | B-0 | 30.87 | 1428.68 | 95 | B-7 | 3951.07 | 11.16 |

##### Table: Equally Tempered Scale

Since it's not just good enough to truncate the cycle sizes, you can easily hear that for high pitch notes (trust me i tried) you have to find another way to map values from your oscillator signal buffer to your output. What i did was to use fixed point integer math to create a pointer into a 64K signal buffer which corresponds to a cycle of my oscillator. the incremenent value of this pointer is buffersize/(samplefrequency/freq) which of course should be scaled with your fixed int precision. In my case that's 65536/44100/freq.

#### Calculate your scale with necessary precision!

I made the mistake of using java float to calculate the scale in an early version of my code, this lead to some notable rounding errors.#### Source code

Here is the source code for calculating the frequences for a western scale./* Copyright (C) 2005 Claus Leth Gregersen (sound@dotleth.dk) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package dk.dotleth.sound.scale; /** * @author Claus Leth Gregersen * */ public class EquallyTempered12ToneScale { private float[] scale; private float detune = 1; // this is the frequency of the A below our first note (C-0) private double BASE_FREQ = 440.0f / (1<<5); public EquallyTempered12ToneScale() { this(1.0f); } public EquallyTempered12ToneScale(float detune) { double aFreq = BASE_FREQ; // 12th root of 2 with plenty digits! double factor = 1.0594630943592952645618252949463; // we need to transpose the A to C-0 double currentC = aFreq*factor*factor*factor; scale = new float[10*12]; for (int oct = 0; oct < 10; oct++) { double currentFreq = currentC; for (int i = 0; i < 12; i++) { scale[oct * 12 + i] = (float) currentFreq; currentFreq = currentFreq * factor; } currentC *= 2; } } public void setDetune(float detune) { this.detune = detune; } public float getMiddleA() { return (float) (BASE_FREQ *(1<<5) * detune); } /** * * @param value the note index in range 0-120 where 0 = lowest c0, * 1 = lowest c#0, 2 = d0, 12 = c1, 24 = c2 etc.. * @return frequency in hz */ public float getFrequency(int value) { return detune * scale[value]; } }