From 9bf12315b7e63f2913271b84b2f05a62cdba38a7 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Wed, 16 Apr 2014 21:11:05 +0200 Subject: [PATCH] Move Pitchtrackers to separate files --- .../Sound/Record/PitchTracker/CAnalyzer.cs | 179 ------------------ .../Lib/Sound/Record/PitchTracker/CPtAKF.cs | 71 +++++++ .../Lib/Sound/Record/PitchTracker/CPtDyWa.cs | 69 +++++++ .../Lib/Sound/Record/PitchTracker/CPtSharp.cs | 53 ++++++ Vocaluxe/Vocaluxe.csproj | 3 + 5 files changed, 196 insertions(+), 179 deletions(-) create mode 100644 Vocaluxe/Lib/Sound/Record/PitchTracker/CPtAKF.cs create mode 100644 Vocaluxe/Lib/Sound/Record/PitchTracker/CPtDyWa.cs create mode 100644 Vocaluxe/Lib/Sound/Record/PitchTracker/CPtSharp.cs diff --git a/Vocaluxe/Lib/Sound/Record/PitchTracker/CAnalyzer.cs b/Vocaluxe/Lib/Sound/Record/PitchTracker/CAnalyzer.cs index fcb45fa8..2a50540f 100644 --- a/Vocaluxe/Lib/Sound/Record/PitchTracker/CAnalyzer.cs +++ b/Vocaluxe/Lib/Sound/Record/PitchTracker/CAnalyzer.cs @@ -87,183 +87,4 @@ namespace Vocaluxe.Lib.Sound.Record.PitchTracker set { _VolumeTreshold = value; } } } - - /// - /// Pitchtracker (Pt) that uses autocorrelation (AKF) and AMDF - /// Quite fast and perfect in artificial tests, but may fail in real world scenarios (e.g. missing fundamental) - /// - class CPtAKF : CPitchTracker - { - #region Imports - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr PtAKF_Create(uint step); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern void PtAKF_Free(IntPtr analyzer); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern int PtAKF_GetNumHalfTones(); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern void PtAKF_InputByte(IntPtr analyzer, [In] byte[] data, int sampleCt); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern void PtAKF_SetVolumeThreshold(IntPtr analyzer, float threshold); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern float PtAKF_GetVolumeThreshold(IntPtr analyzer); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern int PtAKF_GetNote(IntPtr analyzer, [Out] out float maxVolume, [Out] float[] weights); - #endregion - - private IntPtr _Instance; - - public CPtAKF(uint step = 200) - { - _Instance = PtAKF_Create(step); - } - - public override int GetNumHalfTones() - { - return PtAKF_GetNumHalfTones(); - } - - public override float VolumeTreshold - { - get { return PtAKF_GetVolumeThreshold(_Instance); } - set { PtAKF_SetVolumeThreshold(_Instance, value); } - } - - public override void Input(byte[] data) - { - PtAKF_InputByte(_Instance, data, data.Length / 2); - } - - public override int GetNote(out float maxVolume, float[] weights) - { - return PtAKF_GetNote(_Instance, out maxVolume, weights); - } - - protected override void _Dispose(bool disposing) - { - if (_Instance == IntPtr.Zero) - throw new ObjectDisposedException(GetType().Name); - PtAKF_Free(_Instance); - _Instance = IntPtr.Zero; - } - } - - /// - /// Pitchtracker (Pt) that uses a dynamic wavelet (DyWa) algorithm - /// - class CPtDyWa : CPitchTracker - { - #region Imports - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr PtDyWa_Create(uint step); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern void PtDyWa_Free(IntPtr analyzer); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern void PtDyWa_SetVolumeThreshold(IntPtr analyzer, float threshold); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern float PtDyWa_GetVolumeThreshold(IntPtr analyzer); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern void PtDyWa_InputByte(IntPtr analyzer, [In] byte[] data, int sampleCt); - - [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern double PtDyWa_FindNote(IntPtr analyzer, [Out] out float maxVolume); - #endregion - - private IntPtr _Instance; - - public CPtDyWa(uint step = 200) - { - _Instance = PtDyWa_Create(step); - } - - public override void Input(byte[] data) - { - PtDyWa_InputByte(_Instance, data, data.Length / 2); - } - - public override int GetNote(out float maxVolume, float[] weights) - { - int note = (int)Math.Round(PtDyWa_FindNote(_Instance, out maxVolume)); - _SetWeights(note, weights); - return note; - } - - public override int GetNumHalfTones() - { - return NumHalfTonesDef; - } - - public override float VolumeTreshold - { - get { return PtDyWa_GetVolumeThreshold(_Instance); } - set { PtDyWa_SetVolumeThreshold(_Instance, value); } - } - - protected override void _Dispose(bool disposing) - { - if (_Instance == IntPtr.Zero) - throw new ObjectDisposedException(GetType().Name); - PtDyWa_Free(_Instance); - _Instance = IntPtr.Zero; - } - } - - /// - /// Pitchtracker (Pt) in C# that uses IIR filters for smoothing and an intelligent detection algorithm to reduce calculations - /// - class CPtSharp : CPitchTracker - { - private Pitch.PitchTracker _Instance; - - public CPtSharp() - { - _Instance = new Pitch.PitchTracker {SampleRate = 44100, DetectLevelThreshold = 0.01f, PitchRecordHistorySize = 1}; - } - - public override void Input(byte[] data) - { - short[] samplesShort = new short[data.Length / 2]; - Buffer.BlockCopy(data, 0, samplesShort, 0, data.Length); - float[] samplesFloat = new float[samplesShort.Length]; - for (int i = 0; i < samplesShort.Length; i++) - samplesFloat[i] = (float)samplesShort[i] / short.MaxValue; - _Instance.ProcessBuffer(samplesFloat); - } - - public override int GetNote(out float maxVolume, float[] weights) - { - int note = _Instance.CurrentPitchRecord.MidiNote - 15 - 21; - maxVolume = _Instance.LastMaxVol; - _SetWeights(note, weights); - return note; - } - - public override int GetNumHalfTones() - { - return NumHalfTonesDef; - } - - public override float VolumeTreshold - { - get { return _Instance.DetectLevelThreshold; } - set { _Instance.DetectLevelThreshold = value; } - } - - protected override void _Dispose(bool disposing) - { - if (_Instance == null) - throw new ObjectDisposedException(GetType().Name); - _Instance = null; - } - } } \ No newline at end of file diff --git a/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtAKF.cs b/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtAKF.cs new file mode 100644 index 00000000..0dbbc501 --- /dev/null +++ b/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtAKF.cs @@ -0,0 +1,71 @@ +using System; +using System.Runtime.InteropServices; + +namespace Vocaluxe.Lib.Sound.Record.PitchTracker +{ + /// + /// Pitchtracker (Pt) that uses autocorrelation (AKF) and AMDF + /// Quite fast and perfect in artificial tests, but may fail in real world scenarios (e.g. missing fundamental) + /// + class CPtAKF : CPitchTracker + { + #region Imports + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr PtAKF_Create(uint step); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void PtAKF_Free(IntPtr analyzer); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int PtAKF_GetNumHalfTones(); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void PtAKF_InputByte(IntPtr analyzer, [In] byte[] data, int sampleCt); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void PtAKF_SetVolumeThreshold(IntPtr analyzer, float threshold); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern float PtAKF_GetVolumeThreshold(IntPtr analyzer); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int PtAKF_GetNote(IntPtr analyzer, [Out] out float maxVolume, [Out] float[] weights); + #endregion + + private IntPtr _Instance; + + public CPtAKF(uint step = 200) + { + _Instance = PtAKF_Create(step); + } + + public override int GetNumHalfTones() + { + return PtAKF_GetNumHalfTones(); + } + + public override float VolumeTreshold + { + get { return PtAKF_GetVolumeThreshold(_Instance); } + set { PtAKF_SetVolumeThreshold(_Instance, value); } + } + + public override void Input(byte[] data) + { + PtAKF_InputByte(_Instance, data, data.Length / 2); + } + + public override int GetNote(out float maxVolume, float[] weights) + { + return PtAKF_GetNote(_Instance, out maxVolume, weights); + } + + protected override void _Dispose(bool disposing) + { + if (_Instance == IntPtr.Zero) + throw new ObjectDisposedException(GetType().Name); + PtAKF_Free(_Instance); + _Instance = IntPtr.Zero; + } + } +} \ No newline at end of file diff --git a/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtDyWa.cs b/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtDyWa.cs new file mode 100644 index 00000000..2907f1c8 --- /dev/null +++ b/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtDyWa.cs @@ -0,0 +1,69 @@ +using System; +using System.Runtime.InteropServices; + +namespace Vocaluxe.Lib.Sound.Record.PitchTracker +{ + /// + /// Pitchtracker (Pt) that uses a dynamic wavelet (DyWa) algorithm + /// + class CPtDyWa : CPitchTracker + { + #region Imports + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr PtDyWa_Create(uint step); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void PtDyWa_Free(IntPtr analyzer); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void PtDyWa_SetVolumeThreshold(IntPtr analyzer, float threshold); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern float PtDyWa_GetVolumeThreshold(IntPtr analyzer); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void PtDyWa_InputByte(IntPtr analyzer, [In] byte[] data, int sampleCt); + + [DllImport("PitchTracker.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern double PtDyWa_FindNote(IntPtr analyzer, [Out] out float maxVolume); + #endregion + + private IntPtr _Instance; + + public CPtDyWa(uint step = 200) + { + _Instance = PtDyWa_Create(step); + } + + public override void Input(byte[] data) + { + PtDyWa_InputByte(_Instance, data, data.Length / 2); + } + + public override int GetNote(out float maxVolume, float[] weights) + { + int note = (int)Math.Round(PtDyWa_FindNote(_Instance, out maxVolume)); + _SetWeights(note, weights); + return note; + } + + public override int GetNumHalfTones() + { + return NumHalfTonesDef; + } + + public override float VolumeTreshold + { + get { return PtDyWa_GetVolumeThreshold(_Instance); } + set { PtDyWa_SetVolumeThreshold(_Instance, value); } + } + + protected override void _Dispose(bool disposing) + { + if (_Instance == IntPtr.Zero) + throw new ObjectDisposedException(GetType().Name); + PtDyWa_Free(_Instance); + _Instance = IntPtr.Zero; + } + } +} \ No newline at end of file diff --git a/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtSharp.cs b/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtSharp.cs new file mode 100644 index 00000000..af38e362 --- /dev/null +++ b/Vocaluxe/Lib/Sound/Record/PitchTracker/CPtSharp.cs @@ -0,0 +1,53 @@ +using System; + +namespace Vocaluxe.Lib.Sound.Record.PitchTracker +{ + /// + /// Pitchtracker (Pt) in C# that uses IIR filters for smoothing and an intelligent detection algorithm to reduce calculations + /// + class CPtSharp : CPitchTracker + { + private Pitch.PitchTracker _Instance; + + public CPtSharp() + { + _Instance = new Pitch.PitchTracker {SampleRate = 44100, DetectLevelThreshold = 0.01f, PitchRecordHistorySize = 1}; + } + + public override void Input(byte[] data) + { + short[] samplesShort = new short[data.Length / 2]; + Buffer.BlockCopy(data, 0, samplesShort, 0, data.Length); + float[] samplesFloat = new float[samplesShort.Length]; + for (int i = 0; i < samplesShort.Length; i++) + samplesFloat[i] = (float)samplesShort[i] / short.MaxValue; + _Instance.ProcessBuffer(samplesFloat); + } + + public override int GetNote(out float maxVolume, float[] weights) + { + int note = _Instance.CurrentPitchRecord.MidiNote - 15 - 21; + maxVolume = _Instance.LastMaxVol; + _SetWeights(note, weights); + return note; + } + + public override int GetNumHalfTones() + { + return NumHalfTonesDef; + } + + public override float VolumeTreshold + { + get { return _Instance.DetectLevelThreshold; } + set { _Instance.DetectLevelThreshold = value; } + } + + protected override void _Dispose(bool disposing) + { + if (_Instance == null) + throw new ObjectDisposedException(GetType().Name); + _Instance = null; + } + } +} \ No newline at end of file diff --git a/Vocaluxe/Vocaluxe.csproj b/Vocaluxe/Vocaluxe.csproj index 852cd4f4..1f6afd01 100644 --- a/Vocaluxe/Vocaluxe.csproj +++ b/Vocaluxe/Vocaluxe.csproj @@ -268,6 +268,9 @@ + + +