Skip to content

Commit

Permalink
dotnet interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stelian56 committed Feb 11, 2016
1 parent c7239ae commit 13b2c19
Show file tree
Hide file tree
Showing 24 changed files with 832 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/SimpleWebSocketServer.pyc
/dotnet/bin
/dotnet/obj
/dotnet/BodyMusic.csproj.user
/dotnet/BodyMusic.v11.suo
10 changes: 10 additions & 0 deletions bmconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
"name": "angles",
"command": "getTaredOrientationAsEulerAngles",
"components": ["pitch", "yaw", "roll"]
},
{
"name": "matrix",
"command": "getTaredOrientationAsRotationMatrix",
"components": ["m00", "m01", "m02", "m10", "m11", "m12", "m20", "m21", "m22"]
},
{
"name": "acceleration",
"command": "getCorrectedLinearAccelerationInGlobalSpace",
"components": ["ax", "ay", "az"]
}
]
},
Expand Down
6 changes: 6 additions & 0 deletions dotnet/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
8 changes: 8 additions & 0 deletions dotnet/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application x:Class="BodyMusic.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions dotnet/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace BodyMusic
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
107 changes: 107 additions & 0 deletions dotnet/BodyMusic.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{230885AF-92D8-4547-9980-28BC5ABCE3FD}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BodyMusic</RootNamespace>
<AssemblyName>BodyMusic</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Scheduler.cs" />
<Compile Include="Voice.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Player.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</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 changes: 20 additions & 0 deletions dotnet/BodyMusic.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BodyMusic", "BodyMusic.csproj", "{230885AF-92D8-4547-9980-28BC5ABCE3FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{230885AF-92D8-4547-9980-28BC5ABCE3FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{230885AF-92D8-4547-9980-28BC5ABCE3FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{230885AF-92D8-4547-9980-28BC5ABCE3FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{230885AF-92D8-4547-9980-28BC5ABCE3FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added dotnet/Lib/Sanford.Collections.dll
Binary file not shown.
Binary file added dotnet/Lib/Sanford.Multimedia.Midi.dll
Binary file not shown.
Binary file added dotnet/Lib/Sanford.Multimedia.Timers.dll
Binary file not shown.
Binary file added dotnet/Lib/Sanford.Multimedia.dll
Binary file not shown.
Binary file added dotnet/Lib/Sanford.Threading.dll
Binary file not shown.
Binary file added dotnet/Lib/winmm.dll
Binary file not shown.
10 changes: 10 additions & 0 deletions dotnet/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Window x:Class="BodyMusic.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Closed="OnClosed"
KeyDown="OnKey">
<Grid>

</Grid>
</Window>
68 changes: 68 additions & 0 deletions dotnet/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Windows;
using System.Windows.Input;

namespace BodyMusic
{
partial class MainWindow : Window
{
private Player player;
private Scheduler scheduler;

public MainWindow()
{
InitializeComponent();
Init();
}

private void Init()
{
player = new Player();
player.MetronomeEnabled = false;
byte channel = 1;
byte instrumentId = 0;
player.AddVoice(channel, instrumentId, false);
scheduler = new Scheduler(player);
}

private void OnKey(object sender, KeyEventArgs e)
{
if (!e.IsRepeat)
{
Console.Out.WriteLine(e.Key);
int channel = 1;
int pitch = 0;
switch (e.Key)
{
case Key.A:
pitch = 60;
break;
case Key.S:
pitch = 62;
break;
case Key.D:
pitch = 64;
break;
case Key.Space:
player.MetronomeEnabled = !player.MetronomeEnabled;
break;
}
if (pitch > 0)
{
player.Update(channel, pitch);
}
}
}

private void OnClosed(object sender, EventArgs e)
{
scheduler.Stop();
player.Stop();
}

private void ShowMessage(string message)
{
MessageBox.Show(message, "Body Music", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
104 changes: 104 additions & 0 deletions dotnet/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;

namespace BodyMusic
{
class Player
{
private int deviceId = 0;
private byte metronomeChannel = 0;
private byte metronomeInstrumentId = 115;
private int metronomePitch = 72;
private IntPtr midiOut;
private Voice metronome;
private Dictionary<int, Voice> voices = new Dictionary<int, Voice>();

public Player()
{
MMRESULT result = midiOutOpen(out midiOut, (uint)deviceId, IntPtr.Zero, IntPtr.Zero, 0);
if (result != MMRESULT.MMSYSERR_NOERROR)
{
throw new Exception();
}
metronome = AddVoice(metronomeChannel, metronomeInstrumentId, true);
metronome.Update(metronomePitch);
}

internal Voice AddVoice(byte channel, byte instrumentId, bool recurrent)
{
Voice voice = new Voice(midiOut, channel, instrumentId, recurrent);
voices.Add(channel, voice);
return voice;
}

internal void Update(int channel, int pitch)
{
voices[channel].Update(pitch);
}

internal void Play()
{
if (MetronomeEnabled)
{
metronome.Play();
}
foreach (Voice voice in voices.Values)
{
voice.Play();
}
}

internal bool MetronomeEnabled
{
get;
set;
}

internal void Stop()
{
MMRESULT result = midiOutClose(midiOut);
if (result != Player.MMRESULT.MMSYSERR_NOERROR)
{
throw new Exception();
}
}

[Flags]
internal enum MMRESULT : uint
{
MMSYSERR_NOERROR,
MMSYSERR_ERROR,
MMSYSERR_BADDEVICEID,
MMSYSERR_NOTENABLED,
MMSYSERR_ALLOCATED,
MMSYSERR_INVALHANDLE,
MMSYSERR_NODRIVER,
MMSYSERR_NOMEM,
MMSYSERR_NOTSUPPORTED,
MMSYSERR_BADERRNUM,
MMSYSERR_INVALFLAG,
MMSYSERR_INVALPARAM,
MMSYSERR_HANDLEBUSY,
MMSYSERR_INVALIDALIAS,
MMSYSERR_BADDB = 14,
MMSYSERR_KEYNOTFOUND,
MMSYSERR_READERROR,
MMSYSERR_WRITEERROR,
MMSYSERR_DELETEERROR,
MMSYSERR_VALNOTFOUND,
MMSYSERR_NODRIVERCB
}

[DllImport("winmm.dll")]
internal static extern MMRESULT midiOutOpen(out IntPtr midiOut, uint deviceId, IntPtr dwCallback,
IntPtr dwCallbackInstance, uint dwFlags);

[DllImport("winmm.dll")]
internal static extern MMRESULT midiOutClose(IntPtr midiOut);

[DllImport("winmm.dll")]
internal static extern MMRESULT midiOutShortMsg(IntPtr midiOut, uint message);
}
}
Loading

0 comments on commit 13b2c19

Please sign in to comment.