Multi Thread In VB.net

Wednesday, July 22, 2009

Multi Threading in VB.Net

imports system.threading
Dim ThreadObject As Thread
ThreadObject = New Thread(AddressOf ClassName.functioname)
ThreadObject.Start()

_________________________________________________________________


The Thread generally does not accept any argument. you can create a constructor of a class and pass the required argument to that constructor.

E.g.:

ClassObject = New Classname(arg1,arg2,arg3,[]) /* The Constructor will assign pass the argument /*
ThreadObject = New Thread(AddressOf ClassName.functioname)
ThreadObject .Start()

__________________________________________________________________

Thread can not update to the control of the window directly but you can update the control of window by means of Delegate.

Convert .vox to .wav

Friday, May 29, 2009

mdlconvertvox2wav.Convert(SourceFileName, destinationFileName)
_________________________________________________________________
Create a module
Module mdlconvertvox2wav
Dim WaveHeader() As Int32 = New Int32() {&H46464952, &HFFFFFFDB, &H45564157, &H20746D66, 16, &H10001, 6000, 12000, &H100002, &H61746164, -1}
Dim IndexShift() As Int16 = New Int16() {-1, -1, -1, -1, 2, 4, 6, 8}
Dim StepSize() As Int16 = New Int16() {16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794}
Dim Nibble2Bit(,) As Int16 = New Int16(,) {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}
Dim SignTable() As Int16 = New Int16() {1, -1}
Dim StepSizeIndex As Int16 = 0
Dim Signal As Int16 = -2
Dim AvgBuffer() As Int16 = New Int16() {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim AvgIndex As Int16 = 0
Dim Total As Int32 = 0
Dim Length As Int32
Dim Offset As Int32
Dim nibble As Int16
Dim ReadBuffer(4096) As Byte
Dim WriteBuffer(8192) As Int16

Public Sub Convert(ByVal srcVOXFile As String, ByVal destWAVFile As String)
Dim strm As New IO.FileStream(srcVOXFile, IO.FileMode.Open, IO.FileAccess.Read)
FileOpen(2, destWAVFile, OpenMode.Binary, OpenAccess.Write, OpenShare.LockWrite)
FilePut(2, WaveHeader)
Length = 4096
While Length = 4096
Length = strm.Read(ReadBuffer, 0, 4096)
For Offset = 0 To Length - 1
nibble = 0
Signal += Decode(ReadBuffer(Offset) \ 16)
If (Signal > 2047 Or Signal < -2047) Then Truncate()
WriteBuffer(2 * Offset) = Signal
nibble = 1
Signal += Decode(ReadBuffer(Offset) Mod 16)
If (Signal > 2047 Or Signal < -2047) Then Truncate()
WriteBuffer(2 * Offset + 1) = Signal
Next
FilePut(2, WriteBuffer)
Total += Length
End While
strm.Close() : strm = Nothing
Total = 4 * Total
Length = Total + 36
FilePut(2, Length, 4 + 1)
FilePut(2, Total, 40 + 1)
FileClose(2)
End Sub

Public Function Decode(ByVal i As Byte) As Int16
Dim diff As Int16
Dim value As Int32
value = 2 * Total + 2 * Offset + nibble
diff = SignTable(Nibble2Bit(i, 0)) * (StepSize(StepSizeIndex) * Nibble2Bit(i, 1) + (StepSize(StepSizeIndex) / 2) * Nibble2Bit(i, 2) + (StepSize(StepSizeIndex) / 4) * Nibble2Bit(i, 3) + (StepSize(StepSizeIndex) / 8))
StepSizeIndex = StepSizeIndex + IndexShift(i Mod 8)
If StepSizeIndex < 0 Then StepSizeIndex = 0
If StepSizeIndex > 48 Then
StepSizeIndex = 48
End If
Return diff
End Function

Public Sub Truncate()
Dim value As Int32
value = 2 * Total + 2 * Offset + nibble
If Signal > 2047 Then Signal = 2047
If Signal < -2047 Then Signal = -2047
End Sub

End Module
_____________________________________________________________

Text to Speech

Step 1: Add a reference with com object Mocrosoft speech Object Library
St2p 2:Import Sppechlib

Code :

Dim voice As New SpVoice
___________________________________________________________
Read from the File

OpenFileDialog.Filter = "Text Files (*.txt)|*.txt"
If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fs As New System.IO.FileStream(Me.OpenFileDialog.FileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim fr As New System.IO.StreamReader(fs)
Me.txttext.Text = fr.ReadToEnd
End If

___________________________________________________________

Some of the Property to be used

voice.Pause() ' For Pause
voice.resume() ' For Resume
voice.Rate = Val(txtrate.Text) 'speed
voice.Volume = Val(txtvolume.Text) ' volume

voice.Speak(Me.txttext.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync) ' For Speek

________________________________________________________________

Save to a Wav File:
Dim cpFileStream As New SpeechLib.SpFileStream
Dim filename As String = "C:\rajiv.wav"
Try
SaveFileDialog.Filter = ".Wav File|*.wav"
If SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
filename = SaveFileDialog.FileName
If File.Exists(filename) Then Kill(filename)
Dim oVoice As New SpeechLib.SpVoice
cpFileStream.Open(filename, SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite, False)
oVoice.AudioOutputStream = cpFileStream
oVoice.Volume = Val(txtvolume.Text)
oVoice.Rate = Val(txtrate.Text)
oVoice.Speak(txttext.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
cpFileStream.Close()
cpFileStream = Nothing
MsgBox("File Saved Successfully!!!!", vbInformation)
End If
_____________________________________________________________

Use of Delegate in c#

Wednesday, May 6, 2009



// Declare the Delegate
public delegate void Uploaddelegate();
// Create the object of Delegate
Uploaddelegate obj1 = new Uploaddelegate(ABC);


public static void ABC()
{
// Enter the Code to Do
}

Namespaces of .Net

Thursday, April 30, 2009

System: Includes essential classes and base classes for commonly used data types, events, exceptions and so on
System.Collections: Includes classes and interfaces that define various collection of objects such as list, queues,
hash tables, arrays, etc
System.Data: Includes classes which lets us handle data from data sources
System.Data.OleDb: Includes classes that support the OLEDB .NET provider
System.Data.SqlClient: Includes classes that support the SQL Server .NET provider
System.Diagnostics: Includes classes that allow to debug our application and to step through our code
System.Drawing: Provides access to drawing methods
System.Globalization: Includes classes that specify culture-related information
System.IO: Includes classes for data access with Files
System.Net: Provides interface to protocols used on the internet
System.Reflection: Includes classes and interfaces that return information about types, methods and fields
System.Security: Includes classes to support the structure of common language runtime security system
System.Threading: Includes classes and interfaces to support multithreaded applications
System.Web: Includes classes and interfaces that support browser-server communication
System.Web.Services: Includes classes that let us build and use Web Services
System.Windows.Forms: Includes classes for creating Windows based forms
System.XML: Includes classes for XML support