Skip to content

Commit

Permalink
Improve quality of sound output
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmCoder committed Aug 13, 2022
1 parent 066816b commit 5985974
Show file tree
Hide file tree
Showing 10 changed files with 743 additions and 51 deletions.
168 changes: 168 additions & 0 deletions Halovision/FifoStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;
using System.IO;
using System.Linq;
using System.Collections;

namespace lucidcode.LucidScribe.Plugin.Halovision
{
public class FifoStream : Stream
{
private const int BlockSize = 65536;
private const int MaxBlocksInCache = (3 * 1024 * 1024) / BlockSize;

private int m_Size;
private int m_RPos;
private int m_WPos;
private Stack m_UsedBlocks = new Stack();
private ArrayList m_Blocks = new ArrayList();

private byte[] AllocBlock()
{
byte[] Result = null;
Result = m_UsedBlocks.Count > 0 ? (byte[])m_UsedBlocks.Pop() : new byte[BlockSize];
return Result;
}
private void FreeBlock(byte[] block)
{
if (m_UsedBlocks.Count < MaxBlocksInCache)
m_UsedBlocks.Push(block);
}
private byte[] GetWBlock()
{
byte[] Result = null;
if (m_WPos < BlockSize && m_Blocks.Count > 0)
Result = (byte[])m_Blocks[m_Blocks.Count - 1];
else
{
Result = AllocBlock();
m_Blocks.Add(Result);
m_WPos = 0;
}
return Result;
}

// Stream members
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get
{
lock (this)
return m_Size;
}
}
public override long Position
{
get { throw new InvalidOperationException(); }
set { throw new InvalidOperationException(); }
}
public override void Close()
{
Flush();
}
public override void Flush()
{
lock (this)
{
foreach (byte[] block in m_Blocks)
FreeBlock(block);
m_Blocks.Clear();
m_RPos = 0;
m_WPos = 0;
m_Size = 0;
}
}
public override void SetLength(long len)
{
throw new InvalidOperationException();
}
public override long Seek(long pos, SeekOrigin o)
{
throw new InvalidOperationException();
}
public override int Read(byte[] buf, int ofs, int count)
{
lock (this)
{
int Result = Peek(buf, ofs, count);
Advance(Result);
return Result;
}
}
public override void Write(byte[] buf, int ofs, int count)
{
lock (this)
{
int Left = count;
while (Left > 0)
{
int ToWrite = Math.Min(BlockSize - m_WPos, Left);
Array.Copy(buf, ofs + count - Left, GetWBlock(), m_WPos, ToWrite);
m_WPos += ToWrite;
Left -= ToWrite;
}
m_Size += count;
}
}

// extra stuff
public int Advance(int count)
{
lock (this)
{
int SizeLeft = count;
while (SizeLeft > 0 && m_Size > 0)
{
if (m_RPos == BlockSize)
{
m_RPos = 0;
FreeBlock((byte[])m_Blocks[0]);
m_Blocks.RemoveAt(0);
}
int ToFeed = m_Blocks.Count == 1 ? Math.Min(m_WPos - m_RPos, SizeLeft) : Math.Min(BlockSize - m_RPos, SizeLeft);
m_RPos += ToFeed;
SizeLeft -= ToFeed;
m_Size -= ToFeed;
}
return count - SizeLeft;
}
}
public int Peek(byte[] buf, int ofs, int count)
{
lock (this)
{
int SizeLeft = count;
int TempBlockPos = m_RPos;
int TempSize = m_Size;

int CurrentBlock = 0;
while (SizeLeft > 0 && TempSize > 0)
{
if (TempBlockPos == BlockSize)
{
TempBlockPos = 0;
CurrentBlock++;
}
int Upper = CurrentBlock < m_Blocks.Count - 1 ? BlockSize : m_WPos;
int ToFeed = Math.Min(Upper - TempBlockPos, SizeLeft);
Array.Copy((byte[])m_Blocks[CurrentBlock], TempBlockPos, buf, ofs + count - SizeLeft, ToFeed);
SizeLeft -= ToFeed;
TempBlockPos += ToFeed;
TempSize -= ToFeed;
}
return count - SizeLeft;
}
}
}
}
2 changes: 0 additions & 2 deletions Halovision/FormatChunk.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lucidcode.LucidScribe.Plugin.Halovision
{
Expand Down
34 changes: 34 additions & 0 deletions Halovision/Halovision.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="Microsoft.Win32.Registry, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.4.7.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
<Reference Include="NAudio, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.2.1.0\lib\net472\NAudio.dll</HintPath>
</Reference>
<Reference Include="NAudio.Asio, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Asio.2.1.0\lib\netstandard2.0\NAudio.Asio.dll</HintPath>
</Reference>
<Reference Include="NAudio.Core, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Core.2.1.0\lib\netstandard2.0\NAudio.Core.dll</HintPath>
</Reference>
<Reference Include="NAudio.Midi, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Midi.2.1.0\lib\netstandard2.0\NAudio.Midi.dll</HintPath>
</Reference>
<Reference Include="NAudio.Wasapi, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Wasapi.2.1.0\lib\netstandard2.0\NAudio.Wasapi.dll</HintPath>
</Reference>
<Reference Include="NAudio.WinForms, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.WinForms.2.1.0\lib\net472\NAudio.WinForms.dll</HintPath>
</Reference>
<Reference Include="NAudio.WinMM, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.WinMM.2.1.0\lib\netstandard2.0\NAudio.WinMM.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
Expand Down Expand Up @@ -169,6 +193,12 @@
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security.AccessControl, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.AccessControl.4.7.0\lib\net461\System.Security.AccessControl.dll</HintPath>
</Reference>
<Reference Include="System.Security.Principal.Windows, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Principal.Windows.4.7.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
Expand All @@ -181,13 +211,15 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DataChunk.cs" />
<Compile Include="FifoStream.cs" />
<Compile Include="FormatChunk.cs" />
<Compile Include="PluginHandler.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="SignalGenerator.cs" />
<Compile Include="SineGenerator.cs" />
<Compile Include="TCMP.cs" />
<Compile Include="VisionForm.cs">
Expand All @@ -199,6 +231,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VLC.cs" />
<Compile Include="WaveHeader.cs" />
<Compile Include="WaveNative.cs" />
<Compile Include="WaveOut.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
Expand Down
69 changes: 25 additions & 44 deletions Halovision/PluginHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Media;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

Expand All @@ -12,10 +13,7 @@ public static class Device
{
static bool Initialized;
static bool InitError;
static int Value = 0;
static int Readings = 0;

private static bool clearValue;
static List<int> Readings = new List<int>() { 0 };

public static EventHandler<EventArgs> VisionChanged;

Expand Down Expand Up @@ -44,15 +42,9 @@ public static bool Initialize()

private static void VisionForm_ValueChanged(int value)
{
Value = Value + value;
Readings = Readings + 1;
Readings.Add(value);

if (clearValue)
{
clearValue = false;
Readings = 0;
Value = 0;
}
if (Readings.Count > 8) Readings.RemoveAt(0);

if (VisionChanged != null)
{
Expand Down Expand Up @@ -91,13 +83,9 @@ public static void Dispose()
}

public static int GetVision()
{
//if (Readings == 0) return 0;

int value = Value; //
// Readings;
//if (Readings > 0) value = value / Readings;
clearValue = true;
{
int value = Readings.Sum() / Readings.Count;
if (value > 999) return 999;
return value;
}

Expand Down Expand Up @@ -296,10 +284,6 @@ public override string Name

public override bool Initialize()
{

if (Device.Auralize) {
System.Media.SystemSounds.Asterisk.Play();
}
return Device.Initialize();
}

Expand All @@ -314,29 +298,24 @@ public override double Value
if (Device.Auralize && vision > 0) {
Auralize(vision);
}

return vision;
}
}

private void Auralize(double frequency)
{
var header = new WaveHeader();
var format = new FormatChunk();
var austioChunk = new DataChunk();
var sineData = new SineGenerator(frequency);

austioChunk.AddSampleData(sineData.Data, sineData.Data);
header.FileLength += format.Length() + austioChunk.Length();

var soundBytes = new List<byte>();
soundBytes.AddRange(header.GetBytes());
soundBytes.AddRange(format.GetBytes());
soundBytes.AddRange(austioChunk.GetBytes());
var sineWave = new NAudio.Wave.SampleProviders.SignalGenerator()
{
Gain = 0.1,
Frequency = 256 + frequency,
Type = SignalGeneratorType.Sin
}.Take(TimeSpan.FromMilliseconds(256));

var sound = new SoundPlayer();
sound.Stream = new MemoryStream(soundBytes.ToArray());
sound.Play();
var waveOutEvent = new WaveOutEvent();
waveOutEvent.Pause();
waveOutEvent.Init(sineWave);
waveOutEvent.Play();
}

public override void Dispose()
Expand Down Expand Up @@ -381,14 +360,16 @@ public void VisionChanged(object sender, EventArgs e)
ClearTicks = false;
TickCount = "";
}
TickCount += sender + ",";
int value = (int)sender;
if (value > 999) value = 999;
TickCount += value + ",";

if (ClearBuffer)
{
ClearBuffer = false;
BufferData = "";
}
BufferData += sender + ",";
BufferData += value + ",";
}

public void Dispose()
Expand Down
4 changes: 2 additions & 2 deletions Halovision/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 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.1.6.0")]
[assembly: AssemblyFileVersion("1.1.6.0")]
[assembly: AssemblyVersion("1.1.7.0")]
[assembly: AssemblyFileVersion("1.1.7.0")]
Loading

0 comments on commit 5985974

Please sign in to comment.