Initial commit — WPF guitar note name tutor using FFT frequency detection

This commit is contained in:
2026-05-09 03:17:07 +00:00
commit 99b73f710a
30 changed files with 1637 additions and 0 deletions
@@ -0,0 +1,19 @@
using System;
namespace FrequencyDetection
{
public class FrequencyDetectedEventArgs : EventArgs
{
readonly double _frequency;
public double Frequency
{
get { return _frequency; }
}
public FrequencyDetectedEventArgs(double frequency)
{
_frequency = frequency;
}
}
}
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{72165201-B9EA-45BD-96F9-9E81094C9BF0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FrequencyDetection</RootNamespace>
<AssemblyName>FrequencyDetection</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FrequencyDetectedEventArgs.cs" />
<Compile Include="FrequencyInfoSource.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SoundFrequencyInfoSource.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SoundAnalysis\SoundAnalysis.csproj">
<Project>{ABA54DC3-324B-49DE-B79E-C4F573306E4F}</Project>
<Name>SoundAnalysis</Name>
</ProjectReference>
<ProjectReference Include="..\SoundCapture\SoundCapture.csproj">
<Project>{DAE12676-B26C-4487-A1A5-D7FE2E64E6CE}</Project>
<Name>SoundCapture</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+20
View File
@@ -0,0 +1,20 @@
using System;
namespace FrequencyDetection
{
public abstract class FrequencyInfoSource
{
public abstract void Listen();
public abstract void Stop();
public event EventHandler<FrequencyDetectedEventArgs> FrequencyDetected;
protected void OnFrequencyDetected(FrequencyDetectedEventArgs e)
{
if (FrequencyDetected != null)
{
FrequencyDetected(this, e);
}
}
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FrequencyDetection")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FrequencyDetection")]
[assembly: AssemblyCopyright("Copyright © 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("cad84e03-98ea-4500-98e4-eef0b4e47094")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,69 @@
using System;
using SoundAnalysis;
using SoundCapture;
namespace FrequencyDetection
{
public class SoundFrequencyInfoSource : FrequencyInfoSource
{
readonly SoundCaptureDevice _device;
Adapter _adapter;
public SoundFrequencyInfoSource(SoundCaptureDevice device)
{
_device = device;
}
public override void Listen()
{
_adapter = new Adapter(this, _device);
_adapter.Start();
}
public override void Stop()
{
_adapter.Stop();
}
class Adapter : SoundCaptureBase
{
readonly SoundFrequencyInfoSource _owner;
const double MinFreq = 60;
const double MaxFreq = 1300;
internal Adapter(SoundFrequencyInfoSource owner, SoundCaptureDevice device)
: base(device)
{
_owner = owner;
}
protected override void ProcessData(short[] data)
{
double[] x = new double[data.Length];
for (int i = 0; i < x.Length; i++)
{
x[i] = data[i];
}
double[] spectr = FftAlgorithm.Calculate(x);
int index = 0;
double max = spectr[0];
int usefullMaxSpectr = Math.Min(spectr.Length,
(int)(MaxFreq * spectr.Length / SampleRate) + 1);
for (int i = 1; i < usefullMaxSpectr; i++)
{
if (max < spectr[i])
{
max = spectr[i]; index = i;
}
}
double freq = (double)SampleRate * index / spectr.Length;
if (freq < MinFreq) freq = 0;
_owner.OnFrequencyDetected(new FrequencyDetectedEventArgs(freq));
}
}
}
}