< Summary

Information
Class: MusicTheory.Theory.Pitch.PitchUtils
Assembly: MusicTheory
File(s): /home/runner/work/MusicTheory/MusicTheory/Theory/Pitch/PitchUtils.cs
Line coverage
68%
Covered lines: 22
Uncovered lines: 10
Coverable lines: 32
Total lines: 68
Line coverage: 68.7%
Branch coverage
59%
Covered branches: 16
Total branches: 27
Branch coverage: 59.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LetterBasePc(...)87.5%88100%
ToPc(...)100%11100%
ToPc(...)100%11100%
ToMidi(...)100%11100%
FromMidi(...)30.76%311352.63%
Transpose(...)100%11100%
Transpose(...)100%11100%
GetEnharmonicSpellings()83.33%6683.33%

File(s)

/home/runner/work/MusicTheory/MusicTheory/Theory/Pitch/PitchUtils.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace MusicTheory.Theory.Pitch
 5{
 6
 7    // 変換ユーティリティ(綴り→pc、Pitch↔MIDI)
 8    public static class PitchUtils
 9    {
 10        // C=0, D=2, E=4, F=5, G=7, A=9, B=11 を基準に綴り→pc
 11811        public static int LetterBasePc(Letter l) => l switch { Letter.C => 0, Letter.D => 2, Letter.E => 4, Letter.F => 
 2712        public static PitchClass ToPc(SpelledPitch s) => new(LetterBasePc(s.Letter) + s.Acc.AccidentalValue);
 2313        public static PitchClass ToPc(Pitch p) => ToPc(p.Spelling);
 14
 15        // MIDI は C-1=0 とし、綴りのオクターブ系を MIDI に投影
 16        public static int ToMidi(Pitch p)
 17        {
 18            // 例: C4=60(慣習)
 1619            int octaveOffset = (p.Octave + 1) * 12;
 1620            return octaveOffset + ToPc(p).Pc;
 21        }
 22        public static Pitch FromMidi(int midi, SpelledPitch prefer = default, int? preferOctave = null)
 23        {
 24            // 初期版: 等音→簡易綴り(黒鍵は♯寄り)で復元
 525            int pc = ((midi % 12) + 12) % 12;
 526            var (letter, acc) = pc switch
 527            {
 128                0 => (Letter.C, 0),
 229                1 => (Letter.C, 1),
 030                2 => (Letter.D, 0),
 131                3 => (Letter.D, 1),
 032                4 => (Letter.E, 0),
 133                5 => (Letter.F, 0),
 034                6 => (Letter.F, 1),
 035                7 => (Letter.G, 0),
 036                8 => (Letter.G, 1),
 037                9 => (Letter.A, 0),
 038                10 => (Letter.A, 1),
 039                11 => (Letter.B, 0),
 040                _ => (Letter.C, 0)
 541            };
 542            int oct = (midi / 12) - 1;
 543            return new Pitch(new SpelledPitch(letter, new Accidental(acc)), preferOctave ?? oct);
 44        }
 45
 46
 47        //複音程
 48
 49        public static Pitch Transpose(Pitch p, MusicTheory.Theory.Interval.SemitoneInterval ivl)
 250            => FromMidi(ToMidi(p) + ivl.Semitones);
 51        public static PitchClass Transpose(PitchClass pc, MusicTheory.Theory.Interval.SemitoneInterval ivl)
 152            => new PitchClass(pc.Pc + ivl.Semitones);
 53
 54
 55        //異名同音の表記
 56        public static IEnumerable<SpelledPitch> GetEnharmonicSpellings(PitchClass pc)
 57        {
 6158            foreach (Letter l in Enum.GetValues(typeof(Letter)))
 59            {
 2560                int basePc = LetterBasePc(l);
 2561                int acc = pc.Pc - basePc;
 2562                if (acc >= -3 && acc <= 3)
 1263                    yield return new SpelledPitch(l, new Accidental(acc));
 64            }
 065        }
 66    }
 67
 68}