Skip to content

Instantly share code, notes, and snippets.

View olokobayusuf's full-sized avatar

Yusuf Olokoba olokobayusuf

View GitHub Profile
@olokobayusuf
olokobayusuf / StreamCamera.cs
Created February 15, 2020 22:34
Streaming the camera preview from a camera device in NatDevice.
async void StartPreview () {
// Start the camera preview
ICameraDevice device = ...;
Texture2D previewTexture = await device.StartRunning();
// Display the preview texture on a UI panel
rawImage.texture = previewTexture;
}
@olokobayusuf
olokobayusuf / ICameraDevice.cs
Created February 15, 2020 22:30
The fundamental camera device interface in NatDevice, which all camera devices implement.
/// <summary>
/// Camera device which provides pixel buffers.
/// </summary>
public interface ICameraDevice : IMediaDevice {
/// <summary>
/// Is the camera front facing?
/// </summary>
bool frontFacing { get; }
@olokobayusuf
olokobayusuf / RecordAudio.cs
Last active February 15, 2020 22:36
Streaming audio sample buffers from an audio device in NatDevice.
void StartRecording () {
// Start recording audio from the microphone
IAudioDevice device = ...;
device.StartRunning(OnSampleBuffer);
}
void OnSampleBuffer (float[] sampleBuffer, long timestamp) {
// `sampleBuffer` is linear PCM, interleaved by channel
}
@olokobayusuf
olokobayusuf / IMediaDevice.cs
Created February 15, 2020 22:24
The fundamental interface in NatDevice, which all media devices implement.
/// <summary>
/// Media device which provides media buffers.
/// </summary>
public interface IMediaDevice : IEquatable<IMediaDevice> {
/// <summary>
/// Device unique ID.
/// </summary>
string uniqueID { get; }
@olokobayusuf
olokobayusuf / IAudioDevice.cs
Last active February 15, 2020 22:28
The fundamental audio device interface in NatDevice, which all audio devices implement.
/// <summary>
/// Audio device which provides sample buffers.
/// </summary>
public interface IAudioDevice : IMediaDevice {
/// <summary>
/// Audio sample rate.
/// </summary>
int sampleRate { get; }
@olokobayusuf
olokobayusuf / TranscodeDemo.cs
Created June 20, 2019 22:06
An example illustrating transcoding a video in Unity using NatExtractor and NatCorder.
using UnityEngine;
using NatCorder;
using NatExtractor;
public class TranscodeDemo : MonoBehaviour {
public string videoPath; // Path to video to be transcoded
void Start () {
// Create a frame extractor
@olokobayusuf
olokobayusuf / AudioStream.cs
Last active December 16, 2019 01:35
An implementation of a NatMic audio device that produces audio data from a backing AudioSource or AudioListener.
/*
* NatMic
* Copyright (c) 2019 Yusuf Olokoba
*/
namespace NatMic.Recorders {
using UnityEngine;
using System;
using System.Runtime.CompilerServices;
// Get a hardware microphone
var microphoneDevice = AudioDevice.GetDevices()[0];
// Create a virtual device from an AudioSource component in game
var gameDevice = new VirtualDevice(audioSource);
// Create a mixer device that mixes audio from both devices
var audioDevice = new MixerDevice(microphoneDevice, gameDevice);
// Use the audio device like any other NatMic device
...
/// <summary>
/// An audio data receiver supplied by an audio device
/// </summary>
public interface IAudioProcessor {
/// <summary>
/// Delegate invoked when microphone reports a new sample buffer
/// </summary>
/// <param name="sampleBuffer">Audio sample buffer interleaved by channel</param>
/// <param name="sampleRate">Audio sample rate</param>
/// <param name="channelCount">Audio channel count</param>
/// <summary>
/// An audio input device
/// </summary>
public interface IAudioDevice {
/// <summary>
/// Is the device currently recording?
/// </summary>
bool IsRecording { get; }
/// <summary>
/// Start recording