Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Last active November 29, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruby0x1/ae751e7d31a7216ded13 to your computer and use it in GitHub Desktop.
Save ruby0x1/ae751e7d31a7216ded13 to your computer and use it in GitHub Desktop.
AudioMain.hx
import snow.api.Debug.*;
import snow.types.Types;
import snow.modules.opengl.GL;
import snow.systems.audio.AudioSource;
import snow.systems.audio.AudioInstance;
import Oooh;
typedef UserConfig = {}
@:log_as('app')
class Main extends snow.App {
var oooh : Oooh;
function new() {}
override function ready() {
log('ready');
var source = new OoohSource(app);
var handle = app.audio.play(source);
//we want to affect the freq of this specific instance
oooh = cast app.audio.instance_of(handle);
trace('oooh handle: $handle');
} //ready
override function onmousemove(x:Int, y:Int, _, _, _, _) {
oooh.freq = 220+(x/app.runtime.window_width()*440);
trace('oooh freq: ${oooh.freq}');
}
override function onkeyup( keycode:Int, _,_, mod:ModState, _,_ ) {
if( keycode == Key.escape ) {
app.shutdown();
}
} //onkeyup
override function tick( delta:Float ) {
GL.clearColor(1.0,1.0,1.0,1.0);
GL.clear(GL.COLOR_BUFFER_BIT);
} //
} //Main
import snow.api.buffers.Uint8Array;
import snow.api.buffers.Int16Array;
import snow.systems.audio.AudioInstance;
import snow.systems.audio.AudioSource;
import snow.types.Types;
class OoohSource extends AudioSource {
public function new(app:snow.Snow) {
super(app, {
id : 'oooh',
data : {
length: 0,
length_pcm: 0,
channels: 2,
rate: 44100,
bitrate: 88200,
bits_per_sample: 16
}
}, true);
//tenth of a second
stream_buffer_length = Std.int(44100/10);
//make sure enough buffers
stream_buffer_count = 4;
} //new
override function instance(_handle:AudioHandle) : AudioInstance {
return new Oooh(this, _handle);
}
} //OoohSource
class Oooh extends AudioInstance {
public var freq = 220.0;
var counter = 0.0;
override function data_get(_into:Uint8Array, _start:Int, _length:Int, _res:Array<Int>) : Array<Int> {
//view the bytes as short
var ints = new Int16Array(_into.buffer);
var _count = Std.int(_length/2);
//2 bytes per sample
var _samples = (_length/2);
var _samples_per_channel = Std.int(_samples/2);
for(i in 0..._samples_per_channel) {
//left
ints[i*2] = Std.int(Math.cos(counter)*32767);
//right
ints[i*2+1] = Std.int(Math.cos(counter-0.4)*32767);
counter += 1.0/source.info.data.rate*freq*2;
}
_res[0] = _length;
_res[1] = 0;
ints = null;
return _res;
}
override function has_ended() {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment