none
diretx: using SecondaryBuffer and Audio- how can i get the file audio time? RRS feed

Answers

  • heres how we get the audio lenght using Audio:

    dxAudio = New Audio(strFileName)
            dxAudio.Stop()
            intAudioLength = CInt(dxAudio.StopPosition / 10000)

    why 10000!?! i 'm sorry, but i don't know. what i know is the result is on milliseconds.

    heres how we get the audio lenght using SecondaryBuffer:

     SoundDevice = New Device
            SoundDevice.SetCooperativeLevel(GetActiveWindow, CooperativeLevel.Normal)
            BufferDesc = New BufferDescription With {.GlobalFocus = True, .Control3D = True, .ControlVolume = True}
            Buffer = New SecondaryBuffer(AudioStream, BufferDesc, SoundDevice)
            Buffer.Stop()
            Dim i As Integer
            Buffer.GetCurrentPosition(i, Buffer.WritePosition)
            intAudioLength = i
            Buffer.SetCurrentPosition(0)

    now heres a sample for we use the timer:

     dxs = New DXSound(My.Resources.Nível1)
            dxs.Play(True)
    'on timer control event:
     Dim seconds As Double = dxs.AudioPosition
            Dim ts As TimeSpan = TimeSpan.FromSeconds(seconds)
            Dim tslenght As TimeSpan = TimeSpan.FromMilliseconds(dxs.GetAudioLength)
            Me.Text = ts.Minutes.ToString("D2") + " : " + ts.Seconds.ToString("D2") + " / " + tslenght.Minutes.ToString("D2") + " : " + tslenght.Seconds.ToString("D2")

    and heres how we get the audio current position(been Audio or SecondaryBuffer):

    Public Property AudioPosition() As Integer
    
            Get
                If Buffer IsNot Nothing Then
                    Dim i As Integer
                    Buffer.GetCurrentPosition(i, Buffer.WritePosition)
                    Return i
                ElseIf dxAudio IsNot Nothing Then
                    Return CInt(dxAudio.CurrentPosition)
                End If
                Return 0
            End Get
            Set(value As Integer)
                If Buffer IsNot Nothing Then
                    Buffer.SetCurrentPosition(value)
                ElseIf dxAudio IsNot Nothing Then
                    dxAudio.CurrentPosition = value
                End If
            End Set
        End Property

    thanks to all

    • Marked as answer by Cambalinho Monday, March 6, 2017 8:55 PM
    Monday, March 6, 2017 8:55 PM

All replies

  • Get the audio time from what?  A position in the stream?  If that's what you mean it is a matter of doing the math according to your chosen buffer format.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 3, 2017 12:06 AM
    Moderator
  • heres my wrong code:

    Private Sub Load(strFileName As String)
            dxAudio = New Audio(strFileName)
            dxAudio.Stop()
            intAudioLength = CInt(dxAudio.StopPosition)
        End Sub
    
    Private Sub Load(ByVal AudioStream As Stream, Optional blnLooping As Boolean = False)
            If Not (BufferDesc Is Nothing) Then BufferDesc.Dispose()
            If Not (SoundDevice Is Nothing) Then SoundDevice.Dispose()
            If Not (dxAudio Is Nothing) Then dxAudio.Dispose()
            SoundDevice = New Device
            SoundDevice.SetCooperativeLevel(GetActiveWindow, CooperativeLevel.Normal)
            BufferDesc = New BufferDescription With {.GlobalFocus = True, .Control3D = True, .ControlVolume = True}
            Buffer = New SecondaryBuffer(AudioStream, BufferDesc, SoundDevice)
            Buffer.Stop()
            Dim i As Integer
            Buffer.GetCurrentPosition(i, Buffer.WritePosition)
            intAudioLength = i
            Buffer.SetCurrentPosition(0)
            blnBufferPlaying = True
            Looping = blnLooping
        End Sub

    i'm getting big numbers :(

    output:

    790000000

    i'm trying for use a progressbar time. but i don't know interpret the value. can you advice me more?

    Friday, March 3, 2017 7:51 PM
  • heres how we get the audio lenght using Audio:

    dxAudio = New Audio(strFileName)
            dxAudio.Stop()
            intAudioLength = CInt(dxAudio.StopPosition / 10000)

    why 10000!?! i 'm sorry, but i don't know. what i know is the result is on milliseconds.

    heres how we get the audio lenght using SecondaryBuffer:

     SoundDevice = New Device
            SoundDevice.SetCooperativeLevel(GetActiveWindow, CooperativeLevel.Normal)
            BufferDesc = New BufferDescription With {.GlobalFocus = True, .Control3D = True, .ControlVolume = True}
            Buffer = New SecondaryBuffer(AudioStream, BufferDesc, SoundDevice)
            Buffer.Stop()
            Dim i As Integer
            Buffer.GetCurrentPosition(i, Buffer.WritePosition)
            intAudioLength = i
            Buffer.SetCurrentPosition(0)

    now heres a sample for we use the timer:

     dxs = New DXSound(My.Resources.Nível1)
            dxs.Play(True)
    'on timer control event:
     Dim seconds As Double = dxs.AudioPosition
            Dim ts As TimeSpan = TimeSpan.FromSeconds(seconds)
            Dim tslenght As TimeSpan = TimeSpan.FromMilliseconds(dxs.GetAudioLength)
            Me.Text = ts.Minutes.ToString("D2") + " : " + ts.Seconds.ToString("D2") + " / " + tslenght.Minutes.ToString("D2") + " : " + tslenght.Seconds.ToString("D2")

    and heres how we get the audio current position(been Audio or SecondaryBuffer):

    Public Property AudioPosition() As Integer
    
            Get
                If Buffer IsNot Nothing Then
                    Dim i As Integer
                    Buffer.GetCurrentPosition(i, Buffer.WritePosition)
                    Return i
                ElseIf dxAudio IsNot Nothing Then
                    Return CInt(dxAudio.CurrentPosition)
                End If
                Return 0
            End Get
            Set(value As Integer)
                If Buffer IsNot Nothing Then
                    Buffer.SetCurrentPosition(value)
                ElseIf dxAudio IsNot Nothing Then
                    dxAudio.CurrentPosition = value
                End If
            End Set
        End Property

    thanks to all

    • Marked as answer by Cambalinho Monday, March 6, 2017 8:55 PM
    Monday, March 6, 2017 8:55 PM