New! Alvas.Audio

   Alvas.FileControls

   Alvas.ShapeForms

   Alvas.Controls

   Alvas.Scriptor

   Alvas.Labels

   Alvas.Suite

Alvas.Net - .Net developers toolbox
SiteHeart
  Overview    News    FAQ    Order    Support    Free Download(638 Kb)    Online Help    Awards    PAD  
Click here to add this page to your favorites Click here to print this page RSS

Your Questions

1. Is it possible to merge several audio (wave) files that I have on disk and produce a single audio wave file?
2. I want to read an existing wav file, change the format then write it back out.
3. If I buy Single License for Alvas.Audio, will there be no 'about window' on deployed customer apllication whenever he start's application?
4. Does your .net component require any dependency file?
5. Is it runtime license free?
6. Do you provide some c# code with the classes?
7. I would like to know under what .Net language is the Audio application written?
8. Can you please confirm that if I buy a single user license for Alvas Suite, then it also includes Alvas.Audio?
9. Can we use Alvas.Audio for Pocket PC applications?
10. It appears to be necessary to select input and output devices. I would like to simply use the default devices that the user has selected on the control panel. Is this possible?
11. I need to be able to convert Dialogic files to MP3
12. Can you send me the complete list of all audio formats that can be converted FROM and converted TO using Alvas.Audio?
13. I want to decode a GSM-stream, but after a quick glance in the example-code and at Alvas.Audio.chm I can't really see how to do that.
14. I'm using the Alvas.Audio.dll, and i'm trying to play 2 wav's at the same time (I don't want the first one to stop when the second one begins). How can I make this happen?
15. Is this control suitable for use in MC++ Windows forms projects?
16. Would your audio compenent be suitable for Voip, would it decode and encode fast enough to keep latency at a minimum. Has it been used in this type of inverionement.
17. Can I use your audio toolkit to extract portions of a wav file to a new wav file?
18. Have you tested the audio on Vista?
19. Is it possible to insert new recordings into existing WAV files?
20. What does a single license entitle us to do, and how does it affect sales of software produced that include Alvas.Audio.
21. How do I stop (not pause) audio that's allready playing in command?
22. I would like to use your audio control for VoIP. Record to memory (Speex) and send the data by UDP the receive from UDP and playback raw data. Would your control be suitable for this purpose?
23. Using either the WaveReader or Player(Ex) classes, is there anyway for me to tell how long (in seconds) a WAV is?
24. I'm looking for a way via a web page to convert a saved Wav file to MP3. Will your code allow me to do this?
25. I just need to get the duration in MS for a file. This code will be on an ASP.NET website, so I want to instance your class, assign a file name (NOT PLAY THE FILE) and then just get the duration. Is this possible with your Alvas.Audio classes - if so, which class does it?
26. I am developing an application in VB.net 2.0, and am looking for a library to transform u-law audio files into mp3. Could I get some sample code to help me get this to work?
27. Is it possible to default to a particular format on any machine (e.g. 'MP3, 20kbps, 11,025Hz, mono') without the user having to specify it? I need to default to a low quality mp3 format for speech recording and I don't want the user to have to select anything. I want to make it as simple as possible for them. Is it reliable to just iterate through the FormatTags and select the right one based on matching a string? Or will different client machines have different values/indexes for these tags?
28. I need to be able to convert from ADPCM 6khz 4-bit mono, ADPCM 8khz 4-bit mono, PCM8 8khz 8-but uLaw mono formats and standard formats such as WAV.
29. Must I pay a license for every deploy on my customer or only one for developer?
30. Can you tell me whether the player will support playing Dialogic .vox (adpcm) format?
31. Can you convert WAV files to the headerless formats like Dialogic VOX and PCM 8khz?
32. It can play from an array of doubles?
33. I was evaluating your product to use in an application for Asterisk(VOIP) solution, but I found there is no output support for SLINEAR. Do you plan to support this format? This would be a great option since there is no Windows application to make the conversion for Asterisk.
34. How to convert a wave file into an raw alaw 8Khz mono file?
35. We need to be able to essentially convert a wav file while adjusting its speed by a factor.
36. Does the Alvas.Audio library work with Terminal Services?
37. How to send audio stream in the browser?
38. How to Play big audio files?
39. Simple RecorderEx example
40. Simple PlayerEx example


Answers

1. Is it possible to merge several audio (wave) files that I have on disk and produce a single audio wave file?

Yes, of course. If You want to join 2 files their formats should be identical. For Example You can join "CCITT u-Law 8 bit mono" with "CCITT u-Law 8 bit mono". Thus if You have "CCITT u-Law 16 bit mono" You have to convert it to "CCITT u-Law 8 bit mono" (use AudioCompressionManager.Convert method). Please see example below.

        private void Join()

        {

            string path = Application.StartupPath;

            WaveReader wr1 = new WaveReader(File.OpenRead(path + @"\one.wav"));

            IntPtr format = wr1.ReadFormat();

            byte[] data1 = wr1.ReadData();

            wr1.Close();

            WaveReader wr2 = new WaveReader(File.OpenRead(path + @"\two.wav"));

            byte[] data2 = wr2.ReadData();

            wr2.Close();

            WaveWriter ww = new WaveWriter(File.Create(path + @"\join.wav"), AudioCompressionManager.FormatBytes(format));

            ww.WriteData(data1);

            ww.WriteData(data2);

            ww.Close();

        }


Up
2. I want to read an existing wav file, change the format then write it back out.

        private void Convert()

        {

            WaveReader read = new WaveReader(File.OpenRead(@"g:\~data\123.wav"));

            IntPtr oldFormat = read.ReadFormat();

            WaveFormat wf = AudioCompressionManager.GetWaveFormat(oldFormat);

            byte[] oldData = read.ReadData();

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

            IntPtr newFormat = fdArr[fdArr.Length - 1].FormatHandle;

            byte[] data = AudioCompressionManager.Convert(oldFormat, newFormat, oldData, true);

            WaveWriter write = new WaveWriter(File.OpenWrite(@"g:\~data\123_new.wav"),

            AudioCompressionManager.FormatBytes(newFormat));

            write.WriteData(data);

            write.Close();

            read.Close();

        }


Up
3. If I buy Single License for Alvas.Audio, will there be no 'about window' on deployed customer apllication whenever he start's application?

Yes, of course. The 'about window' doesn't appear when You buy the product.

Up
4. Does your .net component require any dependency file?

No, our components don't require any dependency file.

Up
5. Is it runtime license free?

Yes, the runtime license is free.

Up
6. Do you provide some c# code with the classes?

Yes, the source code is available in Team edition of the product.

Up
7. I would like to know under what .Net language is the Audio application written?

Controls are 100% native CLR compliant written in C# and can be used in C#, VB.Net and other .Net languages.

Up
8. Can you please confirm that if I buy a single user license for Alvas Suite, then it also includes Alvas.Audio?

Yes, of course. If You buy Alvas Suite it will include Alvas.Audio.

Up
9. Can we use Alvas.Audio for Pocket PC applications?

No, Alvas.Audio can't be used for Pocket PC applications.

Up
10. It appears to be necessary to select input and output devices. I would like to simply use the default devices that the user has selected on the control panel. Is this possible?

Yes, it is possible. You need to assign -1 to RecorderID. Example, recEx.RecorderID = -1;

Up
11. I need to be able to convert Dialogic files to MP3

     string voxFile = @"C:\AlvasAudio\8000.vox";

     string mp3File = @"C:\AlvasAudio\8000.mp3";

     Vox2Mp3(voxFile, 1, 16, 8000, mp3File);

     //-------

     private static void Vox2Mp3(string voxFile, short channels, short bitsPerSample, int samplesPerSec, string mp3File)

        {

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

           MemoryStream pcmStream = new MemoryStream();

           IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(channels, bitsPerSample, samplesPerSec);

           WaveWriter ww = new WaveWriter(pcmStream, AudioCompressionManager.FormatBytes(pcmFormat));

           Vox.Vox2Wav(br, ww);

           br.Close();

           IntPtr mp3Format = AudioCompressionManager.GetCompatibleFormat(pcmFormat, AudioCompressionManager.MpegLayer3FormatTag);

           WaveReader wr = new WaveReader(pcmStream);

           byte[] pcmData = wr.ReadData();

           wr.Close();

           byte[] data = AudioCompressionManager.Convert(pcmFormat, mp3Format, pcmData, false);

           BinaryWriter bw = new BinaryWriter(File.Create(mp3File));

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

           ww.Close();

           bw.Close();

        }


Up
12. Can you send me the complete list of all audio formats that can be converted FROM and converted TO using Alvas.Audio?


Up
13. I want to decode a GSM-stream, but after a quick glance in the example-code and at Alvas.Audio.chm I can't really see how to do that.

To encode a GSM-stream You need to run AudioExCS.exe demo and select "audio format tag" GSM 6.10. To decode a GSM-stream You need to use the Conversion tab and select the file with GSM data...

Up
14. I'm using the Alvas.Audio.dll, and i'm trying to play 2 wav's at the same time (I don't want the first one to stop when the second one begins). How can I make this happen?

Instead of PlayerEx.AddData use PlayerEx.AddMixData.

Up
15. Is this control suitable for use in MC++ Windows forms projects?

Yes, Alvas.Audio is suitable for use in MC++ (with managed extension) .Net Windows forms projects. Requirements: .Net Framework v1.1 or later (also .NET v3.5)

Up
16. Would your audio compenent be suitable for Voip, would it decode and encode fast enough to keep latency at a minimum. Has it been used in this type of inverionement.

Yes, of course.

Up
17. Can I use your audio toolkit to extract portions of a wav file to a new wav file?

wr.ReadData(2, 1);

Up
18. Have you tested the audio on Vista?

Yes, of course.

Up
19. Is it possible to insert new recordings into existing WAV files?

Yes, you can insert audio data into existing WAV file.

Up
20. What does a single license entitle us to do, and how does it affect sales of software produced that include Alvas.Audio.

Single license allows one developer to use Alvas.Audio. If You have more developers in the project You need to purchase additional licenses. For example Team license is for 4 developers. Any kind of license gives You Royalty-Free redistribution rights with your applications.

Up
21. How do I stop (not pause) audio that's allready playing in command?

Please use playerEx.ClosePlayer() method.

Up
22. I would like to use your audio control for VoIP. Record to memory (Speex) and send the data by UDP the receive from UDP and playback raw data. Would your control be suitable for this purpose?


Up
23. Using either the WaveReader or Player(Ex) classes, is there anyway for me to tell how long (in seconds) a WAV is?

Look at WaveReader.GetDurationInMS.

Up
24. I'm looking for a way via a web page to convert a saved Wav file to MP3. Will your code allow me to do this?

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Alvas.Audio;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        ConvertAudio();

    }

 

    private void ConvertAudio()

    {

        string webFile = @"D:\AudioCS\bin\Debug\_web.mp3";

        string pcmFile = @"D:\AudioCS\bin\Debug\_AudioCS.wav";

        using (WaveReader wr = new WaveReader(File.OpenRead(pcmFile)))

        {

            IntPtr pcmFormat = wr.ReadFormat();

            byte[] pcmData = wr.ReadData();

            IntPtr webFormat = AudioCompressionManager.GetCompatibleFormat(pcmFormat,

            AudioCompressionManager.MpegLayer3FormatTag);

            byte[] webData = AudioCompressionManager.Convert(pcmFormat, webFormat,

            pcmData, false);

            MemoryStream ms = new MemoryStream();

 

            using (WaveWriter ww = new WaveWriter(ms,

            AudioCompressionManager.FormatBytes(webFormat)))

            {

                ww.WriteData(webData);

                using (WaveReader wr2 = new WaveReader(ms))

                {

                    using (FileStream fs = File.OpenWrite(webFile))

                    {

                        wr2.MakeMP3(fs);

                    }

                }

            }

        }

    }

}


Up
25. I just need to get the duration in MS for a file. This code will be on an ASP.NET website, so I want to instance your class, assign a file name (NOT PLAY THE FILE) and then just get the duration. Is this possible with your Alvas.Audio classes - if so, which class does it?

Yes. Please use

            string path = @"c:\test.wav";

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

            int durationInMS = wr.GetDurationInMS();

            wr.Close();


Up
26. I am developing an application in VB.net 2.0, and am looking for a library to transform u-law audio files into mp3. Could I get some sample code to help me get this to work?

        Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

            Dim inFile As String = "path_to_your_file.wav"

            Dim outFile As String = "path_to_output_file.mp3"

            Convert(inFile, outFile)

        End Sub

 

        Public Sub Convert(ByVal inFile As String, ByVal outFile As String)

            Dim inFs As FileStream = File.OpenRead(inFile)

            Dim wr As WaveReader = New WaveReader(inFs)

            Dim inFormat As IntPtr = wr.ReadFormat()

            Dim inData As Byte() = wr.ReadData

            Dim pcmFormat As IntPtr = AudioCompressionManager.GetCompatibleFormat(inFormat, AudioCompressionManager.PcmFormatTag)

            Dim pcmData As Byte() = AudioCompressionManager.Convert(inFormat, pcmFormat, inData, False)

            Dim mp3Format As IntPtr = AudioCompressionManager.GetCompatibleFormat(pcmFormat, AudioCompressionManager.MpegLayer3FormatTag)

            Dim mp3Data As Byte() = AudioCompressionManager.Convert(pcmFormat, mp3Format, pcmData, False)

            Dim outFs As MemoryStream = New MemoryStream()

            Dim ww As WaveWriter = New WaveWriter(outFs, AudioCompressionManager.FormatBytes(mp3Format))

            ww.WriteData(mp3Data)

            Dim outWr As WaveReader = New WaveReader(outFs)

            outWr.MakeMP3(File.Create(outFile))

            ww.Close()

        End Sub


Up
27. Is it possible to default to a particular format on any machine (e.g. 'MP3, 20kbps, 11,025Hz, mono') without the user having to specify it? I need to default to a low quality mp3 format for speech recording and I don't want the user to have to select anything. I want to make it as simple as possible for them. Is it reliable to just iterate through the FormatTags and select the right one based on matching a string? Or will different client machines have different values/indexes for these tags?

        //---

        private static IntPtr GetFormat()

        {

            WaveFormat wf = new WaveFormat();

            wf.wFormatTag = AudioCompressionManager.MpegLayer3FormatTag;

            wf.nChannels = 1;

            wf.nSamplesPerSec = 11025;

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

            foreach (FormatDetails fd in fdArr)

            {

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

                if (fdWf.nAvgBytesPerSec * 8 == 20000)

                {

                    return fd.FormatHandle;

                }

            }

            return IntPtr.Zero;

        }


Up
28. I need to be able to convert from ADPCM 6khz 4-bit mono, ADPCM 8khz 4-bit mono, PCM8 8khz 8-but uLaw mono formats and standard formats such as WAV.

Please see the code below for converting Your three sample audio files.

            Vox2Pcm(@"C:\AlvasAudio\8000.vox", @"C:\AlvasAudio\8000.wav", 8000);

            Vox2Pcm(@"C:\AlvasAudio\6000.vox", @"C:\AlvasAudio\6000.wav", 6000);

            uLaw(@"C:\AlvasAudio\8000.pcm", @"C:\AlvasAudio\8000.pcm.wav");

 

        //-------

        private static void uLaw(string inFile, string outFile)

        {

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

            byte[] data = new byte[br.BaseStream.Length];

            br.Read(data, 0, data.Length);

            WaveFormat wf = new WaveFormat();

            wf.nChannels = 1;

            wf.nSamplesPerSec = 8000;

            wf.wFormatTag = AudioCompressionManager.MuLawFormatTag;

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

            IntPtr format = fdArr[0].FormatHandle;

            WaveWriter ww = new WaveWriter(File.Create(outFile),

            AudioCompressionManager.FormatBytes(format));

            ww.WriteData(data);

        }

 

        private static void Vox2Pcm(string inFile, string outFile, int samplesPerSec)

        {

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

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

            WaveWriter ww = new WaveWriter(File.Create(outFile),

            AudioCompressionManager.FormatBytes(format));

            Vox.Vox2Wav(br, ww);

            br.Close();

            ww.Close();

        }


Up
29. Must I pay a license for every deploy on my customer or only one for developer?

You must pay a license for every developer in your team.

Up
30. Can you tell me whether the player will support playing Dialogic .vox (adpcm) format?


Up
31. Can you convert WAV files to the headerless formats like Dialogic VOX and PCM 8khz?

Please see code for Wav2Vox with resample

            Wav2Vox(@"C:\AlvasAudio\6000.wav", @"C:\AlvasAudio\6000.wav.vox", 8000);

 

 

        private static void Wav2Vox(string inFile, string outFile, int samplesPerSec)

        {

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

            IntPtr format = wr.ReadFormat();

            byte[] data = wr.ReadData();

            wr.Close();

            WaveFormat wf = AudioCompressionManager.GetWaveFormat(format);

            if (wf.wFormatTag != AudioCompressionManager.PcmFormatTag)//Decode if not PCM data

            {

                Decode2Pcm(ref format, ref data, ref wf);

            }

            if (wf.nSamplesPerSec != samplesPerSec)

            {

                Resample(ref format, ref data, ref wf, samplesPerSec);

            }

            BinaryWriter bw = new BinaryWriter(File.OpenWrite(outFile));

            BinaryReader br = new BinaryReader(new MemoryStream(data));

            Vox.Raw2Vox(br, bw, wf.wBitsPerSample);

            br.Close();

            bw.Close();

        }

 

        private static void Resample(ref IntPtr format, ref byte[] data, ref WaveFormat wf,

        int samplesPerSec)

        {

            foreach (FormatDetails fd in

            AudioCompressionManager.GetCompatibleFormatList(format))

            {

                if (fd.FormatTag == AudioCompressionManager.PcmFormatTag)

                {

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

                    if (wf.wBitsPerSample == wfFd.wBitsPerSample && wf.nChannels ==

                    wfFd.nChannels && wfFd.nSamplesPerSec == samplesPerSec)

                    {

                        byte[] buffer = AudioCompressionManager.Convert(format, fd.FormatHandle, data, false);

                        format = fd.FormatHandle;

                        data = buffer;

                        wf = wfFd;

                    }

                }

            }

        }

 

        private static void Decode2Pcm(ref IntPtr format, ref byte[] data, ref WaveFormat wf)

        {

            IntPtr newFormat = AudioCompressionManager.GetCompatibleFormat(format,

            AudioCompressionManager.PcmFormatTag);

            byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, false);

            wf = AudioCompressionManager.GetWaveFormat(newFormat);

            format = newFormat;

            data = buffer;

        }


Up
32. It can play from an array of doubles?

        PlayerEx pl = new PlayerEx();

 

        private static void PlayArray(PlayerEx pl)

        {

            double fs = 8000; // sample freq

            double freq = 1000; // desired tone

            short[] mySound = new short[4000];

            for (int i = 0; i < 4000; i++)

            {

                double t = (double)i / fs; // current time

                mySound[i] = (short)(Math.Cos(t * freq) * (short.MaxValue));

            }

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

            pl.OpenPlayer(format);

            byte[] mySoundByte = new byte[mySound.Length * 2];

            Buffer.BlockCopy(mySound, 0, mySoundByte, 0, mySoundByte.Length);

            pl.AddData(mySoundByte);

            pl.StartPlay();

        }


Up
33. I was evaluating your product to use in an application for Asterisk(VOIP) solution, but I found there is no output support for SLINEAR. Do you plan to support this format? This would be a great option since there is no Windows application to make the conversion for Asterisk.

Alvas.Audio supports SLINEAR files. See code below.

            Wav2Sln(@"C:\AlvasAudio\obrigada.wav", @"C:\AlvasAudio\obrigada.sln");

        //---

        private static void Wav2Sln(string inFile, string outFile)

        {

            IntPtr newFormat = AudioCompressionManager.GetPcmFormat(1, 16, 8000);

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

            IntPtr oldFormat = wr.ReadFormat();

            byte[] data = wr.ReadData();

            wr.Close();

            byte[] buffer = AudioCompressionManager.Convert(oldFormat, newFormat, data, false);

            using (BinaryWriter bw = new BinaryWriter(File.Create(outFile)))

            {

                bw.Write(buffer, 0, buffer.Length);

            }

        }


Up
34. How to convert a wave file into an raw alaw 8Khz mono file?

            string pcmFile = @"C:\AlvasAudio\obrigada.wav";

            string alawFile = @"C:\AlvasAudio\obrigada.alaw";

            Pcm2ALaw(pcmFile, alawFile);

        //---

        private static void Pcm2ALaw(string wavFile, string alawFile)

        {

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

            IntPtr format = wr.ReadFormat();

            byte[] data = wr.ReadData();

            wr.Close();

            WaveFormat wfAlaw = new WaveFormat();

            wfAlaw.wFormatTag = AudioCompressionManager.ALawFormatTag;

            wfAlaw.nChannels = 1;

            wfAlaw.nSamplesPerSec = 8000;

            FormatDetails[] fdArr = AudioCompressionManager.GetFormatList(wfAlaw);

            WaveFormat wf = AudioCompressionManager.GetWaveFormat(format);

            if (wf.wFormatTag != AudioCompressionManager.PcmFormatTag)//Decode if not PCM data

            {

                Decode2Pcm(ref format, ref data, ref wf);

            }

            if (wf.nSamplesPerSec != wfAlaw.nSamplesPerSec || wf.nChannels != wfAlaw.nChannels)

            {

                Resample(ref format, ref data, ref wf, wfAlaw.nSamplesPerSec, wfAlaw.nChannels);

            }

 

            byte[] buffer = AudioCompressionManager.Convert(format, fdArr[0].FormatHandle, data, false);

            BinaryWriter bw = new BinaryWriter(File.Create(alawFile));

            bw.Write(buffer, 0, buffer.Length);

            bw.Close();

        }

 

        private static void Resample(ref IntPtr format, ref byte[] data, ref WaveFormat wf, int samplesPerSec, short channels)

        {

            IntPtr newFormat = AudioCompressionManager.GetPcmFormat(channels, wf.wBitsPerSample, samplesPerSec);

            byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, true);

            format = newFormat;

            wf = AudioCompressionManager.GetWaveFormat(newFormat);

            data = buffer;

        }

 

        private static void Decode2Pcm(ref IntPtr format, ref byte[] data, ref WaveFormat wf)

        {

            IntPtr newFormat = AudioCompressionManager.GetCompatibleFormat(format,

            AudioCompressionManager.PcmFormatTag);

            byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, false);

            wf = AudioCompressionManager.GetWaveFormat(newFormat);

            format = newFormat;

            data = buffer;

        }


Up
35. We need to be able to essentially convert a wav file while adjusting its speed by a factor.

            ChangeSpeed(@"g:\AlvasAudio\obrigada.wav", @"g:\AlvasAudio\obrigada1.wav", 50);//two times as quickly

            ChangeSpeed(@"g:\AlvasAudio\obrigada.wav", @"g:\AlvasAudio\obrigada2.wav", 200);//two times as slowly

 

        public static void ChangeSpeed(string inFile, string outFile, int persent)

        {

            if (persent < 1)

            {

                throw new ArgumentOutOfRangeException("persent");

            }

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

            IntPtr inFormat = wr.ReadFormat();

            byte[] data = wr.ReadData();

            wr.Close();

            WaveWriter ww = new WaveWriter(File.Create(outFile), AudioCompressionManager.FormatBytes(inFormat));

            WaveFormat wf = AudioCompressionManager.GetWaveFormat(inFormat);

            WaveFormat wfPrev = wf;

            if (wf.wFormatTag != AudioCompressionManager.PcmFormatTag)//Decode if not PCM da

            {

                Decode2Pcm(ref inFormat, ref data, ref wf);

            }

            int samplesPerSecond = wf.nSamplesPerSec * persent / 100;

            IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(wf.nChannels, wf.wBitsPerSample, samplesPerSecond);

            byte[] buffer = AudioCompressionManager.Convert(inFormat, pcmFormat, data, false);

            if (!wfPrev.Equals(wf))

            {

                buffer = AudioCompressionManager.Convert(inFormat, ww.GetFormat(), buffer, false);

            }

            ww.WriteData(buffer);

            ww.Close();

        }


Up
36. Does the Alvas.Audio library work with Terminal Services?

Due to the way audio drivers/codecs are remapped using TS with Windows 2000, the library will not work through TS when connecting to a Windows 2000 Server. It works fine from the console.

Up
37. How to send audio stream in the browser?

    public static void ResponseAudioStream(HttpResponse Response, string voxFile, int samplesPerSec)

    {

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

        MemoryStream pcmStream = new MemoryStream();

 

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

        WaveWriter ww = new WaveWriter(pcmStream, AudioCompressionManager.FormatBytes(pcmFormat));

        Vox.Vox2Wav(br, ww);

        WaveReader wr = new WaveReader(pcmStream);

 

        byte[] pcmData = pcmStream.ToArray();

 

        br.Close();

        ww.Close();

        wr.Close();

        pcmStream.Close();

 

        Response.ContentType = "audio/x-wav";

        Response.BinaryWrite(pcmData);

        Response.End();

    }


Up
38. How to Play big audio files?

        private void btnPlay_Click(object sender, EventArgs e)

        {

            StartPlay(File.OpenRead(@"c:\audio\big.wav"));

        }

 

        public MainForm()

        {

            InitializeComponent();

            playEx.Done += new PlayerEx.DoneEventHandler(playEx_Done);

        }

 

        void playEx_Done(object sender, DoneEventArgs e)

        {

            ReadData();

        }

 

        private PlayerEx playEx = new PlayerEx();

        private WaveReader wr = null;

        private int pos = 0;

        private int seconds = 5;

        private int len = 0;

 

        public void StartPlay(Stream _stream)

        {

            if (playEx.State == DeviceState.Paused)

            {

                playEx.ResumePlay();

            }

            else

            {

                pos = 0;

                wr = new WaveReader(_stream);

                len = wr.GetDurationInMS() / 1000;

                IntPtr format = wr.ReadFormat();

                playEx.OpenPlayer(format);

                ReadData();

                playEx.StartPlay();

            }

        }

 

        private void ReadData()

        {

            if (pos <= len)

            {

                byte[] data = wr.ReadData(pos, seconds);

                pos += seconds;

                playEx.AddData(data);

            }

        }


Up
39. Simple RecorderEx example

using System;

using System.Windows.Forms;

using Alvas.Audio;

using System.IO;

 

namespace SimpleRecorder

{

    public partial class RecorderForm : Form

    {

        public RecorderForm()

        {

            InitializeComponent();

            Init();

        }

 

        private void Init()

        {

            btnStop.Enabled = false;

            rex = new RecorderEx();

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

            rex.Format = pcmFormat;//AudioCompressionManager.GetCompatibleFormat(pcmFormat, AudioCompressionManager.Gsm610FormatTag);

            rex.Open += new EventHandler(rex_Open);

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

            rex.Close += new EventHandler(rex_Close);

        }

 

        void rex_Close(object sender, EventArgs e)

        {

            ww.Close();

            ww = null;

        }

 

        void rex_Data(object sender, DataEventArgs e)

        {

            ww.WriteData(e.Data);

        }

 

        void rex_Open(object sender, EventArgs e)

        {

            if (ww != null)

            {

                ww.Close();

            }

            string fileName = Path.ChangeExtension(Application.ExecutablePath, ".wav");

            ww = new WaveWriter(File.Create(fileName),

                AudioCompressionManager.FormatBytes(rex.Format));

        }

 

        private RecorderEx rex;

        private WaveWriter ww;

 

        private void btnRec_Click(object sender, EventArgs e)

        {

            rex.StartRecord();

            EnableButtons();

        }

 

        private void EnableButtons()

        {

            btnStop.Enabled = !btnStop.Enabled;

            btnRec.Enabled = !btnRec.Enabled;

        }

 

        private void btnStop_Click(object sender, EventArgs e)

        {

            rex.StopRecord();

            EnableButtons();

        }

    }

}


Up
40. Simple PlayerEx example

using System;

using System.Windows.Forms;

using Alvas.Audio;

using System.IO;

 

namespace SimpleRecorder

{

    public partial class PlayerForm : Form

    {

        public PlayerForm()

        {

            InitializeComponent();

        }

 

        private PlayerEx plex = new PlayerEx();

 

        private void Play(string fileName)

        {

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

            {

                IntPtr format = wr.ReadFormat();

                byte[] data = wr.ReadData();

                plex.OpenPlayer(format);

                plex.AddData(data);

            }

            plex.StartPlay();

        }

 

        private void btnPlay_Click(object sender, EventArgs e)

        {

            Play(@"G:\AlvasAudio\gsm.wav");

        }

 

        private void btnStop_Click(object sender, EventArgs e)

        {

            if (plex.State != DeviceState.Closed)

            {

                plex.ClosePlayer();

            }

        }

 

    }

}


Up
Copyright © 1998-2008 Alvas.Net. All rights reserved