L1

Lampiran Source Code C# FormAbsen.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

namespace AttendanceSystem { public partial class FormAbsen : Form { DataClassesMentoringDataContext db = new DataClassesMentoringDataContext(); MsStaff mi = new MsStaff(); public FormAbsen() { InitializeComponent(); }

private void trainingToolStripMenuItem_Click(object sender, EventArgs e) { FormIDTrain fti = new FormIDTrain(); fti.Show(); }

private void exitToolStripMenuItem_Click(object sender, EventArgs e)

L2

{ Application.Exit(); }

private void button1_Click(object sender, EventArgs e) { string id = textBox1.Text; if (id == "") { MessageBox.Show("ID Harus Diinput", "error"); } else { try { mi = db.MsStaffs.Single(p => p.IDStaff == id); } catch (Exception ex) { } finally { if (textBox1.Text==mi.IDStaff) { FormVerifikasi fv = new FormVerifikasi(); fv.id = id; fv.nama = mi.Nama; fv.Show(); } else

L3

{ MessageBox.Show("ID tidak terdaftar", "Warning"); }

} } }

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { button1_Click(e, e); } }

private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { FormAbout fa = new FormAbout(); fa.Show(); } } }

FormIDTrain.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;

L4

using System.Text; using System.Windows.Forms;

namespace AttendanceSystem { public partial class FormIDTrain : Form { DataClassesMentoringDataContext db = new DataClassesMentoringDataContext(); MsStaff mi = new MsStaff(); public FormIDTrain() { InitializeComponent(); }

private void FormIDTrain_Load(object sender, EventArgs e) { IQueryable ts = from p in db.MsStaffs select p; foreach (MsStaff ms in ts) { comboBox1.Items.Add(ms.IDStaff); } comboBox1.SelectedIndex = 0; }

private void buttonTrain_Click(object sender, EventArgs e) { DialogResult res = MessageBox.Show("Training dengan ID ini?", "konfirmasi", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) {

L5

FormTraining ft = new FormTraining(); ft.id = comboBox1.Text; ft.Show(); this.Hide(); } else { this.Hide(); } }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { mi = db.MsStaffs.Single(p => p.IDStaff == comboBox1.Text); labelNama.Text = mi.Nama; labelJabatan.Text = mi.Jabatan; } } }

FormTraining.cs using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.VisualBasic;

L6

using Microsoft.VisualBasic.Devices; using System.Runtime.InteropServices;

namespace AttendanceSystem { public partial class FormTraining : Form { DataClassesMentoringDataContext dc = new DataClassesMentoringDataContext(); MsRekam mr = new MsRekam(); private int jumlahTraining = 5; public string id; int num = 1; private WaveInRecorder _recorder; private byte[] _recorderBuffer; private WaveOutPlayer _player; private byte[] _playerBuffer; private FifoStream _stream; private WaveFormat _waveFormat; private AudioFrame _audioFrame; private int _audioSamplesPerSecond = 44100; private int _audioFrameSize = 16384; private byte _audioBitsPerSample = 16; private byte _audioChannels = 2; private bool _isPlayer = false; private bool _isTest = false; private DateTime now; byte[] suara; double[] waveLeft, waveRight; double[] tes; double[] wave;

L7

int u; ContinuousHiddenMarkovModel chmm = new ContinuousHiddenMarkovModel(20, 6);

[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); public FormTraining() { InitializeComponent(); }

private void label1_Click(object sender, EventArgs e) {

}

private void FormTraining_Load(object sender, EventArgs e) { label1.Text = "Training data ke " + num.ToString(); MessageBox.Show("Training data ke " + num.ToString() + ".\nTekan tombol \"Start Recording\"\nUcapkan kata \"Masuk\" dengan suara keras.\"lau tekan tombol \"Stop Recording\"."); }

private void buttonRec_Click(object sender, EventArgs e) { if (buttonRec.Text == "Start Recording") { if (WaveNative.waveInGetNumDevs() == 0) { MessageBox.Show("There are no audio devices available", "Warning"); }

L8

else { timer1.Start(); now = DateTime.Now; buttonRec.Text = "Stop Recording"; if (_isPlayer == true) _stream = new FifoStream(); _audioFrame = new AudioFrame(_isTest); mciSendString("open new Type waveaudio Alias recsound", "", 0, 0); mciSendString("record recsound", "", 0, 0); Start(); } } else { Stop(); buttonRec.Text = "Start Recording"; timer1.Stop(); mciSendString("save recsound d:\\record.wav", "", 0, 0); mciSendString("close recsound ", "", 0, 0); DialogResult res = MessageBox.Show("Training ke " + num.ToString() + " dengan data ini?", "konfirmasi", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) {

suara = File.ReadAllBytes(@"D:\\record.wav"); waveLeft = new double[suara.Length / 4]; waveRight = new double[suara.Length / 4]; u = 0;

for (int i = 0; i < suara.Length - 4; i += 4)

L9

{ waveLeft[u] = (double)BitConverter.ToInt16(suara, i); waveRight[u] = (double)BitConverter.ToInt16(suara, i + 2); u++; } tes = Pre.strong(waveLeft); tes = Pre.silence(tes); tes = Pre.normalisasi(tes); wave = MFCC.Proses(tes); double[] wt = new double[wave.Length]; for (int i = 0; i < wave.Length; i++) { wt[i] = ((wave[i] - wave.Min()) / (wave.Max() - wave.Min())) * 1; } chmm.Learn(wt, 0.00001); foreach (double h in wt) { mr = new MsRekam(); mr.IDStaff = id; mr.DataRek = h; dc.MsRekams.InsertOnSubmit(mr); } dc.SubmitChanges();

MessageBox.Show("Data ke " + num.ToString() + " telah disimpan");

if (num == jumlahTraining) { double b = chmm.Evaluate(wt); mr = new MsRekam();

L10

mr.IDStaff = id; mr.DataRek = b; dc.MsRekams.InsertOnSubmit(mr); dc.SubmitChanges(); this.Close();

MessageBox.Show("Training berhasil. Data telah disimpan dengan Id " + id + "\nTerima kasih"); }

num++; label1.Text = "Training data ke " + num.ToString(); pictureBox1.BackColor = Color.White; Bitmap bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height); Graphics gr = Graphics.FromImage(bmp); gr.DrawRectangle(new Pen(Color.White), pictureBox1.DisplayRectangle); pictureBox1.Image = bmp;

} else { pictureBox1.BackColor = Color.White; Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics gr = Graphics.FromImage(bmp); gr.DrawRectangle(new Pen(Color.White), pictureBox1.DisplayRectangle); pictureBox1.Image = bmp; } } } private void Start() {

L11

Stop(); try { _waveFormat = new WaveFormat(_audioFrameSize, _audioBitsPerSample, _audioChannels); _recorder = new WaveInRecorder(0, _waveFormat, _audioFrameSize * 2, 3, new BufferDoneEventHandler(DataArrived)); if (_isPlayer == true) _player = new WaveOutPlayer(-1, _waveFormat, _audioFrameSize * 2, 3, new BufferFillEventHandler(Filler)); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Stop() { if (_recorder != null) try { _recorder.Dispose(); } finally { _recorder = null; } if (_isPlayer == true) { if (_player != null) try {

L12

_player.Dispose(); } finally { _player = null; } _stream.Flush(); } } private void Filler(IntPtr data, int size) { if (_isPlayer == true) { if (_playerBuffer == null || _playerBuffer.Length < size) _playerBuffer = new byte[size]; if (_stream.Length >= size) _stream.Read(_playerBuffer, 0, size); else for (int i = 0; i < _playerBuffer.Length; i++) _playerBuffer[i] = 0; System.Runtime.InteropServices.Marshal.Copy(_playerBuffer, 0, data, size); } } private void DataArrived(IntPtr data, int size) { if (_recorderBuffer == null || _recorderBuffer.Length < size) _recorderBuffer = new byte[size]; if (_recorderBuffer != null) { System.Runtime.InteropServices.Marshal.Copy(data, _recorderBuffer, 0, size);

L13

if (_isPlayer == true) _stream.Write(_recorderBuffer, 0, _recorderBuffer.Length); _audioFrame.Process(ref _recorderBuffer); _audioFrame.RenderTimeDomain(ref pictureBox1); } }

private void timer1_Tick(object sender, EventArgs e) { DateTime inr = DateTime.Now; TimeSpan tp = inr - now; label2.Text = String.Format("{0:D2} : {1:D2}.{2}", tp.Minutes, tp.Seconds, tp.Milliseconds); } } }

FormVerifikasi.cs using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Media; using Microsoft.VisualBasic; using Microsoft.VisualBasic.Devices; using System.Runtime.InteropServices;

L14

namespace AttendanceSystem { public partial class FormVerifikasi : Form { public string nama; public string id; private WaveInRecorder _recorder; private byte[] _recorderBuffer; private WaveOutPlayer _player; private byte[] _playerBuffer; private FifoStream _stream; private WaveFormat _waveFormat; private AudioFrame _audioFrame; private int _audioSamplesPerSecond = 44100; private int _audioFrameSize = 16384; private byte _audioBitsPerSample = 16; private byte _audioChannels = 2; private bool _isPlayer = false; private bool _isTest = false; private DateTime now; private TimeSpan lama; byte[] suara; double[] waveLeft, waveRight; double[] tes; double[] wave; int u; [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); public FormVerifikasi()

L15

{

InitializeComponent(); }

private void FormKonfirmasi_Load(object sender, EventArgs e) { MessageBox.Show("Verifikasi Pembicara.\nTekan tombol \"Start Recording\"\nUcapkan kata \"Masuk\" dengan suara keras.\"lau tekan tombol \"Stop Recording\".","Speaker Verification"); }

private void button1_Click(object sender, EventArgs e) { if (buttonRec.Text == "Start Recording") { if (WaveNative.waveInGetNumDevs() == 0) { MessageBox.Show("There are no audio devices available", "Warning"); } else {

timer1.Start(); now = DateTime.Now; buttonRec.Text = "Stop Recording"; if (_isPlayer == true) _stream = new FifoStream(); _audioFrame = new AudioFrame(_isTest); mciSendString("open new Type waveaudio Alias recsound", "", 0, 0); mciSendString("record recsound", "", 0, 0); Start();

L16

} } else { Stop(); buttonRec.Text = "Start Recording"; timer1.Stop(); mciSendString("save recsound d:\\record.wav", "", 0, 0); mciSendString("close recsound ", "", 0, 0); Computer c = new Computer(); c.Audio.Stop(); DialogResult res = MessageBox.Show("Verifikasi dengan data ini?", "konfirmasi", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { FormWaiting fw = new FormWaiting(); fw.Show(); DataClassesMentoringDataContext dc = new DataClassesMentoringDataContext(); IQueryable dtw = from p in dc.MsRekams select p; List ld = dtw.ToList(); int ew = 0; int ei = 0; ContinuousHiddenMarkovModel chmm = new ContinuousHiddenMarkovModel(20, 6); double[] tw = new double[32]; foreach (MsRekam ty in ld) { if (ei == 32) { ei = 0; chmm.Learn(tw, 0.001); ew++;

L17

} tw[ei] = (double)ty.DataRek; ei++; } double pola; IQueryable cek2 = from p in dc.MsRekams select p; List tap = cek2.ToList(); pola = (double)tap.Last().DataRek; suara = File.ReadAllBytes(@"D:\\record.wav"); waveLeft = new double[suara.Length / 4]; waveRight = new double[suara.Length / 4]; u = 0;

for (int i = 0; i < suara.Length - 4; i += 4) { waveLeft[u] = (double)BitConverter.ToInt16(suara, i); waveRight[u] = (double)BitConverter.ToInt16(suara, i + 2); u++; } tes = Pre.strong(waveLeft); tes = Pre.silence(tes); tes = Pre.normalisasi(tes); wave = MFCC.Proses(tes); double[] wt = new double[wave.Length]; for (int i = 0; i < wave.Length; i++) { wt[i] = ((wave[i] - wave.Min()) / (wave.Max() - wave.Min())) * 1; } double b = chmm.Evaluate(wt);

L18

fw.Close(); lama = DateTime.Now - now; if (Math.Abs(b - pola) > 0.0001) MessageBox.Show("Verifikasi Gagal.\nSilahkan Mencoba Kembali\nTerima Kasih"); else MessageBox.Show("Verifikasi Berhasil.\nSelamat Datang " + nama + "."); = new SystemLog();

SystemLog sl

sl.IDStaff = id; sl.AttendanceTime = DateTime.Now; dc.SystemLogs.InsertOnSubmit(sl); dc.SubmitChanges(); pictureBox1.BackColor = Color.White; this.Close(); } else { pictureBox1.BackColor = Color.White; this.Close(); } } } private void Start() { Stop(); try { _waveFormat = new WaveFormat(_audioFrameSize, _audioBitsPerSample, _audioChannels); _recorder = new WaveInRecorder(0, _waveFormat, _audioFrameSize * 2, 3, new BufferDoneEventHandler(DataArrived)); if (_isPlayer == true) _player = new WaveOutPlayer(-1, _waveFormat, _audioFrameSize * 2, 3, new BufferFillEventHandler(Filler)); }

L19

catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Stop() { if (_recorder != null) try { _recorder.Dispose(); } finally { _recorder = null; } if (_isPlayer == true) { if (_player != null) try { _player.Dispose(); } finally { _player = null; } } } private void Filler(IntPtr data, int size)

L20

{ if (_isPlayer == true) { if (_playerBuffer == null || _playerBuffer.Length < size) _playerBuffer = new byte[size]; if (_stream.Length >= size) _stream.Read(_playerBuffer, 0, size); else for (int i = 0; i < _playerBuffer.Length; i++) _playerBuffer[i] = 0; System.Runtime.InteropServices.Marshal.Copy(_playerBuffer, 0, data, size); } } private void DataArrived(IntPtr data, int size) { if (_recorderBuffer == null || _recorderBuffer.Length < size) _recorderBuffer = new byte[size]; if (_recorderBuffer != null) { System.Runtime.InteropServices.Marshal.Copy(data, _recorderBuffer, 0, size); if (_isPlayer == true) _stream.Write(_recorderBuffer, 0, _recorderBuffer.Length); _audioFrame.Process(ref _recorderBuffer); _audioFrame.RenderTimeDomain(ref pictureBox1); } }

private void timer1_Tick(object sender, EventArgs e) { DateTime inr = DateTime.Now;

L21

TimeSpan tp = inr - now; label1.Text = String.Format("{0:D2} : {1:D2}.{2}", tp.Minutes, tp.Seconds, tp.Milliseconds); }

private void FormKonfirmasi_FormClosed(object sender, FormClosedEventArgs e) { } } }

Pre.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace AttendanceSystem { class Pre { public static double[] silence(double[] data) { double[] hasil; int k = 0; for (int i = 0; i < data.Length; i++) { if (data[i] < 100.0 && data[i] > -100.0) k++; } hasil = new double[data.Length - k]; int p = 0;

L22

for (int i = 0; i < data.Length; i++) { if (!(data[i] < 650.0 && data[i] > -650.0)) { hasil[p] = data[i]; p++; } } return hasil; } public static double[] strong(double[] data) { double[] hasil= new double[data.Length]; for (int i = 0; i < data.Length; i++) { hasil[i] = data[i] * 3; } return hasil; } public static double[] normalisasi(double[] data) { double[] hasil= new double[data.Length]; double max = data.Max(); double min = data.Min(); for (int i = 0; i < data.Length; i++) { hasil[i] = 1024 * (data[i] - min) / (max - min); } return hasil; } } }

MFCC.cs

L23

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics;

namespace AttendanceSystem { class MFCC { static private int n, nu; static private double linToMelFreq(double inputFreq) { return (2595.0 * (Math.Log(1.0 + inputFreq / 700.0) / Math.Log(10.0))); } static private double melToLinFreq(double inputFreq) { return (700.0 * (Math.Pow(10.0, (inputFreq / 2595.0)) - 1.0)); } static private double[] getMelFilterBankBoundaries(double minFreq, double maxFreq, int numberFilters) { double[] centers = new double[numberFilters + 2]; double maxFreqMel, minFreqMel, deltaFreqMel, nextCenterMel;

maxFreqMel = linToMelFreq(maxFreq); minFreqMel = linToMelFreq(minFreq); deltaFreqMel = (maxFreqMel - minFreqMel) / (numberFilters + 1);

nextCenterMel = minFreqMel; for (int i = 0; i < centers.Length; i++)

L24

{ centers[i] = melToLinFreq(nextCenterMel); nextCenterMel += deltaFreqMel; } centers[0] = minFreq; centers[numberFilters + 1] = maxFreq;

return centers; } static private double getMelFilterWeight(int filterBank, double freq, double[] boundaries) { double result = 0;

double start = boundaries[filterBank - 1]; double center = boundaries[filterBank]; double end = boundaries[filterBank + 1]; double height = 2.0 / (end - start); if (freq >= start && freq pos) { a = xr[target]; xr[target] = xr[pos]; xr[pos] = a; }

L30

int mask = n;

while ((target & (mask >>= 1)) == 1) target &= ~mask; target |= mask; }

for (int step = 1; step < n; step = 0; t--) { s = 0;

for (int i = 0; i < States; i++) { for (int j = 0; j < States; j++) { double v = A[i, j] * B[j].ProbabilityFunction(observations[t + 1]) * bwd[t + 1, j]; bwd[t, i] += v; s += v; } }

if (s != 0)

{

for (int i = 0; i < States; i++) bwd[t, i] /= s; }

L48

}

return bwd; } private double measureLikelihood(double[][][] observations) { double average = 0; for (int i = 0; i < observations.Length; i++) average += Evaluate(observations[i]); return average / observations.Length; } private double[][] convert(Array array) { double[][] multivariate = array as double[][]; if (multivariate != null) return multivariate;

double[] univariate = array as double[]; if (univariate != null) return univariate.Split(Dimension);

throw new ArgumentException("Invalid array argument type.", "array"); } #endregion

} }