Skip to content

Commit

Permalink
Mic Enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
nicsure committed Mar 5, 2024
1 parent a423f9f commit 6bc01da
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 56 deletions.
32 changes: 32 additions & 0 deletions QuanshengDock/Audio/Sound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using QuanshengDock.General;
using QuanshengDock.View;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -32,6 +33,8 @@ public static class Sound
private static SineStream? sineStream2 = null;

private static readonly ViewModel<double> volume = VM.Get<double>("Volume");
private static readonly ViewModel<double> boost = VM.Get<double>("Boost");
private static readonly ViewModel<double> micLevel = VM.Get<double>("MicLevel");
private static readonly ViewModel<bool> passthrough = VM.Get<bool>("Passthrough");
private static readonly ViewModel<double> latency = VM.Get<double>("AudioLatency");
private static readonly ViewModel<double> buffers = VM.Get<double>("AudioBuffers");
Expand Down Expand Up @@ -125,6 +128,30 @@ public static void StartTone2(int outputID, int toneFreq)
}
}

private static double AmplifyPCM16(byte[] buffer, int len, double amp)
{
double lev = 0.0;
unsafe
{
fixed(byte* bptr = buffer)
{
short* shorts = (short*)bptr;
len >>= 1;
for (int i = 0; i < len; i++)
{
double d = (shorts[i] * amp).Clamp(short.MinValue, short.MaxValue);
if (d > 0)
{
d /= 327.68;
if (d > lev) lev = d;
}
shorts[i] = (short)d;
}
}
}
return lev;
}

public static void Start()
{
using (radioRxAudio)
Expand Down Expand Up @@ -190,6 +217,8 @@ public static void Start()
{
if (txPassthrough.BufferedDuration.TotalSeconds > latency.Value / 500.0)
txPassthrough.ClearBuffer();
micFilter[micCnt++ % micFilter.Length] = AmplifyPCM16(e.Buffer, e.BytesRecorded, boost.Value);
micLevel.Value = micFilter.Average();
txPassthrough.AddSamples(e.Buffer, 0, e.BytesRecorded);
};
radioTxAudio.Play();
Expand All @@ -198,8 +227,11 @@ public static void Start()
catch { }
}
}
private static double[] micFilter = new double[3];
private static int micCnt = 0;
}


public class SineStream : WaveStream
{
private readonly WaveFormat format;
Expand Down
5 changes: 5 additions & 0 deletions QuanshengDock/Data/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class Context
public ViewModel<double> VSize { get; } = new(1.0, nameof(VSize));
public ViewModel<double> FStretch { get; } = new(0.23, nameof(FStretch));
public ViewModel<double> Volume { get; } = new(0.75, nameof(Volume), true);
public ViewModel<double> Boost { get; } = new(1.0, nameof(Boost), true);
public ViewModel<double> MicLevel { get; } = new(1.0, nameof(MicLevel));
public ViewModel<double> MicBarHeight { get; } = new(20.0, nameof(MicBarHeight));
public ViewModel<bool> MicBarShown { get; } = new(true, nameof(MicBarHeight), true);
public ViewModel<Typeface> LCDFont { get; } = new(nameof(LCDFont));
public ViewModel<Typeface> LCDBoldFont { get; } = new(nameof(LCDBoldFont));
public ViewModel<string> LCDFontName { get; } = new("Consolas", nameof(LCDFontName), true);
Expand Down Expand Up @@ -216,6 +220,7 @@ public Context()
SpecSteps.ForceUpdate++;
WaterfallCol1.ForceUpdate++;

MicBarHeight.SetConverter(() => MicBarShown.Value ? 20.0 : 0.0, MicBarShown);
TaskBar.SetConverter(() => Title.Value.Length > 0 ? Title.Value : "QD", Title);
XWatchName.SetConverter(() => XWatch.Value?" WR":string.Empty, XWatch);
XScanLCD.SetConverter(() => BusyXVFO.Value ? "SCAN" : string.Empty, BusyXVFO);
Expand Down
9 changes: 6 additions & 3 deletions QuanshengDock/ExtendedVFO/XVFO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace QuanshengDock.ExtendedVFO
{
public enum PttMode
{
None=0, Normal = 1, VOX = 2, External = 4
None = 0, Normal = 1, VOX = 2, External = 4
}

public static class XVFO
Expand Down Expand Up @@ -118,9 +118,12 @@ public static void ToggleMicGain(int dir)
micGain.Value = i;
}

public static void ToggleVOX()
public static void ToggleVOX(int newVal = -1)
{
vox.Value = vox.Value != 0 ? 0 : 1;
if (newVal == -1)
vox.Value = vox.Value != 0 ? 0 : 1;
else
vox.Value = newVal;
VOX.Init();
}

Expand Down
17 changes: 14 additions & 3 deletions QuanshengDock/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,20 @@
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="{Binding MicBarHeight.Value}"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<CheckBox Name="Defocusser" Width="0" Height="0"/>
<Viewbox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="4" Stretch="Fill">
<ui:SMeter MaskColor="#ff202020" Margin="0,0,0,0.5" Width="1" Height="1" Value="{Binding MicLevel.Value}" >
<ui:SMeter.MeterColor>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FF00FF71" Offset="0"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush>
</ui:SMeter.MeterColor>
</ui:SMeter>
</Viewbox>
<StackPanel HorizontalAlignment="Left" Grid.ColumnSpan="4" Orientation="Horizontal" IsHitTestVisible="False" Opacity="0.7" Margin="60,10,0,0">
<TextBlock FontSize="12" Text="{Binding Title.Value}" FontWeight="Bold" VerticalAlignment="Top" />
<TextBlock FontSize="12" Text=" QD " FontWeight="Bold" VerticalAlignment="Top" />
Expand All @@ -131,7 +142,7 @@
<Frame Tag="Minimize" Width="40" Style="{StaticResource ControlButton}" FontSize="19" Content="🗕" HorizontalAlignment="Right" Margin="0,-7,80,5" />
<Frame Tag="Maximize" Width="40" Style="{StaticResource ControlButton}" FontSize="13" Content="🗖" HorizontalAlignment="Right" Margin="0,-7,40,5" />
</Grid>
<Grid Name="RadioGrid" Grid.Column="0" Grid.Row="1">
<Grid Name="RadioGrid" Grid.Column="0" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="6*"/>
<RowDefinition Height="1*"/>
Expand Down Expand Up @@ -179,7 +190,7 @@
<Frame Tag="15" Grid.Column="4" Grid.Row="4" Style="{StaticResource ControlButton}" Content="F&#10; # 🔒"/>

</Grid>
<Grid Name="SpectrumGrid" Grid.Column="2" Grid.Row="1">
<Grid Name="SpectrumGrid" Grid.Column="2" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
Expand Down Expand Up @@ -334,7 +345,7 @@
<Slider Orientation="Vertical" Value="{Binding WaterfallSpeed.Value}" Minimum="0.1" Maximum="20" Height="140" Margin="10,0,10,0"/>
</Viewbox>
</Grid>
<Border IsEnabled="{Binding NotBusyXVFO.Value}" Grid.Column="3" Grid.Row="1" CornerRadius="20" Background="#ff181818" Margin="2,2,2,2">
<Border IsEnabled="{Binding NotBusyXVFO.Value}" Grid.Column="3" Grid.Row="2" CornerRadius="20" Background="#ff181818" Margin="2,2,2,2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.25*"/>
Expand Down
1 change: 1 addition & 0 deletions QuanshengDock/QuanshengDock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<UseWPF>true</UseWPF>
<UseWindowsForms>True</UseWindowsForms>
<ApplicationIcon>quansheng.ico</ApplicationIcon>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 6bc01da

Please sign in to comment.