-
Jan 7th, 2022, 05:17 PM
#1
Thread Starter
Member
[RESOLVED] NAudio - add silence into existing wav file
Hi all,
I have some code for recording, trimming and joining wav files and it works fine.
How can I add silence with specific duration on the end of wav file?
Code:
'button1
wavein = New WaveIn()
wavein.WaveFormat = New WaveFormat(44100, 2)
AddHandler wavein.DataAvailable, AddressOf OnDataAvailable
AddHandler wavein.RecordingStopped, AddressOf OnRecordingStopped
writer = New WaveFileWriter("test.wav", wavein.WaveFormat)
wavein.StartRecording()
'button2
wavein.StopRecording() 'length 5 seconds
'button3
'insert a few seconds of silence on the end of "test.wav" file(ex. 5sec records+3sec silence)
-
Jan 7th, 2022, 07:40 PM
#2
Re: NAudio - add silence into existing wav file
Hi,
Personally, I'd do it in a separate audio editor.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 8th, 2022, 09:42 AM
#3
Thread Starter
Member
Re: NAudio - add silence into existing wav file
Originally Posted by Poppa Mintin
Hi,
Personally, I'd do it in a separate audio editor.
Poppa
Can't be. It must be done by my application
I have code for generating file with silence and if will not find solution I will generate file with silence and next join two files - not sophisticated solution
Code:
Public Sub WriteSilence(ByVal waveFormat As WaveFormat, ByVal silenceMilliSecondLength As Integer, ByVal waveFileWriter As WaveFileWriter)
Dim bytesPerMillisecond As Integer = waveFormat.AverageBytesPerSecond / 1000
Dim silentBytes = New Byte(silenceMilliSecondLength * bytesPerMillisecond - 1) {}
waveFileWriter.Write(silentBytes, 0, silentBytes.Length)
waveFileWriter.Dispose()
End Sub
instead of recording to wavein, should be something for generating silence
-
Jan 8th, 2022, 11:48 AM
#4
Thread Starter
Member
Re: NAudio - add silence into existing wav file
Ok I got it. If someone need, I added code into existing function for trimming(from internet). TrimWavFile normally do work but if you add last parameter "cisza" it add silence on the end of manipulated file with time what you set inside the "cisza" variable.
I don't know it is correctly but what I see is working.
Code:
Public Sub TrimWavFile(ByVal inPath As String, ByVal outPath As String, ByVal cutFromStart As TimeSpan, ByVal cutFromEnd As TimeSpan, ByVal cisza As Integer)
Using reader As New WaveFileReader(inPath)
Using writer As New WaveFileWriter(outPath, reader.WaveFormat)
Dim bytesPerMillisecond As Integer = reader.WaveFormat.AverageBytesPerSecond / 1000
Dim startPos As Integer = CInt(cutFromStart.TotalMilliseconds) * bytesPerMillisecond
startPos = startPos - startPos Mod reader.WaveFormat.BlockAlign
Dim endBytes As Integer = CInt(cutFromEnd.TotalMilliseconds) * bytesPerMillisecond
endBytes = endBytes - endBytes Mod reader.WaveFormat.BlockAlign
Dim endPos As Integer = CInt(reader.Length) - endBytes
TrimWavFile(reader, writer, startPos, endPos, cisza)
End Using
End Using
End Sub
Private Sub TrimWavFile(ByVal reader As WaveFileReader, ByVal writer As WaveFileWriter, ByVal startPos As Integer, ByVal endPos As Integer, ByVal cisza As Integer)
reader.Position = startPos
Dim buffer As Byte() = New Byte(1023) {}
While reader.Position < endPos
Dim bytesRequired As Integer = CInt(endPos - reader.Position)
If bytesRequired > 0 Then
Dim bytesToRead As Integer = Math.Min(bytesRequired, buffer.Length)
Dim bytesRead As Integer = reader.Read(buffer, 0, bytesToRead)
If bytesRead > 0 Then
writer.Write(buffer, 0, bytesRead) 'writedata
End If
End If
End While
If cisza > 0 Then
Dim bytesPerMillisecond As Integer = writer.WaveFormat.AverageBytesPerSecond / 1000
Dim silentBytes = New Byte(cisza * bytesPerMillisecond - 1) {}
writer.Write(silentBytes, 0, silentBytes.Length)
writer.Dispose()
End If
End Sub
-
Jan 9th, 2022, 11:51 AM
#5
Thread Starter
Member
Re: [RESOLVED] NAudio - add silence into existing wav file
Somone have any idea why this function write a little bit less silence than I set? When I set 1 minute(60000ms), I got 59864ms.
writer.WaveFormat.AverageBytesPerSecond is 176400 (44100). Should I multiplicate something?
Code:
Dim bytesPerMillisecond As Integer = writer.WaveFormat.AverageBytesPerSecond / 1000
Dim silentBytes = New Byte(cisza * bytesPerMillisecond - 1) {}
writer.Write(silentBytes, 0, silentBytes.Length)
writer.Dispose()
-
Jan 9th, 2022, 12:18 PM
#6
Re: [RESOLVED] NAudio - add silence into existing wav file
The issue is because bytesPerMillisecond is an Integer.
The value of 176400 / 1000 should be 176.4, but because you are storing it in an Integer only 176 is stored, and that difference creates the value of 59863.94 milliseconds.
Try it like this:
Code:
Dim bytesPerMillisecond As Double = writer.WaveFormat.AverageBytesPerSecond / 1000
Dim silentBytes = New Byte(CInt(cisza * bytesPerMillisecond - 1) {}
-
Jan 9th, 2022, 03:02 PM
#7
Thread Starter
Member
Re: [RESOLVED] NAudio - add silence into existing wav file
Originally Posted by si_the_geek
The issue is because bytesPerMillisecond is an Integer.
The value of 176400 / 1000 should be 176.4, but because you are storing it in an Integer only 176 is stored, and that difference creates the value of 59863.94 milliseconds.
Try it like this:
Code:
Dim bytesPerMillisecond As Double = writer.WaveFormat.AverageBytesPerSecond / 1000
Dim silentBytes = New Byte(CInt(cisza * bytesPerMillisecond - 1) {}
Yes, You are absolutely right. Thank You for your help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|