How to play a raw PCM data?

Is there a way to create/play sounds using raw PCM data in memory (in run time)? It seems that the documentation only describes APIs regarding static audio resources and reading the source code didn’t help much.

Thanks.

You can subclass hxd.res.Sound and implement your own data source by overriding getData()

Firstly thanks @ncannasse ! I managed to implement it. Here’s how I did it:

import hxd.fs.FileEntry;
import haxe.io.BytesOutput;
import haxe.io.Bytes;
import hxd.snd.Data;
import hxd.res.Sound;
import hxd.App;

class MyData extends Data {
	var pcm:Bytes;

	public function new() {
		channels = 1;
		samplingRate = 22050;
		samples = samplingRate * 3;
		sampleFormat = F32;

		final samplesPerCycle = Std.int(samplingRate / 196);

		var bo = new BytesOutput();

		for (s in 0...samples) {
			final phase = (s % samplesPerCycle) / samplesPerCycle;
			final amp = Math.sin(Math.PI * 2 * phase);

			bo.writeFloat(amp);
		}

		pcm = bo.getBytes();
	}

	override function decodeBuffer(out:Bytes, outPos:Int, sampleStart:Int, sampleCount:Int) {
		final bpp = getBytesPerSample();
		out.blit(outPos, pcm, sampleStart * bpp, sampleCount * bpp);
	}
}

class MyEntry extends FileEntry {
	override function get_path():String {
		return 'dummy';
	}

	public function new() {
		name = 'dummy';
	}
}

class MySound extends Sound {
	public function new() {
		super(new MyEntry());
	}

	override function getData():Data {
		return new MyData();
	}
}

class Main extends App {
	override function init() {
		final snd = new MySound();
		snd.play();
	}

	static function main() {
		new Main();
	}
}

Just a quick question: Sound requires a hxd.fs.FileEntry in its constructor so I created this “dummy” one. If I understand it correctly the path property used as a key for sounds. Is it enough to just make sure that get_path() returns a unique string for everything to work (unlike in my example)?

You should look at the way it’s used but yes that seems correct.