Imports Microsoft.DirectX Imports Microsoft.DirectX.DirectSound Imports System Imports System.IO Imports System.Threading Public Class Form1 Private devices As CaptureDevicesCollection Private info As DeviceInformation Public CaptureDeviceGuid As Guid Public applicationDevice As Capture Public CaptureBufferSize As Integer Public InputFormat As WaveFormat Public applicationBuffer As CaptureBuffer Public NumberRecordNotifications As Integer = 16 ' Public PositionNotify(NumberRecordNotifications + 1) As BufferPositionNotify Public PositionNotify(NumberRecordNotifications - 1) As BufferPositionNotify Public NextCaptureOffset As Integer Private NotifyThread As Thread Private Capturing As Boolean = False Private Recording As Boolean = False Public NotificationEvent As AutoResetEvent Public NotifySize As Integer Public applicationNotify As Notify Private FileName, FileName2 As String Private datasum As Double = 0 Private datasum2 As Double = 0 Private WaveFile, datafile As FileStream Private Writer2 As StreamWriter Private Reader As BinaryReader Public SampleCount As Integer = 0 Public timelength() As Double = New Double(100000) {} Public startTime As New DateTime Public endTime As New DateTime Private n As Long = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load devices = New CaptureDevicesCollection() For Each info As DeviceInformation In devices ComboBoxDevice.Items.Add(info.Description) Next info FileName = Nothing WaveFile = Nothing ButtonFileName.Enabled = False ButtonStart.Enabled = False ButtonStop.Enabled = False Me.DataGridView1.ColumnCount = 2 ' Me.DataGridView1.Columns(0).Name = "No." Me.DataGridView1.Columns(0).Width = 200 ' Me.DataGridView1.Columns(1).Name = "積/絶対値" Me.DataGridView1.Columns(1).Width = 100 ' Me.DataGridView1.Font = New Font(Me.DataGridView1.Font.Bold, 22) End Sub Private Sub ButtonDevice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDevice.Click ' MsgBox(Convert.ToString(ComboBoxDevice.SelectedIndex)) If ComboBoxDevice.Items.Count > 0 Then Me.CaptureDeviceGuid = devices(0).DriverGuid End If CaptureBufferSize = 0 Try applicationDevice = New Capture(CaptureDeviceGuid) 'MsgBox("キャプチャーデバイスオブジェクトが作成されました。") ButtonDevice.Enabled = False Catch ex As Exception MsgBox(ex.ToString) End Try ' applicationBuffer.Start(True) ButtonFileName.Enabled = True End Sub Private Sub CreateCaptureBuffer() Dim dscheckboxd As CaptureBufferDescription dscheckboxd = New CaptureBufferDescription InputFormat.FormatTag = WaveFormatTag.Pcm InputFormat.SamplesPerSecond = 48000 InputFormat.BitsPerSample = 16 InputFormat.Channels = 2 'InputFormat.Channels = 2 InputFormat.BlockAlign = Int((InputFormat.Channels * (InputFormat.BitsPerSample / 8))) InputFormat.AverageBytesPerSecond = InputFormat.BlockAlign * InputFormat.SamplesPerSecond If InputFormat.AverageBytesPerSecond / 8 < 1024 Then '8を大きな値に NotifySize = 1024 Else NotifySize = InputFormat.AverageBytesPerSecond / 8 '8を大きな値に End If 'MsgBox(InputFormat.AverageBytesPerSecond) NotifySize -= NotifySize Mod InputFormat.BlockAlign CaptureBufferSize = NotifySize * NumberRecordNotifications dscheckboxd.BufferBytes = CaptureBufferSize dscheckboxd.Format = InputFormat ' dscheckboxd.ControlEffects = True Try applicationBuffer = New CaptureBuffer(dscheckboxd, applicationDevice) 'MsgBox("キャプチャーバッファを作成しました。") Catch ex As Exception MsgBox(ex.ToString) Me.Close() End Try NextCaptureOffset = 0 InitNotification() 'MsgBox("InitNotificationが終了した模様") End Sub Public Sub InitNotification() If applicationBuffer = Nothing Then Me.Close() End If NotifyThread = New Thread(New ThreadStart(AddressOf WaitThread)) Capturing = True NotifyThread.Start() NotificationEvent = New AutoResetEvent(False) applicationNotify = New Notify(applicationBuffer) Dim i As Integer For i = 0 To NumberRecordNotifications - 1 PositionNotify(i).Offset = NotifySize * i + NotifySize - 1 PositionNotify(i).EventNotifyHandle = NotificationEvent.Handle Next applicationNotify.SetNotificationPositions(PositionNotify) End Sub 'Private Sub Wait() ' MsgBox("Start Wait") ' While Capturing = True ' NotificationEvent.WaitOne(Timeout.Infinite, True) ' RecordCapturedData() ' End While ' MsgBox("Stop Wait") 'End Sub Private Sub WaitThread() ' MsgBox("Start WaitThread") While Capturing = True NotificationEvent.WaitOne(Timeout.Infinite, True) RecordCapturedData() End While MsgBox("Stopped Recording") End Sub Delegate Sub SetTextBox1Delegate(ByVal Value As String()) Private TextBox1Delegate As _ New SetTextBox1Delegate(AddressOf SetTextBox1) Private Sub SetTextBox1(ByVal Value As String()) DataGridView1.Rows.Add(Value) DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.Rows.Count - 1 End Sub Delegate Sub SetTextBox2Delegate(ByVal Value As String) Private TextBox2Delegate As _ New SetTextBox2Delegate(AddressOf SetTextBox2) Private Sub SetTextBox2(ByVal Value As String) Label1.Text = Value End Sub Private Sub RecordCapturedData() endTime = DateTime.Now timelength(n) = (endTime.Subtract(startTime)).TotalMilliseconds n = n + 1 startTime = DateTime.Now Dim CaptureData() As Int16 = Nothing Dim ReadPos, CapturePos, LockSize As Integer applicationBuffer.GetCurrentPosition(CapturePos, ReadPos) LockSize = ReadPos - NextCaptureOffset If LockSize < 0 Then LockSize += CaptureBufferSize LockSize -= (LockSize Mod NotifySize) If LockSize = 0 Then Return CaptureData = applicationBuffer.Read(NextCaptureOffset, GetType(Int16), LockFlag.None, LockSize / 2) Dim right, left, wa, seki, seki2 As Int64 Dim i, j, datalength As Integer For j = 1 To 4 seki = 0 seki2 = 0 wa = 0 datalength = CaptureData.Length / 4 For i = 0 + datalength * (j - 1) To datalength - 1 + datalength * (j - 1) Step 2 'Writer.Write(CaptureData(i)) 'Writer.Write(CaptureData(i + 1)) 'Writer2.Write(CaptureData(i)) 'Writer2.Write(CaptureData(i + 1)) right = CInt(CaptureData(i)) left = CInt(CaptureData(i + 1)) seki += right * left wa += Math.Abs(right) seki2 += right * right Next ' Writer2.Write(CaptureData.Length) Dim s1, s2 As String s1 = endTime.AddMilliseconds(timelength(n - 1) * (-1) * (5 - j) / 4).ToString("yyyy/MM/dd HH:mm:ss.fff") 's2 = (seki / seki2 * 6.0785 + 0.001463).ToString("00.000000") s2 = Math.Sqrt(seki2 / datalength).ToString("00.000000") Dim factor As Double Dim kaisu As Integer = 3 factor = Math.Exp(-1 / kaisu) 'datasum = factor * datasum + (seki / seki2 * 6.0785 + 0.001463) 'datasum2 = factor * datasum2 + datasum * (1 - factor) datasum = factor * datasum + Math.Sqrt(seki2 / datalength) datasum2 = factor * datasum2 + datasum * (1 - factor) Dim s As String() = {s1, s2} ' Writer2.WriteLine(s1 + "," + s2) Writer2.WriteLine(s1 + "," + s2) Me.Invoke(TextBox1Delegate, New Object() {s}) Me.Invoke(TextBox2Delegate, New Object() {(datasum2 * (1 - factor)).ToString("00.000000")}) Next SampleCount += CaptureData.Length * 2 NextCaptureOffset += CaptureData.Length * 2 NextCaptureOffset = NextCaptureOffset Mod CaptureBufferSize Return End Sub Private Sub ButtonFileName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFileName.Click If SaveFileDialog1.ShowDialog Then FileName2 = SaveFileDialog1.FileName Try datafile = New FileStream(FileName2, FileMode.Append) Catch ex As SystemException MsgBox(ex.Message) Return End Try Writer2 = New StreamWriter(datafile) End If ButtonFileName.Text = FileName ButtonFileName.Enabled = False ButtonStart.Enabled = True End Sub Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click ButtonStart.Enabled = False 'NotifyThread.Start() 'Recording = Not Recording 'If Recording Then 'Capturing = True CreateCaptureBuffer() startTime = DateTime.Now applicationBuffer.Start(True) ButtonStop.Enabled = True 'Wait() 'Else 'applicationBuffer.Stop() 'RecordCapturedData() 'Writer.Seek(4, SeekOrigin.Begin) 'Writer.Write(Convert.ToInt32(SampleCount + 36)) 'Writer.Seek(40, SeekOrigin.Begin) 'Writer.Write(SampleCount) 'Writer.Close() 'Writer = Nothing 'WaveFile = Nothing ''ButtonStop.Enabled = False 'For n = 1 To 100 ' MsgBox(timelength(n)) 'Next 'End If End Sub Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click applicationBuffer.Stop() 'Writer2.Close() ButtonStop.Enabled = False 'Capturing = False RecordCapturedData() ButtonStart.Enabled = True End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If MessageBox.Show("閉じますか?", "確認", MessageBoxButtons.YesNo) = DialogResult.No Then e.Cancel = True End If Writer2.Close() Capturing = False NotificationEvent.Set() End Sub Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub End Class