Monday, December 28, 2009

Communicate with aliens on Boo (continued)

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

namespace AudioConsBoo

 

import System

import Alvas.Audio

 

class Program:

 

    static def Main():

        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()

 

    static rex = RecorderEx(true)

 

    static play = PlayerEx(true)

 

    static pcmFormat as IntPtr = AudioCompressionManager.GetPcmFormat(1, 16, 44100)

 

    static def rex_Open(sender as object, e as EventArgs):

        play.OpenPlayer(pcmFormat)

        play.StartPlay()

 

    static def rex_Close(sender as object, e as EventArgs):

        play.ClosePlayer()

 

    static def rex_Data(sender as object, e as DataEventArgs):

        data as (byte) = AudioCompressionManager.Reverse(pcmFormat, e.Data)

        play.AddData(data)

 

Program.Main()


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

kick it on DotNetKicks.com

Shout it

Communicate with aliens on Nemerle (continued)

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

using System;

using System.Console;

using Nemerle.Utility;

using Alvas.Audio;

 

module Program

{

  Main() : void

  {

      rex.Data += RecorderEx.DataEventHandler(rex_Data);

      rex.Open += EventHandler(rex_Open);

      rex.Close += EventHandler(rex_Close);

      rex.Format = pcmFormat;

      rex.StartRecord();

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

      _ = ReadLine();

      rex.StopRecord();

  }

 

  rex : RecorderEx = RecorderEx(true);

  play : PlayerEx  = PlayerEx(true);

  pcmFormat : IntPtr  = AudioCompressionManager.GetPcmFormat(1, 16, 44100);

 

  rex_Open(_ : object, _ : EventArgs) : void

  {

      play.OpenPlayer(pcmFormat);

      play.StartPlay();

  }

 

  rex_Close(_ : object , _ : EventArgs) : void

  {

      play.ClosePlayer();

  }

 

  rex_Data(_ : object, e : DataEventArgs) : void

  {

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

      play.AddData(data);

  } 

}


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

kick it on DotNetKicks.com

Shout it

Inspired by Vladimir Putin's New Year address to the aliens.

Did you see Vladimir Putin's New Year address to aliens last year?


Now you can communicate with the aliens. I took the code from: I would like to know if it is possible to record both line-in and line-out from a soundcard (full duplex).
Changed it a little. See code below.

    class Program

    {

 

        static void Main(string[] args)

        {

            rex.Data += new RecorderEx.DataEventHandler(rex_Data);

            rex.Open += new EventHandler(rex_Open);

            rex.Close += new EventHandler(rex_Close);

            rex.Format = pcmFormat;

            rex.StartRecord();

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

            Console.ReadLine();

            rex.StopRecord();

        }

 

        static RecorderEx rex = new RecorderEx(true);

        static PlayerEx play = new PlayerEx(true);

        static IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 44100);

 

        static void rex_Open(object sender, EventArgs e)

        {

            play.OpenPlayer(pcmFormat);

            play.StartPlay();

        }

 

        static void rex_Close(object sender, EventArgs e)

        {

            play.ClosePlayer();

        }

 

        static void rex_Data(object sender, DataEventArgs e)

        {

            byte[] data = AudioCompressionManager.Reverse(pcmFormat, e.Data);

            play.AddData(data);

        }

    }


Compile. Run. Wear earphones with a microphone. Now try to say something in the microphone ?!.. Enjoy :)

The source code and precompiled examples are here. (AudioConsCs.zip)

kick it on DotNetKicks.com

Shout it

What is a PCM format?

PCM (Pulse-code modulation) is an uncompressed audio format. We take the Wav file, which contains the PCM data. How do see What is a Wave file? Method AudioCompressionManager.GetWaveFormat can look inside the audio format.
FormatTag = 1 for PCM.
Channels = 1 for mono, 2 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 LFE (low frequency effects) which is usually sent to a subwoofer).
SamplesPerSec = number of digitized values in the 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 base, then 8 bits can be considered as a compressed format. It requires two times less space, but the options values can be only 28 = 256 instead of 216 = 65536 for 16 bits. Therefore, 8 bits sound quality will be significantly lower than 16 bits.
BlockAlign = Channels * BitsPerSample / 8. Where 8 is the number of bits per byte.
AvgBytesPerSec (bitrate) = Channels * SamplesPerSec * BitsPerSample / 8.
The code below can be used to see PCM audio format in more detail.

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

            }

        }


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



kick it on DotNetKicks.com

Shout it

Sunday, December 27, 2009

What is a Wave file?

WAV (WAVE) is a container format for storing records of the digitized audio stream. Under Windows, this format is often used as a wrapper for uncompressed audio (PCM). However, in WAV container you can put sound, compressed almost in any codec.
Thus your WAV-file can contain data in different audio formats, such as the IMA ADPCM, Microsoft ADPCM, CCITT A-Law, CCITT u-Law, GSM 6.10, MPEG Layer-3 (mp3) and others.
Code below, you can use to test audio-format data stored in your WAV-file.

        private void WhatIsWaveFile(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);

        }


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

kick it on DotNetKicks.com

Shout it