Live Support Skype: alexander_vasilevsky

New! Alvas.Audio

   Alvas.FileControls

   Alvas.ShapeForms

   Alvas.Scriptor

   Alvas.Suite

   Alvas.Controls

   Alvas.Labels

Alvas.Net - .Net developers toolbox
EnglishGermanFrenchSpanishJapaneseRussianPortugueseItalianKorean
Translate by Google
  Overview    News    Articles    How to..    Order    Support    Free Download    Demo       PR    Get Beta    PAD  
Click here to print this page news RSS feed

Articles about Alvas.Audio

1. How to save audio to mp3 on Silverlight 4.
2. ID3 tags for Wave files.
3. What is ACM driver?
4. How can be one audio file mixed to another?
5. What is compressed audio format?
6. What is a PCM format?
7. What is a Wav-file?

1. How to save audio to mp3 on Silverlight 4.

As you know Silverlight 4 supports access to the microphone. Besides it can call COM objects on the client side.

To save the audio in mp3 format we need:
  1. Make the COM object wrapper, which allows us to invoke the necessary Alvas.Audio functionality.
    1. Create AlvasCom project as ClassLibrary.
    2. In AssemblyInfo.cs change the line

      [assembly: ComVisible(false)]

      to

      [assembly: ComVisible(true)]

    3. Add a reference to Alvas.Audio.dll
    4. Add Alvas.Audio-wrapper interface and its implementation. See the code below.
      namespace AlvasCom
      {
       
          [Guid("50B29476-F2C0-4445-B0E1-7D4023453EBD")]
          interface IAudioHost
          {
              void Close();
              void WriteData(short channels, short bitsPerSample, int samplesPerSec, byte[] sampleData);
          }
       
          [ClassInterface(ClassInterfaceType.None)]
          [Guid("1EA86BAE-420F-4309-BF06-23730718BF5E")]
          public class AudioHost : IAudioHost
          {
              Mp3Writer mw;
       
              public AudioHost()
              {
                  string dir = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
                  string fileName = Path.ChangeExtension(Path.GetFileName(Path.GetTempFileName()), ".mp3");
                  string path = Path.Combine(dir, fileName);
                  global::System.Windows.Forms.MessageBox.Show(path);
                  mw = new Mp3Writer(File.Create(path));
              }
       
              public void WriteData(short channels, short bitsPerSample, int samplesPerSec, byte[] sampleData)
              {
                  IntPtr sampleFormat = AudioCompressionManager.GetPcmFormat(channels, bitsPerSample, samplesPerSec);
                  IntPtr format = AudioCompressionManager.GetCompatibleFormat(sampleFormat, AudioCompressionManager.MpegLayer3FormatTag);
                  byte[] data = AudioCompressionManager.Convert(sampleFormat, format, sampleData, false);
                  mw.WriteData(data);
              }
       
              public void Close()
              {
                  mw.Close();
              }
       
          }
      }
    5. Sign the assembly with the snk file.
    6. Register in GAC AlvasCom.dll and Alvas.Audio.dll using gacutil.
    7. Register the COM object using regasm.

    You can find AlvasCom.bat in the "SilverlightAudio\AlvasCom\bin\Debug\" folder to register these libraries.

  2. Make the Silverlight application with microphone support.
    1. Create SilverlightAudio project as SilverlightApplication.
    2. To call a COM object, we need elevated privileges and run it out of browser. Add to OutOfBrowserSettings.xml
        <OutOfBrowserSettings.SecuritySettings>
          <SecuritySettings ElevatedPermissions="Required" />
        </OutOfBrowserSettings.SecuritySettings>
    3. Writing code call using the "dynamic" type
      namespace SilverlightAudio
      {
          public class MyAudioSync : AudioSink
          {
              dynamic ah;
              AudioFormat audioFormat;
       
              protected override void OnCaptureStarted()
              {
                  ah = ComAutomationFactory.CreateObject("AlvasCom.AudioHost");
              }
       
              protected override void OnCaptureStopped()
              {
                  ah.Close();
              }
       
              protected override void OnFormatChange(AudioFormat audioFormat)
              {
                  this.audioFormat = audioFormat;
              }
       
              protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
              {
                  ah.WriteData((short)audioFormat.Channels, (short)audioFormat.BitsPerSample, audioFormat.SamplesPerSecond, sampleData);
              }
          }
      }
    4. Compile and run the application.
    5. Enjoy.
The source code and precompiled examples are here. (
SilverlightAudio.zip)

How can we extend this example? Come immediately to mind
  1. Save mp3 file on the server.
  2. Make voice chat, but it is better to use gsm instead of mp3 encoding. How to do it - leave it in the form of homework. Hint #tip60 :)
  3. And what can you propose?
P.S. If you have any problems - please write us.
kick it on DotNetKicks.com Shout it
Up

2. 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);

            }

        }


Up

3. 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
# # #

Up

4. 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


Up

5. What is compressed audio format?

Audio compression is a kind of data compression developed to reduce the transmission bandwidth of digital sound streams and the size of audio files. Audio compression algorithms are applied in computer software as audio codecs. The compromise between a little reduced sound quality and transmission or size is outweighed by the last for most practical audio apps in which users may not feel the loss in playback rendition quality.
For instance, one compact disk (CD) contains about one hour of uncompressed (PCM) high quality music, less than 2 hours of music compressed without loss in playback, or 7 hours of music compressed in the MP3 format at medium bit rates.

Lets analyse compressed audio format, for instance, "GSM 6.10". This format is applied to compress the mobile operators speaking on cellular. GSM is applied for over 3 billion people in more than 212 countries and territories.

AudioCompressionManager.GetWaveFormat can analyse the audio format inside.
  • FormatTag = 49 is "GSM 6.10" format.
  • Channels = 1 is single-channel.
  • SamplesPerSec = quantity of digitized values per second (or sampling): 8000 Hz, 11025 Hz, 22050 Hz, 44100 Hz.
  • BitsPerSample = 0, unimportant for this format.
  • BlockAlign = 65. The size of the compressed block (in bytes).
  • AvgBytesPerSec is byterate(Bitrate equal byterate * 8) = 1625. For "GSM 6.10 22050 Hz; Mono".
You can use the code below to look through "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);

            }

        }


Up

6. What is a PCM format?

  • FormatTag = 1 is PCM.
  • Channels = for single-channel (mono), two-channel (stereo), 8 for 7.1 Surround Sound (left, right, center, left surround, right surround, left rear, right rear positions. 7.1 systems also have 1 channel for low frequency effects (LFE) which is usually sent to a subwoofer).
  • SamplesPerSec = digitized quantity values per second (or sampling). Could be anything, but the standard values: 8000 Hz, 11025 Hz, 12000 Hz, 16000 Hz, 22050 Hz, 24000 Hz, 32000 Hz, 44100 Hz, 48000 Hz.
  • BitsPerSample - most common uses 8 bits (1 byte) and 16 bits (2 bytes). Rarely 24 bits (3 bytes), 32 bits (4 bytes) and 64 bits (4 bytes). If we consider the 16 bit as a basic, then 8 bits can be considered as a compressed format. It has two times less size, but the variants of values can be only 28 = 256 instead of 216 = 65536 for 16 bits. That's why, 8 bits sound quality will be significantly lower than 16 bits.
  • BlockAlign = Channels * BitsPerSample / 8. Where 8 is the quantity of bits per byte.
  • AvgBytesPerSec (bitrate) = Channels * SamplesPerSec * BitsPerSample / 8.
You can use the code below to analyze PCM audio format in more particularly.

        private void WhatIsPcmFormat(string fileName)

        {

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

            IntPtr format = wr.ReadFormat();

            wr.Close();

            WaveFormat wf = AudioCompressionManager.GetWaveFormat(format);

            if (wf.wFormatTag == AudioCompressionManager.PcmFormatTag)

            {

                int bitsPerByte = 8;

                Console.WriteLine("Channels: {0}, SamplesPerSec: {1}, BitsPerSample: {2}, BlockAlignIsEqual: {3}, BytesPerSecIsEqual: {4}", wf.nChannels, wf.nSamplesPerSec, wf.wBitsPerSample, (wf.nChannels * wf.wBitsPerSample) / bitsPerByte == wf.nBlockAlign, (int)(wf.nChannels * wf.nSamplesPerSec * wf.wBitsPerSample) / bitsPerByte == wf.nAvgBytesPerSec);

            }

        }


Up

7. What is a Wav-file?

WAVE (WAV) is a container format for recording the digitized audio stream. Under Windows, this format is often applied as a wrapper for uncompressed audio (PCM). But, you can put sound in WAV container compressed perhaps in any codec.
In that way your WAV-file can maintain data in various audio formats, such as the CCITT A-Law, CCITT u-Law, ADPCM, Speex, GSM 6.10, MPEG Layer-3 (mp3), Ogg Vorbis and others.
You can use the code below to investigate audio-format data recorded in your WAV-file.

        private void WhatIsWavFile(string fileName)

        {

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

            IntPtr format = wr.ReadFormat();

            wr.Close();

            WaveFormat wf = AudioCompressionManager.GetWaveFormat(format);

            string tag = null;

            switch (wf.wFormatTag)

            {

                case AudioCompressionManager.PcmFormatTag :

                    tag = "PCM";

                    break;

                case AudioCompressionManager.ALawFormatTag :

                    tag = "A-Law";

                    break;

                case AudioCompressionManager.MuLawFormatTag :

                    tag = "Mu-Law";

                    break;

                case AudioCompressionManager.Gsm610FormatTag :

                    tag = "GSM 6.10";

                    break;

                case AudioCompressionManager.ImaAdpcmFormatTag :

                    tag = "IMA ADPCM";

                    break;

                case AudioCompressionManager.AdpcmFormatTag :

                    tag = "Microsoft ADPCM";

                    break;

                case AudioCompressionManager.MpegLayer3FormatTag :

                    tag = "ISO/MPEG Layer3";

                    break;

                default:

                    tag = wf.wFormatTag.ToString();

                    break;

            }

            Console.WriteLine("File '{0}' contains '{1}' data", fileName, tag);

        }


Up
Blog :: Contact Us :: Forum :: Copyright © 1998-2010 Alvas.Net. All rights reserved
Ask a Question on alvasnet