Link to home
Start Free TrialLog in
Avatar of tealnet
tealnet

asked on

Get length of audio file

Does anyone know an easy way of grabbing the length (duration) of an audio file?  Specifically we need to work with Olympus DSS and MP3 files.
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Here is a sample that finds the length you may need to download directX 9...

'FORM 1

Public Class frmTesting
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(104, 92)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(48, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(148, 36)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Label1"
        '
        'frmTesting
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "frmTesting"
        Me.Text = "frmTesting"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim o As New Sound.clsMSDirectX

        Select Case o.FindLength
            Case 1 To 9
                Label1.Text = Fix(o.FindLength) & " Sec."
            Case Else
                Label1.Text = o.FindLength.ToString("#:##") & " mins."
        End Select
        'o.AudioSound()
        ' o.VideoRun()
        'o.SecondBuffer(Me)
        '   Me.Close()
    End Sub
End Class

'CLASS

'CODE FROM C#
Imports Microsoft.DirectX.AudioVideoPlayback
Imports Microsoft.DirectX.DirectSound
Public Class clsMSDirectX
    Public FileTimeLength As Double
    Public FileName As String = "C:\MYFILE.mp3"

    'The AudioVideoPlayback namespace is an oddity in MDX as it is essentially
    'composed of only two main classes: Audio and Video. There are no methods
    'to enumerate hardware devices nor are device classes required for playback
    'of digital content. Usage consists of creating an Audio or a Video object
    'either from a file or from a URL, and calling the Play method on the object.
    'Once playback has completed, you simply free the resources held up by the object
    'by calling the Dispose method as usual and set the references to null.

    '    using Microsoft.DirectX.AudioVideoPlayback;

    '//The audio file to playback using the Audio class.
    'string strVideoFile = @"C:\SampleVideo.avi";

    '//Create a video object from a video stream.
    'Video video = Video.FromFile(strVideoFile);

    '//Run fullscreen
    'video.Fullscreen = true;

    ' //Play the video file.
    'video.Play();

    ' //Annoying bug in how the CurrentPosition and the StopPosition
    '//are calculated in the Audio and the Video classes. Also, When the streams are done with playback
    '//they do NOT update the "Status" or the "Playing" properties.
    'while ( video.Playing)
    '{
    '    if ( video.CurrentPosition * 10000000 == video.StopPosition)
    '    video.Stop();
    '}

    '//Dispose of the resources.
    'video.Dispose();
    'video = null;

    Public Sub AudioSound()
        'The Audio class functions similarly and the usage of the API
        'is almost identical to that of a Video object.

        'using Microsoft.DirectX.AudioVideoPlayback;

        '//Create an Audio object from the following file…
        Dim audio As Audio = audio.FromFile(FileName)

        '//Completely playback the stream.
        audio.Play()
        While audio.Playing
            If (audio.CurrentPosition * 10000000 = audio.StopPosition) Then
                audio.Stop()
            End If
        End While

        '//Dispose of the resources.
        audio.Dispose()
        audio = Nothing

    End Sub
    Public Function FindLength() As Double
        Dim audio As Audio = audio.FromFile(FileName)
        'Find length of song
        Return (audio.StopPosition / 10000000)
    End Function

    'In the sample below, a SecondaryBuffer object is created from a persistent media
    'file and is played back using default playback options. The application attempts
    'synchronous playback by waiting for the duration of the media file and at termination,
    'frees up resources stored by the buffer and the DirectSound device. Lastly, references
    'are set to null as a hint for the garbage collector.

    '    //The audio file to be loaded onto the secondary buffer.
    'string strFile = "C:\\Sample.wav";

    ' //Create a secondary buffer compatible with the sound device and load the file.
    '//soundDevice is the object created during the enumeration phase…
    'SecondaryBuffer buffer = new SecondaryBuffer(strFile, soundDevice);

    ' //Set the cooperative level of the sound device.
    'soundDevice.SetCooperativeLevel( this, CooperativeLevel.Normal);

    ' //Playback the data in the buffer normally. For other options,
    ' //inspect the BufferPlayFlags enumeration.
    'buffer.Play(  0, BufferPlayFlags.Default);

    ' while (buffer.Status.Playing)
    '{
    '    //Wait on this buffer to complete before continuing
    '}

    ' //Free the resources in the buffer
    'buffer.Dispose();
    'buffer = null;  

    ' //Free the DirectSound device.
    'soundDevice.Dispose();
    'soundDevice = null;

    Public Sub VideoRun()
        '//The audio file to playback using the Audio class.
        Dim strVideoFile As String = "C:\windows\clock.avi"

        '//Create a video object from a video stream.
        Dim video As Video = video.FromFile(strVideoFile)

        '//Run fullscreen
        video.Fullscreen = True

        'video.Size()
        '//Play the video file.
        video.Play()

        ' //Annoying bug in how the CurrentPosition and the StopPosition
        '//are calculated in the Audio and the Video classes. Also, When
        '// the streams are done with playback
        '//they do NOT update the "Status" or the "Playing" properties.
        While (video.Playing)

            If (video.CurrentPosition * 10000000 = video.StopPosition) Then
                video.Stop()
            End If
        End While

        '//Dispose of the resources.
        video.Dispose()
        video = Nothing
    End Sub
    Public Sub SecondBuffer(ByVal sControl As Control)
        '//Collection class used to obtain textual and GUID information of audio devices.
        Dim soundDevices As DevicesCollection = New DevicesCollection
        Dim info As DeviceInformation
        '//Create a device based on the GUID currently obtained from the //soundDevices collection
        Dim soundDevice As Device = New Device(info.DriverGuid)
        For Each info In soundDevices
            '//Create a device based on the GUID currently obtained from the //soundDevices collection
            '   Dim soundDevice As Device = New Device(info.DriverGuid)

            '//Make sure the device supports 16-bit samples of primary and secondary buffer support.
            '//Also, make sure we're using a 5:1 stereo setup.
            If (soundDevice.Caps.Secondary16Bit = True Or soundDevice.Caps.Primary16Bit = True Or soundDevice.SpeakerConfig.FivePointOne = True) Then
                '//Keep the sound device created and use the reference in your application...
            Else '//dispose of the device and continue querying the installed hardware...
                soundDevice.Dispose()
            End If
        Next

        '//The audio file to be loaded onto the secondary buffer.
        Dim strFile As String = FileName

        ' //Create a secondary buffer compatible with the sound device and load the file.
        '//soundDevice is the object created during the enumeration phase…
        Dim buffer As SecondaryBuffer = New SecondaryBuffer(strFile, soundDevice)

        '//Set the cooperative level of the sound device.
        soundDevice.SetCooperativeLevel(sControl, CooperativeLevel.Normal)

        '//Playback the data in the buffer normally. For other options,
        '//inspect the BufferPlayFlags enumeration.
        buffer.Play(0, BufferPlayFlags.Default)

        While (buffer.Status.Playing)
            ' //Wait on this buffer to complete before continuing
        End While
        ' //Free the resources in the buffer
        buffer.Dispose()
        buffer = Nothing

        '//Free the DirectSound device.
        soundDevice.Dispose()
        soundDevice = Nothing

    End Sub
    'The code below retrieves a list of all GameControl class devices and
    'returns the results in an enumerable DeviceList collection. Next, for
    'each instance in the list, we create a DirectInout device using the GUID
    'of the enumerated instance. The Caps structure is then consulted to
    'determine the number of buttons. Finally, ForceFeedback effects are
    'retrieved from the device by calling the GetEffects method on the input
    'device simply to show the ease of use of MDX's DirectInput API.

    '    //Get a list of all DirectInput devices that support ForceFeedback and are game controllers.
    'DeviceList devices = Manager.GetDevices( DeviceClass.GameControl, EnumDevicesFlags.AllDevices);
    'foreach (DeviceInstance dev in devices)
    '{
    '    string strName = dev.ProductName; //Example: "InterAct HammerHead Fx (USB)"

    '     //Create the actual device from the GUID.
    '    Device gamepadDevice = new Device( dev.ProductGuid);

    '    //Optionally, run the control panel for this joystick to allow the user to set it up.
    '    d.RunControlPanel();

    '    //How many buttons are supported?
    '    int numButtons = gamepadDevice.Caps.NumberButtons;

    '    //What ForceFeedback forces are available on this device?
    '    EffectList effects = gamepadDevice.GetEffects(EffectType.All);
    '}
End Class
Avatar of tealnet
tealnet

ASKER

It appears the issue may be more related to the codec.  I guess we need some sort of codec or way of reading a DSS file.  MP3 files don't seem to be a problem.
You might check with Olympus about software like maybe a SDK for Olympus DSS.
Did your Olympus DSS  come with any software?
Avatar of tealnet

ASKER

Olympus was not very helpful.  We ended up finding a freeware command line utility (http://www.nch.com.au/switch) to automatically convert the DSS file to MP3 then extract the length.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial