Re: Please help : How to embed sound file into a project..
Thanks alot! I got it now. Couldn't do it without your help :D
Re: Please help : How to embed sound file into a project..
I'll just simply set the 'read' button to 'invisible' and then in the Form_Load()
event, cmdRead_Click() :p
Re: Please help : How to embed sound file into a project..
I don't know how JM is getting on, but I've created an example of the 'append it to the .exe file' method.
I will probably get it finished sometime today but I have been busy with other details at work. I think, however, that your method is probably better than what I am doing since what I am doing is putting the sound file as part of the VB scouce code so it will be compiled just like all the rest of the variables and stuff. It's not a true binary embedded file but rather the binary file converted to string data and then pasted into the source code as a string array of the sound bytes. But I pointed out earlier that this method is akward and rather silly to do, in my opinion, when the resource file method is so much better but if he is still interested then OK otherwise I may just put it on the self if what you did satisfies him.
1 Attachment(s)
Re: Please help : How to embed sound file into a project..
vibien, please have a quick look at this. It uses a Res file but I'm under the impression that you don't actually understand what a Res file is. This code won't work in the IDE, compile and run the executable to hear the sound play. The sound is embedded inside the exe (not in another file somewhere else)
Re: Please help : How to embed sound file into a project..
Quote:
Originally Posted by jmsrickland
[B]But I pointed out earlier that this method is akward and rather silly to do, in my opinion, when the resource file method is so much better but if he is still interested then OK otherwise I may just put it on the self if what you did satisfies him.
Sometimes we (I) learn doing 'silly' things. Keep going with what you are trying, you will learn a lot :D
Re: Please help : How to embed sound file into a project..
I am having a little problem working out a suitable For Loop with my array of character bytes. Example: 1 Byte = "7A" = a 2 character string byte
The array is (1 to 152). Please don't ask me to change that
Each element of the array has 50 2-character bytes in it except for element #152 which has only 4 2-character bytes
So, 50 * 151 = 7550
plus 4 from element 152 = 7554 bytes
the sound file is exaclty 7554 bytes is size
I need a loop that pulls out each 2-character byte pair form the array and place them in another array which is pure binary so it will be 1-byte for every 2-character pair from the first array.
Here is my code but I am unable to get it to come out right. Everything works out just fine until I get to the point where I am only dealing with the final element of 4 bytes (element 152).
Code:
Private Sub cmdMakeByteArray_Click()
Dim ByteString() As Byte
Dim h As String
'
' The value 152 is only for this particular sound file and will change
' when other sound files or any binary file is used to whatever their
' total bytes are divided by 50
'
' 50 is just a number I picked to put in each element
'
ReDim BinArray(1 To 152) '<---- Must be as listed
LoadArray '<----------- will load BinArray with 7554 2-character byte value as I stated above
'Total number of bytes in sound file = 7554
'
' Have no real idea if this is even needed
'
If UBound(BinArray) Mod BytesPerLine = 0 Then
t = UBound(BinFile) \ BytesPerLine
ReDim ByteString(1 To BytesPerLine * UBound(BinArray)) ' \ BytesPerLine)
Else
t = UBound(BinArray) \ BytesPerLine + 1
ReDim ByteString(1 To BytesPerLine * UBound(BinArray) + t) ' \ BytesPerLine + t)
End If
k = 100 '<---number of bytes per element * 2 excep for last element
For q = 1 To UBound(BinArray)
For q1 = 1 To k Step 2
j = j + 1
ByteString(j) = CByte(Hex2Dec(CStr(Mid(BinArray(q), q1, 2))))
Next q1
'
' Here is where I'm not sure what to do
'
p = p + 1
If p = UBound(BinArray) \ BytesPerLine Then
k = t '<---- change the loop counter to 4
End If
Next q
'
' Now output this as a new wave file so I can test it using WMP
'
Open App.Path & "\NEW_SOUND.WAV" For Binary As #1
Put #1, 1, ByteString
Close #1
End Sub
Re: Please help : How to embed sound file into a project..
OK, never mind. I figured out a very simple solution for the For...Next loop
Re: Please help : How to embed sound file into a project..
Quote:
Originally Posted by Doogle
Sometimes we (I) learn doing 'silly' things. Keep going with what you are trying, you will learn a lot :D
Yeah, I greatly agree with that! :bigyello:
Re: Please help : How to embed sound file into a project..
Well, it will probably be in the morning as I am having a little problem playing the sound array within my program although it plays correctly using WMP for testing
Re: Please help : How to embed sound file into a project..
Wow, big bunch of codes! I'll check these all :thumb: and inform you soon
thanks
Re: Please help : How to embed sound file into a project..
Wow! I'm using Milk's code, and it goes GREAT and understandable! And moreover you made it pretty simple. Just like one i wishing for :D
Thank you so much. I appreciate your assistances :thumb:
Re: [RESOLVED] Please help : How to embed sound file into a project..
It's not surprising that code I posted is shorter, I'm using a RES file to embed. JMS and Doogle are offering alternative embedding methods which invariably will be more complex.
I think what JMS is doing is quite interesting because the data can be stored within the code. I'm thinking of a portable class that requires some data to run, if all the data could be held in the code then it makes the class that bit easier to just drop into different projects.
Re: [RESOLVED] Please help : How to embed sound file into a project..
Now like I said it's not the desirable way of doing it but for whatever it's worth here it is.
It comprises of two projects. One project reads in a binary file that you select from a dialog box and converts it to an array of text data and saves it on your hard drive.
The second project is what you use to play that text data.
After you run the first program it will make a text file which you will need to load into a text editor like Notepad and copy the contents to the ClipBoard.
Open up the second project and paste the code into the source code area according to the instructions and then click on the play button.
Here is the source code for the first project which makes the text file.
On a Form simply add a Command button.
Code:
Option Explicit
Const MAX_BYTES_PER_LINE = 50
Dim FileName As String
Dim BinArray() As String
Dim BinFile() As Byte
Dim ByteString() As Byte
Private Sub Command1_Click()
Dim FilePath As String
On Error GoTo ErrHandler
CommonDialog1.InitDir = App.Path & "\"
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
FilePath = CommonDialog1.FileName
FileName = UCase(CommonDialog1.FileTitle)
Open FilePath For Binary As #1
ReDim BinFile(LOF(1) - 1)
Get #1, 1, BinFile
Close #1
MakeTextArray
Exit Sub
ErrHandler:
MsgBox "No file selected"
End Sub
Private Sub MakeTextArray()
Dim BYTEDATA As String
Dim FileString As String
Dim k As String
Dim g As Integer, i As Integer, m As Integer, p As Integer, q As Integer
FileString = ""
k = ""
m = 0
i = 0
If UBound(BinFile) Mod MAX_BYTES_PER_LINE = 0 Then
ReDim BinArray(1 To UBound(BinFile) \ MAX_BYTES_PER_LINE)
Else
ReDim BinArray(1 To UBound(BinFile) \ MAX_BYTES_PER_LINE + 1)
End If
g = MAX_BYTES_PER_LINE
For q = 0 To UBound(BinFile)
k = k & Right("100" & Hex(UCase(BinFile(q))), 2)
m = m + 1
If m = g Then
i = i + 1
k = " BinArray(" & i & ") = " & Chr(34) & k & Chr(34)
FileString = FileString & k & vbCrLf
DoEvents
p = p + 1
If p = UBound(BinFile) \ MAX_BYTES_PER_LINE Then g = UBound(BinFile) - q
m = 0
k = ""
End If
Next q
FileString = "" & FileString
FileString = " '" & vbCrLf & _
" ' Copy and Paste below into Sub LoadArray in you Application" & vbCrLf & _
" '" & vbCrLf & FileString
Open App.Path & UCase("\Binary_Text_Array_For_") & FileName & UCase(".txt") For Output As #1: Close #1
Open App.Path & UCase("\Binary_Text_Array_For_") & FileName & UCase(".txt") For Binary As #1
Put #1, 1, FileString
Close #1
MsgBox "Your binary text array file is now ready. You must load it into" & vbCrLf & _
"into a text editor program (ex Notepad) and Copy and Past" & vbCrLf & _
"the contents into the Sub LoadArray in your VB project" & vbCrLf & vbCrLf & _
"The name of the file is:" & vbCrLf & vbCrLf & _
UCase("Binary_Text_Array_For_") & FileName & UCase(".txt")
End Sub
Here is the code for the second project which plays the sound file
On a Form simply add a Command button
Code:
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByRef lpszName As Any, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_MEMORY = &H4
'
' NOTE: This program is for VB Project Design Time Only
'
' Do Not Compile this Project
'
Private Const MAX_BYTES_PER_LINE = 50
Dim BinArray() As String
Dim BinFile() As Byte
Dim ByteString() As Byte
Private Sub Command1_Click()
MakeByteArray
Play ByteString
End Sub
Private Sub MakeByteArray()
Dim i As Integer
Dim j As Integer
Dim k As Integer
If Not LoadArray Then Exit Sub
k = 0
ReDim ByteString(1 To MAX_BYTES_PER_LINE * (UBound(BinArray) - 1) + Len(BinArray(UBound(BinArray))) \ 2)
For i = 1 To (UBound(BinArray))
For j = 1 To Len(BinArray(i)) Step 2
k = k + 1
ByteString(k) = CByte(Hex2Dec(CStr(Mid(BinArray(i), j, 2))))
Next j
Next i
End Sub
Private Function Hex2Dec(InputData As String) As Double
'
' Converts Hexadecimal to Decimal
'
Dim i As Integer
Dim DecOut As Double
Dim Lenhex As Integer
Dim HexStep As Double
'
' Zero the output
'
DecOut = 0
'
' The length of the input
'
InputData = UCase(InputData)
Lenhex = Len(InputData)
'
' Check to make sure its a valid Hex Number
'
For i = 1 To Lenhex
If IsNumeric(Mid(InputData, i, 1)) Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "A" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "B" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "C" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "D" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "E" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "F" Then
GoTo NumOk
Else
MsgBox "Number given is not in Hex format", vbCritical
Exit Function
End If
NumOk:
Next i
HexStep = 0
'
' Convert the Number to Decimal
'
For i = Lenhex To 1 Step -1
HexStep = HexStep * 16
If HexStep = 0 Then
HexStep = 1
End If
If Mid(InputData, i, 1) = "0" Then
DecOut = DecOut + (0 * HexStep)
ElseIf Mid(InputData, i, 1) = "1" Then
DecOut = DecOut + (1 * HexStep)
ElseIf Mid(InputData, i, 1) = "2" Then
DecOut = DecOut + (2 * HexStep)
ElseIf Mid(InputData, i, 1) = "3" Then
DecOut = DecOut + (3 * HexStep)
ElseIf Mid(InputData, i, 1) = "4" Then
DecOut = DecOut + (4 * HexStep)
ElseIf Mid(InputData, i, 1) = "5" Then
DecOut = DecOut + (5 * HexStep)
ElseIf Mid(InputData, i, 1) = "6" Then
DecOut = DecOut + (6 * HexStep)
ElseIf Mid(InputData, i, 1) = "7" Then
DecOut = DecOut + (7 * HexStep)
ElseIf Mid(InputData, i, 1) = "8" Then
DecOut = DecOut + (8 * HexStep)
ElseIf Mid(InputData, i, 1) = "9" Then
DecOut = DecOut + (9 * HexStep)
ElseIf Mid(InputData, i, 1) = "A" Then
DecOut = DecOut + (10 * HexStep)
ElseIf Mid(InputData, i, 1) = "B" Then
DecOut = DecOut + (11 * HexStep)
ElseIf Mid(InputData, i, 1) = "C" Then
DecOut = DecOut + (12 * HexStep)
ElseIf Mid(InputData, i, 1) = "D" Then
DecOut = DecOut + (13 * HexStep)
ElseIf Mid(InputData, i, 1) = "E" Then
DecOut = DecOut + (14 * HexStep)
ElseIf Mid(InputData, i, 1) = "F" Then
DecOut = DecOut + (15 * HexStep)
Else
MsgBox "Error has occured", vbCritical
End If
Next i
Hex2Dec = DecOut
eds:
End Function
Private Sub StopPlay()
PlaySound "", ByVal 0&, SND_MEMORY
End Sub
Private Sub Play(WavSound() As Byte)
On Error GoTo ErrHandler
PlaySound WavSound(1), 0, SND_MEMORY Or SND_ASYNC
Exit Sub
ErrHandler:
End Sub
Private Function LoadArray() As Boolean
On Error Resume Next
'
' NOTE: The second value must always be the total
' number of elements you pasted from the
' text file into this Function
'
ReDim BinArray(1 To nnn)
If Err Then
LoadArray = False
MsgBox "The Array has not been added to your source code"
Exit Function
Else
LoadArray = True
End If
'
' You must ReDim BinArray above after you have
' pasted the code from the text file below
'
End Function
Re: [RESOLVED] Please help : How to embed sound file into a project..
JMS: You may like to see another method to convert Hex to Decimal, using the &H operator:
Code:
Private Function HexToDecimal(strHex As String) As Long
Dim intI As Integer
Dim boInvalid As Boolean
If Len(strHex) <= 8 Then
strHex = UCase(strHex)
intI = 1
Do
Select Case Mid$(strHex, intI, 1)
Case "0" To "9", "A" To "F"
intI = intI + 1
Case Else
boInvalid = True
End Select
Loop Until intI > Len(strHex) Or boInvalid = True
If boInvalid = False Then
HexToDecimal = CLng("&H" & strHex)
Else
MsgBox "Invalid Hexadecimal Data: " & strHex, "HexToDecimal"
End If
Else
MsgBox "Hexadecimal String too Long (Max 8 characters): " & strHex, "HexToDecimal"
End If
End Function
Re: [RESOLVED] Please help : How to embed sound file into a project..
Yeh, I didn't know how to do it so I got the code I used off the Internet and I simply took the first example I found so I'm sure there are many better ways to do it but I was only interested in getting anything that worked because I was only interested in the overall project and not wheather I got the best code to do anything as long as it worked.
Definitely though your code is far better and I will use it instead of what I had.