コード例 #1
0
        public static ToneResult FromTone(int toneIdx, int octave)
        {
            ToneResult ret = new ToneResult();

            ret.ToneIdx = toneIdx;
            ret.Tone    = _toneNames[toneIdx];
            ret.Octave  = octave;
            ret.Clarity = 1.0;

            double f4 = Cents[toneIdx] * A4 / Cents[9];

            if (ret.Octave > 4)
            {
                int m = 1 << (ret.Octave - 4);
                f4 *= m;
            }
            else if (ret.Octave < 4)
            {
                int m = 1 << (4 - ret.Octave);
                f4 /= (double)m;
            }
            ret.Pitch     = f4;
            ret.PitchDiff = 0;
            return(ret);
        }
コード例 #2
0
        public static ToneResult Analyze(double freq, double clarity)
        {
            if (double.IsNaN(freq) || double.IsInfinity(freq))
            {
                return(ToneResult.Default);
            }

            int    octave = 4;
            double ft     = freq;

#warning この処理もうちょっと綺麗に書けないか
            if (freq < A4)
            {
                while (ft < A4)
                {
                    ft *= 2.0;
                    octave--;
                }
            }
            else if (freq > A4)
            {
                octave = 3;
                while (ft > A4)
                {
                    ft /= 2.0;
                    octave++;
                }
            }
            double a = Math.Pow(2, octave - 4) * A4;

            int aoffset;
            for (aoffset = 0; aoffset < 12; aoffset++)
            {
                double t = a * RangeCents[aoffset];
                if (freq <= t)
                {
                    break;
                }
            }
            if (aoffset >= 3)
            {
                octave++;
                if (aoffset == 12)
                {
                    a *= 2;
                }
            }
            aoffset %= 12;
            double baseTone = a * Cents[aoffset];

            ToneResult result = ToneResult.Default;
            result.ToneIdx   = (9 + aoffset) % 12;
            result.Tone      = _toneNames[result.ToneIdx];
            result.Octave    = octave;
            result.PitchDiff = freq - baseTone;
            result.Pitch     = freq;
            result.Clarity   = clarity;
            return(result);
        }
コード例 #3
0
ファイル: ToneAnalyzer.cs プロジェクト: davinx/PitchPitch
 public ToneResult Copy()
 {
     ToneResult result = new ToneResult();
     result.Tone = Tone;
     result.ToneIdx = ToneIdx;
     result.Octave = Octave;
     result.Pitch = Pitch;
     result.Clarity = Clarity;
     result.PitchDiff = PitchDiff;
     return result;
 }
コード例 #4
0
        public ToneResult Copy()
        {
            ToneResult result = new ToneResult();

            result.Tone      = Tone;
            result.ToneIdx   = ToneIdx;
            result.Octave    = Octave;
            result.Pitch     = Pitch;
            result.Clarity   = Clarity;
            result.PitchDiff = PitchDiff;
            return(result);
        }
コード例 #5
0
        // 今溜まっている分を解析する
        private void analyzeBuffer()
        {
            _prevAnalyzeTime = Environment.TickCount;
            _pitchAnalyzer.SampleFrequency = _capFormat.nSamplesPerSec;

            double[] buf = _buffer.ToArray();
            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] /= _capFormat.nChannels;
            }
            PitchResult result = _pitchAnalyzer.Analyze(buf);
            ToneResult  tone   = ToneAnalyzer.Analyze(result.Pitch, result.Clarity);

            // イベント発火
            DataUpdatedEventHandler del = DataUpdated;

            if (del != null)
            {
                del.Invoke(this, new DataUpdatedEventArgs(result, tone));
            }
        }
コード例 #6
0
 /// <summary>
 /// AudioInput初期化
 /// </summary>
 private void initAudio()
 {
     _audioInput = new audio.AudioInput();
     _audioInput.DeviceInfoUpdated += (s, e) =>
     {
         if (e.DeviceInfo.FindIndex((audio.DeviceInfo di) => { return(di.DeviceId == Config.Instance.DeviceId); }) >= 0)
         {
             if (!string.IsNullOrEmpty(Config.Instance.DeviceId))
             {
                 _audioInput.SelectDevice(Config.Instance.DeviceId);
             }
         }
         else
         {
             // 繋ぎたいデバイスが消えた
             _deviceRemoved = true;
         }
     };
     _audioInput.DeviceSelected += (s, e) =>
     {
         Config.Instance.DeviceId = e.Device.Id;
         _selectedDeviceIndex     = e.Index;
         _audioInput.StartCapture();
     };
     _audioInput.CaptureStarted += (s, e) => { };
     _audioInput.CaptureStopped += (s, e) => { };
     _audioInput.Disposed       += (s, e) => { };
     _audioInput.DataUpdated    += (s, e) =>
     {
         lock (_audioLockObj)
         {
             _pitchResult = e.Pitch;
             _toneResult  = e.Tone;
         }
     };
     _audioInput.UpdateDeviceInfo();
 }
コード例 #7
0
ファイル: ToneAnalyzer.cs プロジェクト: davinx/PitchPitch
        public static ToneResult FromTone(int toneIdx, int octave)
        {
            ToneResult ret = new ToneResult();
            ret.ToneIdx = toneIdx;
            ret.Tone = _toneNames[toneIdx];
            ret.Octave = octave;
            ret.Clarity = 1.0;

            double f4 = Cents[toneIdx] * A4 / Cents[9];
            if (ret.Octave > 4)
            {
                int m = 1 << (ret.Octave - 4);
                f4 *= m;
            }
            else if (ret.Octave < 4)
            {
                int m = 1 << (4 - ret.Octave);
                f4 /= (double)m;
            }
            ret.Pitch = f4;
            ret.PitchDiff = 0;
            return ret;
        }
コード例 #8
0
ファイル: PitchPitch.cs プロジェクト: davinx/PitchPitch
 /// <summary>
 /// AudioInput初期化
 /// </summary>
 private void initAudio()
 {
     _audioInput = new audio.AudioInput();
     _audioInput.DeviceInfoUpdated += (s, e) =>
     {
         if (e.DeviceInfo.FindIndex((audio.DeviceInfo di) => { return di.DeviceId == Config.Instance.DeviceId; }) >= 0)
         {
             if (!string.IsNullOrEmpty(Config.Instance.DeviceId))
             {
                 _audioInput.SelectDevice(Config.Instance.DeviceId);
             }
         }
         else
         {
             // 繋ぎたいデバイスが消えた
             _deviceRemoved = true;
         }
     };
     _audioInput.DeviceSelected += (s, e) =>
     {
         Config.Instance.DeviceId = e.Device.Id;
         _selectedDeviceIndex = e.Index;
         _audioInput.StartCapture();
     };
     _audioInput.CaptureStarted += (s, e) => { };
     _audioInput.CaptureStopped += (s, e) => { };
     _audioInput.Disposed += (s, e) => { };
     _audioInput.DataUpdated += (s, e) =>
     {
         lock (_audioLockObj)
         {
             _pitchResult = e.Pitch;
             _toneResult = e.Tone;
         }
     };
     _audioInput.UpdateDeviceInfo();
 }
コード例 #9
0
ファイル: SceneGameStage.cs プロジェクト: davinx/PitchPitch
        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;
        }
コード例 #10
0
 public DataUpdatedEventArgs(PitchResult pitchResult, ToneResult toneResult)
 {
     Pitch = pitchResult;
     Tone  = toneResult;
 }
コード例 #11
0
ファイル: SceneOption.cs プロジェクト: davinx/PitchPitch
        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();
            }
        }
コード例 #12
0
ファイル: AudioInput.cs プロジェクト: davinx/PitchPitch
 public DataUpdatedEventArgs(PitchResult pitchResult, ToneResult toneResult)
 {
     Pitch = pitchResult;
     Tone = toneResult;
 }