Home > Blog Posts > Dev > Are You Still There?

Are You Still There?

Tags:

dev
Published: Jul 25 2018

Estimated Read Time:

Backstory

I recently began streaming on Twitch. So far it's only been video games but as I am not the best and the games I play are saturated with the best, I often have no viewers. If I get a viewer I want to make sure they don't come in and just see a silent stream. 

I had read that a good rule of thumb for streaming is to not have more than 3 seconds of silence. When I watched my stream there were often full minutes of silence! Not very entertaining to any passersby. Three seconds is not long but when watching someone it can feel like an eternity. 

I wanted a solution to know that I was being silent. I didn't turn up anything useful in a few searches and a question to the programming subreddit. Eventually I found the NAudio library for C# and read through a few of the example code files. I also tried to follow a few tutorials but I was using a WinForm and they were using WPF, which caused some inconsistency with what I saw in my solution to theirs. 

The Plan

The solution I wanted needed to do only a couple things and it really didn't end up being more complicated than I thought once I had a way to get audio.

The pseudocode I envisioned went like this: 

  1. Is there sound (above a threshold)?
  2. Set a timer for X seconds.
  3. If there is sound, reset timer, else display message. 

As you can see I didn't want much. I ended up finding the NAudio API to get the microphone input. The rest was simple controls and logic. In the end I added a few things I didn't know I might need or want. The screenshot below shows the final product. A Windows Form that has a few settings and a few labels for output. 

Audio form I created

As you can see, it is not "pretty" but it functions. I didn't figure out what the actual values of the input were in decibels. I am sure it can be done but for the task I worked with the values as they are. I created some controls to pick an input, to adjust the silence timer, to adjust the sample rate (mostly for debugging and setting up settings), and to adjust the minimum noise to capture (gating). The buttons stop/start the timers, and the labels display the input information (or lack of). I had tried to implement a progress bar instead of the label to show captured values against but it was more trouble than it was worth.

I used the wav (16 bit) format code/examples. There was a wasapi (32 bit) example but I didn't need to use it. The result was the same either way: pass the captured value to my logic. 

Drawbacks

There are a handful of things this doesn't or can't do. It doesn't have a real time, decibel reference (a progress bar for decibel levels). C# and the NAudio API have a limited capacity to sample input. Audio capture/processing is difficult for me to understand (with the resources I was using and my lack of domain experience). Had I wanted to build something more complicated and with more bells and whistles I don't think I would have been able to, in any reasonable time frame. The good thing was that the code from their tutorial didn't require a deep understanding, the decibel level is an unneeded luxury, and the sampling rate is more than sufficient for my use.

The code is on GitHub even though it's so simple it might not seem worth it. I mean it's seriously less than 200 lines and that is with a lot of junk (comments, spacing, etc). It isn't perfect but it's another tool to help me and it was pretty interesting learning some (I totally didn't learn anything in depth, it goes deep!) about how audio works.

Related Posts:

Previous:
Sharing Is Caring

Next:
Leetcode 643