Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cambiata committed Dec 7, 2014
1 parent 862d1b5 commit bab70ec
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 38 deletions.
4 changes: 2 additions & 2 deletions OpenFLMixWav.hxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<movie input="" />
<movie path="OpenFLMixWav.xml" />
<movie fps="60" />
<movie width="800" />
<movie height="480" />
<movie width="1000" />
<movie height="600" />
<movie version="1" />
<movie minorVersion="0" />
<movie platform="Lime" />
Expand Down
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 10 additions & 11 deletions src/OpenFLMixWav.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import audiotools.openfl.OpenflWav16Tools;
import audiotools.openfl.ui.WavSprite;
import audiotools.utils.BytesLoader;
import audiotools.utils.BytesLoaders;
import audiotools.Wav16DSP;
import audiotools.Wav16Mono;
import audiotools.Wav16Stereo;
import audiotools.Wav16Tools;
Expand All @@ -12,6 +13,8 @@ import openfl.display.Sprite;
import openfl.Lib;
import openfl.media.Sound;



/**
* ...
* @author Jonas Nyström
Expand All @@ -28,7 +31,7 @@ class OpenFLMixWav extends Sprite
var aBytes = Lambda.array(loadedBytes);
var w1 = Wav16Mono.fromBytes(aBytes[0]);
var w2 = Wav16Mono.fromBytes(aBytes[1]);
var w3 = Wav16Tools.mix(w1, w2);
var w3 = Wav16DSP.mix(w1, w2);

var ws1 = new WavSprite(w1, 0, 0, 0xaa0000);
ws1.y = 20; ws1.x = 20;
Expand All @@ -42,26 +45,22 @@ class OpenFLMixWav extends Sprite
ws3.y = 260; ws3.x = 20;
this.addChild(ws3);


#if (! html5)
var sound = new Sound();
var soundBytearray = OpenflWav16Tools.intsToMono16ByteArray(w3.ints);
sound.loadPCMFromByteArray(soundBytearray, w3.ints.length, 'short', false);
var soundChannel = sound.play();
var sound = new Sound();
var soundBytearray = OpenflWav16Tools.intsToMono16ByteArray(w3.ints);
sound.loadPCMFromByteArray(soundBytearray, w3.ints.length, 'short', false);
var soundChannel = sound.play();
#end


}).loadBytes();


new BytesLoader('assets/audio/stereo/sample.wav').setOnLoaded(function(bytes:Bytes, filename:String) {
trace('loaded $filename');
var wStereo = Wav16Stereo.fromBytes(bytes);
trace(wStereo.ints.length);
trace(wStereo.rightInts.length);
var ws = new WavSprite(wStereo);
ws.x = 500; ws.y = 20;
ws.x = 500; ws.y = 20;
this.addChild(ws);

}).loadBytes();

}
Expand Down
23 changes: 23 additions & 0 deletions src/audiotools/Wav16DSP.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package audiotools;

/**
* Wav16DSP
* @author Jonas Nyström
*/
class Wav16DSP
{

static public function mix(w1:Wav16Mono, w2:Wav16Mono): Wav16Mono
{
var result:WavInts = [];
for (pos in 0...w1.ints.length)
{
var v1 = w1.ints[pos];
var v2 = w2.ints[pos];
var v3 = Math.floor((v1 + v2) / 2);
result.push(v3);
}
return new Wav16Mono(result);
}

}
5 changes: 1 addition & 4 deletions src/audiotools/Wav16Mono.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ import sys.io.File;
*/
class Wav16Mono extends Wav16
{




static public function fromBytes(wavData:Bytes) return new Wav16Mono(Wav16Tools.monoBytesToInts(wavData));
static public function fromBytes(wavData:Bytes, stripHeader:Bool=true) return new Wav16Mono(Wav16Tools.monoBytesToInts(wavData, stripHeader));

#if (sys)
static public function fromFile(filename:String) {
Expand Down
6 changes: 4 additions & 2 deletions src/audiotools/Wav16Stereo.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import haxe.io.Bytes;
class Wav16Stereo extends Wav16
{
public var rightInts(default, null):WavInts;
public var leftInts(get, null):WavInts;
function get_leftInts() return this.ints;

public function new(leftInts:WavInts, rightInts:WavInts) {
this.rightInts = rightInts;
super(leftInts);
}

static public function fromBytes(wavData:Bytes) {
static public function fromBytes(wavData:Bytes, stripHeader:Bool=true) {

var intsArray = Wav16Tools.stereoToInts(wavData);
var intsArray = Wav16Tools.stereoToInts(wavData, stripHeader);
return new Wav16Stereo(intsArray[0], intsArray[1]);
}

Expand Down
23 changes: 4 additions & 19 deletions src/audiotools/Wav16Tools.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import haxe.io.Bytes;
*/
class Wav16Tools
{

static inline function inRange(val: Int, min:Int, max:Int):Bool return (val >= min && val <= max);

static public function monoBytesToInts(wavData:Bytes, stripHeader:Bool=true): WavInts
Expand Down Expand Up @@ -75,7 +74,7 @@ class Wav16Tools

inline static public function shortToUChars(short:Int):Array<Int>
{
if (! inRange(short, -32767, 32767)) short = 32767; // throw 'ConversionTools: range error: $short';
if (! inRange(short, -32767, 32767)) throw 'ConversionTools: range error: $short';
var result = [0, 0];
if (short >= 0)
{
Expand All @@ -87,8 +86,9 @@ class Wav16Tools
return result;
}

static public function createHeader(channels:Int=1, samplingRate:Int=44100, bitsPerSample:Int=16):WAVEHeader
static public function createHeader(stereo:Bool=false, samplingRate:Int=44100, bitsPerSample:Int=16):WAVEHeader
{
var channels = (stereo) ? 2 : 1;
return {
format : format.wav.Data.WAVEFormat.WF_PCM,
channels : channels,
Expand All @@ -99,22 +99,10 @@ class Wav16Tools
}
}

static public function mix(w1:Wav16Mono, w2:Wav16Mono): Wav16Mono
{
var result:WavInts = [];
for (pos in 0...w1.ints.length)
{
var v1 = w1.ints[pos];
var v2 = w2.ints[pos];
var v3 = Math.floor((v1 + v2) / 2);
result.push(v3);
}
return new Wav16Mono(result);
}


static public function getWaveformSamples(wavInts:WavInts, nrOfSamples:Int, sampleAcc:Int=100): Array<Float>
{

var windowSize = Math.floor(wavInts.length / nrOfSamples+1);
//trace([leftInts.length, nrOfSamples, windowSize]);
var result: Array<Float> = [];
Expand All @@ -125,7 +113,6 @@ class Wav16Tools
var maxlevel = 0.0;
for (j in start...end)
{

var level = Math.abs(wavInts[j]) / 32767;
if (level < 0.0001) level = 0;
if (j > wavInts.length) level = 0;
Expand All @@ -136,6 +123,4 @@ class Wav16Tools
}
return result;
}


}

0 comments on commit bab70ec

Please sign in to comment.