]> wolfpit.net Git - hardware/bopit/.git/blob - BopIt/MainForm.cs
056d0abdff6ca45d756647e237b571186fd0419d
[hardware/bopit/.git] / BopIt / MainForm.cs
1 ´╗┐using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.IO.Ports;
7 using System.Linq;
8 using System.Text;
9 using System.Threading;
10 using System.Threading.Tasks;
11 using System.Windows.Forms;
12 using System.Windows.Forms.DataVisualization.Charting;
13 using Timer = System.Timers.Timer;
14
15 namespace BopIt
16 {
17 public partial class MainForm : Form
18 {
19 private Timer progressReset = new Timer();
20 private Series wave = new Series();
21
22 public MainForm()
23 {
24 InitializeComponent();
25 }
26
27 private void MainForm_Load(object sender, EventArgs args)
28 {
29 var waveIn = new NAudio.Wave.WaveInEvent();
30 waveIn.DeviceNumber = 0;
31 waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
32 waveIn.DataAvailable +=
33 (object waveSender, NAudio.Wave.WaveInEventArgs e) =>
34 {
35 //Dynamic waveform buffer offset, i want to fucking die btw
36 short[] waveValues = new short[(int)(e.BytesRecorded * .55)];
37 for (int i = 0 ; i < e.BytesRecorded; i += 2)
38 waveValues[(i / 2) + ((waveValues.Length - e.BytesRecorded / 2) / 2)] = (short)(BitConverter.ToInt16(e.Buffer, i) / 50);
39 waveform.Invoke((MethodInvoker)(() => waveform.Series[0].Points.DataBindY(waveValues)));
40 UpdateVolumeMeter(Math.Abs(waveValues[waveValues.Length / 2]));
41 };
42 waveIn.StartRecording();
43
44 //Volume meter de-incrimenter
45 #region
46 progressReset.Interval = 50;
47 progressReset.Elapsed += (o, arg) =>
48 {
49 if (progressBar.Value > 0)
50 progressBar.Invoke((MethodInvoker)(() => progressBar.Value--));
51
52 };
53 progressReset.Start();
54 #endregion
55
56 wave.ChartType = SeriesChartType.Area;
57 wave.Color = Color.Blue;
58 }
59
60 private void UpdateVolumeMeter(int data)
61 {
62 if(data < progressBar.Maximum && data > progressBar.Value)
63 progressBar.Invoke((MethodInvoker)(() => progressBar.Value = data));
64 }
65
66 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
67 {
68 progressReset.Dispose();
69 }
70 }
71 }