Skip to content

Commit

Permalink
Version 1.4
Browse files Browse the repository at this point in the history
Added:

Wave In has now buffer mode, which acts like times per second for peak
mode, but instead of delaying, it still reads the value 10 times per
second and takes the averages. This is usefull with mechanical
keyboards, which makes short loud noises hence a lower average is the
result (this may vary per mic)!

Bug fixes:
none

Worth mentioning:
Settings file has a new parameter, hence old settings files aren't
recognized anymore. You can simply copy your old setting to the new file
(Settingsv1_4.ini), but leave the buffer = 1; at the end!
  • Loading branch information
MrRare96 committed Feb 12, 2017
1 parent 7ebacb1 commit a4f2bd1
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 83 deletions.
120 changes: 80 additions & 40 deletions PressKeyByVoice/PressKeyByVoice/Form1.Designer.cs

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

117 changes: 79 additions & 38 deletions PressKeyByVoice/PressKeyByVoice/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public partial class Form1 : Form
public bool keyReleaserRunning = false;
public int keepingTrack = 0;
public int globalCounter = 0;
public int globalBuffer = 1;

//all the extern methods imported

Expand Down Expand Up @@ -74,6 +75,7 @@ public Form1()
smoothing = data.smoothing;
waveMode = data.waveMode;
peakMode = data.peakMode;
globalBuffer = data.globalBuffer;

int val = keyToBePressed;
foreach (VirtualKeyCode vk in Enum.GetValues(typeof(VirtualKeyCode)))
Expand All @@ -95,6 +97,8 @@ public Form1()
KeyPressDelayLabel.Text = keyPressDuration.ToString();
KeyPressDelayTrackBar.Value = keyPressDuration;
SmoothingCheckbox.Checked = smoothing;
BufferLabel.Text = globalBuffer.ToString();
BufferTrackBar.Value = globalBuffer;
CurrentKey.Text = char.ToLower(keyToBePressed).ToString();
KeyTextBox.Text = char.ToLower(keyToBePressed).ToString();
ToggleKeyLabel.Text = ((VirtualKeyCode)DisableEnableKey).ToString();
Expand Down Expand Up @@ -130,12 +134,21 @@ public Form1()
WaveModeCheckbox.Checked = waveMode;
PeakModeCheckbox.Checked = peakMode;

ChunksPerSecondTrackBar.Enabled = false;
ChunksPerSecondTrackBar.Visible = false;
ChunksPerSecondLabel.Visible = false;
TimesPerSecondLabel.Visible = false;
SmoothingCheckbox.Enabled = false;
SmoothingCheckbox.Visible = false;
if (waveMode)
{
ChunksPerSecondTrackBar.Enabled = false;
ChunksPerSecondTrackBar.Visible = false;
ChunksPerSecondLabel.Visible = false;
TimesPerSecondLabel.Visible = false;
SmoothingCheckbox.Enabled = false;
SmoothingCheckbox.Visible = false;
} else
{
BufferLabel.Visible = false;
BufferTrackBar.Visible = false;
BufferTimesLabel.Visible = false;
BufferTrackBar.Enabled = false;
}


//Get all process currently running:
Expand Down Expand Up @@ -193,7 +206,7 @@ private void AudioDeviceComboBox_SelectedIndexChanged(object sender, EventArgs e
waveDeviceId = index;
sourceStream = new WaveInEvent();
sourceStream.DeviceNumber = waveDeviceId;
sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(index).Channels);
sourceStream.WaveFormat = new WaveFormat(48000, WaveIn.GetCapabilities(index).Channels);
sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable);
sourceStream.StartRecording();
DebugTextBox.Text = "Selected device: " + index;
Expand All @@ -202,45 +215,58 @@ private void AudioDeviceComboBox_SelectedIndexChanged(object sender, EventArgs e

private void sourceStream_DataAvailable(object sender, WaveInEventArgs e)
{
float[] amplitudes = new float[e.BytesRecorded];
byte[] buffer = e.Buffer;

int counter = 0;
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((buffer[index + 1] << 8) |
buffer[index + 0]);
float sample32 = sample / 32768f;
amplitudes[counter] = sample32;
counter++;
}
//int volume = (int)(((((bytesAsInts.Average() / 10) - 10) * 100) - 200) / (1000 / sensitivity));
try

if (!keyReleaserRunning)
{
int volume = (int)(amplitudes.Max() * 10 * sensitivity);
float[] amplitudes = new float[e.BytesRecorded];
byte[] buffer = e.Buffer;

if (volume > treshold && volume < maxTreshold)
int counter = 0;
for (int index = 0; index < e.BytesRecorded; index += 2)
{
if (selectedProcess != null)
short sample = (short)((buffer[index + 1] << 8) |
buffer[index + 0]);
float sample32 = sample / 32768f;
amplitudes[counter] = sample32;
counter++;
}
//int volume = (int)(((((bytesAsInts.Average() / 10) - 10) * 100) - 200) / (1000 / sensitivity));
try
{
int volume = (int)(amplitudes.Max() * 10 * sensitivity);
keepingTrack = keepingTrack + volume;
if(globalCounter > (globalBuffer - 1))
{
if (selectedProcess.Id == GetActiveProcessID())
volume = keepingTrack / globalBuffer;
if (volume > treshold && volume < maxTreshold)
{
if (!keyReleaserRunning)
if (selectedProcess != null)
{
keyReleaser = new Thread(new ThreadStart(() => KeyReleaser()));
try { keyReleaser.Start(); } catch { };

if (selectedProcess.Id == GetActiveProcessID())
{
/* keyReleaser = new Thread(new ThreadStart(() => KeyReleaser()));
try { keyReleaser.Start(); } catch { };*/
DateTime _desired = DateTime.Now.AddMilliseconds(keyPressDuration);
while (DateTime.Now < _desired)
{
sim.Keyboard.KeyDown(key);
Thread.Sleep(1);
}
UpdateStatusBoxColor(Color.Red);
}
}
}
UpdatePeakVolumeBar(volume);
keepingTrack = 0;
globalCounter = 0;
}

globalCounter++;
} catch(Exception ex)
{
UpdateDebugText("Couldnt update volume bar:" + ex.ToString());
}
UpdatePeakVolumeBar(volume);
} catch(Exception ex)
{
UpdateDebugText("Couldnt update volume bar:" + ex.ToString());
}


}

private void UpdateKeyPressStatusText(string text)
Expand Down Expand Up @@ -338,9 +364,8 @@ private void UpdatePeakVolumeBar(int volume)
{
if (selectedProcess.Id == GetActiveProcessID())
{
/*Thread keyReleaser = new Thread(new ThreadStart(() => KeyReleaser()));
keyReleaser.Start();*/
sim.Keyboard.KeyDown(key);
Thread keyReleaser = new Thread(new ThreadStart(() => KeyReleaser()));
keyReleaser.Start();
KeyPressStatusLabel.Text = "Key " + keyToBePressed.ToString() + " is currently being pressed!";
}
}
Expand Down Expand Up @@ -658,6 +683,13 @@ private void ChunksPerSecondTrackBar_ValueChanged(object sender, EventArgs e)
ChunksPerSecondLabel.Text = ChunksPerSecondTrackBar.Value.ToString();
}


private void BufferTrackBar_ValueChanged(object sender, EventArgs e)
{
globalBuffer = BufferTrackBar.Value;
BufferLabel.Text = globalBuffer.ToString();
}

private void SmoothingCheckbox_CheckedChanged(object sender, EventArgs e)
{
if (SmoothingCheckbox.Checked)
Expand Down Expand Up @@ -709,6 +741,10 @@ private void WaveModeCheckbox_Click(object sender, EventArgs e)
TimesPerSecondLabel.Visible = false;
SmoothingCheckbox.Enabled = false;
SmoothingCheckbox.Visible = false;
BufferLabel.Visible = true;
BufferTrackBar.Visible = true;
BufferTimesLabel.Visible = true;
BufferTrackBar.Enabled = true;
waveMode = WaveModeCheckbox.Checked;

int waveInDevices = WaveIn.DeviceCount;
Expand Down Expand Up @@ -742,6 +778,10 @@ private void PeakModeCheckbox_Click(object sender, EventArgs e)
TimesPerSecondLabel.Visible = true;
SmoothingCheckbox.Enabled = true;
SmoothingCheckbox.Visible = true;
BufferLabel.Visible = false;
BufferTrackBar.Visible = false;
BufferTimesLabel.Visible = false;
BufferTrackBar.Enabled = false;
peakMode = PeakModeCheckbox.Checked;


Expand Down Expand Up @@ -881,6 +921,7 @@ private void SaveButton_Click(object sender, EventArgs e)
data.peakDeviceId = peakDeviceId;
data.keyToBePressed = keyToBePressed;
data.smoothing = smoothing;
data.globalBuffer = globalBuffer;
data.waveMode = waveMode;
data.peakMode = peakMode;
data.SaveData();
Expand Down
Loading

0 comments on commit a4f2bd1

Please sign in to comment.