Ejemplo n.º 1
0
        protected void hitTest()
        {
            int    mw = _map.ChipData.ChipWidth;
            int    mh = _map.ChipData.ChipHeight;
            PointD pp = convertToViewCoord(_parent.Player.X, _parent.Player.Y);

            foreach (map.Chip chip in _map.EnumViewChipData())
            {
                if (chip.Hardness <= 0)
                {
                    continue;
                }

                if (_parent.Player.Hit(chip, pp, mw, mh))
                {
                    double     plog  = _minFreqLog + (_maxFreqLog - _minFreqLog) * (Constants.StageViewHeight - _parent.Player.Y) / (double)Constants.StageViewHeight;
                    double     pitch = Math.Pow(Math.E, plog);
                    ToneResult tone  = ToneAnalyzer.Analyze(pitch, 1.0);
                    ResourceManager.SoundExplosion[tone.ToneIdx.ToString("D2")].Play();

                    _parent.Player.Y   = _map.GetDefaultY(pp.X);
                    _parent.Player.Rad = _parent.Player.MinRadius;
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        protected override void proc(KeyboardEventArgs e)
        {
            if (_needUpdate)
            {
                updateDevices();
            }

            double pitch = -1;

            if (_audioInput.Capturing)
            {
                if (_parent.ToneResult.Clarity > ToneAnalyzer.ClarityThreshold &&
                    Constants.MinPitch <= _parent.ToneResult.Pitch && _parent.ToneResult.Pitch <= Constants.MaxPitch)
                {
                    _curToneResult = _parent.ToneResult.Copy();
                    pitch          = _curToneResult.Pitch;
                }
            }

            if (Keyboard.IsKeyPressed(Key.Return))
            {
                #region キー押し中
                if (_state == SelectionState.Calibration)
                {
                    if (!_isCalStarted)
                    {
                        startCalibration();
                    }
                    _isCalStarted = true;
                    if (pitch >= 0)
                    {
                        updateCalibration(pitch);
                    }
                }
                #endregion
            }
            else
            {
                stopCalibration();
            }
        }
Ejemplo n.º 3
0
        public override void Load(string filePath)
        {
            List <List <TrackEvent> > trackEvents = new List <List <TrackEvent> >();
            BinaryReader reader = new BinaryReader(new FileStream(filePath, FileMode.Open), Encoding.UTF8);

            _header = readHeader(reader);
            if (!_header.DeltaTimeInPPQN)
            {
                throw new SMFException("対応していないMIDIファイル:TimeInSec Division");
            }
            if (_header.FormatType == 2)
            {
                throw new SMFException("対応していないMIDIファイル:Format");
            }

            for (int i = 0; i < _header.TrackNum; i++)
            {
                List <TrackEvent> evs = new List <TrackEvent>();
                readTrack(reader, ref evs);
                trackEvents.Add(evs);
            }

            double maxPitch = double.MinValue;
            double minPitch = double.MaxValue;

            long   prevTick = 0;
            double time     = 0;
            int    tempo    = 500000;
            double ppqnInv  = 1 / (double)_header.DeltaTime;
            Dictionary <int, Dictionary <int, SMFNote> > channels = new Dictionary <int, Dictionary <int, SMFNote> >();

            _points = new List <MusicNote>();
            while (true)
            {
                if (trackEvents.Count == 0)
                {
                    break;
                }
                if (trackEvents.TrueForAll((lst) => { return(lst.Count == 0); }))
                {
                    break;
                }

                int idx = -1; long minTick = long.MaxValue;
                for (int i = 0; i < trackEvents.Count; i++)
                {
                    if (trackEvents[i].Count == 0)
                    {
                        continue;
                    }
                    long tick = trackEvents[i][0].AbsoluteTick;
                    if (tick < minTick)
                    {
                        idx = i; minTick = tick;
                    }
                }

                if (idx < 0)
                {
                    break;
                }

                TrackEvent fstev = trackEvents[idx][0];
                trackEvents[idx].RemoveAt(0);
                time += (fstev.AbsoluteTick - prevTick) * tempo * ppqnInv / 1000000.0;

                switch (fstev.EventType)
                {
                case EventType.Tempo:
                    tempo = (int)fstev.Params[0];
                    break;

                case EventType.NoteOn:
                {
                    int channel = (int)fstev.Params[0];
                    int noteNum = (int)fstev.Params[1];

                    Dictionary <int, SMFNote> noteNums;
                    if (channels.ContainsKey(channel))
                    {
                        noteNums = channels[channel];
                    }
                    else
                    {
                        noteNums = new Dictionary <int, SMFNote>();
                        channels.Add(channel, noteNums);
                    }

                    SMFNote note;
                    if (noteNums.ContainsKey(noteNum))
                    {
                        note = noteNums[noteNum];

                        MusicNote n = new MusicNote();
                        n.TimeInSec = time;
                        n.Pitch     = note.Pitch;
                        n.Start     = false;
                        _points.Add(n);
                    }

                    note = new SMFNote(true, pitchFromNote(noteNum));
                    if (noteNums.ContainsKey(noteNum))
                    {
                        noteNums[noteNum] = note;
                    }
                    else
                    {
                        noteNums.Add(noteNum, note);
                    }

                    MusicNote nn = new MusicNote();
                    nn.TimeInSec = time;
                    nn.Pitch     = note.Pitch;
                    nn.Start     = true;
                    _points.Add(nn);

                    if (note.Pitch < minPitch)
                    {
                        minPitch = note.Pitch;
                    }
                    if (note.Pitch > maxPitch)
                    {
                        maxPitch = note.Pitch;
                    }
                }
                break;

                case EventType.NoteOff:
                {
                    int channel = (int)fstev.Params[0];
                    int noteNum = (int)fstev.Params[1];

                    Dictionary <int, SMFNote> noteNums;
                    if (channels.ContainsKey(channel))
                    {
                        noteNums = channels[channel];
                    }
                    else
                    {
                        noteNums = new Dictionary <int, SMFNote>();
                        channels.Add(channel, noteNums);
                    }

                    SMFNote note;
                    if (noteNums.ContainsKey(noteNum))
                    {
                        note = noteNums[noteNum];

                        MusicNote n = new MusicNote();
                        n.TimeInSec = time;
                        n.Pitch     = note.Pitch;
                        n.Start     = false;
                        _points.Add(n);

                        if (note.Pitch < minPitch)
                        {
                            minPitch = note.Pitch;
                        }
                        if (note.Pitch > maxPitch)
                        {
                            maxPitch = note.Pitch;
                        }

                        noteNums.Remove(noteNum);
                    }
                }
                break;
                }

                prevTick = fstev.AbsoluteTick;
            }

            _length = time;

            ToneResult minTone = ToneAnalyzer.Analyze(minPitch, 1.0);
            ToneResult maxTone = ToneAnalyzer.Analyze(maxPitch, 1.0);

            maxTone.ToneIdx++;
            if (maxTone.ToneIdx >= 12)
            {
                maxTone.ToneIdx -= 12; maxTone.Octave++;
            }
            minTone.ToneIdx--;
            if (minTone.ToneIdx <= 0)
            {
                minTone.ToneIdx += 12; minTone.Octave--;
            }

            MinPitch = ToneAnalyzer.PitchFromTone(minTone.ToneIdx, minTone.Octave);
            MaxPitch = ToneAnalyzer.PitchFromTone(maxTone.ToneIdx, maxTone.Octave);

            _points.Sort((MusicNote n1, MusicNote n2) =>
            {
                if (n1.TimeInSec != n2.TimeInSec)
                {
                    return(n1.TimeInSec.CompareTo(n2.TimeInSec));
                }
                if (n1.Start)
                {
                    if (n2.Start)
                    {
                        return(0);
                    }
                    return(1);
                }
                else
                {
                    if (n2.Start)
                    {
                        return(-1);
                    }
                    return(0);
                }
            });
        }
Ejemplo n.º 4
0
        public override void Load(string filePath)
        {
            _curTime = 0;
            _points.Clear();
            load(filePath);

            foreach (Note n in _notes)
            {
                if (n.ToneIdx >= 0)
                {
                    double pLen = n.Length;
                    if (n.LenRatio >= 0 && n.LenRatio < 8)
                    {
                        pLen *= n.LenRatio / 8.0;
                    }

                    MusicNote onNote = new MusicNote();
                    onNote.Pitch     = ToneAnalyzer.PitchFromTone(n.ToneIdx, n.Octave);
                    onNote.Start     = true;
                    onNote.TimeInSec = _curTime;
                    _points.Add(onNote);

                    MusicNote offNote = new MusicNote();
                    offNote.Pitch     = onNote.Pitch;
                    offNote.Start     = false;
                    offNote.TimeInSec = _curTime + pLen;
                    _points.Add(offNote);

                    if (_maxP < onNote.Pitch)
                    {
                        _maxP = onNote.Pitch;
                    }
                    if (_minP > onNote.Pitch)
                    {
                        _minP = onNote.Pitch;
                    }
                }
                _curTime += n.Length;
            }

            Length = _curTime;

            ToneResult minTone = ToneAnalyzer.Analyze(_minP, 1.0);
            ToneResult maxTone = ToneAnalyzer.Analyze(_maxP, 1.0);

            maxTone.ToneIdx++;
            if (maxTone.ToneIdx >= 12)
            {
                maxTone.ToneIdx -= 12; maxTone.Octave++;
            }
            minTone.ToneIdx--;
            if (minTone.ToneIdx <= 0)
            {
                minTone.ToneIdx += 12; minTone.Octave--;
            }

            MinPitch = ToneAnalyzer.PitchFromTone(minTone.ToneIdx, minTone.Octave);
            MaxPitch = ToneAnalyzer.PitchFromTone(maxTone.ToneIdx, maxTone.Octave);

            _points.Sort((MusicNote n1, MusicNote n2) =>
            {
                if (n1.TimeInSec != n2.TimeInSec)
                {
                    return(n1.TimeInSec.CompareTo(n2.TimeInSec));
                }
                if (n1.Start)
                {
                    if (n2.Start)
                    {
                        return(0);
                    }
                    return(1);
                }
                else
                {
                    if (n2.Start)
                    {
                        return(-1);
                    }
                    return(0);
                }
            });
        }
Ejemplo n.º 5
0
        protected virtual void updatePlayerPos()
        {
            gameobj.Player player = _parent.Player;

            ToneResult tmp     = _parent.ToneResult.Copy();
            double     pitch   = Math.Log(tmp.Pitch);
            double     clarity = tmp.Clarity;

            double target  = player.Y;
            bool   soundOn = false;

            if (_parent.AudioInput.Capturing)
            {
                if (clarity >= ToneAnalyzer.ClarityThreshold &&
                    Constants.MinPitch <= tmp.Pitch && tmp.Pitch <= Constants.MaxPitch)
                {
                    soundOn = true;
                }
            }

            if (soundOn)
            {
                _toneResult = tmp;

                double yr = (pitch - _minFreqLog) / (_maxFreqLog - _minFreqLog);
                target = _view.Height - yr * _view.Height + _view.Y;

                _prevYDiff = _yDiff;
                _yDiff     = player.Y - target;
                _integral += _diffT * (_prevYDiff + _yDiff) / 2.0;

                double p   = _coefP * _yDiff;
                double i   = _coefI * _integral;
                double d   = _coefD * (_yDiff - _prevYDiff) / _diffT;
                int    pid = (int)(p + i + d);
                player.Y -= (pid > _maxDiffY ? _maxDiffY : (pid < -_maxDiffY ? -_maxDiffY : pid));

                player.Rad -= player.RadDec;
            }
            else
            {
                _prevYDiff  = 0; _yDiff = 0;
                player.Rad += player.RadInc;
            }

#if DEBUG
            if (Keyboard.IsKeyPressed(Key.UpArrow))
            {
                player.Y--;
            }
            else if (Keyboard.IsKeyPressed(Key.DownArrow))
            {
                player.Y++;
            }
            if (Keyboard.IsKeyPressed(Key.Space))
            {
                player.Rad -= player.RadDec;
            }
            if (Keyboard.IsKeyPressed(Key.RightArrow))
            {
                player.X += 1;
            }
#endif

            if (player.Y < _view.Y)
            {
                player.Y = _view.Y;
            }
            if (player.Y > _view.Height + _view.Y)
            {
                player.Y = _view.Height + _view.Y;
            }

            long tick = Environment.TickCount;
            player.X     += player.Vx * (tick - _prevProcTime) * SdlDotNet.Core.Events.TargetFps / 1000.0;
            _prevProcTime = tick;
        }
Ejemplo n.º 6
0
        protected void createKeyRects()
        {
            _whiteKeys = new List <KeyValuePair <Rectangle, string> >();
            _blackKeys = new List <KeyValuePair <Rectangle, string> >();

            ToneResult maxTone = ToneAnalyzer.Analyze(_maxFreq, 1.0);
            ToneResult minTone = ToneAnalyzer.Analyze(_minFreq, 1.0);

            double nearMaxFreq = Math.Log(maxTone.Pitch - maxTone.PitchDiff);
            double nearMinFreq = Math.Log(minTone.Pitch - minTone.PitchDiff);

            double maxY = (nearMaxFreq - _minFreqLog) / (_maxFreqLog - _minFreqLog);
            double minY = (nearMinFreq - _minFreqLog) / (_maxFreqLog - _minFreqLog);

            maxY = _view.Height - maxY * _view.Height;
            minY = _view.Height - minY * _view.Height;

            int num = 12 * maxTone.Octave + maxTone.ToneIdx -
                      (12 * minTone.Octave + minTone.ToneIdx);
            double dy = (minY - maxY) / (double)num;

            double y       = minY;
            int    toneIdx = minTone.ToneIdx;
            int    oct     = minTone.Octave;

            num += 3;
            toneIdx--;
            if (toneIdx < 0)
            {
                toneIdx += 12;
                oct--;
            }
            y += dy;

            int bw = (int)(_keyRect.Width / 2.0);

            int[] blackIdx = { 1, 3, 6, 8, 10 };
            for (int i = 0; i < num; i++)
            {
                string str = string.Format("{0}{1}", ToneAnalyzer.ToneNames[toneIdx], oct);

                if (Array.IndexOf(blackIdx, toneIdx) < 0)
                {
                    // 白鍵
                    int nh, ny;
                    adjustWhiteKey(toneIdx, y, dy, out ny, out nh);
                    if (_whiteKeys.Count > 0)
                    {
                        Rectangle prevR = _whiteKeys[_whiteKeys.Count - 1].Key;
                        nh = prevR.Top - ny;
                    }
                    _whiteKeys.Add(new KeyValuePair <Rectangle, string>(
                                       new Rectangle(0, ny, _keyRect.Width - 1, nh),
                                       str));
                }
                else
                {
                    // 黒鍵
                    _blackKeys.Add(new KeyValuePair <Rectangle, string>(
                                       new Rectangle(0, (int)(y - dy / 2.0), bw, (int)dy),
                                       str));
                }

                y -= dy;
                toneIdx++;
                if (toneIdx >= 12)
                {
                    toneIdx = 0; oct++;
                }
            }
        }