コード例 #1
0
ファイル: Program.cs プロジェクト: Sk41d/MidiGremlin
        static void Main(string[] args)
        {
            Orchestra  orchestra = new Orchestra(new WinmmOut(0, 70));
            Instrument bagpipe   = orchestra.AddInstrument(InstrumentType.BagPipe);

            bagpipe.Play(Tone.CSharp, 1);
            orchestra.WaitForFinished();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            #region Orchestra
            const int midiDeviceId   = 0;           //Most computers will only have this one.
            const int beatsPerSecond = 60;          //Tempo of the music. 60 beats per second 1 one beat equals 1 second.
            IMidiOut  output         = new WinmmOut(midiDeviceId, beatsPerSecond);
            Orchestra orchestra      = new Orchestra(output);

            Instrument piano = orchestra.AddInstrument(InstrumentType.BrightAcousticPiano);
            #endregion

            #region SingleSound

            //Play a single sound
            piano.Play(Tone.C, 1);
            orchestra.WaitForFinished();

            #endregion

            #region MusicObjects
            MusicObject longF  = new Note(Tone.F, 1);
            MusicObject shortF = new Note(Tone.F, 0.5);
            MusicObject longA  = new Note(Tone.A, 1);
            MusicObject shortA = new Note(Tone.A, 0.5);
            MusicObject longG  = new Note(Tone.G, 1);
            MusicObject shortG = new Note(Tone.G, 0.5);


            Console.WriteLine("Press enter to play a single sound");
            Console.ReadLine();
            //We can play any of those on an instrument if we wish
            piano.Play(shortG);
            orchestra.WaitForFinished();

            #endregion

            #region LargeMusicObject

            //Create 2 smaller MusicObjects made out of the base pieces
            MusicObject sequence1 = new SequentialMusicList(longF, shortA, longG, shortA);
            MusicObject sequence2 = new SequentialMusicList(shortA, shortA, shortA, longA, shortF);

            //Now create a bigger MusicObject made of those 2 smaller ones and one new
            SequentialMusicList bigMusicObject = new SequentialMusicList(sequence1, sequence1, sequence2, new Note(Tone.D, 2));

            //We can play this too
            //We can play any of those on an instrument if we wish
            Console.WriteLine("Press enter to play a longer sequence of sound");
            Console.ReadLine();
            piano.Play(bigMusicObject);
            orchestra.WaitForFinished();

            #endregion

            #region Transformation
            //Make the 1st object a little lower on the scale, using a transform
            int[] offsets = { 0, -3, -3, -2 };
            bigMusicObject[1] = bigMusicObject[1].Select <Note>((x, y) => x.OffsetBy(Scale.MajorScale, offsets[y]));

            //Play our final piece.
            Console.WriteLine("Press enter to play \"Drømte mig en drøm i nat\", ");
            Console.ReadLine();
            piano.Play(bigMusicObject);
            orchestra.WaitForFinished();

            //You have just heard https://en.wikipedia.org/wiki/Dr%C3%B8mde_mik_en_dr%C3%B8m_i_nat
            #endregion

            #region OtherInstrument
            //Create a flute
            Instrument flute = orchestra.AddInstrument(InstrumentType.Flute);



            Console.WriteLine("Press enter to play \"Drømte mig en drøm i nat\" on a flute");
            Console.ReadLine();
            flute.Play(bigMusicObject);
            orchestra.WaitForFinished();
            #endregion

            #region TransformToChords
            //Using Select<>() it's also possible to change the type of a MusicObject.
            ChordVariety minor    = ChordVariety.Minor;
            MusicObject  asChords = bigMusicObject.Select <Note>(n => minor.WithBaseTone(n.Keystroke.Tone, n.Pause.Duration));

            Console.WriteLine("Press enter to play \"Drømte mig en drøm i nat\" as minor.");
            Console.ReadLine();

            piano.Play(asChords);

            orchestra.WaitForFinished();
            #endregion
        }
コード例 #3
0
ファイル: FurEliseIntro.cs プロジェクト: Sk41d/MidiGremlin
        static void Main()
        {
            int bpm = 30; //Tempo of the music. Beats per minute.

            //Like most old western music, this is played in the A minor scale.
            //Note that as the tone enum starts at C, tones A and B are part of the lower octave.
            Tone[] majorScaleTones = { Tone.A - 12, Tone.B - 12, Tone.C, Tone.D, Tone.E, Tone.F, Tone.G };
            Scale  majorScale      = new Scale(majorScaleTones);
            //The left hand is lowered by 1 octave.
            Scale loweredMajorScale = new Scale(majorScaleTones.Select(x => x - 12).ToArray());

            //We will be using these constants a lot.
            double eigth        = 1 / 8.0;
            byte   baseVelocity = 50;

            //To play music, first we need an orchestra with access to an output.
            Orchestra o = new Orchestra(new WinmmOut(0, bpm));
            //The piece should be played on a grand piano. Let's just get one.
            Instrument piano = o.AddInstrument(InstrumentType.AccousticGrandPiano, majorScale, 0);

            //----------------------------
            //   Creating the right hand:
            //----------------------------

            //The treble clef shows the position of the G
            //(which has index 6 in majorScale)
            // so by counting a note's offset from the G
            // and adding the value corresponding to G in the scale
            // you get the tone you want.
            int trebleClef = majorScale.Interval(Tone.G);

            //Making all six bars:

            MusicObject rBar0 = //It seems, the first bar is only two eigths long.
                                //The two notes have the same duration and velocity, so we are using a helper class.(The zero means the sustain pedal is not used.)
                                SimilarNotes(eigth, baseVelocity, 0,
                                             majorScale[5 + trebleClef],
                                             majorScale[4 + trebleClef] + 1); //The ♯ elevates all tones on the line with 1 until cancelled by a ♮ or the bar ends.

            MusicObject rBar1And5 =
                SimilarNotes(eigth, baseVelocity, 0,
                             majorScale[5 + trebleClef],
                             majorScale[4 + trebleClef] + 1,
                             majorScale[5 + trebleClef],
                             majorScale[2 + trebleClef],
                             majorScale[4 + trebleClef],
                             majorScale[3 + trebleClef]);

            MusicObject rBar2 = new SequentialMusicList
                                (
                new Note(majorScale[1 + trebleClef], eigth * 2, baseVelocity),
                new Pause(eigth),
                SimilarNotes(eigth, baseVelocity, 0,
                             majorScale[-4 + trebleClef],
                             majorScale[-2 + trebleClef],
                             majorScale[1 + trebleClef])
                                );

            MusicObject rBar3 = new SequentialMusicList
                                (
                new Note(majorScale[2 + trebleClef], eigth * 2, baseVelocity),
                new Pause(eigth),
                SimilarNotes(eigth, baseVelocity, 0,
                             majorScale[-2 + trebleClef],
                             majorScale[0 + trebleClef] + 1,
                             majorScale[2 + trebleClef])
                                );

            MusicObject rBar4 = new SequentialMusicList
                                (
                new Note(majorScale[3 + trebleClef], eigth * 2, baseVelocity),
                new Pause(eigth),
                SimilarNotes(eigth, baseVelocity, 0,
                             majorScale[-2 + trebleClef],
                             majorScale[5 + trebleClef],
                             majorScale[4 + trebleClef] + 1)
                                );

            //rBar5 is already accounted for

            //The whole right hand.
            MusicObject rightHand = new SequentialMusicList(rBar0, rBar1And5, rBar2, rBar3, rBar4, rBar1And5);

            //----------------------------
            //   Creating the left hand:
            //----------------------------

            //The bass clef shows the position of the F
            //(which has index 5 in majorScale)
            // so by counting a note's offset from the F
            // and adding the value corresponding to F in the scale
            // you get the tone you want.
            int bassClef = loweredMajorScale.Interval(Tone.F);

            //Making all six bars:

            MusicObject lBar0 = new Pause(eigth * 2);

            MusicObject lBar1And5 = new Pause(eigth * 6);

            MusicObject lBar2And4 = new SequentialMusicList
                                    (
                SimilarNotes(eigth, baseVelocity, 6,    //These notes should be sustained for the rest of the bar.
                             loweredMajorScale[-5 + bassClef],
                             loweredMajorScale[-1 + bassClef],
                             loweredMajorScale[2 + bassClef]),
                new Pause(eigth),
                new Pause(eigth * 2)
                                    );

            MusicObject lBar3 = new SequentialMusicList
                                (
                SimilarNotes(eigth, baseVelocity, 6,    //These notes should be sustained for the rest of the bar.
                             loweredMajorScale[-8 + bassClef],
                             loweredMajorScale[-1 + bassClef],
                             loweredMajorScale[1 + bassClef] + 1),
                new Pause(eigth),
                new Pause(eigth * 2)
                                );


            //lBar4 is already accounted for.

            //lBar5 is already accounted for.

            //The whole left hand.
            MusicObject leftHand = new SequentialMusicList(lBar0, lBar1And5, lBar2And4, lBar3, lBar2And4, lBar1And5);

            //----------------------------
            //   Ready to play:
            //----------------------------

            //The two hands should start playing at the same time:
            ParallelMusicCollection FurEliseIntro = new ParallelMusicCollection(leftHand, rightHand);

            //And then we just start it.
            piano.Play(FurEliseIntro);

            o.WaitForFinished();
            Console.ReadLine();
        }