Question

I have a bit of a problem : in an winForm app in VS2010 win 7 compiling to x86, I try to do what Alvas.Audio seems to work. See (c# ex: http://alvas.net/alvas.audio,tips.aspx#tip94) for reference.

 Dim data() As Byte = wr.ReadData(second * i, second)

The result give me data.length()=0. I do not have any exception, I can read format from it and whatever reader I use I got this problem.

EDIT : After some tests, it seems like the uncompressed file I create in the first step (in PCM format, with .wav extension) can not be recognized by the Alvas.audio library for the second step. I must miss something around Audio file markups or something alike.

Here is the code that might be the source (basically this is step 1):

    Dim functOut As String = String.Empty
    Dim wr As Alvas.Audio.IAudioReader = Nothing
    Dim fs As IO.FileStream = Nothing
    Dim i As Integer = 0
    Dim tmpData() As Byte = Nothing
    Dim dataPCM() As Byte = Nothing
    Dim newFormat As IntPtr = IntPtr.Zero
    Try
        Select Case IO.Path.GetExtension(filename).ToLower()
            Case ".wav"
                wr = New Alvas.Audio.WaveReader(IO.File.OpenRead(filename))
                filename = IO.Path.GetTempPath & IO.Path.GetFileNameWithoutExtension(filename) & "2" & IO.Path.GetExtension(filename)
            Case ".mp3"
                wr = New Alvas.Audio.Mp3Reader(IO.File.OpenRead(filename))
            Case Else : wr = New Alvas.Audio.DsReader(filename)
        End Select

        functOut = IO.Path.ChangeExtension(filename, ".wav")

        Dim format As IntPtr = wr.ReadFormat()
        Dim formatDetail As Alvas.Audio.WaveFormat = Alvas.Audio.AudioCompressionManager.GetWaveFormat(format)


        If formatDetail.wFormatTag = Alvas.Audio.AudioCompressionManager.MpegLayer3FormatTag Then
            Alvas.Audio.AudioCompressionManager.Mp3ToWav(filename, functOut)
            Return True
        Else
            IO.File.Create(functOut).Close()
            While True
                tmpData = wr.ReadData(SECONDS * i, SECONDS)
                If tmpData Is Nothing Or tmpData.Length = 0 Then Exit While

                If formatDetail.wBitsPerSample < 16 Then
                    Alvas.Audio.AudioCompressionManager.ToPcm16Bit(format, tmpData, newFormat, dataPCM)
                Else 
                    newFormat = format
                    dataPCM = tmpData
                End If

                formatDetail = Alvas.Audio.AudioCompressionManager.GetWaveFormat(newFormat)

                fs = IO.File.Open(filename, IO.FileMode.Append, IO.FileAccess.Write)
                Using ww As New Alvas.Audio.WaveWriter(fs, Alvas.Audio.AudioCompressionManager.FormatBytes(newFormat))
                    ww.Write(dataPCM, 0, dataPCM.Length())
                End Using

                i += 1
                tmpData = Nothing
            End While
            Return True
        End If

        wr.Close()
    Catch ex As Exception
        filename = String.Empty
        Throw ex
    Finally
        fs.Close()
        wr.Close()
        filename = functOut
        GC.Collect()
    End Try

How can I write the resulted stream to be sure I can read it again later?

Any ideas will be great appreciated.

Was it helpful?

Solution

I found a solution.

PCM files are headerless. So when saved, even with waveWriter and the line Alvas.Audio.AudioCompressionManager.FormatBytes(newFormat) was ignored.

Two things can be done:

  • find a reader that recognize headerless files and allows to read from it.

  • refactor code to decode into PCM file and encode it into the new format (e.g .mp3) without writing the resulted file into File system (much better)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top