Click here to Skip to main content
13,707,147 members
Click here to Skip to main content
Add your own
alternative version

Stats

7.1K views
115 downloads
7 bookmarked
Posted 29 Sep 2017
Licenced CPOL

Cool dummy volume meter in "DOS mode", a fun starter project for anyone who wants to know a little more on VC++ IDE and Win32 Core APIs

, 29 Sep 2017
Rate this:
Please Sign up or sign in to vote.
The simple native DLL (made in VC++) uses Win32 Core API to capture the live audio signals from the default audio device (speaker or headphone). A little C# console EXE grabs the signal from the DLL and set the volume bars up/down in the real time. It's fun to watch and easy to understand.

Introduction

(The souce code link is at the bottom)

You will have lots of fun on this beginner+ article&code especially if you haven't done any hardware related programming before. 

If you curious to try this code but feel scared on VC++ IDE, just read my last article: A super easy DLL made in VC++ IDE, especially for the first time tasters. It should help.

In this article/code, we are making a simple native DLL in VC++ IDE (VS2013). It direclty uses Win32 Core APIs, without any third-party middle man in between. This DLL will "listen" to the PC's speaker/headphone and cache the signal data in a little buffer. Then our old fashioned C# console app will fetch this bufferred signal and present a dummy volume meter to show the whole world how cool we are!

I strive to keep the code as simple as possible. The design is very strait forward. The main purpose of this article/code is to make you really mess around with Win32 hardware core APIs and have some fun!

 

Background

There is a secret only shared within long-time Windows programmers: Win32 APIs last FOR EVER.

Lots of those APIs are from Windows XP or even earlier, and still been used the same way as decades ago. Imaging that!

Most common Win32 APIs are already deeply wrapped by Microsoft.

.NET programmers can easily use them by  those convenient "using"/"import" keywords. Or at most you have to make "reference" of it before anything else. 

But some "core" APIs are not wrapped. At least not by Microsoft. Maybe it's because there are too many memory/pointer operations involved, usually related to hardware/performance/resource sensitive stuff. Just my guess.

To use those unwrapped low level functions, you either have to use VC++ (and no, you can't avoid pointers and pointers' pointers, LOL) and follow the SDK/MSDN documentation or rely on 3rd party does the wrapping for you. 3rd party can be either an open source community or just another software vendor.

Anyway, today we are going to directly touch two "core" Win32 APIs (MMDevice & WASAPI). They are there since Windows Vista, 10 years ago, and stablized ever since.

NOTE: I marked APIs we are going to use. You can find the original chart here: https://msdn.microsoft.com/en-gb/library/windows/desktop/dd316780.aspx

NOTE AGAIN: Don't be fooled by this DIZZY chart! I first got scared the same way as you are, but once it's translated into our sample code, it's just as easy as a finger snap! (ok, my bad, not THAT easy, but not that hard, really).

Using the code

The design is simple. Two threads: one UI thread, one worker thread (backend):

 

 

One solution, two projects:

VolumeMeterConsole is C# based, which is doing the UI part. It's also the startup project.

VolumeMeterDLL is VC++ based, it's the real deal in this article/code. All Win32 API calls happen here.

 

This app has been tested on Windows 7 & 8. To run it, you can either direclty hit the green"start" in the VS IDE solution window. Or, if you ever need to deploy the result to a brand new PC (your boy/girl friend's PC maybe?), only two final files are needed. put them into the same folder. Double click the VolumeMeterConsole.exe and open any Youtube video, ta-da---!

 

Points of Interest

(1) It is cool, but why it's dummy?

Because it is dummy. To make things easier to understand, I didn't use the real formula, 20 X LOG10(Sample Voltage/Max Voltage) for proper DB level calculation.

Instead, I only calculate the ratio (sample's bytes value/max bytes value), and even further, to save some boilerplate code effort on bytes concatenation (those auido signal data could have 16bit, 24bit, 32bit difference), I just pick the most significant byte to do the calculation.

I admit It's rough, but it's quick and statistically speaking, the result isn't too bad. 

Just comparing:

- all digits calc: 123456789/1000000000 approx.= 0.12

- the most significant digit calc: 100000000/1000000000 = 0.1

(Now you know how lazy I am...)

The method used is also documented in code comments. By the way, do read comments in the code. It does have some goodies...

(2) In C# console app, why not to use await/async for multi-thread programming?

Because I don't want to introduce extra distractions especially that spaghetti Task<type> thingy. The main focus for this beginner+ article/code is on Win32 API calling from VC++ IDE. That's all.

await/async/Task<type> do have their places, especially if it comes to modern asynchronous .NET programming with some cool progress bar and "cancel" functions involved.

(3) Why it's dummy again?

The DLL part is not C++.

I didn't put class/constructor/destructor in. It's just water-fall-style plain C code. A little COM calling, even that if it's not necessary, I wouldn't even bother. If one beginner article fits too many things, you know what will happen: people will just give up.

That said, this code should not be used as a template for your next cool, iron clad, industry strength project. The code is fragile because it has nearly no resource-freeing and fail-over features (those try...catch...finally stuff, you know).  

The code sacrifices those features to gain some kind of "mean and lean". That's why it's dummy again.

(4) How to debug it?

The console EXE (C#) part debugging is easy. I won't explain it here. Let's talk about DLL debugging.

The solution/projects are configured so the compiled DLL and EXE are always output in one "bin" folder:

and in DLL project property page, for the "Debugging", configure it like this:

 

When you actually start to debug the DLL, don't click the green "start" button on top. Instead, right click the DLL project, hit "Debug" and hit "Start new instance". and now you can do "break point", step-in, step-out, F11 to your DLL code.

 

(5) What is that SDF file in source code? and it's 30 MB by itslef!

If you browse your solution folder, you can see all our source code files are very small. Yeah, they are just pure text files, several hundreds bytes here and there, no big deal. But you can see VolumeMeter.sdf, 30+MB!

This SDF file is a compact database, created by VC++ IDE. It's for building intellisense. Because VC++ is old fashined, it needs such a feature to pull info from .H/#include to give you those fancy colored code and dropdown list while you are coding.

If you don't like this 30MB, you can cancel it. In project options: Text Editor-->C/C++-->Advanced. [Disable Database] to check "True". Well, you save 30MB in this case, but lose intellisense in coding. Your call.

 

(6) What's the difference between "core" and "kernal"?

Well, we do touch the Win32 "Core" APIs, but this core is not kernal.

"kernal" in windows OS, or any general OS, has specific meaning usually it refers to the part of the OS which is directly operating the hardware (memory/CPU/HD/devices). Lots device drivers are programmed in "kernal mode"

More specifically speaking, our code is using "user mode" win32 core APIs.

some fancy reading here if you really interested in this "kernal" topic (I'm not there yet):   https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/

(7) Did you figure this out all by yourself?

Of course NOT!

I did quite a few searching and researching, and find one MSDN article is really helpful for my case:

https://blogs.msdn.microsoft.com/matthew_van_eerde/2008/12/16/sample-wasapi-loopback-capture-record-what-you-hear/

His (Mathhew Van Eerde) code is available at Github https://github.com/mvaneerde/blog/tree/master/loopback-capture

I learned a lot from it. Appreciate it. Remember I mentioned "iron clad", "industrial strength" code? His code could be used as a start point for that.

Lots of audio DSP articles reading as well.  If you get into this, it's not boring at all. If not, well then don't bother, no big deal. 

A good start point is here: https://en.wikipedia.org/wiki/Audio_bit_depth

(8) Let's see how fun it is (I put the result in action on youtube):

 

 

=============== 

You can download source code solution here:

Download VolumeMeter.zip

=============== 

Hope you enjoy this just as I did~~~~

History

Not yet.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Geek2Simon
Software Developer
Canada Canada
Just A Geek
Blog: geek2.info


You may also be interested in...

Comments and Discussions

 
QuestionThat is way cool. Pin
Ron Anders1-Oct-17 3:04
memberRon Anders1-Oct-17 3:04 
AnswerRe: That is way cool. Pin
Geek2Simon1-Oct-17 6:09
memberGeek2Simon1-Oct-17 6:09 
SuggestionGraphics Pin
Richard Andrew x6430-Sep-17 5:00
professionalRichard Andrew x6430-Sep-17 5:00 
GeneralRe: Graphics Pin
Geek2Simon30-Sep-17 12:39
memberGeek2Simon30-Sep-17 12:39 
GeneralMy vote of 5 Pin
jfriedman30-Sep-17 3:39
professionaljfriedman30-Sep-17 3:39 
GeneralRe: My vote of 5 Pin
Geek2Simon30-Sep-17 12:33
memberGeek2Simon30-Sep-17 12:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180920.1 | Last Updated 29 Sep 2017
Article Copyright 2017 by Geek2Simon
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid