Friday, January 8, 2010

How can be one audio file mixed to another?

Question: I have multiple audio tracks with nothing but music.
I have multiple recorded voice tracks no longer than 2-3 seconds each.
What I would like to do is 'overlay' a voice track onto an audio
track at a specified time and output the results to a single file.
E.g. Audio track is 2 minutes long, and I would like to insert
the voice track at 23 seconds as well 1m 05 seconds and at 1m 40 seconds.
Can Alvas.Audio accomplish this sort of task for me??

Answer: See the code below

Imports System.IO

Imports Alvas.Audio

 

Public Class Form1

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

        MixMany("c:\audio\files\")

 

    End Sub

 

    Private Shared Sub Debug(ByVal name As String, ByVal wf As WaveFormat)

        Dim format As String = "Variable: [{0}], FormatTag: {1}, Channels: {2}, SamplesPerSec: {3}, BitsPerSample: {4}, BlockAlign: {5}, AvgBytesPerSec: {6}"

        Console.WriteLine(format, name, wf.wFormatTag, wf.nChannels, wf.nSamplesPerSec, wf.wBitsPerSample, wf.nBlockAlign, wf.nAvgBytesPerSec)

    End Sub

 

    Private Shared Sub MixMany(ByVal dir As String)

 

        Dim format1 As IntPtr

        Dim data1 As Byte()

 

        ReadData(dir + "The Friendship Song.mp3", format1, data1)

 

        Dim format2 As IntPtr

        Dim data2 As Byte()

 

        ReadData(dir + "Kailin 1.mp3", format2, data2)

 

        Dim wf1 As WaveFormat = AudioCompressionManager.GetWaveFormat(format1)

        Debug("wf1", wf1)

        Dim wf2 As WaveFormat = AudioCompressionManager.GetWaveFormat(format2)

        Debug("wf1", wf2)

 

        If Not wf1.Equals(wf2) Then

            data2 = AudioCompressionManager.Convert(format2, format1, data2, False)

        End If

 

        Dim data As Byte() = AudioCompressionManager.MixMany(format1, data1, data2, wf1.nSamplesPerSec * 27.1, wf1.nSamplesPerSec * 83.28)

        Dim formatMp3 As IntPtr = AudioCompressionManager.GetCompatibleFormat(format1, AudioCompressionManager.MpegLayer3FormatTag)

        Debug("wfMp3", AudioCompressionManager.GetWaveFormat(formatMp3))

 

        Dim dataMp3 As Byte() = AudioCompressionManager.Convert(format1, formatMp3, data, False)

 

        Dim mw As New Mp3Writer(File.Create(dir + "Test002.mp3"))

        mw.WriteData(dataMp3)

        mw.Close()

 

        MsgBox("Done!")

 

    End Sub

 

    Private Shared Sub ReadData(ByVal fileName As String, ByRef format1 As IntPtr, ByRef data1 As Byte())

        Dim dr1 As New Mp3Reader(File.OpenRead(fileName))

        format1 = dr1.ReadFormat()

        data1 = dr1.ReadData()

        dr1.Close()

        Dim format1Pcm As IntPtr = AudioCompressionManager.GetCompatibleFormat(format1, AudioCompressionManager.PcmFormatTag)

        Dim data1Pcm() As Byte = AudioCompressionManager.Convert(format1, format1Pcm, data1, False)

        format1 = format1Pcm

        data1 = data1Pcm

    End Sub

 

End Class


Audio Library needed for this example is here(Alvas.Audio Free Trial).

kick it on DotNetKicks.com

Shout it

No comments:

Post a Comment