ride the wav
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# @install: lix --silent download "haxelib:/lime#8.2.3" into lime/8.2.3/haxelib
|
||||
# @run: haxelib run-dir lime "${HAXE_LIBCACHE}/lime/8.2.3/haxelib"
|
||||
-cp ${HAXE_LIBCACHE}/lime/8.2.3/haxelib/src
|
||||
-D lime=8.2.3
|
||||
# @install: lix --silent download "git:https://k7izh9.gitea.cloud/kiss-lang/lime#a926ee70d9171656f5ac573e4fe89e339d7ed1b4" into lime/8.3.0/git/a926ee70d9171656f5ac573e4fe89e339d7ed1b4
|
||||
# @run: haxelib run-dir lime "${HAXE_LIBCACHE}/lime/8.3.0/git/a926ee70d9171656f5ac573e4fe89e339d7ed1b4"
|
||||
-cp ${HAXE_LIBCACHE}/lime/8.3.0/git/a926ee70d9171656f5ac573e4fe89e339d7ed1b4/src
|
||||
-D lime=8.3.0
|
||||
--macro lime._internal.macros.DefineMacro.run()
|
||||
-lib ndll:${HAXE_LIBCACHE}/lime/8.2.3/haxelib/ndll/
|
||||
--macro haxe.macro.Compiler.addMetadata("@:autoBuild(lime._internal.macros.AssetsMacro.embedBytes())", "haxe.io.Bytes")
|
||||
|
||||
-lib ndll:${HAXE_LIBCACHE}/lime/8.3.0/git/a926ee70d9171656f5ac573e4fe89e339d7ed1b4/ndll/
|
||||
110
src/kiss_flixel/FlxRecorder.hx
Normal file
110
src/kiss_flixel/FlxRecorder.hx
Normal file
@@ -0,0 +1,110 @@
|
||||
package kiss_flixel;
|
||||
|
||||
// Based on LimeRecordingTest.hx by ACrazyTown
|
||||
|
||||
import openfl.media.Sound;
|
||||
import lime.utils.UInt8Array;
|
||||
import lime.media.AudioBuffer;
|
||||
import haxe.io.Bytes;
|
||||
import flixel.FlxG;
|
||||
import flixel.FlxBasic;
|
||||
import flixel.util.FlxTimer;
|
||||
import flixel.sound.FlxSound;
|
||||
import lime.media.openal.ALDevice;
|
||||
import lime.media.openal.ALC;
|
||||
import lime.media.openal.AL;
|
||||
|
||||
typedef OnFinishArg = {
|
||||
?onSound:Sound->Void,
|
||||
?onWav:String->Void
|
||||
};
|
||||
|
||||
@:kiss.Kiss.build()
|
||||
class FlxRecorder extends FlxBasic {
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
}
|
||||
|
||||
var device:ALDevice;
|
||||
public static var FREQUENCY = 48000;
|
||||
static final FORMAT = AL.FORMAT_STEREO16;
|
||||
static final BITS_PER_SAMPLE = 16;
|
||||
static final CHANNELS = 2;
|
||||
public var recording(default, null) = false;
|
||||
var onFinish:OnFinishArg = null;
|
||||
var bytes:Bytes;
|
||||
var bufferSize:Int;
|
||||
var available = 0;
|
||||
|
||||
public function recordForTime(seconds:Float, onFinish:OnFinishArg, ?deviceName:String) {
|
||||
bufferSize = Std.int(seconds * FREQUENCY);
|
||||
device = ALC.captureOpenDevice(deviceName, FREQUENCY, FORMAT, bufferSize);
|
||||
bytes = Bytes.alloc(bufferSize * getSampleSize(FORMAT));
|
||||
deviceName = ALC.getString(device, ALC.CAPTURE_DEVICE_SPECIFIER);
|
||||
|
||||
available = 0;
|
||||
recording = true;
|
||||
this.onFinish = onFinish;
|
||||
ALC.captureStart(device);
|
||||
}
|
||||
|
||||
public override function update(elapsed:Float) {
|
||||
super.update(elapsed);
|
||||
|
||||
if (recording) {
|
||||
var available = ALC.getIntegerv(device, ALC.CAPTURE_SAMPLES, 1)[0];
|
||||
if (available >= bufferSize) {
|
||||
recording = false;
|
||||
ALC.captureSamples(device, bytes, bufferSize);
|
||||
// trace(ALC.getError(device));
|
||||
// trace(ALC.getErrorString(device));
|
||||
ALC.captureStop(device);
|
||||
|
||||
if (onFinish?.onWav != null) {
|
||||
// TODO put it in a temp folder with unique name
|
||||
var file = "test.wav";
|
||||
var o = sys.io.File.write(file);
|
||||
var writer = new kiss_flixel.wav.Writer(o);
|
||||
writer.write({
|
||||
cuePoints: [],
|
||||
data: bytes,
|
||||
header: {
|
||||
samplingRate: FREQUENCY,
|
||||
format: WF_PCM,
|
||||
channels: CHANNELS,
|
||||
byteRate: Std.int(FREQUENCY * CHANNELS * BITS_PER_SAMPLE / 8),
|
||||
blockAlign: Std.int(CHANNELS * BITS_PER_SAMPLE / 8),
|
||||
bitsPerSample: BITS_PER_SAMPLE
|
||||
}
|
||||
});
|
||||
o.flush();
|
||||
onFinish.onWav(file);
|
||||
}
|
||||
|
||||
if(onFinish?.onSound != null) {
|
||||
var buffer = new AudioBuffer();
|
||||
buffer.sampleRate = FREQUENCY;
|
||||
buffer.bitsPerSample = BITS_PER_SAMPLE;
|
||||
buffer.channels = CHANNELS;
|
||||
buffer.data = UInt8Array.fromBytes(bytes);
|
||||
|
||||
var snd = Sound.fromAudioBuffer(buffer);
|
||||
onFinish.onSound(snd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static function getSampleSize(fmt:Int):Int
|
||||
{
|
||||
return switch (fmt)
|
||||
{
|
||||
case AL.FORMAT_MONO8: 1;
|
||||
case AL.FORMAT_STEREO8: 2;
|
||||
case AL.FORMAT_MONO16: 2;
|
||||
case AL.FORMAT_STEREO16: 4;
|
||||
default: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
95
src/kiss_flixel/wav/Writer.hx
Normal file
95
src/kiss_flixel/wav/Writer.hx
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* format - Haxe File Formats
|
||||
*
|
||||
* WAVE File Format
|
||||
* Copyright (C) 2009 Robin Palotai
|
||||
*
|
||||
* Copyright (c) 2009, The Haxe Project Contributors
|
||||
* All rights reserved.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
||||
* DAMAGE.
|
||||
*/
|
||||
|
||||
package kiss_flixel.wav;
|
||||
|
||||
typedef WAVE = {
|
||||
header : WAVEHeader,
|
||||
data : haxe.io.Bytes,
|
||||
cuePoints : Array<CuePoint>
|
||||
}
|
||||
|
||||
typedef WAVEHeader = {
|
||||
format : WAVEFormat,
|
||||
channels : Int,
|
||||
samplingRate : Int,
|
||||
byteRate : Int, // samplingRate * channels * bitsPerSample / 8
|
||||
blockAlign : Int, // channels * bitsPerSample / 8
|
||||
bitsPerSample : Int
|
||||
}
|
||||
|
||||
typedef CuePoint = {
|
||||
id : Int,
|
||||
sampleOffset : Int
|
||||
}
|
||||
|
||||
enum WAVEFormat {
|
||||
WF_PCM;
|
||||
}
|
||||
|
||||
class Writer {
|
||||
|
||||
var o : haxe.io.Output;
|
||||
|
||||
public function new(output : haxe.io.Output) {
|
||||
o = output;
|
||||
o.bigEndian = false;
|
||||
}
|
||||
|
||||
public function write(wav : WAVE) {
|
||||
var hdr = wav.header;
|
||||
|
||||
o.writeString("RIFF");
|
||||
writeInt(36 + wav.data.length);
|
||||
o.writeString("WAVE");
|
||||
|
||||
o.writeString("fmt ");
|
||||
writeInt(16);
|
||||
o.writeUInt16(1);
|
||||
o.writeUInt16(hdr.channels);
|
||||
writeInt(hdr.samplingRate);
|
||||
writeInt(hdr.byteRate);
|
||||
o.writeUInt16(hdr.blockAlign);
|
||||
o.writeUInt16(hdr.bitsPerSample);
|
||||
|
||||
o.writeString("data");
|
||||
writeInt(wav.data.length);
|
||||
o.write(wav.data);
|
||||
}
|
||||
|
||||
inline function writeInt( v : Int ) {
|
||||
#if haxe3
|
||||
o.writeInt32(v);
|
||||
#else
|
||||
o.writeUInt30(v);
|
||||
#end
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user