rtaudio

Haxe port of RtAudio, to play sample by sample audio, for cpp target.
https://github.com/Mahdrentys/Haxe-RtAudio

To install, run:

haxelib install rtaudio 1.0.5 

See using Haxelib in Haxelib documentation for more information.

README.md

Haxe RtAudio

Haxe port of RtAudio, to play sample by sample audio, for cpp target.

Works on linux (for Alsa, PulseAudio and Jack APIs).

Usage

import rtaudio.Audio;
import rtaudio.Api;
import rtaudio.Device;

class Main
{
    public static function main():Void
    {
        // API selection:
        Audio.api = Api.PulseAudio; // To use the PulseAudio api (the default mainstream audio api on linux)
        Audio.api = Api.Alsa; // To use the Alsa api
        Audio.api = Api.Jack; // To use the Jack api (the professional linux audio api)
        // If you don't set Audio.api, the default used api will be PulseAudio

        // Devices management:
        var numberOfDevices:Int = Device.count();
        var devices:Array<Device> = Device.all();

        for (device in devices)
        {
            var id:Int = device.id;
            var isUsable:Bool = device.isUsable;
            var name:String = device.name;
            var numberOfAvailableInputChannels:Int = device.inputChannels;
            var numberOfAvailableOutputChannels:Int = device.outputChannels;
            var numberOfAvailableDuplexChannels:Int = device.duplexChannels;
            var isDefaultInput:Bool = device.isDefaultInput;
            var isDefaultOutput:Bool = device.isDefaultOutput;
            var acceptedSampleRates:Array<Int> = device.sampleRates;
            var preferredSampleRate:Int = device.preferredSampleRate;
        }

        // Get a specific device by its ID
        var device:Device = Device.get(3);

        // Get default input and output device
        var inputDevice:Device = Device.defaultInput();
        var outputDevice:Device = Device.defaultOutput();

        // Set buffer size and sample rate (in Hertz)
        Audio.bufferSize = 64; // Default value: 512
        Audio.sampleRate = 44100; // Default value: 48000
        Audio.inputDevice = Device.get(0); // Default value: Device.defaultInput()
        Audio.outputDevice = Device.get(0); // Default value: Device.defaultOutput()
        Audio.inputChannels = 2; // Default value: 0
        Audio.outputChannels = 2; // Default value: 2

        // Play audio
        var time:Float = 0;
        
        Audio.start(function(inputBuffer:Array<Array<Float>>):Array<Array<Float>>
        {
            // Takes a input buffer as argument and returns an output buffer (with values between -1 and 1)
            // A buffer is a bidimensional array like buffer[sampleIndex][channelIndex]
            // For example, here it playbacks the input and it mixes it with a 440Hz sine wave:

            var outputBuffer:Array<Array<Float>> = [];

            for (sampleIndex in 0...Audio.bufferSize)
            {
                var sine = Math.sin(2 * Math.PI * time * 440);
                outputBuffer[sampleIndex] = [];

                for (channelIndex in 0...Audio.inputChannels)
                {
                    outputBuffer[sampleIndex][channelIndex] = inputBuffer[sampleIndex][channelIndex] * 0.8 + sine * 0.2;
                }

                time += 1 / 44100;
            }

            return outputBuffer;
        });

        // Stop audio playing
        Audio.stop();

        // You can check if an audio callback is currently running
        var running:Bool = Audio.started;

        // Optional: Exception handling:
        Audio.errorHandler = function(e:Dynamic):Void
        {
            trace(e);
        };
    }
}
Contributors
Mahdrentys
Version
1.0.5
Published
7 years ago
Dependencies
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub