A couple of years ago, it was really tricky to record the audio that was currently being played in your speakers thanks to the sound card of your computer using C#. Fortunately with the matter of time, an useful API was introduced to Windows Vista. The Windows Audio Session API (WASAPI) is Microsoft's most modern method for talking with audio devices. It is available in Windows Vista, Windows 7, and later versions of Windows. It allows delivering an unmodified bitstream to a sound device, and provides benefits similar to those provided by ASIO drivers. NAudio offers a really useful wrapper around this API that allows you to record the audio from your soundcard easily with C#.
Your options for adding a sound card to an IBM PS/2 with the MCA bus are limited, but the IBM M-Audio Capture & Playback Adapter (M-ACPA) is somewhat easier. Bought from Shopee the V8 Sound Card console which has some sound effects and can plug in 3 sources of sounds and 2 microphones. Can be used as an alternativ. I have a Sigma Tel C-Major Audio sound card. In my Sounds and Audio Devices Properties, Hardware I have the following listed. Sigma Tel C- Major Audio PCI bus 0, device 30, function 2 Device status: working properly Driver Version: 5:10.0.4255 and this device (enabled) Audio Codecs - Standard System Device - Location unknown. Legacy Audio Drivers.
In this article, we'll show you how to record the audio that's coming from your sound card using NAudio with C# in WinForms.
After testing more than 20 new and updated microSD cards, we’ve found that the SanDisk MicroSDXC card for Nintendo Switch (128 GB) is the best for most phones, tablets, and, yes, Nintendo. I'm running XP on a Sony Vaio with a Sigma Tel sound card. I tried running everything through my usual recording set-up which is mic through my effects box, plugged into an indigo audio card (I know, convoluted but I normally need the burly sound processor in the box and it all fits the cables I have) + headphones plugged into the sound card.
1. Install NAudio with NuGet
You will need to install the NAudio library in your project using the NuGet package manager. Open your Winforms C# project and open the NuGet package manager in the solution explorer:
Go to the Browse tab and search for
From the list, select the NAudio package by Mark Heath and install it simply clicking on the Install button. Once the installation finishes you will be able to import the Wave namespace of NAudio in the class where you want to use it like this:
If you already have NAudio installed, then you can proceed with the next step.
2. Understand recording audio logic
Thanks to NAudio, most of the hard logic behind the complex process of recording the audio coming out from your speakers (sound card) has been fortunately abstracted and written in a way that almost every developer can understand and use. The WasapiLoopbackCapture is the class from NAudio that you will use to record it, this class is a very well writen wrapper of the WASAPI (Windows Audio Session API) that can be used together with some file writer classes of NAudio to record easily the audio from the default sound source of your system.
The logic that you will need to handle to record the audio is minimal, initially, you need to create an instance of the WaveFileWriter and the WasapiLoopBackCapture classes.l The WaveFileWriter expects as first argument the path where the wav file should be written (the one with the recorded audio from the speakers/sound card) and as second argument the WaveFormat property of the capturer instance. Doing this, you're ready to write the sound coming from the sound card, however it isn't done automatically as you need to attach a listener to the capturer instance, namely the DataAvailable. This event is triggered when the some audio is received, in this callback you need to use the audio writer, providing as first argument of the write method, the buffer and the offest of the recorded bytes and the recorded bytes itself. You need as well dispose the audio writer once the capturer stops so the file can be accessible. Finally, you can simply start the recorder using the StartRecording method.
It's worth to say that the code is asynchronous, so you don't need to care basically about threading or other complex stuff (maybe initially):
To stop the audio recording, you would simply run the
StopRecording method of the capturer:
The buffer writing in the desired file will stop, producing a wav file with the audio produced from your system during the execution of the process. Obviously, don't forget to play some audio while you test or you will hear nothing !
In this example, we are going to simply create a form with 2 buttons, one to start the recorder (button1) and other to stop it (button2). The Stop button is initially disabled and it will enabled once the recorder starts and viceversa with the start button. Graphically the form looks like:
The logic that we'll implement is very simple, yet functional. When the user clicks on start, the audio of the system will be recorded and when the user clicks on stop, the recorder stops and finishes the writing buffer of our desired output Audio file. The instances of
WaveFileWriter and the
WasapiLoopbackCapture are meant to be accesible at class level, so our start and stop button have access to them, they're because of this behaviour originally set to null.
As next, attach a click listener to both buttons and assign the logic respectively. For the start button, define a variable that contains the path to the output audio file and redefine the
WaveFileWriter and the
WasapiLoopbackCapture instances with a new one. Then, attach the onDataAvailable and onRecordingStopped callbacks to the capturer instance and write the logic respectively, in this case when the data is available, write the output buffer into the declared file used by the wave writer. When the recording stops it should obviously dispose the capture instance. Finally when the user clicks on the stop button, the recorder instance should simply stop the recorder, disabling as well the stop button and enabling the start button again so a new file can be recorded again:
Happy coding !
From the experience I gathered so far: The first line enables the first device, which according to lspci is the hdmi/gpu card and then sets its index to 0, ergo this line changes nothing, which is the reason I said it is redundant. The second line sets the first card according to hardware order, which again is the gpu/hdmi to the second slot, ergo index is set to 1. This pushes the next available card to index 0 and since there only is one other sound card, it is set as default.
You must reboot, after changing modprobe options or reload the modules correctly, ergo with no alsa sound active. Simply using only the second line is discouraged, since with multiple sound card the other card might get the index 0. Snd-hda-intel accepts multiple values, up to 31 it seems, so what I proposed seems to be the most appropriate approach irrelevant of the setup used, since the second parameter addresses the second sound card on the bus, which is the FCH Azalia/ALC3227 in this case and sets it to index 0 making it default.
Sigma Micro Sound Cards & Media Devices Drivers
Should you consider this issue solved, then please mark the thread as such.
Edit: Added the solution for further clarification.
Edit2: I would like to know what this is if you do not mind:
Sigma Micro Sound Cards & Media Devices Driver
What is its purpose?
Last edited by emeres (2014-09-04 23:56:49)