Results 1 to 8 of 8

Thread: Naudio - recording from a specific point in time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    16

    Naudio - recording from a specific point in time

    Hi all,

    I based on this solution to recording from microphone and its working.
    https://github.com/naudio/NAudio/blo...ordingPanel.cs
    but can't find function to achieve recording from specific point in time in existing file.

    Example
    Im recorded some audio about 1 minute, but from 30 seconds I made mistake and want to record again from this point.
    How to open existing file, set the position to specific miliseconds and start recording?

    Maybe any function to trim file and append recording on the end? Supplier didn't predict?

    For playing is function but for recording not?

    Code:
     Dim file = New AudioFileReader(Filez)
                Dim trimmed = New OffsetSampleProvider(file)
                trimmed.SkipOver = TimeSpan.FromMilliseconds(1000)
                trimmed.Take = TimeSpan.FromMilliseconds(20000)
                waveout.Init(trimmed)
                waveout.Play()
    Last edited by 6h057; Jul 22nd, 2021 at 03:27 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,338

    Re: Naudio - recording from a specific point in time

    i dont know naudio, but what you need is quite specific and i can imagine that this functionality is not included in naudio.
    to work around, you would need to record to a new file or RAM and then write that data to the file and position in question using normal file io.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    16

    Re: Naudio - recording from a specific point in time

    Quote Originally Posted by digitalShaman View Post
    i dont know naudio, but what you need is quite specific and i can imagine that this functionality is not included in naudio.
    to work around, you would need to record to a new file or RAM and then write that data to the file and position in question using normal file io.
    Ok, but If I will join files with that way, the result wav file will be correct? There is no marks inside wav file which tell about type of file or other parameters?

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,338

    Re: Naudio - recording from a specific point in time

    there is the wav header at the beginning of the file. yes, this needs tweaking. assuming you use the same recording format (Samplerate, bitdepth etc.) you will only need to adjust a few length entries in the wav header. it should then play fine.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    16

    Re: Naudio - recording from a specific point in time

    Ok, I will try, but please tell me how to set in naudio recording device. I spent hours and can't find. I know how to list devices, but how to indicate specific number

    Code:
    wavein = New WaveIn()
    wavein.WaveFormat = New WaveFormat(44100, 2)
    wavein.devicenumber = 1????????

    I don't understand this C# code

    Code:
     var deviceNumber = comboWaveInDevice.SelectedIndex - 1;
    if (checkBoxEventCallback.Checked)
    {
    newWaveIn = new WaveInEvent() { DeviceNumber = deviceNumber }; what mean {}, is there substitute in VB?
    }
    else
    {
    newWaveIn = new WaveIn() { DeviceNumber = deviceNumber };
    }
    Last edited by 6h057; Jul 23rd, 2021 at 02:56 PM.

  6. #6
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    682

    Re: Naudio - recording from a specific point in time

    Some of this might help:

    Code:
    Imports NAudio.Wave
    Imports NAudio.CoreAudioApi
    
    Module modNaudio
    
        Private NAudioWaveIn As New NAudio.Wave.WaveIn
    
        Sub Start_RX_Sampling(iSoundInID As Integer)
    
            NAudioWaveIn.WaveFormat = New WaveFormat(44100, 16, 1)
            NAudioWaveIn.DeviceNumber = iSoundInID
            NAudioWaveIn.BufferMilliseconds = 200
            NAudioWaveIn.NumberOfBuffers = 3
    
            AddHandler NAudioWaveIn.DataAvailable, AddressOf WaveIn_Data_Available
    
            NAudioWaveIn.StartRecording()
    
        End Sub
    
        Sub WaveIn_Data_Available(sender As Object, e As WaveInEventArgs)
    
            ' Transfer new data to global RX buffer and increment GiBufferOffset
    
            For i As Integer = 0 To e.Buffer.Length - 1 Step 2
                GrxBuffer(GiBufferOffset) = BitConverter.ToInt16(e.Buffer, i) ' Convert bytes from buffer to shorts
                GiBufferOffset += 1
                If GiBufferOffset = GiBufferQuota + 1 Then GiBufferOffset = 0 ' Reached end of buffer so start again from position 0
            Next
    
        End Sub
    
        Sub Stop_RX_Sampling()
            NAudioWaveIn.StopRecording()
        End Sub
    
        Function fnGet_Sound_Device_List() As List(Of String)
    
            Dim sDeviceList As New List(Of String)
    
            Try
    
                Dim devicecount As Integer = NAudio.Wave.WaveIn.DeviceCount()
    
                ' Get list of sound IN devices - Windows Vista or later - full device names will be returned
    
                If devicecount > 0 Then
    
                    Dim enumerator As New MMDeviceEnumerator()
                    For Each device As MMDevice In enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active)
                        sDeviceList.Add(device.FriendlyName)
                    Next
    
                Else
                    sDeviceList.Add("No devices")
                End If
    
            Catch ex As Exception
                sDeviceList.Add("Error!")
            End Try
    
            Return sDeviceList
    
        End Function
    
        Function fnGet_ID_From_Device_Name(sName As String) As Integer
    
            Dim devicecount As Integer = NAudio.Wave.WaveIn.DeviceCount()
    
            If devicecount > 0 Then
                For i As Integer = 0 To devicecount - 1
                    If sName.Contains(NAudio.Wave.WaveIn.GetCapabilities(i).ProductName) Then
                        Return i
                    End If
                Next i
            End If
    
            Return -1
    
        End Function
    
    End Module

  7. #7
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    682

    Re: Naudio - recording from a specific point in time

    Also, if you only have one sound input device (a microphone perhaps) you can probably just use 0 as the device number.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    16

    Re: Naudio - recording from a specific point in time

    Ok, device number works correctly.

    Now I want to trim part of wav file to reach "recording from specific point". I translated this code from C#

    Code:
        Public Sub TrimWavFile(ByVal inPath As String, ByVal outPath As String, ByVal cutFromStart As TimeSpan, ByVal cutFromEnd As TimeSpan)
            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)
                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)
            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)
                    End If
                End If
            End While
        End Sub
    When stopping records I call this function

    Code:
    TrimWavFile(Filez, My.Computer.FileSystem.GetFileInfo(Filez).DirectoryName & "\s1part" & My.Computer.FileSystem.GetFileInfo(Filez).Name, TimeSpan.FromMilliseconds(startcut), TimeSpan.FromMilliseconds(Duration(Filez)))
    I want save wav file from begining to startcut, rest shoud be trimmed.
    File is created but with only 46bytes and error when trying to play.
    Something is wrong with this code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width