Hi all! In this tutorial, I will be discussing NatCorder’s audio API and different workflows for working with audio when recording video. First, I will go over NatCorder’s audio API, then I will discuss how to use it for recording game audio.
The Audio API
NatCorder offers a very simple API for recording PCM audio with video. There are two things that NatCorder will need from you:
- A description of the audio. NatCorder needs to know some things ahead of time. How many audio channels are there? How many audio samples will be provided in one audio frame? And how often will you be providing samples (sample rate)?
- Actual audio! As you record, you will need to provide NatCorder the actual audio to be recorded as an array of 32-bit floating point PCM samples (a.k.a, float).
To provide this information, the client (you) is expected to use an instance of theIAudioSource interface. Let’s look at the definition:
Another expectation that NatCorder has, though a weaker one which is not enforced, is that the IAudioSource instance provide the audio samples to NatCorder using the NatCorder.CommitSamples function. This expectation need not be upheld, so you can call CommitSamples from anywhere you want. Now let’s switch gears a little and look at how we can record some audio in Unity.
Recording Unity Audio
Unity provides access to PCM audio samples through the OnAudioFilterRead method:
This is incredibly convenient for use with NatCorder. What this means is that when we want to record audio from the scene (using an AudioListener ) or from a particular game object (using an AudioSource), we simply need to attach the script to the source/listener and in OnAudioFilterRead , call NatCorder.CommitSamples :
There is one last step. When we actually start recording, we need to provide NatCorder an instance of IAudioSource so that it knows to expect audio data for recording. To do this, we can make our AudioGetter component implement IAudioSource :
And finally, we pass in our AudioGetter component to StartRecording:
And that’s it! In the next tutorial, I will go over advanced recording using the lower-level NatCorder API. Till next time!
— Lanre Olokoba.
NatCorder is a cross-platform video recording API for Unity Engine. Get NatCorder here.