2012年11月29日 星期四

Web Audio API 簡單範例

這是Jimmy's papa整理出來的Web Audio API 簡單範例,可發出特定頻率的正弦聲波,
僅適用Chrome
        var context = new webkitAudioContext();
        var sampleRate = context.sampleRate;
        var node = context.createJavaScriptNode(128, 1, 1);
        var amplitude=0.8,frequency=300,currentSoundSample=0;

        node.onaudioprocess = function(e) { 
              var right = e.outputBuffer.getChannelData(0),
                  left = e.outputBuffer.getChannelData(1);

            var k = 2* Math.PI * frequency / sampleRate;
              for (var i = 0; i < right.length; ++i) {
                right[i] = left[i] = amplitude * Math.sin(k * currentSoundSample++);} 
            };

        function play() {
            frequency = parseFloat(document.getElementById("freq").value);
            currentSoundSample=0;
            node.connect(context.destination);
        }

        function pause() {
              node.disconnect();
        }

Jimmy的樂高積木修行--虎之卷95頁--手動搖搖窗

手動搖搖窗,很適合用在城堡的巨大鐵窗,開窗時只要輕輕鬆鬆搖幾下就好
寄件者 scratchlab02
寄件者 scratchlab02
寄件者 scratchlab02

2012年11月21日 星期三

[ Forth.js ] OOP

下為實驗中的Forth.js OOP指令用法,
或是到此下載(forthjstest1.zip )測試檔
class Vehicle weight  power  travelon   end_class
instance Vehicle MyBike
20 to MyBike.weight
new MyCar
2000 MyCar price 
MyCar.price 
1000 +to MyCar.price
: move bra bra bra ;
assign  move MyCar.move 
:method MyCar.stop  bra bra bra ;


以下測試案例,運用see指令,透視每個新建類別(class),物件(object),屬性(attribute)及方法(method)的內部

Example: OOP

<p class="forth" forthcode=' .( (Test1 define class ) ) class Vehicle weight power travelon  end_class see Vehicle '>nil</p>

(Test1 define class )
Scanning Vehicle
(000113) doInstanceAllAttr
(000114) weight
(000115) power
(000116) travelon
(000117) exit

<p class="forth" forthcode=' .( (Test2 class instance ) )  instance Vehicle MyBike see MyBike see MyBike.weight 20 to MyBike.weight see MyBike.weight'>nil</p>

(Test2 class instance )
Scanning MyBike
(000118) doAttr
(000119) MyBike
(000120) exit
Scanning MyBike.weight
(000121) doValue
(000122) 0
(000123) exit
Scanning MyBike.weight
(000121) doValue
(000122) 20
(000123) exit

<p class="forth" forthcode=' .( (Test3 new object) ) new MyCar see MyCar 2000 MyCar price see MyCar.price cr MyCar.price . 1000 to MyCar.price MyCar.price . '>nil</p>

(Test3 new object)
Scanning MyCar
(000130) doAttr
(000131) MyCar
(000132) exit
Scanning MyCar.price
(000133) doValue
(000134) 2000
(000135) exit
2000 1000

<p class="forth" forthcode=' .( (Test4 object method) ) : move ." Just Moving!!!" ; see move assign move MyCar.move     see MyCar.move cr cr MyCar.move cr see MyCar'>nil</p>

(Test4 object method)
Scanning move
(000136) (.")
(000137) Just Moving!!!
(000138) exit
Scanning MyCar.move
(000136) (.")
(000137) Just Moving!!!
(000138) exit

Just Moving!!!

Scanning MyCar
(000130) doAttr
(000131) MyCar
(000132) exit

<p class="forth" forthcode=' .( (Test5 object method) ) :method MyCar.stop ." Just Stopping!!!" ; see MyCar.stop  cr cr  MyCar.stop  cr cr  see MyCar'>nil</p>

(Test5 object method)
Scanning MyCar.stop
(000139) (.")
(000140) Just Stopping!!!
(000141) exit

Just Stopping!!!


Scanning MyCar
(000130) doAttr
(000131) MyCar
(000132) exit