Last active
August 29, 2015 14:17
-
-
Save jonbro/9fd4772a14d786486e9b to your computer and use it in GitHub Desktop.
generative audio in luxe/snow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import snow.platform.native.audio.openal.SoundStream; | |
| import snow.audio.Audio; | |
| import snow.types.Types; | |
| import snow.io.typedarray.Uint8Array; | |
| import snow.io.typedarray.Uint16Array; | |
| import snow.io.typedarray.Int16Array; | |
| import snow.io.typedarray.Float32Array; | |
| import snow.Log; | |
| import luxe.Rectangle; | |
| import luxe.Text; | |
| import luxe.Vector; | |
| import luxe.Color; | |
| import luxe.Input; | |
| class Main extends luxe.Game { | |
| public var mouse : Vector; | |
| var myStream : MyStream; | |
| override function ready() { | |
| mouse = new Vector(); | |
| myStream = new MyStream(Luxe.core.app.audio, "streamname"); | |
| Luxe.core.app.audio.stream_list.set("streamname", myStream); | |
| Luxe.core.app.audio.sound_list.set("streamname", myStream); | |
| Luxe.audio.on("streamname", "load", function(_){ | |
| Luxe.audio.loop("streamname"); | |
| }); | |
| } //ready | |
| override function onmousemove( e:MouseEvent ) { | |
| mouse.set_xy(e.x,e.y); | |
| myStream.freq = e.x/800*440; | |
| } //onmousemove | |
| override function onmousedown( e:MouseEvent ) { | |
| mouse.set_xy(e.x,e.y); | |
| myStream.loop(); | |
| } //onmousedown | |
| override function onmouseup( e:MouseEvent ) { | |
| mouse.set_xy(e.x,e.y); | |
| } //onmouseup | |
| override function onkeyup( e:KeyEvent ) { | |
| if(e.keycode == Key.escape) { | |
| Luxe.shutdown(); | |
| } | |
| } //onkeyup | |
| override function onrender() { | |
| } //onrender | |
| } //Main | |
| class MyStream extends snow.platform.native.audio.openal.SoundStream { | |
| var sound : Uint8Array ; | |
| var audioData : AudioDataBlob; | |
| var floatCounter:Float; | |
| public var freq:Float = 220; | |
| public function new( _manager:Audio, _name:String ) { | |
| snow.Log.level(5); | |
| is_stream = true; | |
| super( _manager , _name); //make sure it has a manager to do api calls with | |
| var adinfo:AudioDataInfo = { | |
| length: -1, | |
| length_pcm: -1, | |
| channels: 2, | |
| rate: 44100, | |
| bitrate: 16, | |
| bits_per_sample: 16, | |
| bytes: null | |
| }; | |
| var info:AudioInfo = { | |
| id: "test", | |
| format:1, | |
| handle: null, data: adinfo | |
| }; | |
| set_info(info); | |
| buffer_count = _manager.lib.config.native.audio_buffer_count; | |
| // tenth of a second | |
| buffer_length = Std.int(44100/10); | |
| sound = new Uint8Array(buffer_length); | |
| audioData = { bytes : sound , complete : false}; | |
| data_seek = function( to:Int ) { return false; } | |
| floatCounter = 0; | |
| data_get = myDataGet; | |
| } //new | |
| function myDataGet( _start:Int, _length:Int ) : AudioDataBlob{ | |
| var soundInts:Int16Array = new Int16Array(Std.int(_length)); | |
| // dividing by 2 for the two channels | |
| for(i in 0...Std.int(_length/2)){ | |
| soundInts[i*2] = Std.int(Math.cos(floatCounter)*32767); | |
| soundInts[i*2+1] = soundInts[i*2]; | |
| floatCounter += 1/44100*freq*2; | |
| } | |
| // sound = Uint8Array.fromBytes(soundfloats.toBytes()); | |
| audioData = { bytes : new Uint8Array(soundInts.buffer) , complete : false}; | |
| return audioData; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment