Music major scale converter

0 votes

I'm trying to make a major scale converter for music. Is there anyone who knows how to do it?

So far, I've this

rootNote is a scale base note, such as cMajor or gMajor, that I want to convert to a major scale from 0-126. If I put rootNote 60 and note 60 together, the right return is 0, if I put rootNote 60 and note 61 together, the right return is 2, if I put rootNote 60 and note 62 together, the right return is 4, and if I put rootNote 60 and note 63 together, the right return is 5.

If I put rootNote 61 and note 60 together, the right return is 0, but if I put rootNote 61 and note 61 together, the right return is 1. If I put rootNote 61 and note 62 together, the right return is 3, and if I put rootNote 61 and note 63 together, the right return is 5.

Okay, I have this other one, and it appears to function. I'd like to lay out my sequence in major scale, but is there a formula I can use? This isn't working

public int getINMajorScale(int note, int rootNote)
    {

            List<int> majorScale = new List<int>();
            //int bNote = (int)_bNote.CurrentValue;

            int bNoteMpl = bNote / 12;
            bNote = 12 + (bNote - (12 * bNoteMpl)) - 7;
            majorScale.Add(bNote + (12 * bNoteMpl));
            int tBnote = bNote;
            int res = 0;
            for (int i = bNote; i < bNote + 6; i++)
            {
                //algorytm
                res = tBnote + 7;
                int mod = 0;
                if (res >= 12)
                {
                    mod = res / 12;
                    res = res - 12 * mod;
                }
                tBnote = res;
                majorScale.Add(res + (bNoteMpl * 12));
            }
            majorScale.Sort();
            int modNuller = 0;
            if (nmr >= 7)
            {
                modNuller = nmr / 7;
                nmr = nmr - 7 * modNuller;
            }
            return (majorScale[nmr] + (modNuller *12));
        }
Jun 11, 2022 in C# by pranav
• 2,590 points
514 views

1 answer to this question.

0 votes

I believe I know what you're looking for based on your input-output data.

1. Identify the steps = rootNote
2. Determine the interval between the rootNote and the note steps up the scale (number of semitones).
3. find the phase of the rootNote - 60

This algorithm yields precise results:

static int getINMajorScale(int note, int rootNote)
{
    if (note < rootNote) return 0;
    var scale = new[] { 2, 2, 1, 2, 2, 2, 1 };
    var phase = rootNote - 60;
    var steps = note - rootNote;
    var interval = steps == 0 
        ? 0 : Enumerable.Range(0, steps).Sum(step => scale[step % scale.Length]);
    var number = phase + interval;
    return number;
} 

Giving:

static void Main(string[] args)
{
    //rootNote = 60(C), note = 60(C) - output 0
    //rootNote = 60(C), note = 61(C#) - output 2
    //rootNote = 60(C), note = 62(D) - output 4
    //rootNote = 60(C), note = 63(D#) - output 5
    //rootNote = 61(C#), note = 60 (C) - output 0
    //rootNote = 61(C#), note = 61 (C#) - output 1
    //rootNote = 61(C#), note = 62 (D) - output 3
    //rootNote = 61(C#), note = 63 (D#) - output 5

    Console.WriteLine(getINMajorScale(60, 60));  // 0
    Console.WriteLine(getINMajorScale(61, 60));  // 2
    Console.WriteLine(getINMajorScale(62, 60));  // 4
    Console.WriteLine(getINMajorScale(63, 60));  // 5
    Console.WriteLine(getINMajorScale(60, 61));  // 0
    Console.WriteLine(getINMajorScale(61, 61));  // 1
    Console.WriteLine(getINMajorScale(62, 61));  // 3
    Console.WriteLine(getINMajorScale(63, 61));  // 5

    Console.ReadKey();
}
answered Jun 14, 2022 by krishna
• 2,820 points

Related Questions In C#

0 votes
1 answer

What is the best C# to VB.net converter

Telerik has a solid SharpDevelop-based converter that ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
1,461 views
0 votes
1 answer

What are major differences between C# and Java

Instead of listing out mutiple points, I ...READ MORE

answered Jun 9, 2022 in C# by rajiv
• 1,620 points

edited Jul 4, 2023 by Khan Sarfaraz 358 views