3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C# で純正三和音を鳴らす

Posted at

C#で純正三和音を鳴らしてみる

純正三和音とは周波数比が 4:5:6 であるような和音。純正律と呼ばれる音律(つまりドレミファソラシド)のもとになる周波数関係。

めちゃくちゃ美しい響きを持つけれどもピアノとかギターとかでは実現不可能。

ピタゴラスの時代には完全五度が存在していたけど純正律の三度が登場したのはルネサンス期(ダビンチとかそのへん?ガリレオガリレイの父ビンセントガリレイあたりかね?)

プロジェクトタイプ

Consoleアプリケーションでは機能しない。
Windows Forms か WPFとする。

追加する nuget package

    <PackageReference Include="NAudio" Version="2.0.1" />

音を鳴らすコード

3つのSignalGeneratorにそれぞれ 1度、3度、5度を割り当ててWaveOutEventに投げる。

        private void Play()
        {
            var duration = 5F;

            var chord = new ISampleProvider[] {
                new SignalGenerator()
                {
                    Gain = 0.2,
                    Frequency = 442,
                    Type = SignalGeneratorType.Sin,

                }.Take(TimeSpan.FromSeconds(duration)),
                new SignalGenerator()
                {
                    Gain = 0.2,
                    Frequency = 442 * 5/4,
                    Type = SignalGeneratorType.Sin
                }.Take(TimeSpan.FromSeconds(duration)),
                new SignalGenerator()
                {
                    Gain = 0.2,
                    Frequency = 442 * 6 / 4,
                    Type = SignalGeneratorType.Sin
                }.Take(TimeSpan.FromSeconds(duration))
            };


            MixingSampleProvider provider = new MixingSampleProvider(chord);
            
            using (var wo = new WaveOutEvent())
            {
                wo.Init(provider);
                wo.Play();
                while (wo.PlaybackState == PlaybackState.Playing)
                {
                    Thread.Sleep(500);
                }
            }
        }

その他

プログラムで音を鳴らすだけなら当然 NAudio 機能のファイル演奏機能とか MIDI演奏機能を使ったほうが圧倒的に早い。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?