Add project files.
This commit is contained in:
parent
e85f1048e4
commit
4edd94b302
8 changed files with 298 additions and 0 deletions
25
SAPIServer.sln
Normal file
25
SAPIServer.sln
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 16
|
||||||
|
VisualStudioVersion = 16.0.34931.43
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SAPIServer", "SAPIServer\SAPIServer.csproj", "{BF824074-4C4E-4DE1-8DCA-F022682B00E1}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{BF824074-4C4E-4DE1-8DCA-F022682B00E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{BF824074-4C4E-4DE1-8DCA-F022682B00E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{BF824074-4C4E-4DE1-8DCA-F022682B00E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{BF824074-4C4E-4DE1-8DCA-F022682B00E1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {80220FEF-238D-4B58-B201-8960474A4AC1}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
93
SAPIServer/HTTPServer.cs
Normal file
93
SAPIServer/HTTPServer.cs
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace SAPIServer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Simple routing HTTP server for .NET Framework
|
||||||
|
/// </summary>
|
||||||
|
class HTTPServer
|
||||||
|
{
|
||||||
|
private bool listening = false;
|
||||||
|
private HttpListener listener;
|
||||||
|
private Dictionary<string, Func<HttpListenerContext, byte[]>> GETListeners;
|
||||||
|
private Dictionary<string, Func<HttpListenerContext, byte[]>> POSTListeners;
|
||||||
|
|
||||||
|
public HTTPServer()
|
||||||
|
{
|
||||||
|
listener = new HttpListener();
|
||||||
|
this.GETListeners = new Dictionary<string, Func<HttpListenerContext, byte[]>>();
|
||||||
|
this.POSTListeners = new Dictionary<string, Func<HttpListenerContext, byte[]>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Get(string uri, Func<HttpListenerContext, byte[]> handler)
|
||||||
|
{
|
||||||
|
if (GETListeners.ContainsKey(uri)) throw new InvalidOperationException("A handler by that URI already exists.");
|
||||||
|
if (!uri.StartsWith("/")) throw new ArgumentException("URIs must start with /");
|
||||||
|
this.GETListeners.Add(uri, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Post(string uri, Func<HttpListenerContext, byte[]> handler)
|
||||||
|
{
|
||||||
|
if (POSTListeners.ContainsKey(uri)) throw new InvalidOperationException("A handler by that URI already exists.");
|
||||||
|
if (!uri.StartsWith("/")) throw new ArgumentException("URIs must start with /");
|
||||||
|
this.POSTListeners.Add(uri, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Listen(ushort port)
|
||||||
|
{
|
||||||
|
if (listening) throw new InvalidOperationException("This HTTPServer is already listening.");
|
||||||
|
listening = true;
|
||||||
|
listener.Prefixes.Add(string.Format("http://127.0.0.1:{0}/", port));
|
||||||
|
listener.Start();
|
||||||
|
while (listener.IsListening)
|
||||||
|
{
|
||||||
|
var context = listener.GetContext();
|
||||||
|
ThreadPool.QueueUserWorkItem(_ =>
|
||||||
|
{
|
||||||
|
Dictionary<string, Func<HttpListenerContext, byte[]>> handlerDict;
|
||||||
|
// TODO: Make query params parsable
|
||||||
|
var uri = context.Request.RawUrl.Split('?')[0];
|
||||||
|
byte[] response;
|
||||||
|
switch (context.Request.HttpMethod)
|
||||||
|
{
|
||||||
|
case "GET":
|
||||||
|
{
|
||||||
|
handlerDict = GETListeners;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "POST":
|
||||||
|
{
|
||||||
|
handlerDict = POSTListeners;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
response = Encoding.UTF8.GetBytes($"Method not allowed: {context.Request.HttpMethod}");
|
||||||
|
context.Response.StatusCode = 405;
|
||||||
|
context.Response.ContentType = "text/plain";
|
||||||
|
context.Response.ContentLength64 = response.Length;
|
||||||
|
context.Response.OutputStream.Write(response, 0, response.Length);
|
||||||
|
context.Response.OutputStream.Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!handlerDict.TryGetValue(uri, out var handler))
|
||||||
|
{
|
||||||
|
response = Encoding.UTF8.GetBytes($"No route defined for {uri}");
|
||||||
|
context.Response.StatusCode = 404;
|
||||||
|
context.Response.ContentType = "text/plain";
|
||||||
|
}
|
||||||
|
else response = handler(context);
|
||||||
|
context.Response.ContentLength64 = response.Length;
|
||||||
|
context.Response.OutputStream.Write(response, 0, response.Length);
|
||||||
|
context.Response.OutputStream.Close();
|
||||||
|
Console.WriteLine($"[{DateTime.Now:u}] {context.Request.RemoteEndPoint.Address} - {context.Request.HttpMethod} {context.Request.RawUrl} - {context.Response.StatusCode}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
67
SAPIServer/Program.cs
Normal file
67
SAPIServer/Program.cs
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Speech.Synthesis;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace SAPIServer
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
var http = new HTTPServer();
|
||||||
|
http.Get("/api/voices", ctx =>
|
||||||
|
{
|
||||||
|
using (var synth = new SpeechSynthesizer())
|
||||||
|
{
|
||||||
|
ctx.Response.ContentType = "application/json";
|
||||||
|
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new VoicesResponse
|
||||||
|
{
|
||||||
|
voices = synth.GetInstalledVoices().Select(v => v.VoiceInfo.Name).ToArray()
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
http.Post("/api/synthesize", ctx =>
|
||||||
|
{
|
||||||
|
SynthesizePayload body;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string bodyraw;
|
||||||
|
using (var reader = new StreamReader(ctx.Request.InputStream))
|
||||||
|
{
|
||||||
|
bodyraw = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
body = JsonConvert.DeserializeObject<SynthesizePayload>(bodyraw);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ctx.Response.StatusCode = 400;
|
||||||
|
return Encoding.UTF8.GetBytes("Bad payload");
|
||||||
|
}
|
||||||
|
if (body == null || body.text == null || body.voice == null)
|
||||||
|
{
|
||||||
|
ctx.Response.StatusCode = 400;
|
||||||
|
return Encoding.UTF8.GetBytes("Bad payload");
|
||||||
|
}
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
using (var synth = new SpeechSynthesizer())
|
||||||
|
{
|
||||||
|
if (!synth.GetInstalledVoices().Any(v => v.VoiceInfo.Name == body.voice))
|
||||||
|
{
|
||||||
|
ctx.Response.StatusCode = 400;
|
||||||
|
return Encoding.UTF8.GetBytes("Voice not found");
|
||||||
|
}
|
||||||
|
synth.SelectVoice(body.voice);
|
||||||
|
synth.SetOutputToWaveStream(ms);
|
||||||
|
synth.Speak(body.text);
|
||||||
|
ctx.Response.ContentType = "audio/wav";
|
||||||
|
return ms.ToArray();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
http.Listen(8080);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
SAPIServer/Properties/AssemblyInfo.cs
Normal file
36
SAPIServer/Properties/AssemblyInfo.cs
Normal file
|
@ -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("SAPIServer")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("SAPIServer")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © 2024")]
|
||||||
|
[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("bf824074-4c4e-4de1-8dca-f022682b00e1")]
|
||||||
|
|
||||||
|
// 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")]
|
58
SAPIServer/SAPIServer.csproj
Normal file
58
SAPIServer/SAPIServer.csproj
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" 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>{BF824074-4C4E-4DE1-8DCA-F022682B00E1}</ProjectGuid>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<RootNamespace>SAPIServer</RootNamespace>
|
||||||
|
<AssemblyName>SAPIServer</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<Deterministic>true</Deterministic>
|
||||||
|
</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="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Speech" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="HTTPServer.cs" />
|
||||||
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="SynthesizePayload.cs" />
|
||||||
|
<Compile Include="VoicesResponse.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="packages.config" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
</Project>
|
8
SAPIServer/SynthesizePayload.cs
Normal file
8
SAPIServer/SynthesizePayload.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace SAPIServer
|
||||||
|
{
|
||||||
|
class SynthesizePayload
|
||||||
|
{
|
||||||
|
public string voice { get; set; }
|
||||||
|
public string text { get; set; }
|
||||||
|
}
|
||||||
|
}
|
7
SAPIServer/VoicesResponse.cs
Normal file
7
SAPIServer/VoicesResponse.cs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
namespace SAPIServer
|
||||||
|
{
|
||||||
|
class VoicesResponse
|
||||||
|
{
|
||||||
|
public string[] voices { get; set; }
|
||||||
|
}
|
||||||
|
}
|
4
SAPIServer/packages.config
Normal file
4
SAPIServer/packages.config
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net40" />
|
||||||
|
</packages>
|
Loading…
Reference in a new issue