Exemple #1
0
 static Scale generatePythagoreanScale() {
     ToneClass A = new ToneClass("A");
     ToneClass B = new ToneClass("B");
     ToneClass C = new ToneClass("C");
     ToneClass D = new ToneClass("D");
     ToneClass E = new ToneClass("E");
     ToneClass F = new ToneClass("F");
     ToneClass G = new ToneClass("G");
     ToneClass ASharp = new ToneClass(A, 1);
     ToneClass CSharp = new ToneClass(C, 1);
     ToneClass DSharp = new ToneClass(D, 1);
     ToneClass FSharp = new ToneClass(F, 1);
     ToneClass GSharp = new ToneClass(G, 1);
     return new Scale(new List<ToneClass>() { C, CSharp, D, DSharp, E, F, FSharp, G, GSharp, A, ASharp, B, });
 }
Exemple #2
0
        static Temperament generatePythagoreanTemperament(String StartingFromString)
        {
            SortedSet<double> pitches = new SortedSet<double>();
            Scale pythagoreanScale = generatePythagoreanScale();
            ToneClass StartingFrom = pythagoreanScale.GetTone(StartingFromString);
            double StartingFrequency = generateEqualTemperament().GetFrequency(new Tone(StartingFrom, 0));
            pitches.Add(1);
            double frequency = 1;
            double willOverflow = Math.Sqrt(8) / 3;
            for (int i = 6; i > 0; i--)
            {
                frequency *= 3.0 / (frequency > willOverflow ? 4 : 2);
                pitches.Add(frequency);
            }
            willOverflow = 3 / Math.Sqrt(8);
            frequency = 1;
            for (int i = 5; i > 0; i--)
            {
                frequency /= 3.0 / (frequency < willOverflow ? 4 : 2);
                pitches.Add(frequency);
            }
            int toneIndex = pythagoreanScale.ToneIndex(StartingFrom);
			if (toneIndex <= 5)
			{
				for (int i = 5 - toneIndex; i >= 0; i--)
				{
					double min = pitches.Min;
					pitches.Remove(min);
					pitches.Add(min * 2);
				}
			}
			else
			{
				for (int i = toneIndex - 6; i > 0; i--)
				{
					double max = pitches.Max;
					pitches.Remove(max);
					pitches.Add(max / 2);
				}
			}
			Dictionary<ToneClass, double> pitchDict = new Dictionary<ToneClass, double>();
            for (int i = 0; pitches.Count > 0; i++)
            {
                pitchDict.Add(pythagoreanScale[i], pitches.Min);
                pitches.Remove(pitches.Min);
            }
            return new Temperament(pitchDict, pythagoreanScale, 2, StartingFrequency);
        }
Exemple #3
0
 public ToneClass ToneClassMap(ToneClass baseToneClass)
 {
     return(baseToneClass);
 }
        public Measure Parse(string representation)
        {
            State state = State.GettingTone;
            int   index = 0;

            while (index < representation.Length)
            {
                switch (state)
                {
                case State.GettingTone:
                    durationNumerator   = "";
                    durationDenominator = "1";
                    if (representation[index] == 'z' || representation[index] == 'x')
                    {
                        tone  = new Rest();
                        state = State.HasTone;
                    }
                    else if (char.IsLetter(representation[index]))
                    {
                        toneClass = new ToneClass(representation[index].ToString().ToUpper());
                        toneClass = Key.ToneClassMap(toneClass);
                        tone      = new Tone(toneClass, char.IsLower(representation[index]) ? 1 : 0);
                        state     = State.HasTone;
                    }
                    else if (representation[index] == '^')
                    {
                        accidentals++;
                    }
                    else if (representation[index] == '_')
                    {
                        accidentals--;
                    }
                    else if (representation[index] == '%')
                    {
                        state = State.Comment;
                    }
                    else if (representation[index] == '[')
                    {
                        inChord = true;
                    }
                    else if (representation[index] == '"')
                    {
                        state = State.Annotation;
                    }
                    break;

                case State.HasTone:
                    if (char.IsDigit(representation[index]))
                    {
                        durationNumerator += representation[index];
                    }
                    else if (representation[index] == '/')
                    {
                        durationDenominator = "";
                        state = State.Denominator;
                    }
                    else if (char.IsLetter(representation[index]) || representation[index] == '^' || representation[index] == '_')
                    {
                        AddNote();
                        state = State.GettingTone;
                        continue;
                    }
                    else if (representation[index] == ',')
                    {
                        accidentals -= Key.Scale.ToneCount();
                    }
                    else if (representation[index] == '\'')
                    {
                        accidentals += Key.Scale.ToneCount();
                    }
                    else if (representation[index] == '%')
                    {
                        state = State.Comment;
                    }
                    else if (representation[index] == '[')
                    {
                        AddNote();
                        state = State.GettingTone;
                        continue;
                    }
                    else if (representation[index] == ']')
                    {
                        if (inChord)
                        {
                            AddNote();
                            tone       = new Chord(chordTones);
                            chordTones = new List <Tone>();
                            inChord    = false;
                            AddNote();
                            state = State.GettingTone;
                        }
                    }
                    else if (representation[index] == '"')
                    {
                        AddNote();
                        state = State.GettingTone;
                        continue;
                    }
                    break;

                case State.Denominator:
                    if (char.IsDigit(representation[index]))
                    {
                        durationDenominator += representation[index];
                    }
                    else if (char.IsLetter(representation[index]) || representation[index] == '^' || representation[index] == '_')
                    {
                        AddNote();
                        state = State.GettingTone;
                        continue;
                    }
                    else if (representation[index] == '%')
                    {
                        state = State.Comment;
                    }
                    else if (representation[index] == '[')
                    {
                        AddNote();
                        state = State.GettingTone;
                        continue;
                    }
                    else if (representation[index] == ']')
                    {
                        state = State.HasTone;
                        continue;
                    }
                    break;

                case State.Comment:
                    if (representation[index] == '\n')
                    {
                        state = State.GettingTone;
                    }
                    break;

                case State.Annotation:
                    if (representation[index] == '"')
                    {
                        state = State.GettingTone;
                    }
                    break;
                }
                index++;
            }
            AddNote();
            Measure measure = new Measure(notes);

            notes = new List <Note>();
            return(measure);
        }