Example #1
0
 protected override void PlayNote(Note note, int channel, TimeSpan time)
 {
     // Only play melody (channel 0) and first note of chords (channel 1)
     if (channel <= 1) {
         // length is halved because Console.Beep messes up using full length on consecutive notes
         Console.Beep((int)note.GetFrequency(), (int)(note.Length.TotalMilliseconds * 0.5));
     }
 }
Example #2
0
        protected override void PlayNote(Note note, int channel, TimeSpan time)
        {
            if (!Muted && channel == 0) {
                //Console.WriteLine(lastTime + ": " + note.Type + (note.Sharp ? "#" : "") + "[" + note.Octave + "] " + note.Length);
            }

            if (normalize) {
                note.Volume = Math.Min(Math.Max(note.Volume * normalizeScalar, 0), 1);
            }

            midi.PlayNote(channel, note, elapsed + note.Length);
        }
Example #3
0
 protected override void PlayNote(Note note, int channel, TimeSpan time)
 {
     // length is halved because Console.Beep messes up using full length on consecutive notes
     Console.Beep((int)note.GetFrequency(), (int)(note.Length.TotalMilliseconds * 0.5));
 }
Example #4
0
        protected override void PlayNote(Note note, int channel, TimeSpan time)
        {
            // get the pitch relative to the C note in the same octave
            var pitch = GetPitch(note);
            // get the appropriate C note sound
            var sound = SoundBank.Sounds[note.Octave];
            // play it pitched up to match the note we're looking for
            var source = sound.Play(note.Volume, pitch);

            // set the sound source's time out so it'll be ended when the
            // note should end instead of playing through to completion
            source.TimeOut = now + note.Length;
            activeSources.Add(source);
        }
Example #5
0
 private float GetPitch(Note note)
 {
     // get relative pitch, this is the constant below to the power of the number of semitones between the notes
     var c = new Note() { Type = 'c', Octave = note.Octave };
     var dist = note.GetStep() - c.GetStep();
     return (float)Math.Pow(1.0594630943592952645618252949463, dist);
 }
Example #6
0
        /// <summary>
        /// Calculates the note's frequency in Hz.
        /// </summary>
        /// <param name="tuningNote">Optional note to use for tuning, this is Note.A4 (440 Hz) by default.</param>
        /// <returns>Frequency in Hz.</returns>
        public double GetFrequency(Note? tuningNote = null)
        {
            if (!tuningNote.HasValue)
                tuningNote = Note.A4;

            int dist = GetStep() - tuningNote.Value.GetStep();
            double freq = 440 * Math.Pow(1.0594630943592952645618252949463, dist);

            return freq;
        }
Example #7
0
        /// <summary>
        /// Modifies referenced note up or down in tone by given number of steps. There are 12 steps per octave.
        /// </summary>
        /// <param name="note">Note to modify.</param>
        /// <param name="steps">Steps up or down to modify the note.</param>
        protected void Step(ref Note note, int steps)
        {
            if (steps == 0)
                return;

            if (steps > 0) {
                for (int i = 0; i < steps; i++) {
                    switch (note.Type) {
                        case 'a':
                            if (!note.Sharp)
                                note.Sharp = true;
                            else {
                                note.Type = 'b';
                                note.Sharp = false;
                            }
                            break;
                        case 'b':
                            note.Type = 'c';
                            note.Octave++;
                            break;
                        case 'c':
                            if (!note.Sharp)
                                note.Sharp = true;
                            else {
                                note.Type = 'd';
                                note.Sharp = false;
                            }
                            break;
                        case 'd':
                            if (!note.Sharp)
                                note.Sharp = true;
                            else {
                                note.Type = 'e';
                                note.Sharp = false;
                            }
                            break;
                        case 'e':
                            note.Type = 'f';
                            break;
                        case 'f':
                            if (!note.Sharp)
                                note.Sharp = true;
                            else {
                                note.Type = 'g';
                                note.Sharp = false;
                            }
                            break;
                        case 'g':
                            if (!note.Sharp)
                                note.Sharp = true;
                            else {
                                note.Type = 'a';
                                note.Sharp = false;
                            }
                            break;
                    }
                }
            }
            else {
                for (int i = 0; i < Math.Abs(steps); i++) {
                    switch (note.Type) {
                        case 'a':
                            if (note.Sharp)
                                note.Sharp = false;
                            else {
                                note.Type = 'g';
                                note.Sharp = true;
                            }
                            break;
                        case 'b':
                            note.Type = 'a';
                            note.Sharp = true;
                            break;
                        case 'c':
                            if (note.Sharp)
                                note.Sharp = false;
                            else {
                                note.Type = 'b';
                                note.Octave--;
                            }
                            break;
                        case 'd':
                            if (note.Sharp)
                                note.Sharp = false;
                            else {
                                note.Type = 'c';
                                note.Sharp = true;
                            }
                            break;
                        case 'e':
                            note.Type = 'd';
                            note.Sharp = true;
                            break;
                        case 'f':
                            if (note.Sharp)
                                note.Sharp = false;
                            else {
                                note.Type = 'e';
                            }
                            break;
                        case 'g':
                            if (note.Sharp)
                                note.Sharp = false;
                            else {
                                note.Type = 'f';
                                note.Sharp = true;
                            }
                            break;
                    }
                }
            }
        }
Example #8
0
 /// <summary>
 /// Plays a note on a given channel.
 /// </summary>
 /// <param name="note">Note to play.</param>
 /// <param name="channel">Zero-based channel to play the note on.</param>
 /// <param name="time">Current playback time.</param>
 protected abstract void PlayNote(Note note, int channel, TimeSpan time);
Example #9
0
        protected override void PlayNote(Note note, int channel, TimeSpan time)
        {
            if (normalize)
                note.Volume = Math.Min(Math.Max(note.Volume * normalizeScalar, 0), 1);

            midi.PlayNote(channel, note, elapsed + note.Length);
        }