title: Run script based on sound VU level in vb.net
body: <p>
  I'm designing an application to farm one of my games for me. However, The approach I'm going with is to listen for a sound or <strong>spike</strong> in <em>sound levels</em> before running a desired macro combination. The audio I'm looking to monitor is the current PC's audio output, not a microphone or external device.</p>
  
  <p>This subject seem's to be vary vague on tutorials or information, however from my knowledge I found a post <a href="https://stackoverflow.com/a/9503766/8667430">here</a> that explains to use a <a href="https://www.codeproject.com/Articles/20503/LED-Style-Volume-Meter-Using-DirectX" rel="noreferrer">BuckSoft.DirectSound</a> project?</p>
  
  <p>So based on the information I found, I assume you do something like the following?</p>
  
  <pre class="lang-vb prettyprint-override"><code>If AnalogSignalMeter1.LeftLevel &gt; 0 Or AnalogSignalMeter1.RightLevel &gt; 0 Then
  
      ' Do Something
  
  End If
  </code></pre>
  
  <p>From personal prospective I would love to help clarify a solution to the public as this topic is not well documented and avoided. I'm open to all solutions or suggestions, however, my focus is on <strong>vb.net</strong> and will consider <em>C#</em> if needed.</p>
  
  <hr>
  
  <p>Another option that I have seen is the CoreAudio API. I have seen this API used on multiple posts for the ability to extract the current sound levels, however I have not seen examples for reading the current <code>Master VU meter and fader</code> data/Levels.</p>
  
  <pre class="lang-vb prettyprint-override"><code>Private Function GetVol() As Integer        'Function to read current volume setting
      Dim MasterMinimum As Integer = 0
      Dim DevEnum As New MMDeviceEnumerator()
      Dim device As MMDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
      Dim Vol As Integer = 0
  
      With device.AudioEndpointVolume
          Vol = CInt(.MasterVolumeLevelScalar * 100)
          If Vol &lt; MasterMinimum Then
              Vol = MasterMinimum / 100.0F
          End If
      End With
      Return Vol
  End Function
  </code></pre>
  
  <p><strong>Data I'm looking to gather:</strong></p>
  
  <p>As showed bellow, I'm NOT looking to gather the physical <code>sound level %</code> but rather looking to run actions if the VU levels spike from 0. This means if I play a video or sound file, the application will hear a sound coming from the current work station and perform a desired action.</p>
  
  <p><a href="https://i.stack.imgur.com/LBCYE.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LBCYE.png" alt="As Seen Here"></a></p>
  
  <p>Bellow will be my rough example of how I plan to use or collect data from my prospective. Using an timer within vb.net I can have an statement consistently looking for an change in <code>"VUSoundLevels"</code> (Not a real statement) and run a script when an change/input happens.</p>
  
  <pre class="lang-vb prettyprint-override"><code>Private Sub Timer1_Tick()
  
      If VUSoundLevels &gt; 0 Then
  
          ' Run Code &amp; Exit Loop
  
      End IF
  
  End Sub
  </code></pre>