Skip to content

Instantly share code, notes, and snippets.

View olokobayusuf's full-sized avatar
😔

Yusuf Olokoba olokobayusuf

😔
View GitHub Profile
@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
@olokobayusuf
olokobayusuf / NatCam.cs
Created November 15, 2018 16:17
NatCam 2.1 new front-end API.
/// <summary>
/// Start the camera preview
/// </summary>
/// <param name="camera">Camera that the preview should start from</param>
/// <param name="startCallback">Callback invoked when the preview starts</param>
/// <param name="frameCallback">Optional. Callback invoked when a new preview frame is available</param>
public static void StartPreview (DeviceCamera camera, Action startCallback, Action frameCallback = null);
/// <summary>
/// Stop the camera preview
@olokobayusuf
olokobayusuf / NatCam+OpenCV.cs
Created October 26, 2018 17:38
A script illustrating the recommended NatCam-OpenCV workflow.
byte[] pixelBuffer;
Mat previewMatrix;
void Start () {
// Start the camera preview
NatCam.Play(DeviceCamera.RearCamera);
// Register for preview events
NatCam.OnStart += OnStart;
NatCam.OnFrame += OnFrame;
}