0
Answer

creating silence in WAV files

Delta Storm

Delta Storm

17y
2.8k
1
Hi,

This is going to be a bih question but please if you can help me in any way.

I am building a application that is creating wav files. I have managed to create a wav file but I need to insert a pause in the WAV - a straight line, no sound. but still part of the wav. The code is following:

Dim i As Long
Dim lngLimit As Double
Dim lngDataL, lngDataR As Double
Dim dblDataPt As Double
Dim blnPositive As Boolean
Dim intCycleCount As Integer
Dim lngFileSize As Double
Dim lngSamples As Double
Dim lngDataSize As Double
Dim dblDataSlice As Double

Dim dblWaveTime As Double
Dim dblTotalTime As Double
Dim dblSampleTime As Double

dblFrequency = Frequency

'If dblFrequency > 1000 Then
' intCycles = 100
'Else
' intCycles = 10
'End If

intCycles = CInt(trajanje)


dblWaveTime = 1 / dblFrequency
dblTotalTime = dblWaveTime * intCycles
dblSampleTime = 1 / CDbl(lngSampleRate)
dblDataSlice = (2 * PI) / (dblWaveTime / dblSampleTime)

lngSamples = 0
intCycleCount = 0
blnPositive = True
Do
dblDataPt = System.Math.Sin(lngSamples * dblDataSlice)
If lngSamples > 0 Then
If dblDataPt < 0 Then
blnPositive = False
Else
' Detect Zero Crossing
If Not blnPositive Then
intCycleCount = intCycleCount + 1
If intCycleCount >= intCycles Then Exit Do
blnPositive = True
End If

End If
End If
lngSamples = lngSamples + 1

Loop



'-------------------------------------------------------------------------------
' Bytes 40 - 43 Length of Data Samples * Channels * Bits per Sample / 8
'-------------------------------------------------------------------------------
lngDataSize = lngSamples * intAudioWidth * (intBits / 8)
ReDim Preserve WavArray(0 To 43 + lngDataSize)

WavArray(40) = ExtractByte(lngDataSize, 0)
WavArray(41) = ExtractByte(lngDataSize, 1)
WavArray(42) = ExtractByte(lngDataSize, 2)
WavArray(43) = ExtractByte(lngDataSize, 3)

'-------------------------------------------------------------------------------
' Bytes 04 - 07 Total Length to Follow (Length of File - 8)
'-------------------------------------------------------------------------------
lngFileSize = lngDataSize + 36

WavArray(4) = ExtractByte(lngFileSize, 0)
WavArray(5) = ExtractByte(lngFileSize, 1)
WavArray(6) = ExtractByte(lngFileSize, 2)
WavArray(7) = ExtractByte(lngFileSize, 3)

'-------------------------------------------------------------------------------
' Bytes 44 - End Data Samples
'-------------------------------------------------------------------------------

If intBits = 8 Then
lngLimit = 127
Else
lngLimit = 32767
End If

For i = 0 To lngSamples - 1

If intBits = 8 Then
'-----------------------------------------------------------------------
' 8 Bit Data
'-----------------------------------------------------------------------
' Calculate data point.
dblDataPt = System.Math.Sin(i * dblDataSlice) * lngLimit
lngDataL = Int(dblDataPt * dblVolumeL) + lngLimit
lngDataR = Int(dblDataPt * dblVolumeR) + lngLimit

' Place data point in wave tile.
If intAudioMode = MODE_MONO Then WavArray(i + 44) = ExtractByte(lngDataL, 0)

If intAudioMode = MODE_LR Then 'L+R stereo
WavArray((2 * i) + 44) = ExtractByte(lngDataL, 0)
WavArray((2 * i) + 45) = ExtractByte(lngDataR, 0)
End If

If intAudioMode = MODE_L Then ' L only stereo
WavArray((2 * i) + 44) = ExtractByte(lngDataL, 0)
WavArray((2 * i) + 45) = 0
End If

If intAudioMode = MODE_R Then ' R only stereo
WavArray((2 * i) + 44) = 0
WavArray((2 * i) + 45) = ExtractByte(lngDataR, 0)
End If

Else

'-----------------------------------------------------------------------
' 16 Bit Data
'-----------------------------------------------------------------------
' Calculate data point.
dblDataPt = System.Math.Sin(i * dblDataSlice) * lngLimit
lngDataL = Int(dblDataPt * dblVolumeL)
lngDataR = Int(dblDataPt * dblVolumeR)

' Place data point in wave tile.
If intAudioMode = MODE_MONO Then
WavArray((2 * i) + 44) = ExtractByte(lngDataL, 0)
WavArray((2 * i) + 45) = ExtractByte(lngDataL, 1)
End If

If intAudioMode = MODE_LR Then
WavArray((4 * i) + 44) = ExtractByte(lngDataL, 0)
WavArray((4 * i) + 45) = ExtractByte(lngDataL, 1)
WavArray((4 * i) + 46) = ExtractByte(lngDataR, 0)
WavArray((4 * i) + 47) = ExtractByte(lngDataR, 1)
End If

If intAudioMode = MODE_L Then
WavArray((4 * i) + 44) = ExtractByte(lngDataL, 0)
WavArray((4 * i) + 45) = ExtractByte(lngDataL, 1)
WavArray((4 * i) + 46) = 0
WavArray((4 * i) + 47) = 0
End If

If intAudioMode = MODE_R Then
WavArray((4 * i) + 44) = 0
WavArray((4 * i) + 45) = 0
WavArray((4 * i) + 46) = ExtractByte(lngDataR, 0)
WavArray((4 * i) + 47) = ExtractByte(lngDataR, 1)
End If

End If

Next

this is the function that creates the array. another function just writes the wav as binary writer, and all is good. I get a perfect result; a perfect sine wave. But i need to insert some pauses -SILENCE that is in the WAV.

This is a great challenge for me, maybe it will be for you too.

Please help.

Thank you.
Next Recommended Forum