Friday, July 22, 2011

Console and multithreaded recording and playback

Question: How to use RecorderEx and PlayerEx in console application or Windows Service?

Answer: In order to use RecorderEx and PlayerEx in a console application or Windows Service need to create instances of these classes by using a parameterized constructor:
new RecorderEx(true) and new PlayerEx(true), respectively. The same is true for multi-threaded recorder and player.

Below is an example of full duplex Recorder and Player.

using System;

using Alvas.Audio;

 

namespace AudioConsCs

{

    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 = e.Data;

            play.AddData(data);

        }

    }

}

Below is an example of multithreaded full duplex Recorder and Player in Windows Service.

/*

 * rem run cmd as administrator

 * rem install service

 * \\Windows\Microsoft.NET\Framework\v2.0.50727\installutil AudioWindowsServiceCs.exe

 * rem uninstall service

 * \\Windows\Microsoft.NET\Framework\v2.0.50727\installutil AudioWindowsServiceCs.exe /u

 */

using System;

using System.ServiceProcess;

using System.Threading;

using Alvas.Audio;

 

namespace AudioWindowsServiceCs

{

    public partial class AudioService : ServiceBase

    {

        public AudioService()

        {

            InitializeComponent();

        }

 

        protected override void OnStart(string[] args)

        {

            Thread t = new Thread(Start);

            t.Start();

        }

 

        protected override void OnStop()

        {

            rex.StopRecord();

        }

 

        static void Start()

        {

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

            rex.Open += new EventHandler(rex_Open);

            rex.Close += new EventHandler(rex_Close);

            rex.Format = pcmFormat;

            rex.StartRecord();

        }

 

        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 = e.Data;

            play.AddData(data);

        }

    }

}


Download examples and Alvas.Audio Trial

Friday, July 15, 2011

Vox audio file format

Question: I found that your vox converter doesn't handle vox files from the Orator phone dictation system right.

Answer: There are so many questions about Vox format. What is the Vox format? Vox audio file format is a headerless audio format data, which generally contains Dialogic_ADPCM, but may also include A-law, Mu-law, PCM, and any other type of audio data.
It means that vox file doesn't have the header and extention ".vox" doesn't say anything at all. Therefore it is very important to know in which format you have the audio data in the vox file!

See code below for the different types of Vox files.

        //Dialogic_ADPCM

        private static void Vox2Wav(string voxFile, int samplesPerSec)

        {

            BinaryReader br = new BinaryReader(File.OpenRead(voxFile));

            IntPtr format = AudioCompressionManager.GetPcmFormat(1, 16, samplesPerSec);

            WaveWriter ww = new WaveWriter(File.Create(voxFile + ".wav"),

                AudioCompressionManager.FormatBytes(format));

            Vox.Vox2Wav(br, ww);

            br.Close();

            ww.Close();

        }

 

 

        private void VoxConv2(string voxFile)

        {

            //CCITT u-Law, 8000 Hz, 1 channels

            WaveFormat wf = new WaveFormat();

            wf.wFormatTag = AudioCompressionManager.MuLawFormatTag;

            wf.nChannels = 1;

            wf.nSamplesPerSec = 8000;

            FormatDetails[] formatList = AudioCompressionManager.GetFormatList(wf);

            IntPtr format = formatList[0].FormatHandle;

            RawReader rr = new RawReader(File.OpenRead(voxFile), format);

            byte[] data = rr.ReadData();

            rr.Close();

            WaveWriter ww = new WaveWriter(File.Create(voxFile + ".wav"),

                AudioCompressionManager.FormatBytes(format));

            ww.WriteData(data);

            ww.Close();

        }

 

        static void Raw2Wav(string fileName)

        {

            //A-Law mono at 8000 HZ

            WaveFormat wf = new WaveFormat();

            wf.wFormatTag = AudioCompressionManager.ALawFormatTag;

            wf.nChannels = 1;

            wf.nSamplesPerSec = 8000;

            FormatDetails[] formatList = AudioCompressionManager.GetFormatList(wf);

            IntPtr format = formatList[0].FormatHandle;

            RawReader rr = new RawReader(File.OpenRead(fileName), format);

            byte[] data = rr.ReadData();

            rr.Close();

            WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"),

                AudioCompressionManager.FormatBytes(format));

            ww.WriteData(data);

            ww.Close();

        }


For more examples of working with vox files you can find on How to.. page. The search string "vox" gives at least 79 matches.