Skip to content

Commit

Permalink
* Add Config File
Browse files Browse the repository at this point in the history
* Edit namespace
  • Loading branch information
luvletter2333 committed May 11, 2019
1 parent 5e8fc82 commit 25269b9
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 30 additions & 4 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
using System.Threading;
using System.IO;
using System.Drawing.Text;
using Newtonsoft.Json.Linq;

namespace Ndi_SubTitle
namespace NDI_SubTitle
{
public partial class Form1 : Form
{
Expand All @@ -26,12 +27,16 @@ public Form1()

SubTitle Previewing = SubTitle.Empty;

JObject Config;
NDIConfig NDI_Config;
NDIRender Renderer;
CancellationTokenSource cancelNDI;
float Font_Size;

#region Form
private void Form1_Load(object sender, EventArgs e)
{
// Init Fonts
using (InstalledFontCollection fontsCollection = new InstalledFontCollection())
{
FontFamily[] fontFamilies = fontsCollection.Families;
Expand All @@ -42,6 +47,27 @@ private void Form1_Load(object sender, EventArgs e)
}
}
cmb_Fonts.SelectedIndex = 1;
// Read Config
var config_path = Path.Combine(Directory.GetCurrentDirectory(), "NDI-SubTitle.config");
try
{
Config = JObject.Parse(File.ReadAllText(config_path));
if (Config.ContainsKey("NDI"))
NDI_Config = NDIConfig.ReadNDIConfig(Config["NDI"] as JObject);
else
NDI_Config = new NDIConfig(true);
if (Config.ContainsKey("Font-Size"))
Font_Size = Convert.ToSingle(Config["Font-Size"].ToString());
else
Font_Size = 50;
}
catch(Exception ex)
{
Console.WriteLine("Reading Config File Failed");
Console.WriteLine(ex.ToString());
NDI_Config = new NDIConfig(true);
Font_Size = 50;
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
Expand Down Expand Up @@ -95,7 +121,7 @@ private List<SubTitle> GenerateSubTitles(string filePath)
i--;
}
while (string.IsNullOrWhiteSpace(lines[lines.Count - 1]))
lines.RemoveAt(lines.Count - 1);
lines.RemoveAt(lines.Count - 1);
// Clean Blank Lines in the bottom of txt

var lst = new List<SubTitle>();
Expand Down Expand Up @@ -144,7 +170,7 @@ private void btn_start_Click(object sender, EventArgs e)
if (Renderer != null)
return;
cancelNDI = new CancellationTokenSource();
Renderer = new NDIRender(cancelNDI.Token, new Font(new FontFamily("Arial"), 50));
Renderer = new NDIRender(cancelNDI.Token, NDI_Config, new Font(new FontFamily(cmb_Fonts.SelectedItem.ToString()), Font_Size));
Task.Run(async () => await Renderer.Run());
lb_Status.ForeColor = Color.Green;
lb_Status.Text = "NDI On";
Expand Down Expand Up @@ -250,7 +276,7 @@ private void cmb_Fonts_SelectedIndexChanged(object sender, EventArgs e)
Console.WriteLine("Change Font To " + cmb.SelectedItem.ToString());
//Render_Font = new Font(new FontFamily(cmb.SelectedItem.ToString()), 50);
if (Renderer != null)
Renderer.ChangeFont(new Font(new FontFamily(cmb.SelectedItem.ToString()), 50));
Renderer.ChangeFont(new Font(new FontFamily(cmb.SelectedItem.ToString()), Font_Size));
}
}

Expand Down
2 changes: 1 addition & 1 deletion GetTxtEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using System.Text;

namespace Ndi_SubTitle
namespace NDI_SubTitle
{
public class GetTxtEncoding
{
Expand Down
21 changes: 21 additions & 0 deletions NDI-SubTitle.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"NDI":{
"NDI_width":1920,
"NDI_height":180,
"frameRateNumerator":50,
"frameRateDenominator":1,
"Point1_X":90,
"Point1_Y":20,
"Point2_X":90,
"Point2_Y":90,
"Color_Name":"White",
"Color":{
"alpha":255,
"red":255,
"green":255,
"blue":255
},
"Fade_Time":500
},
"Font-Size":50
}
73 changes: 73 additions & 0 deletions NDIConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NDI_SubTitle
{
public class NDIConfig
{
// NDI Output int width, int height, float aspectRatio, int frameRateNumerator, int frameRateDenominator
public int NDI_width;
public int NDI_height;
public float aspectRatio;
public int frameRateNumerator;
public int frameRateDenominator;

// Render
public Point Point_Sub1;
public Point Point_Sub2;
public Color Default_Color;
public int Fade_Time; //ms

public NDIConfig(bool is_default = false)
{
if (is_default)
{
NDI_width = 1920;
NDI_height = 180;
aspectRatio = Convert.ToSingle(NDI_width) / Convert.ToSingle(NDI_height);
frameRateNumerator = 50;
frameRateDenominator = 1;

Point_Sub1 = new Point(90, 20);
Point_Sub2 = new Point(90, 90);
Default_Color = Color.White;
Fade_Time = 500;
}
}

public static NDIConfig ReadNDIConfig(JObject jo)
{
try
{
NDIConfig config = new NDIConfig();
config.NDI_width = Convert.ToInt32(jo["NDI_width"].ToString());
config.NDI_height = Convert.ToInt32(jo["NDI_height"].ToString());
config.aspectRatio = Convert.ToSingle(config.NDI_width) / Convert.ToSingle(config.NDI_height);
config.frameRateNumerator = Convert.ToInt32(jo["frameRateNumerator"].ToString());
config.frameRateDenominator = Convert.ToInt32(jo["frameRateDenominator"].ToString());

config.Point_Sub1 = new Point(Convert.ToInt32(jo["Point1_X"].ToString()), Convert.ToInt32(jo["Point1_Y"].ToString()));
config.Point_Sub2 = new Point(Convert.ToInt32(jo["Point2_X"].ToString()), Convert.ToInt32(jo["Point2_Y"].ToString()));
if (jo.ContainsKey("Color_Name"))
config.Default_Color = Color.FromName(jo["Color_Name"].ToString());
else
config.Default_Color = Color.FromArgb(Convert.ToInt32(jo["Color"]["alpha"].ToString()),
Convert.ToInt32(jo["Color"]["red"].ToString()),
Convert.ToInt32(jo["Color"]["green"].ToString()),
Convert.ToInt32(jo["Color"]["blue"].ToString()));
config.Fade_Time = Convert.ToInt32(jo["Fade_Time"].ToString());
return config;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return new NDIConfig(true);//Return Default Setting
}
}
}
}
18 changes: 9 additions & 9 deletions NDIRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
using System.Threading.Tasks;


namespace Ndi_SubTitle
namespace NDI_SubTitle
{
public class NDIRender
{
public const int Alpha_Max = 255;
public const int Alpha_Min = 255;

private static Object syncLock = new Object();
private static readonly object syncLock = new object();
private readonly CancellationToken cancellationToken;

private readonly VideoFrame videoFrame;
Expand All @@ -31,30 +31,30 @@ public class NDIRender
private int Fading_X;
private readonly int delta_X;

public NDIRender(CancellationToken cancellationToken, Font font)
public NDIRender(CancellationToken cancellationToken, NDIConfig config, Font font)
{
this.cancellationToken = cancellationToken;

// We are going to create a 1920x180 frame at 50p, progressive (default).
this.videoFrame = new VideoFrame(1920, 180, (32.0f / 3.0f), 50, 1);
this.videoFrame = new VideoFrame(config.NDI_width, config.NDI_height, config.aspectRatio, config.frameRateNumerator, config.frameRateDenominator);
bmp = new Bitmap(videoFrame.Width, videoFrame.Height, videoFrame.Stride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb, videoFrame.BufferPtr);
System.Drawing.Imaging.PixelFormat.Format32bppArgb, videoFrame.BufferPtr);
graphics = Graphics.FromImage(bmp);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
// NDI Renderer
program = SubTitle.Empty;
fading = SubTitle.Empty;

// Style
defaultBrush = new SolidBrush(Color.White);
defaultBrush = new SolidBrush(config.Default_Color);
this.font = font;

// Fade Setting
isFading = false;
// For delta_X, We plus delta_X into alpha
// Fade Effect will last for 500ms
// So every frame it will change 255/25frames = 10
delta_X = Alpha_Max / 25;
// So every frame it will change 255/(50frames/2steps) = 10
delta_X = Alpha_Max / (config.frameRateNumerator / config.frameRateDenominator) * 2;

// If Fading_X < Alpha_Max / 2 ,it's the former part (from program to empty)
// If Fading_X >= Alpha_Max / 2 ,it's the latter part (from empty to fading )
Expand Down Expand Up @@ -84,7 +84,7 @@ public void Fade(SubTitle sub)
Fading_X = 0; //Reset Fading_X
}
}

public void ChangeFont(Font font)
{
lock (syncLock)
Expand Down
13 changes: 10 additions & 3 deletions Ndi-SubTitle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{191419D2-20C4-45F5-ABC3-1048740B54CF}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>Ndi_SubTitle</RootNamespace>
<AssemblyName>Ndi-SubTitle</AssemblyName>
<RootNamespace>NDI_SubTitle</RootNamespace>
<AssemblyName>NDI-SubTitle</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
Expand Down Expand Up @@ -71,6 +71,9 @@
<Reference Include="NDILibDotNet2">
<HintPath>Importx64\NDILibDotNet2.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -84,6 +87,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="NDIConfig.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -106,7 +110,9 @@
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down Expand Up @@ -134,6 +140,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>xcopy /y "$(ProjectDir)Importx64\*.dll" "$(ProjectDir)$(OutDir)"</PostBuildEvent>
<PostBuildEvent>xcopy /y "$(ProjectDir)Importx64\*.dll" "$(ProjectDir)$(OutDir)"
xcopy /y "$(ProjectDir)NDI-SubTitle.config" "$(ProjectDir)$(OutDir)"</PostBuildEvent>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Runtime.InteropServices;


namespace Ndi_SubTitle
namespace NDI_SubTitle
{
static class Program
{
Expand Down
Loading

0 comments on commit 25269b9

Please sign in to comment.