搜尋此網誌

2009年11月29日 星期日

好用的firewall


資訊安全是一門非常重要的課題(os:還不是微軟太弱...一堆漏洞..要不是很多軟體需要跑在微軟上..不然我真想投奔Ubuntu..) 一般電腦除了 防毒軟體+防木馬軟體 最好能再加上"防火牆" 這邊要介紹的是 完全免費且輕巧 並且設定上也相當簡單的軟體 它叫做Ghostwall 據說是一群黑客高手所做出來的軟體 其實原理上 他是將所有的port都封鎖 但軟體有先預設一些基本的port有打開 例如80port等 但是一些自行安裝的軟體 我們就必須自己去打開port,例如我打魔獸爭霸要當主機 那就要開啟6112port了 ftp 就要開啟21port等
功能頁面:

Ghostwall主頁:下載與介紹

2009年11月26日 星期四

慘..

臨時要用語音分析的東西 最近看了很多語音訊號的東西 因為要接觸這一塊 是我以前到現在都沒接觸過的 有太多的專有名詞我看不懂 短時間內要會 實在有點困難 看了一些paper 大致了解 要找出特徵 大致都是要經過FFT產生頻譜圖 去看出特徵 目前使用matlab能夠跑出波型圖 但是頻譜圖還不會弄 看到一些資料有什麼sin cos phase 這是什麼 真的看不懂... 問了老包那間的學弟 但他們都還沒弄到這 所以也沒辦法幫忙我什麼 目前的我真是吃力orz...很無助的感覺
畢業時間已經快到 結果我卻卡住了..也難為俊亨了 總覺得我一直再問很基本的問題 一點基礎都沒

2009年11月9日 星期一

Java 計算音源檔音量

我想這應該是很多寫這方面程式的人 都想知道的 由於java 的getlevel有bug 因此沒辦法直接獲取音源的振幅,所以我們必須先瞭解 音源檔的結構 是怎麼組成 必須先繞個彎去計算出振幅 我也是找了好久才找到的 稍微修改一下 就能正常運作 但注意這程式碼是計算出振幅 不是分貝 所以只要找到怎麼將振幅轉換成分貝 加入一行公式 就可以計算出來(目前先停擺..我在寫paper 等有空我再補充進去)
以下是程式碼

public class PCMFilePlayerLeveler implements Runnable {
File file;
AudioInputStream in;
SourceDataLine line;
int frameSize;
byte[] buffer;
Thread playThread;
boolean playing;
boolean notYetEOF;
AudioFormat format;
float level;

final static float MAX_8_BITS_SIGNED = Byte.MAX_VALUE;
final static float MAX_8_BITS_UNSIGNED = 0xff;
final static float MAX_16_BITS_SIGNED = Short.MAX_VALUE;
final static float MAX_16_BITS_UNSIGNED = 0xffff;

public PCMFilePlayerLeveler ()
throws IOException,
UnsupportedAudioFileException,
LineUnavailableException {
File fi= new File("c:\\JDKAudioRecord.wav");
in = AudioSystem.getAudioInputStream (fi);
//in = AudioSystem.getAudioInputStream (new BufferedInputStream (new FileInputStream(f)));
format = in.getFormat();
AudioFormat.Encoding formatEncoding = format.getEncoding();
if (! (formatEncoding.equals (AudioFormat.Encoding.PCM_SIGNED) ||
formatEncoding.equals (AudioFormat.Encoding.PCM_UNSIGNED)))
throw new UnsupportedAudioFileException (
file.getName() + " is not PCM audio");
System.out.println ("got PCM format: " +
format.getChannels() + " channels, " +
format.getSampleSizeInBits() + " bit samples");
frameSize = format.getFrameSize();
System.out.println ("got frame size: ");
DataLine.Info info =
new DataLine.Info (SourceDataLine.class, format);
System.out.println ("got info");
line = (SourceDataLine) AudioSystem.getLine (info);

// figure out a small buffer size
int bytesPerSec = format.getSampleSizeInBits() *
(int) format.getSampleRate();
System.out.println ("bytesPerSec = " + bytesPerSec);
int bufferSize = bytesPerSec / 20;
buffer = new byte[bufferSize];

System.out.println ("got line");
//line.open();
System.out.println ("opened line");
playThread = new Thread (this);
playing = false;
notYetEOF = true;
playThread.start();
}

public void run() {
int readPoint = 0;
int bytesRead = 0;
try {
// while (notYetEOF) {
System.out.println("test");
//if (playing) {
// only write if the line will take at
// least a buffer-ful of data
if (line.available() < buffer.length) {
Thread.yield();
// continue;
}
bytesRead = in.read (buffer,
readPoint,
buffer.length - readPoint);
if (bytesRead == -1) {
notYetEOF = false;
// break;
}
// how many frames did we get,
// and how many are left over?
System.out.println("test");
int frames = bytesRead / frameSize;
int leftover = bytesRead % frameSize;
// calculate level
calculateLevel (buffer, readPoint, leftover);
// if (level > 1)
// System.out.println ("WTF? level = " + level);
// System.out.println ("level: " + level);
// send to line
line.write (buffer, readPoint, bytesRead-leftover);
// save the leftover bytes
System.arraycopy (buffer, bytesRead,
buffer, 0,
leftover);
readPoint = leftover;

/* } else {
// if not playing
// Thread.yield();
try { Thread.sleep (10);}
catch (InterruptedException ie) {}
}*/
// } // while notYetEOF
System.out.println ("reached eof");
line.drain();
line.stop();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
// line.close();
}
} // run

/** resets level by finding max value in buffer, taking
into account whether these are 8 or 16 bit values
(doesn't care about mono vs stereo - if one channel
is disproportionately louder than the other, it wins)
*/
private void calculateLevel (byte[] buffer,
int readPoint,
int leftOver) {
int max = 0;
boolean use16Bit = (format.getSampleSizeInBits() == 16);
boolean signed = (format.getEncoding() ==
AudioFormat.Encoding.PCM_SIGNED);
boolean bigEndian = (format.isBigEndian());
if (use16Bit) {
for (int i=readPoint; i int value = 0;
// deal with endianness
int hiByte = (bigEndian ? buffer[i] : buffer[i+1]);
int loByte = (bigEndian ? buffer[i+1] : buffer [i]);
if (signed) {
short shortVal = (short) hiByte;
shortVal = (short) ((shortVal << 8) | (byte) loByte);
value = shortVal;
} else {
value = (hiByte << 8) | loByte;
}
max = Math.max (max, value);
} // for
} else {
// 8 bit - no endianness issues, just sign
for (int i=readPoint; i int value = 0;
if (signed) {
value = buffer [i];
} else {
short shortVal = 0;
shortVal = (short) (shortVal | buffer [i]);
value = shortVal;
}
max = Math.max (max, value);
} // for
} // 8 bit
// express max as float of 0.0 to 1.0 of max value
// of 8 or 16 bits (signed or unsigned)
if (signed) {
if (use16Bit) { level = (float) max / MAX_16_BITS_SIGNED; }
else { level = (float) max / MAX_8_BITS_SIGNED; }
} else {
if (use16Bit) { level = (float) max / MAX_16_BITS_UNSIGNED; }
else { level = (float) max / MAX_8_BITS_UNSIGNED; }
}
System.out.println(level);
} // calculateLevel





public void start() {
playing = true;
if (! playThread.isAlive())
playThread.start();
line.start();
}

public void stop() {
playing = false;
line.stop();
}

public SourceDataLine getLine() {
return line;
}

public File getFile() {
return file;
}

public float getLevel() {
return level;
}
public static void main(String args[]) throws IOException, UnsupportedAudioFileException, LineUnavailableException
{
PCMFilePlayerLeveler calculate= new PCMFilePlayerLeveler();
calculate.start();
//System.out.println("無法錄音,錄音失敗");
//System.out.println(calculate.getLevel());
}
}


Java 用麥克風錄音

===========================只講解主要程式=============================
public void main() throws InterruptedException{
//設定錄完音之後的檔名以及路徑
String Filename = "C://JDKAudioRecord.wav";
File outputFile = new File(Filename);

AudioFormat audioFormat = null;
//設定音源檔的格式
//詳細api請看這 Java sound api
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
44100.0F, 16, 2, 4, 44100.0F, false);
//DataLine.Info subclass 去取得並且開啟 target data line
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
audioFormat);
TargetDataLine targetDataLine = null;
try{
//從麥克風取得音源
targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
}catch (Exception e){
System.out.println("無法錄音,錄音失敗");
e.printStackTrace();
System.exit(-1);
}