字體:小 中 大 | |
|
|
2014/05/01 23:47:46瀏覽288|回應0|推薦0 | |
嚕嚕嚕嚕嚕~ 今天是這學期的第九堂物裡課 . 話說今天閃耀耀的耀哥給我們看了一個東西 https://dl.dropboxusercontent.com/u/62508554/physics/courseWeb/sound/files/mysound.zip 這個是今天的範例 . EJS有一個非成神奇的特性 還是該說是屬性呢... 他可以使用一些之前已經寫好的打包的Java函式 . 要怎麼使用呢? 首先是點開右上方雖然不起眼但其時閃亮亮的按鈕 . 接著選擇Run options 就可以看到外掛的函式庫了 . 他的檔名叫做mySG.jar . 而在設定初始值裡,似乎也有他的蹤跡呢... . 有玩過Minecraft的人應該都知道.jar檔是可以用解壓縮軟體去看到裡面的檔案構造 雖然說看得到啦,但mySG裡面的class檔案不管是用記事本開還是用eclipse,不是開不起來就是亂碼 . https://dl.dropboxusercontent.com/u/62508554/physics/courseWeb/sound/files/mysound.zip 所以如果要開,還是要用原始檔,不能夠用已經封裝好的 . 範本是從耀哥的網站上抓下來的 至於要如何打開這個勇者呢?選擇File裡面的Inport . 然後選擇這個 . 剛剛下載的壓縮檔可以不用解壓縮沒關係,在這裡選擇檔案的位置 . 然後把他開起來 選擇mySG>>src>>mySG>>SG.java Package mySG 裡面有一個叫SG的 Class . 因為內容實在是太多了,無法一一截圖 就把整個內容複製過來 猜猜看剛剛在EJS裡"new mySG.SG(frequence,amplitude) " 究竟會使用到這東程式碼的哪一段呢 ==============我是佔版面用的================ public class SG { public void setBaseFrequency(double d) { baseF = d; if(baseF < 100D) baseF = 100D; else if(baseF > 2000D) baseF = 2000D; period = (int)(UNIT / baseF); record = new byte[period]; Y = new double[period]; omega = 6.2831853071795862D / (double)period; setit(); } public void setAmplitude(double d) { amplitude = d; setit(); } private void setit() { if(soundStream != null) { stop(); playstatus = true; } calc(); if(playstatus) play(); } public void setCosCoeff(double ad[]) { if(ad.length < maxK) maxi = ad.length; else maxi = maxK; for(int i = 0; i < maxi; i++) cosCoeff[i] = ad[i]; } public void setSinCoeff(double ad[]) { if(ad.length < maxK) maxi = ad.length; else maxi = maxK; for(int i = 0; i < maxi; i++) sinCoeff[i] = ad[i]; } public void setFourier(double ad[], double ad1[]) { setCosCoeff(ad1); setSinCoeff(ad); calFourier(); } public void setCosCoeff(int i, double d) { if(i < maxK) cosCoeff[i] = d; } public void setSinCoeff(int i, double d) { if(i < maxK) sinCoeff[i] = d; } public void calFourier() { if(soundStream != null) { stop(); playstatus = true; } for(int i = 0; i < period; i++) { y = cosCoeff[0]; for(int j = 1; j < maxi; j++) y += cosCoeff[j] * Math.cos((double)(i * j) * omega) + sinCoeff[j] * Math.sin((double)(i * j) * omega); Y[i] = amplitude * y; } setWave(Y); if(playstatus) play(); } public void play() { try { if(soundStream == null) soundStream = new ContinuousAudioDataStream(new AudioData(record)); AudioPlayer.player.start(soundStream); playstatus = true; } catch(SecurityException securityexception) { } } public void pause() { if(soundStream != null) { try { AudioPlayer.player.stop(soundStream); playstatus = false; } catch(SecurityException securityexception) { } } } public void stop() { if(soundStream != null) { try { AudioPlayer.player.stop(soundStream); soundStream = null; } catch(SecurityException securityexception) { } } playstatus = false; } private void calc() { A = amplitude * amplitudeCST; for(int i = 0; i < period; i++) { y = A * Math.sin((double)i * omega); record[i] = int2ulaw((int)y); Y[i] = y; } } public void setbaseLevel(double d) { amplitudeCST = d; } public void setWave(double ad[]) { setWave(ad, 1.0D); } public void setWave(double ad[], double d) { if(period != ad.length) { period = ad.length; record = new byte[period]; Y = new double[period]; } for(int i = 0; i < period; i++) { Y[i] = ad[i] * d * amplitudeCST; record[i] = int2ulaw((int)Y[i]); } } public void changeLevel(double d) { for(int i = 0; i < period; i++) { Y[i] = Y[i] * d; record[i] = int2ulaw((int)Y[i]); } } public static byte int2ulaw(int i) { char c; if(i < 0) { i = -i; c = '\177'; } else { c = '\377'; } if(i < 32) i = 0xf0 | 15 - i / 2; else if(i < 96) i = 0xe0 | 15 - (i - 32) / 4; else if(i < 224) i = 0xd0 | 15 - (i - 96) / 8; else if(i < 480) i = 0xc0 | 15 - (i - 224) / 16; else if(i < 992) i = 0xb0 | 15 - (i - 480) / 32; else if(i < 2016) i = 0xa0 | 15 - (i - 992) / 64; else if(i < 4064) i = 0x90 | 15 - (i - 2016) / 128; else if(i < 8160) i = 0x80 | 15 - (i - 4064) / 256; else i = 128; return (byte)(c & i); } public SG() { baseF = 440D; soundStream = null; playstatus = false; amplitude = 10D; amplitudeCST = 1000D; maxK = 20; cosCoeff = new double[maxK]; sinCoeff = new double[maxK]; amplitude = 1.0D; baseF = 440D; setBaseFrequency(baseF); } public SG(double d) { baseF = 440D; soundStream = null; playstatus = false; amplitude = 10D; amplitudeCST = 1000D; maxK = 20; cosCoeff = new double[maxK]; sinCoeff = new double[maxK]; amplitude = 1.0D; setBaseFrequency(d); } public SG(double d, double d1) { baseF = 440D; soundStream = null; playstatus = false; amplitude = 10D; amplitudeCST = 1000D; maxK = 20; cosCoeff = new double[maxK]; sinCoeff = new double[maxK]; amplitude = d1; setBaseFrequency(d); } private static double UNIT = 8000D; private int period; private double baseF; private double omega; private byte record[]; private double Y[]; private InputStream soundStream; private boolean playstatus; private double amplitude; private double y; private double amplitudeCST; private double A; private int maxK; private int maxi; private double cosCoeff[]; private double sinCoeff[]; } ==============佔版面用的工作以經結束了================ . 大家猜對了嗎? 其實剛剛的 new mySG.SG(frequence,amplitude) 是指說 新建一個 mySG裡面的SG函式 其實和下禮拜會教到的C++的一些Class的架構大同小異呢 沒想到耀哥居然有預知的能力,真的是太強大了呀!!! . . |
|
( 興趣嗜好|電腦3C ) |