Click here to Skip to main content
15,884,917 members
Articles / Programming Languages / C#

Low Latency Audio using ASIO Drivers in .NET

Rate me:
Please Sign up or sign in to vote.
4.87/5 (30 votes)
7 May 2008CPOL10 min read 560.7K   10.3K   104  
Demonstrates access to your sound hardware with ASIO drivers
//
// BlueWave.Interop.Asio by Rob Philpott. Please send all bugs/enhancements to
// rob@bigdevelopments.co.uk.  This file and the code contained within is freeware and may be
// distributed and edited without restriction. You may be bound by licencing restrictions
// imposed by Steinberg - check with them prior to distributing anything.
// 

//#include "AsioRedirect.h"
#include "Channel.h"

namespace BlueWave
{
	namespace Interop
	{
		namespace Asio
		{
			Channel::Channel(IAsio* pAsio, bool IsInput, int channelNumber, void* pTheirBuffer0, void* pTheirBuffer1, int bufferSize)
			{
				// remember the two buffers (one plays the other updates)
				_pTheirBuffer0 = (DWORD*)pTheirBuffer0;
				_pTheirBuffer1 = (DWORD*)pTheirBuffer1;

				// and the size
				_bufferSize = bufferSize;

				// we need one of these to query the driver
				ASIOChannelInfo* pChannelInfo = new ASIOChannelInfo();

				// populated with this
				pChannelInfo->channel = channelNumber;
				pChannelInfo->isInput = IsInput;

				// now we can get the data
				pAsio->getChannelInfo(pChannelInfo);

				// get channelinfo
				_isInput = pChannelInfo->isInput != 0;
				_name = gcnew String(pChannelInfo->name);
				_sampleType = pChannelInfo->type;
			}

			String^ Channel::Name::get()
			{
				return _name;
			}

			int Channel::BufferSize::get()
			{
				return _bufferSize;
			}

			double Channel::SampleType::get()
			{
				return _sampleType;
			}

			void Channel::SetDoubleBufferIndex(long doubleBufferIndex)
			{
				if (doubleBufferIndex == 0)
				{
					_pTheirCurrentBuffer = _pTheirBuffer0;
				}
				else
				{
					_pTheirCurrentBuffer = _pTheirBuffer1;
				}
			}

			void Channel::default::set(int sample, float value)
			{
				// clip value to avoid problems with conversion.
				if (value > __maxSampleValue)
				{
					value = __maxSampleValue;
				}
				else if (value < -1.0f)
				{
					value = -1.0f;
				}
			    
				// convert float between -1.0 and 1.0 to signed integer
				_pTheirCurrentBuffer[sample] = (DWORD)(value * 2147483648.0f);
			}

			float Channel::default::get(int sample)
			{
				// convert signed integer to float between -1.0 and 1.0
				return (float)((int)_pTheirCurrentBuffer[sample] / 2147483648.0f);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
I am a .NET architect/developer based in London working mostly on financial trading systems. My love of computers started at an early age with BASIC on a 3KB VIC20 and progressed onto a 32KB BBC Micro using BASIC and 6502 assembly language. From there I moved on to the blisteringly fast Acorn Archimedes using BASIC and ARM assembly.

I started developing with C++ since 1990, where it was introduced to me in my first year studying for a Computer Science degree at the University of Nottingham. I started professionally with Visual C++ version 1.51 in 1993.

I moved over to C# and .NET in early 2004 after a long period of denial that anything could improve upon C++.

Recently I did a bit of work in my old language of C++ and I now realise that frankly, it's a total pain in the arse.

Comments and Discussions