Skip to content

Using with CSCore

Coloride edited this page May 15, 2024 · 4 revisions

Introduction

The following page will show you how to handle a simple voice playback feed for OpenVoiceSharp using CSCore; a free .NET audio library which is completely written in C#.

Warning

This part will not cover recording microphone data or effects, but simply how to buffer the PCM samples (voice data) to play back.

How to Playback samples

To play back PCM samples, CSCore takes in byte[] PCM adjustable bit depth & sample rate data. Pretty cool, I know.

The following examples are going to come out of the AudioManager.cs file of the agnostic console example.

Creating the source and playback

First of, we create the audio source for our player and then we store it in a dictionary:

private static CSCore.WaveFormat CSCoreWaveFormat = new(VoiceChatInterface.SampleRate, 16, 2); // stereo

...

private static void CreateAudioSource(SteamId steamId)
{
	if (AudioSources.ContainsKey(steamId)) return;

	// create the buffering source to supply our data
	WriteableBufferingSource audioSource = new(CSCoreWaveFormat) { FillWithZeros = true };

	// create an audio source for the playback
	CSCore.SoundOut.WasapiOut soundOut = new();
	soundOut.Initialize(audioSource);
	soundOut.Play();

	// store it for later
	AudioSources.TryAdd(steamId, new(audioSource, soundOut));
}

As shown, the WriteableBufferingSource class here will handle the work for the PCM buffering and will act as a source for our playback.

Supplying the data

case PlaybackBackend.CSCore:
	// get source and queue samples
	var (audioSource, _) = GetAudioPlayback(steamId);

	// queue samples
	audioSource.Write(data, 0, length);
	break;

In the following sample of code, I grab the audioSource (WriteableBufferingSource) and I write the incoming data along with its decoded length.

Clone this wiki locally