Hello,
I need to create a Wave and then write it to a file to be read back and played with the media player.
A Wave has a very specific format consisting of a Header with certain data sizes and positions followed by an array of data which represent samples.
In C I would do this using a struct. This guarantees the data size and relative position of each field.
Can I do the same thing in B4A? Below I show my attempt by using a type, then initializing the fields, and finally writing it to a file using streams.
Is this the correct way to do this? I get a compile error because my stream WriteBytes first parameter is not an array of bytes.
Does using a "Type" work like a struct? That is, are the data sizes what you would expect and is the data positioned contiguously - like it would be in a C struct?
How do I cast or reference WaveData in the stream.WriteBytes line of code so that it thinks my WaveData is an array of bytes?
Can this be done in Basic? Do I need to take a different approach?
Thanks,
Barry.
I need to create a Wave and then write it to a file to be read back and played with the media player.
A Wave has a very specific format consisting of a Header with certain data sizes and positions followed by an array of data which represent samples.
In C I would do this using a struct. This guarantees the data size and relative position of each field.
Can I do the same thing in B4A? Below I show my attempt by using a type, then initializing the fields, and finally writing it to a file using streams.
Is this the correct way to do this? I get a compile error because my stream WriteBytes first parameter is not an array of bytes.
Does using a "Type" work like a struct? That is, are the data sizes what you would expect and is the data positioned contiguously - like it would be in a C struct?
How do I cast or reference WaveData in the stream.WriteBytes line of code so that it thinks my WaveData is an array of bytes?
Can this be done in Basic? Do I need to take a different approach?
Thanks,
Barry.
B4X:
Sub Globals
...
Type TWaveHeader _
(ChunkID(4) As Char, _
ChunkSize As Int, _
Format(4) As Char, _
Subchunk1ID(4) As Char, _
Subchunk1Size As Int, _
AudioFormat As Short, _
NumChannels As Short, _
SampleRate As Int, _
ByteRate As Int, _
BlockAlign As Short, _
BitsPerSample As Short, _
Subchunk2ID(4) As Char, _
Subchunk2Size As Int, _
Data(75000) As Short)
Dim WaveData As TWaveHeader
End Sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim OutS As OutputStream
WaveData.Initialize
WaveData.ChunkID(0) = "R"
WaveData.ChunkID(1) = "I"
WaveData.ChunkID(2) = "F"
WaveData.ChunkID(3) = "F"
WaveData.Format(0) = "W"
WaveData.Format(1) = "A"
WaveData.Format(2) = "V"
WaveData.Format(3) = "E"
WaveData.Subchunk1ID(0) = "f"
WaveData.Subchunk1ID(1) = "m"
WaveData.Subchunk1ID(2) = "t"
WaveData.Subchunk1ID(3) = " "
WaveData.Subchunk1Size = 16
WaveData.AudioFormat = 1
WaveData.NumChannels = 1
WaveData.SampleRate = 44100
WaveData.BitsPerSample = 16
WaveData.ByteRate = WaveData.SampleRate * WaveData.NumChannels * WaveData.BitsPerSample / 2
WaveData.BlockAlign = WaveData.NumChannels * WaveData.BitsPerSample / 2
WaveData.Subchunk2ID(0) = "d"
WaveData.Subchunk2ID(1) = "a"
WaveData.Subchunk2ID(2) = "t"
WaveData.Subchunk2ID(3) = "a"
WaveData.Subchunk2Size = WaveData.Data.Length * 2
WaveData.ChunkSize = 36 + WaveData.Subchunk2Size
OutS = File.OpenOutput(File.DirDefaultExternal, "data.wav", False)
OutS.WriteBytes(Array As Byte(WaveData), 0, WaveData.ChunkSize + 8)