Esempio n. 1
0
        private void MainImage_PointerPressed(object sender, PointerPressedEventArgs e)
        {
            PointerPoint p = e.GetCurrentPoint(_mainImageControl);

            if (_pointerPos != p.Position)
            {
                return;
            }
            // _gridDrawer.SetCursor already called from OnMouseMove

            if (p.Properties.IsLeftButtonPressed)
            {
                // Get tempered note
                SomeInterval t = null;
                if (e.KeyModifiers.HasFlag(KeyModifiers.Alt))   // by cents
                {
                    float c = _gridDrawer.GetCursorCents();
                    t = new SomeInterval {
                        cents = c
                    };
                }
                else     // nearest rational
                {
                    _gridDrawer.UpdateCursorItem();
                    Rational r = _gridDrawer.GetCursorRational();
                    if (!r.IsDefault())
                    {
                        t = new SomeInterval {
                            rational = r
                        };
                    }
                }
                if (t != null)
                {
                    // Toggle selection
                    if (e.KeyModifiers.HasFlag(KeyModifiers.Control))
                    {
                        ToggleSelection(t);
                        //!!! invalidate image
                    }
                    // Play note
                    else
                    {
                        PlayNote(t);
                    }
                }
            }
            else if (p.Properties.IsMiddleButtonPressed)
            {
                _pointerPosDrag = _pointerPos;
            }
        }
Esempio n. 2
0
        private void MainImage_KeyDown(object sender, KeyEventArgs e)
        {
            Keyboard.Coords c;
            if (!Keyboard.KeyCoords.TryGetValue(e.Key, out c))
            {
                return;
            }

            SomeInterval t = _gridDrawer.GetKeyboardInterval(c.x, c.y, 0);

            if (t != null)
            {
                Debug.WriteLine("KeyDown {0} {1} -> {2}", c.x, c.y, t.ToString());
                PlayNote(t);
            }
        }
        public void ToggleSelection(SomeInterval t)
        {
            var s     = _drawerSettings.selection ?? new SomeInterval[] { };
            int count = s.Length;

            s = s.Where(i => !i.Equals(t)).ToArray(); // try to remove
            if (s.Length == count)                    // otherwise add
            {
                s = s.Concat(new SomeInterval[] { t }).ToArray();
            }
            _drawerSettings.selection = s;

            // Update 'selection' control
            _settingInternally    = true;
            textBoxSelection.Text = DS.FormatIntervals(s);
            _settingInternally    = false;

            // Update drawer
            _gridDrawer.SetSelection(_drawerSettings.selection);
        }
Esempio n. 4
0
        public static SomeInterval[] ParseIntervals(string textIntervals)
        {
            if (String.IsNullOrWhiteSpace(textIntervals))
            {
                return(null);
            }
            string[] parts     = textIntervals.Trim().ToLower().Split(";, ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
            var      intervals = new SomeInterval[parts.Length];

            for (int i = 0; i < parts.Length; ++i)
            {
                var t = SomeInterval.Parse(parts[i]);
                if (t == null)
                {
                    return(null);           // invalid format
                }
                intervals[i] = t;
            }
            return(intervals);
        }
Esempio n. 5
0
        /*
         * private void textBoxWavePartials_TextChanged(object sender, RoutedEventArgs e) {
         *  if (_settingInternally) return;
         *
         *  Rational[] partials = Rational.ParseRationals(_textBoxWavePartials.Text, ",");
         *  //!!! validate by subgroup matrix ?
         *  _soundSettings.partials = partials;
         * }
         */

        private void PlayNote(SomeInterval t)
        {
            if (_soundSettings.output == SoundSettings.EOutput.None)
            {
                return;
            }

            // get interval cents
            float cents = t.IsRational()
                ? _gridDrawer.Temperament.CalculateMeasuredCents(t.rational)
                : t.cents;

            switch (_soundSettings.output)
            {
            case SoundSettings.EOutput.Midi:
#if USE_MIDI
                if (_midiPlayer != null && _midiPlayer.IsClockStarted())
                {
                    _midiPlayer.NoteOn(0, cents, duration: 8f);     // duration in beats
                }
#endif
                break;

            case SoundSettings.EOutput.Wave:
            case SoundSettings.EOutput.WavePartialsTempered:
#if USE_WAVE
                if (_waveEngine != null && _waveEngine.IsPlaying() && _partialProvider != null)
                {
                    IList <Rational> partials = null;    // _soundSettings.partials; // get partials from settings
                    if (partials == null)
                    {
                        // generate default integer partials
                        partials = new List <Rational>();
                        for (int i = 1; i < 100; ++i)
                        {
                            var r = new Rational(i);
                            if (!_gridDrawer.Subgroup.IsInRange(r))
                            {
                                continue;     // skip if out of subgroup
                            }
                            partials.Add(r);
                            if (partials.Count == _systemSettings.wavePartialCount)
                            {
                                break;
                            }
                        }
                    }
                    //
                    bool temper = _soundSettings.output == SoundSettings.EOutput.WavePartialsTempered;
                    foreach (Rational r in partials)
                    {
                        float c = cents;
                        c += temper ? _gridDrawer.Temperament.CalculateMeasuredCents(r)
                                        : (float)r.ToCents();
                        double hz = Wave.Partials.CentsToHz(c);
                        float  h  = _gridDrawer.GetRationalHarmonicity(r);
                        Debug.Assert(0 <= h && h <= 1f, "Normalized harmonicity expected");
                        float level    = (float)(0.1 * Math.Pow(h, 4.5));
                        int   duration = (int)(2000 * Math.Pow(h, 1));
                        Debug.WriteLine("Add partial: {0} {1:0.000} -> {2:0.00}c {3:0.00}hz level {4:0.000}", r, h, c, hz, level);
                        _partialProvider.AddPartial(hz, 10, duration, level, -4f);
                    }
                    _partialProvider.FlushPartials();
                }
#endif
                break;
            }
        }