Thursday, January 28, 2010

ID3 tags for Wave files.

ID3 (Identify a MP3) is metadata container most often used for MP3 audio files. ID3 signature contains information about the title track, album, artist name, etc. about the file to be stored in the file itself. Please see ID3.

Wave files can also store metadata similar ID3.
The code below shows how to add, delete, and read metadata from the Wav files.

        private void WaveTag()

        {

            string fileName = "in.wav";

            WaveReadWriter wrw = new WaveReadWriter(File.Open(fileName, FileMode.Open, FileAccess.ReadWrite));

            //removes INFO tags from audio stream

            wrw.WriteInfoTag(null);

            //writes INFO tags into audio stream

            Dictionary<WaveInfo, string> tag = new Dictionary<WaveInfo, string>();

            tag[WaveInfo.Comments] = "Comments...";

            wrw.WriteInfoTag(tag);

            wrw.Close();

            //reads INFO tags from audio stream

            WaveReader wr = new WaveReader(File.OpenRead(fileName));

            Dictionary<WaveInfo, string> dir = wr.ReadInfoTag();

            wr.Close();

            if (dir.Count > 0)

            {

                foreach (string val in dir.Values)

                {

                    Console.WriteLine(val);

                }

            }

        }


In addition, we show how to do the same for mp3 files with ID3 Tag V1.0

        private void Mp3Tag()

        {

            string fileName = "in.mp3";

            Mp3ReadWriter mrw = new Mp3ReadWriter(File.Open(fileName, FileMode.Open, FileAccess.ReadWrite));

            //removes ID3v1 tags from audio stream

            mrw.WriteID3v1Tag(null);

            //writes ID3v1 tags into audio stream

            ID3v1 tag = new ID3v1();

            tag.Comment = "Comment...";

            mrw.WriteID3v1Tag(tag);

            mrw.Close();

            //reads ID3v1 tags from audio stream

            Mp3Reader mr = new Mp3Reader(File.OpenRead(fileName));

            ID3v1 id3v1 = mr.ReadID3v1Tag();

            mr.Close();

            if (id3v1 != null)

            {

                Console.WriteLine(id3v1.Comment);

            }

        }


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

Shout it

kick it on DotNetKicks.com

Wednesday, January 27, 2010

What is ACM driver?

Audio Compression Manager (ACM) is the Windows multimedia framework that manages audio codecs. Codec is a computer program that compresses/decompresses digital audio data according to a given audio file format or streaming audio format.
ACM driver is dynamic-link library (DLL) which contains audio codecs for different audio formats.
ACM drivers can be recognized by their filename extension ".acm". For more details please see Audio_Compression_Manager

The code below can be used to list ACM codecs installed on your computer and see audio formats for specified codec.

        private void WhatIsAcmDriver()

        {

            foreach (DriverDetails dd in AudioCompressionManager.GetDriverList())

            {

                Console.WriteLine("# # #");

                Console.WriteLine("Driver: {0}", dd.LongName);

                foreach (FormatTagDetails ftd in AudioCompressionManager.GetFormatTagList(dd.Driver))

                {

                    Console.WriteLine("FormatTag: {0}", ftd.FormatTagName);

                    foreach (FormatDetails fd in AudioCompressionManager.GetFormatList(ftd.FormatTag, dd.Driver))

                    {

                        Console.WriteLine("Format: {0}", fd.FormatName);

                    }

                }

            }

        }


Output for "Microsoft GSM 6.10 Audio CODEC" (for example) driver see below
# # #
Driver: Microsoft GSM 6.10 Audio CODEC
FormatTag: GSM 6.10
Format: 8,000 kHz; Mono
Format: 11,025 kHz; Mono
Format: 22,050 kHz; Mono
Format: 44,100 kHz; Mono
FormatTag: PCM
Format: 8,000 kHz; 8 Bit; Mono
Format: 8,000 kHz; 16 Bit; Mono
Format: 11,025 kHz; 8 Bit; Mono
Format: 11,025 kHz; 16 Bit; Mono
Format: 22,050 kHz; 8 Bit; Mono
Format: 22,050 kHz; 16 Bit; Mono
Format: 44,100 kHz; 8 Bit; Mono
Format: 44,100 kHz; 16 Bit; Mono
# # #

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

Shout it

kick it on DotNetKicks.com

Sunday, January 17, 2010

Communicate with aliens on IronPython (ending)

Previously, we are talking with aliens in C#, Nemerle, Boo and F#. Now let's try to do the same on IronPython.
See code below.

from System import *
from System.Collections.Generic import *
from System.Text import *
from Alvas.Audio import *

def rex_Open(sender, e):
    _play.OpenPlayer(_pcmFormat)
    _play.StartPlay()

def rex_Close(sender, e):
    _play.ClosePlayer()

def rex_Data(sender, e):
    data = AudioCompressionManager.Reverse(_pcmFormat, e.Data)
    _play.AddData(data)

_play = PlayerEx(True)
_pcmFormat = AudioCompressionManager.GetPcmFormat(
11644100)
_rex = RecorderEx(True)
_rex.Data += rex_Data
_rex.Open += rex_Open
_rex.Close += rex_Close
_rex.Format = _pcmFormat
_rex.StartRecord()
Console.WriteLine(
"Please press enter to exit!")
Console.ReadLine()
_rex.StopRecord()
    


Enjoy:)
The source code and precompiled examples are here. (AudioConsPy.zip)

kick it on DotNetKicks.com

Shout it

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

Tuesday, January 5, 2010

Communicate with aliens on F# (continued)

Previously, we are talking with aliens in C#, Nemerle and Boo. Now let's try to do the same on F#.
See code below.

#light

 

open System

open Alvas.Audio

 

 

let rex = new RecorderEx(true);

let play = new PlayerEx(true);

let pcmFormat = AudioCompressionManager.GetPcmFormat(int16 1, int16 16, 44100);

 

let rex_Data (_) (e : DataEventArgs) : unit =

    let data = AudioCompressionManager.Reverse(pcmFormat, e.Data);

    ignore(play.AddData(data));

let rex_DataHandler = new RecorderEx.DataEventHandler(rex_Data)

rex.Data.AddHandler(rex_DataHandler)

 

let rex_Open _ _ = play.OpenPlayer(pcmFormat);

                   play.StartPlay();

let rex_OpenHandler = new EventHandler(rex_Open)

rex.Open.AddHandler(rex_OpenHandler)

 

let rex_Close _ _ = play.ClosePlayer();

let rex_CloseHandler = new EventHandler(rex_Close)

rex.Close.AddHandler(rex_CloseHandler)

 

rex.Format <- pcmFormat;

rex.StartRecord();

Console.WriteLine("Please press enter to exit!");

ignore(Console.ReadLine());

rex.StopRecord();


Enjoy:)
The source code and precompiled examples are here. (AudioConsFs.zip)

kick it on DotNetKicks.com

Shout it

Monday, January 4, 2010

What is compressed audio format?

Audio compression is a form of data compression designed to reduce the transmission bandwidth requirement of digital audio streams and the storage size of audio files. Audio compression algorithms are implemented in computer software as audio codecs.
The trade-off between slightly reduced audio quality and transmission or storage size is outweighed by the latter for most practical audio applications in which users may not perceive the loss in playback rendition quality. For example, one compact disk (CD) holds approximately one hour of uncompressed (PCM) high fidelity music, less than 2 hours of music compressed losslessly, or 7 hours of music compressed in the MP3 format at medium bit rates.

Lets examine compressed audio format taking as the example of "GSM 6.10". This format is used to compress the mobile operators talking on mobile phones. GSM is used by over 3 billion people across more than 212 countries and territories.
AudioCompressionManager.GetWaveFormat can look inside the audio format.
FormatTag = 49 is "GSM 6.10" format.
Channels = 1 is mono.
SamplesPerSec = quantity of digitized values in the second (or sampling): 8000 Hz, 11025 Hz, 22050 Hz, 44100 Hz.
BitsPerSample = 0, not matter for this format.
BlockAlign = 65. Mean the size of the compressed block.
AvgBytesPerSec is byterate(Bitrate equal byterate * 8) = 1625. For "GSM 6.10 22050 Hz; Mono".
The code below can be used to see "GSM 6.10" audio format in more detail.

        private void WhatIsCompressedAudioFormat()

        {

            foreach (FormatDetails fd in AudioCompressionManager.GetFormatList(AudioCompressionManager.Gsm610FormatTag, true))

            {

                WaveFormat wf = AudioCompressionManager.GetWaveFormat(fd.FormatHandle);

                string format = "Format: [{0}], FormatTag: {1}, Channels: {2}, SamplesPerSec: {3}, BitsPerSample: {4}, BlockAlign: {5}, AvgBytesPerSec: {6}";

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

            }

        }


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

kick it on DotNetKicks.com

Shout it

How to determine the silence?

Question: I have to connect to a SHOUTcast stream, take 5 seconds of the sound then detect if there is silence (no sound above -14db). Would you be so kind to provide us with some sample?

Answer: See the code below

using System;

using System.Windows.Forms;

using System.Net;

using System.IO;

using Alvas.Audio;

 

namespace RadioCs

{

    public partial class frmMain : Form

    {

        public frmMain()

        {

            InitializeComponent();

            t.Interval = 15 * 60 * 1000;

            t.Tick += new EventHandler(t_Tick);

            t.Enabled = true;

        }

 

        Timer t = new Timer();

 

        void t_Tick(object sender, EventArgs e)

        {

            if (CheckSilent("http://www.wmph.org:7050/wmph64k", 10000, -14))

            {

                Console.WriteLine("Alert!");

            }

        }

 

        private static short dB2Short(double dB)

        {

            double times = Math.Pow(10, dB / 10);

            return (short)(short.MaxValue * times);

        }

 

        private bool CheckSilent(string StreamURL, int timeOut, double mindb)

        {

            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(StreamURL);

 

            Uri MyURI = new Uri(StreamURL);

 

            WebReq.Headers.Clear();

            WebReq.Timeout = timeOut;

            WebReq.Headers.Add("GET", MyURI.AbsolutePath + " HTTP/1.0");

            WebReq.UserAgent = "WinampMPEG/5.0";

            WebReq.Headers.Add("Icy-MetaData", "1");

            WebResponse WebRes = WebReq.GetResponse();

            Stream oReader = WebRes.GetResponseStream();

            int MetaInterval = int.Parse(WebRes.Headers["icy-metaint"]);

            byte[] data = new byte[MetaInterval];

            byte b = 0;

            int BlockCount = 0;

            int pass = 0;

            MemoryStream ms = new MemoryStream();

            try

            {

                while (true)

                {

 

                    b = (byte)oReader.ReadByte();

                    if (BlockCount == MetaInterval)

                    {

                        ms.Write(data, 0, data.Length);

                        pass += 1;

                        if (pass >= 5)

                        {

                            Mp3Reader mr = new Mp3Reader(ms);

                            IntPtr format = mr.ReadFormat();

                            short[] buffer = AudioCompressionManager.RecalculateData(format, ms.ToArray(), -1);

                            mr.Close();

                            int silentLevel = dB2Short(mindb);

                            for (int i = 0; i < buffer.Length; i++)

                            {

                                if (buffer[i] > silentLevel)

                                {

                                    return false;

                                }

                            }

                            return true;

                        }

                        int MetaDataLength = b * 16;

 

                        if (MetaDataLength > 0)//skip metadata

                        {

                            byte[] MetaArray = new byte[MetaDataLength];

                            oReader.Read(MetaArray, 0, MetaDataLength);

                        }

                        BlockCount = 0;

                        continue;

                    }

 

                    else

                    {

                        data[BlockCount] = b;

                    }

 

                    BlockCount = BlockCount + 1;

                }

            }

            finally

            {

                oReader.Close();

            }

        }

 

    }

}


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

kick it on DotNetKicks.com

Shout it